Merge branch '1958-send_chat_with_avatar' of verdnatura/salix into dev
gitea/salix/dev This commit looks good
Details
gitea/salix/dev This commit looks good
Details
This commit is contained in:
commit
4d6709712d
|
@ -31,26 +31,23 @@ module.exports = Self => {
|
||||||
const recipient = to.replace('@', '');
|
const recipient = to.replace('@', '');
|
||||||
|
|
||||||
if (sender.name != recipient)
|
if (sender.name != recipient)
|
||||||
return sendMessage(to, `@${sender.name}: ${message}`);
|
return sendMessage(sender, to, `@${sender.name}: ${message} `);
|
||||||
};
|
};
|
||||||
|
|
||||||
async function sendMessage(name, message) {
|
async function sendMessage(sender, channel, message) {
|
||||||
const models = Self.app.models;
|
const config = await getConfig();
|
||||||
const chatConfig = await models.ChatConfig.findOne();
|
|
||||||
|
|
||||||
if (!Self.token)
|
const avatar = `${config.host}/avatar/${sender.name}`;
|
||||||
Self.token = await login();
|
const uri = `${config.api}/chat.postMessage`;
|
||||||
|
return sendAuth(uri, {
|
||||||
const uri = `${chatConfig.uri}/chat.postMessage`;
|
'channel': channel,
|
||||||
return send(uri, {
|
'avatar': avatar,
|
||||||
'channel': name,
|
|
||||||
'text': message
|
'text': message
|
||||||
}).catch(async error => {
|
}).catch(async error => {
|
||||||
if (error.statusCode === 401 && !Self.loginAttempted) {
|
if (error.statusCode === 401 && !this.resendAttempted) {
|
||||||
Self.token = await login();
|
this.resendAttempted = true;
|
||||||
Self.loginAttempted = true;
|
|
||||||
|
|
||||||
return sendMessage(name, message);
|
return sendMessage(sender, channel, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new Error(error.message);
|
throw new Error(error.message);
|
||||||
|
@ -61,24 +58,51 @@ module.exports = Self => {
|
||||||
* Returns a rocketchat token
|
* Returns a rocketchat token
|
||||||
* @return {Object} userId and authToken
|
* @return {Object} userId and authToken
|
||||||
*/
|
*/
|
||||||
async function login() {
|
async function getAuthToken() {
|
||||||
const models = Self.app.models;
|
if (!this.auth || this.auth && !this.auth.authToken) {
|
||||||
const chatConfig = await models.ChatConfig.findOne();
|
const config = await getConfig();
|
||||||
const uri = `${chatConfig.uri}/login`;
|
const uri = `${config.api}/login`;
|
||||||
return send(uri, {
|
const res = await send(uri, {
|
||||||
user: chatConfig.user,
|
user: config.user,
|
||||||
password: chatConfig.password
|
password: config.password
|
||||||
}).then(res => res.data);
|
});
|
||||||
|
|
||||||
|
this.auth = res.data;
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.auth;
|
||||||
}
|
}
|
||||||
|
|
||||||
function send(uri, body) {
|
/**
|
||||||
|
* Returns a rocketchat config
|
||||||
|
* @return {Object} Auth config
|
||||||
|
*/
|
||||||
|
async function getConfig() {
|
||||||
|
if (!this.chatConfig) {
|
||||||
|
const models = Self.app.models;
|
||||||
|
|
||||||
|
this.chatConfig = await models.ChatConfig.findOne();
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.chatConfig;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send unauthenticated request
|
||||||
|
* @param {*} uri - Request uri
|
||||||
|
* @param {*} body - Request params
|
||||||
|
* @param {*} options - Request options
|
||||||
|
*
|
||||||
|
* @return {Object} Request response
|
||||||
|
*/
|
||||||
|
async function send(uri, body, options) {
|
||||||
if (process.env.NODE_ENV !== 'production') {
|
if (process.env.NODE_ENV !== 'production') {
|
||||||
return new Promise(resolve => {
|
return new Promise(resolve => {
|
||||||
return resolve({statusCode: 200, message: 'Fake notification sent'});
|
return resolve({statusCode: 200, message: 'Fake notification sent'});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const options = {
|
const defaultOptions = {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
uri: uri,
|
uri: uri,
|
||||||
body: body,
|
body: body,
|
||||||
|
@ -86,11 +110,29 @@ module.exports = Self => {
|
||||||
json: true
|
json: true
|
||||||
};
|
};
|
||||||
|
|
||||||
if (Self.token) {
|
if (options) Object.assign(defaultOptions, options);
|
||||||
options.headers['X-Auth-Token'] = Self.token.authToken;
|
|
||||||
options.headers['X-User-Id'] = Self.token.userId;
|
return request(defaultOptions);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send authenticated request
|
||||||
|
* @param {*} uri - Request uri
|
||||||
|
* @param {*} body - Request params
|
||||||
|
*
|
||||||
|
* @return {Object} Request response
|
||||||
|
*/
|
||||||
|
async function sendAuth(uri, body) {
|
||||||
|
const login = await getAuthToken();
|
||||||
|
const options = {
|
||||||
|
headers: {'content-type': 'application/json'}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (login) {
|
||||||
|
options.headers['X-Auth-Token'] = login.authToken;
|
||||||
|
options.headers['X-User-Id'] = login.userId;
|
||||||
}
|
}
|
||||||
|
|
||||||
return request(options);
|
return send(uri, body, options);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -13,7 +13,10 @@
|
||||||
"type": "Number",
|
"type": "Number",
|
||||||
"description": "Identifier"
|
"description": "Identifier"
|
||||||
},
|
},
|
||||||
"uri": {
|
"host": {
|
||||||
|
"type": "String"
|
||||||
|
},
|
||||||
|
"api": {
|
||||||
"type": "String"
|
"type": "String"
|
||||||
},
|
},
|
||||||
"user": {
|
"user": {
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
ALTER TABLE `vn`.`chatConfig`
|
||||||
|
ADD COLUMN `host` VARCHAR(255) NOT NULL AFTER `id`,
|
||||||
|
CHANGE COLUMN `uri` `api` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NOT NULL ;
|
||||||
|
|
||||||
|
UPDATE `vn`.`chatConfig` SET `host` = 'https://chat.verdnatura.es' WHERE (`id` = '1');
|
|
@ -20,9 +20,9 @@ INSERT INTO `vn`.`bionicConfig` (`generalInflationCoeficient`, `minimumDensityVo
|
||||||
VALUES
|
VALUES
|
||||||
(1.30, 167.00, 138000, 71);
|
(1.30, 167.00, 138000, 71);
|
||||||
|
|
||||||
INSERT INTO `vn`.`chatConfig` (`uri`)
|
INSERT INTO `vn`.`chatConfig` (`host`, `api`)
|
||||||
VALUES
|
VALUES
|
||||||
('https://chat.verdnatura.es/api/v1');
|
('https://chat.verdnatura.es', 'https://chat.verdnatura.es/api/v1');
|
||||||
|
|
||||||
INSERT IGNORE INTO `vn`.`greugeConfig`(`id`, `freightPickUpPrice`)
|
INSERT IGNORE INTO `vn`.`greugeConfig`(`id`, `freightPickUpPrice`)
|
||||||
VALUES
|
VALUES
|
||||||
|
|
Loading…
Reference in New Issue