refactor(chat): check rocketchat status before sending message
gitea/salix/pipeline/head This commit looks good
Details
gitea/salix/pipeline/head This commit looks good
Details
This commit is contained in:
parent
17b178b6bf
commit
0d8dffac30
|
@ -0,0 +1,55 @@
|
||||||
|
const axios = require('axios');
|
||||||
|
const tokenLifespan = 10;
|
||||||
|
module.exports = Self => {
|
||||||
|
Self.remoteMethodCtx('getServiceAuth', {
|
||||||
|
description: 'Send a RocketChat message',
|
||||||
|
accessType: 'READ',
|
||||||
|
accepts: [],
|
||||||
|
returns: {
|
||||||
|
type: 'object',
|
||||||
|
root: true
|
||||||
|
},
|
||||||
|
http: {
|
||||||
|
path: `/getServiceAuth`,
|
||||||
|
verb: 'GET'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Self.getServiceAuth = async() => {
|
||||||
|
if (!this.login)
|
||||||
|
this.login = await requestToken();
|
||||||
|
|
||||||
|
if (!this.login) return;
|
||||||
|
|
||||||
|
if (Date.now() > this.login.expires)
|
||||||
|
this.login = await requestToken();
|
||||||
|
|
||||||
|
return this.login;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Requests a new Rocketchat token
|
||||||
|
*/
|
||||||
|
async function requestToken() {
|
||||||
|
const models = Self.app.models;
|
||||||
|
const chatConfig = await models.ChatConfig.findOne();
|
||||||
|
|
||||||
|
const {data} = await axios.post(`${chatConfig.api}/login`, {
|
||||||
|
user: chatConfig.user,
|
||||||
|
password: chatConfig.password
|
||||||
|
});
|
||||||
|
|
||||||
|
const requestData = data.data;
|
||||||
|
if (requestData) {
|
||||||
|
return {
|
||||||
|
host: chatConfig.host,
|
||||||
|
api: chatConfig.api,
|
||||||
|
auth: {
|
||||||
|
userId: requestData.userId,
|
||||||
|
token: requestData.authToken
|
||||||
|
},
|
||||||
|
expires: Date.now() + (1000 * 60 * tokenLifespan)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
|
@ -1,4 +1,4 @@
|
||||||
const got = require('got');
|
const axios = require('axios');
|
||||||
module.exports = Self => {
|
module.exports = Self => {
|
||||||
Self.remoteMethodCtx('send', {
|
Self.remoteMethodCtx('send', {
|
||||||
description: 'Send a RocketChat message',
|
description: 'Send a RocketChat message',
|
||||||
|
@ -30,86 +30,12 @@ module.exports = Self => {
|
||||||
const sender = await models.Account.findById(accessToken.userId);
|
const sender = await models.Account.findById(accessToken.userId);
|
||||||
const recipient = to.replace('@', '');
|
const recipient = to.replace('@', '');
|
||||||
|
|
||||||
if (sender.name != recipient) {
|
if (sender.name != recipient)
|
||||||
let {body} = await sendMessage(sender, to, message);
|
return sendMessage(sender, to, message);
|
||||||
if (body)
|
|
||||||
body = JSON.parse(body);
|
|
||||||
else
|
|
||||||
body = false;
|
|
||||||
|
|
||||||
return body;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
async function sendMessage(sender, channel, message) {
|
async function sendMessage(sender, channel, message) {
|
||||||
const config = await getConfig();
|
/* if (process.env.NODE_ENV !== 'production') {
|
||||||
const avatar = `${config.host}/avatar/${sender.name}`;
|
|
||||||
const uri = `${config.api}/chat.postMessage`;
|
|
||||||
|
|
||||||
return sendAuth(uri, {
|
|
||||||
'channel': channel,
|
|
||||||
'avatar': avatar,
|
|
||||||
'alias': sender.nickname,
|
|
||||||
'text': message
|
|
||||||
}).catch(async error => {
|
|
||||||
if (error.statusCode === 401) {
|
|
||||||
this.auth = null;
|
|
||||||
|
|
||||||
return sendMessage(sender, channel, message);
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new Error(error.message);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns a rocketchat token
|
|
||||||
* @return {Object} userId and authToken
|
|
||||||
*/
|
|
||||||
async function getAuthToken() {
|
|
||||||
if (!this.auth || this.auth && !this.auth.authToken) {
|
|
||||||
const config = await getConfig();
|
|
||||||
const uri = `${config.api}/login`;
|
|
||||||
let {body} = await send(uri, {
|
|
||||||
user: config.user,
|
|
||||||
password: config.password
|
|
||||||
});
|
|
||||||
|
|
||||||
if (body) {
|
|
||||||
body = JSON.parse(body);
|
|
||||||
this.auth = body.data;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.auth;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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 {*} params - Request params
|
|
||||||
* @param {*} options - Request options
|
|
||||||
*
|
|
||||||
* @return {Object} Request response
|
|
||||||
*/
|
|
||||||
async function send(uri, params, options = {}) {
|
|
||||||
if (process.env.NODE_ENV !== 'production') {
|
|
||||||
return new Promise(resolve => {
|
return new Promise(resolve => {
|
||||||
return resolve({
|
return resolve({
|
||||||
body: JSON.stringify(
|
body: JSON.stringify(
|
||||||
|
@ -118,34 +44,23 @@ module.exports = Self => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
const login = await Self.getServiceAuth();
|
||||||
|
|
||||||
const defaultOptions = {
|
const avatar = `${login.host}/avatar/${sender.name}`;
|
||||||
form: params
|
|
||||||
};
|
|
||||||
|
|
||||||
if (options) Object.assign(defaultOptions, options);
|
|
||||||
|
|
||||||
return got.post(uri, 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 = {
|
const options = {
|
||||||
headers: {}
|
headers: {
|
||||||
|
'X-Auth-Token': login.auth.token,
|
||||||
|
'X-User-Id': login.auth.userId
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
if (login) {
|
return axios.post(`${login.api}/chat.postMessage`, {
|
||||||
options.headers['X-Auth-Token'] = login.authToken;
|
'channel': channel,
|
||||||
options.headers['X-User-Id'] = login.userId;
|
'avatar': avatar,
|
||||||
}
|
'alias': sender.nickname,
|
||||||
|
'text': message
|
||||||
return send(uri, body, options);
|
}, options);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,21 +1,23 @@
|
||||||
|
const axios = require('axios');
|
||||||
|
|
||||||
module.exports = Self => {
|
module.exports = Self => {
|
||||||
Self.remoteMethodCtx('sendCheckingPresence', {
|
Self.remoteMethodCtx('sendCheckingPresence', {
|
||||||
description: 'Sends a RocketChat message to a working worker or department channel',
|
description: 'Sends a RocketChat message to a working worker or department channel',
|
||||||
accessType: 'WRITE',
|
accessType: 'WRITE',
|
||||||
accepts: [{
|
accepts: [{
|
||||||
arg: 'workerId',
|
arg: 'recipientId',
|
||||||
type: 'Number',
|
type: 'number',
|
||||||
required: true,
|
required: true,
|
||||||
description: 'The worker id of the destinatary'
|
description: 'The recipient user id'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
arg: 'message',
|
arg: 'message',
|
||||||
type: 'String',
|
type: 'string',
|
||||||
required: true,
|
required: true,
|
||||||
description: 'The message'
|
description: 'The message'
|
||||||
}],
|
}],
|
||||||
returns: {
|
returns: {
|
||||||
type: 'Object',
|
type: 'object',
|
||||||
root: true
|
root: true
|
||||||
},
|
},
|
||||||
http: {
|
http: {
|
||||||
|
@ -33,30 +35,51 @@ module.exports = Self => {
|
||||||
Object.assign(myOptions, options);
|
Object.assign(myOptions, options);
|
||||||
|
|
||||||
const models = Self.app.models;
|
const models = Self.app.models;
|
||||||
const account = await models.Account.findById(recipientId, null, myOptions);
|
|
||||||
const userId = ctx.req.accessToken.userId;
|
const userId = ctx.req.accessToken.userId;
|
||||||
|
const recipient = await models.Account.findById(recipientId, null, myOptions);
|
||||||
|
|
||||||
|
// Prevent sending messages to yourself
|
||||||
if (recipientId == userId) return false;
|
if (recipientId == userId) return false;
|
||||||
|
|
||||||
if (!account)
|
if (!recipient)
|
||||||
throw new Error(`Could not send message "${message}" to worker id ${recipientId} from user ${userId}`);
|
throw new Error(`Could not send message "${message}" to worker id ${recipientId} from user ${userId}`);
|
||||||
|
|
||||||
const query = `SELECT worker_isWorking(?) isWorking`;
|
const {data} = await getUserStatus(recipient.name);
|
||||||
const [result] = await Self.rawSql(query, [recipientId], myOptions);
|
if (data) {
|
||||||
|
if (data.status === 'offline') {
|
||||||
|
// Send message to department room
|
||||||
|
const workerDepartment = await models.WorkerDepartment.findById(recipientId, {
|
||||||
|
include: {
|
||||||
|
relation: 'department'
|
||||||
|
}
|
||||||
|
}, myOptions);
|
||||||
|
const department = workerDepartment && workerDepartment.department();
|
||||||
|
const channelName = department && department.chatName;
|
||||||
|
|
||||||
if (!result.isWorking) {
|
if (channelName)
|
||||||
const workerDepartment = await models.WorkerDepartment.findById(recipientId, {
|
return Self.send(ctx, `#${channelName}`, `@${recipient.name} ➔ ${message}`);
|
||||||
include: {
|
} else
|
||||||
relation: 'department'
|
return Self.send(ctx, `@${recipient.name}`, message);
|
||||||
}
|
|
||||||
}, myOptions);
|
|
||||||
const department = workerDepartment && workerDepartment.department();
|
|
||||||
const channelName = department && department.chatName;
|
|
||||||
|
|
||||||
if (channelName)
|
|
||||||
return Self.send(ctx, `#${channelName}`, `@${account.name} ➔ ${message}`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return Self.send(ctx, `@${account.name}`, message);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the current user status on Rocketchat
|
||||||
|
*
|
||||||
|
* @param {string} username - The recipient user name
|
||||||
|
* @return {Promise} - The request promise
|
||||||
|
*/
|
||||||
|
async function getUserStatus(username) {
|
||||||
|
const login = await Self.getServiceAuth();
|
||||||
|
|
||||||
|
const options = {
|
||||||
|
params: {username},
|
||||||
|
headers: {
|
||||||
|
'X-Auth-Token': login.auth.token,
|
||||||
|
'X-User-Id': login.auth.userId
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
return axios.get(`${login.api}/users.getStatus`, options);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
module.exports = Self => {
|
module.exports = Self => {
|
||||||
|
require('../methods/chat/getServiceAuth')(Self);
|
||||||
require('../methods/chat/send')(Self);
|
require('../methods/chat/send')(Self);
|
||||||
require('../methods/chat/sendCheckingPresence')(Self);
|
require('../methods/chat/sendCheckingPresence')(Self);
|
||||||
require('../methods/chat/notifyIssues')(Self);
|
require('../methods/chat/notifyIssues')(Self);
|
||||||
|
|
Loading…
Reference in New Issue