diff --git a/back/methods/chat/sendQueued.js b/back/methods/chat/sendQueued.js new file mode 100644 index 000000000..d31ff9686 --- /dev/null +++ b/back/methods/chat/sendQueued.js @@ -0,0 +1,69 @@ +const axios = require('axios'); +module.exports = Self => { + Self.remoteMethodCtx('sendQueued', { + description: 'Send a RocketChat message', + accessType: 'WRITE', + accepts: [{ + arg: 'to', + type: 'string', + required: true, + description: 'User (@) or channel (#) to send the message' + }, { + arg: 'message', + type: 'string', + required: true, + description: 'The message' + }], + returns: { + type: 'object', + root: true + }, + http: { + path: `/sendQueued`, + verb: 'POST' + } + }); + + Self.sendQueued = async(ctx, to, message) => { + const models = Self.app.models; + const accessToken = ctx.req.accessToken; + const sender = await models.Account.findById(accessToken.userId); + const recipient = to.replace('@', ''); + + if (sender.name != recipient) { + await sendMessage(sender, to, message); + + return true; + } + + return false; + }; + + async function sendMessage(sender, channel, message) { + if (process.env.NODE_ENV !== 'production') { + return new Promise(resolve => { + return resolve({ + statusCode: 200, + message: 'Fake notification sent' + }); + }); + } + + 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`, { + 'channel': channel, + 'avatar': avatar, + 'alias': sender.nickname, + 'text': message + }, options); + } +}; diff --git a/db/changes/10470-family/00-chat.sql b/db/changes/10470-family/00-chat.sql new file mode 100644 index 000000000..0c03f2bcb --- /dev/null +++ b/db/changes/10470-family/00-chat.sql @@ -0,0 +1,10 @@ +CREATE TABLE `vn`.`chat` ( + `id` int(11) DEFAULT NULL, + `senderFk` int(11) DEFAULT NULL, + `recipient` varchar(50) COLLATE utf8mb3_unicode_ci DEFAULT NULL, + `dated` date DEFAULT NULL, + `checkUserStatus` tinyint(1) DEFAULT NULL, + `message` varchar(200) COLLATE utf8mb3_unicode_ci DEFAULT NULL, + `status` tinyint(1) DEFAULT NULL, + `attempts` int(1) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci \ No newline at end of file