2022-06-01 11:49:45 +00:00
|
|
|
const axios = require('axios');
|
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethodCtx('sendQueued', {
|
|
|
|
description: 'Send a RocketChat message',
|
|
|
|
accessType: 'WRITE',
|
|
|
|
returns: {
|
|
|
|
type: 'object',
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/sendQueued`,
|
2022-09-13 11:53:44 +00:00
|
|
|
verb: 'POST'
|
2022-06-01 11:49:45 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-01-04 10:23:06 +00:00
|
|
|
Self.sendQueued = async() => {
|
2022-06-01 11:49:45 +00:00
|
|
|
const models = Self.app.models;
|
2022-06-03 10:22:07 +00:00
|
|
|
|
2022-06-02 12:55:39 +00:00
|
|
|
const chats = await models.Chat.find({
|
2023-01-04 10:23:06 +00:00
|
|
|
where: {
|
|
|
|
status: {
|
2023-03-21 10:26:25 +00:00
|
|
|
nin: [
|
|
|
|
'sent',
|
|
|
|
'sending'
|
|
|
|
]
|
|
|
|
|
2023-01-04 10:23:06 +00:00
|
|
|
},
|
2023-02-06 09:11:41 +00:00
|
|
|
attempts: {lt: 3}
|
2023-01-04 10:23:06 +00:00
|
|
|
}
|
2022-06-02 12:55:39 +00:00
|
|
|
});
|
2022-06-01 11:49:45 +00:00
|
|
|
|
2022-06-02 12:55:39 +00:00
|
|
|
for (let chat of chats) {
|
2022-06-03 10:22:07 +00:00
|
|
|
if (chat.checkUserStatus) {
|
|
|
|
try {
|
2022-06-03 11:36:21 +00:00
|
|
|
await Self.sendCheckingUserStatus(chat);
|
2023-03-21 10:26:25 +00:00
|
|
|
await Self.updateChat(chat, 'sent');
|
2022-06-03 10:22:07 +00:00
|
|
|
} catch (error) {
|
2023-03-21 10:26:25 +00:00
|
|
|
await Self.updateChat(chat, 'error', error);
|
2022-06-03 10:22:07 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
try {
|
2022-06-03 11:36:21 +00:00
|
|
|
await Self.sendMessage(chat.senderFk, chat.recipient, chat.message);
|
2023-03-21 10:26:25 +00:00
|
|
|
await Self.updateChat(chat, 'sent');
|
2022-06-03 10:22:07 +00:00
|
|
|
} catch (error) {
|
2023-03-21 10:26:25 +00:00
|
|
|
await Self.updateChat(chat, 'error', error);
|
2022-06-03 10:22:07 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-01 11:49:45 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-06-03 11:44:42 +00:00
|
|
|
/**
|
|
|
|
* Check user status in Rocket
|
|
|
|
*
|
|
|
|
* @param {object} chat - The sender id
|
|
|
|
* @return {Promise} - The request promise
|
|
|
|
*/
|
2022-06-03 11:36:21 +00:00
|
|
|
Self.sendCheckingUserStatus = async function sendCheckingUserStatus(chat) {
|
2022-06-02 12:55:39 +00:00
|
|
|
const models = Self.app.models;
|
2022-06-03 10:22:07 +00:00
|
|
|
|
|
|
|
const recipientName = chat.recipient.slice(1);
|
2022-06-02 12:55:39 +00:00
|
|
|
const recipient = await models.Account.findOne({
|
|
|
|
where: {
|
2022-06-03 10:22:07 +00:00
|
|
|
name: recipientName
|
2022-06-02 12:55:39 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-06-03 11:36:21 +00:00
|
|
|
const {data} = await Self.getUserStatus(recipient.name);
|
2022-06-03 10:22:07 +00:00
|
|
|
if (data) {
|
|
|
|
if (data.status === 'offline' || data.status === 'busy') {
|
|
|
|
// Send message to department room
|
|
|
|
const workerDepartment = await models.WorkerDepartment.findById(recipient.id, {
|
|
|
|
include: {
|
|
|
|
relation: 'department'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const department = workerDepartment && workerDepartment.department();
|
|
|
|
const channelName = department && department.chatName;
|
|
|
|
|
|
|
|
if (channelName)
|
2022-07-27 07:16:49 +00:00
|
|
|
return Self.sendMessage(chat.senderFk, `#${channelName}`, `@${recipient.name} ➔ ${chat.message}`);
|
2022-06-03 10:22:07 +00:00
|
|
|
else
|
2022-06-03 11:48:44 +00:00
|
|
|
return Self.sendMessage(chat.senderFk, `@${recipient.name}`, chat.message);
|
2022-06-03 10:22:07 +00:00
|
|
|
} else
|
2022-06-03 11:48:44 +00:00
|
|
|
return Self.sendMessage(chat.senderFk, `@${recipient.name}`, chat.message);
|
2022-06-03 10:22:07 +00:00
|
|
|
}
|
2022-06-03 11:36:21 +00:00
|
|
|
};
|
|
|
|
|
2022-06-03 10:22:07 +00:00
|
|
|
/**
|
2022-06-03 11:44:42 +00:00
|
|
|
* Send a rocket message
|
2022-06-03 10:22:07 +00:00
|
|
|
*
|
|
|
|
* @param {object} senderFk - The sender id
|
|
|
|
* @param {string} recipient - The user (@) or channel (#) to send the message
|
|
|
|
* @param {string} message - The message to send
|
|
|
|
* @return {Promise} - The request promise
|
|
|
|
*/
|
2022-06-03 11:36:21 +00:00
|
|
|
Self.sendMessage = async function sendMessage(senderFk, recipient, message) {
|
2022-06-03 12:02:23 +00:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
return resolve({
|
|
|
|
statusCode: 200,
|
|
|
|
message: 'Fake notification sent'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2022-06-01 11:49:45 +00:00
|
|
|
|
2022-06-03 10:22:07 +00:00
|
|
|
const models = Self.app.models;
|
|
|
|
const sender = await models.Account.findById(senderFk);
|
2022-06-02 12:55:39 +00:00
|
|
|
|
2022-06-01 11:49:45 +00:00
|
|
|
const login = await Self.getServiceAuth();
|
|
|
|
const avatar = `${login.host}/avatar/${sender.name}`;
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
headers: {
|
|
|
|
'X-Auth-Token': login.auth.token,
|
|
|
|
'X-User-Id': login.auth.userId
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
return axios.post(`${login.api}/chat.postMessage`, {
|
2022-06-03 12:02:23 +00:00
|
|
|
'channel': recipient,
|
2022-06-01 11:49:45 +00:00
|
|
|
'avatar': avatar,
|
|
|
|
'alias': sender.nickname,
|
2022-06-03 10:22:07 +00:00
|
|
|
'text': message
|
2022-06-01 11:49:45 +00:00
|
|
|
}, options);
|
2022-06-03 11:36:21 +00:00
|
|
|
};
|
2022-06-03 10:22:07 +00:00
|
|
|
|
|
|
|
/**
|
2022-06-03 11:36:21 +00:00
|
|
|
* Update status and attempts of a chat
|
|
|
|
*
|
|
|
|
* @param {object} chat - The chat
|
|
|
|
* @param {string} status - The new status
|
2022-07-27 07:16:49 +00:00
|
|
|
* @param {string} error - The error
|
2023-02-08 14:15:26 +00:00
|
|
|
* @param {object} options - Query options
|
2022-06-03 11:36:21 +00:00
|
|
|
* @return {Promise} - The request promise
|
2023-02-08 14:15:26 +00:00
|
|
|
*/
|
2023-01-04 10:23:06 +00:00
|
|
|
|
2023-02-08 14:15:26 +00:00
|
|
|
Self.updateChat = async(chat, status, error) => {
|
2022-06-03 10:22:07 +00:00
|
|
|
return chat.updateAttributes({
|
|
|
|
status: status,
|
2022-07-27 07:16:49 +00:00
|
|
|
attempts: ++chat.attempts,
|
|
|
|
error: error
|
2022-06-03 10:22:07 +00:00
|
|
|
});
|
2023-02-08 14:15:26 +00:00
|
|
|
};
|
2022-06-03 10:22:07 +00:00
|
|
|
|
|
|
|
/**
|
2022-06-03 11:36:21 +00:00
|
|
|
* Returns the current user status on Rocketchat
|
|
|
|
*
|
|
|
|
* @param {string} username - The recipient user name
|
|
|
|
* @return {Promise} - The request promise
|
|
|
|
*/
|
|
|
|
Self.getUserStatus = async function getUserStatus(username) {
|
2022-06-03 10:22:07 +00:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
return resolve({
|
|
|
|
data: {
|
|
|
|
status: 'online'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2022-06-03 11:36:21 +00:00
|
|
|
};
|
2022-06-01 11:49:45 +00:00
|
|
|
};
|