feat: endpoint sendQueued addded
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Vicent Llopis 2022-06-02 14:55:39 +02:00
parent 7de0fd6634
commit f6028316f4
5 changed files with 80 additions and 30 deletions

View File

@ -3,17 +3,7 @@ module.exports = Self => {
Self.remoteMethodCtx('sendQueued', { Self.remoteMethodCtx('sendQueued', {
description: 'Send a RocketChat message', description: 'Send a RocketChat message',
accessType: 'WRITE', accessType: 'WRITE',
accepts: [{ 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: { returns: {
type: 'object', type: 'object',
root: true root: true
@ -24,22 +14,35 @@ module.exports = Self => {
} }
}); });
Self.sendQueued = async(ctx, to, message) => { Self.sendQueued = async ctx => {
const models = Self.app.models; 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) { const sentStatus = 1;
await sendMessage(sender, to, message); const chats = await models.Chat.find({
where: {
status: {neq: sentStatus}
}
});
return true; for (let chat of chats) {
if (chat.checkUserStatus)
await sendCheckingPresence(ctx, chat);
else
await sendMessage(chat);
} }
return false;
}; };
async function sendMessage(sender, channel, message) { async function sendCheckingPresence(ctx, chat) {
const models = Self.app.models;
const recipient = await models.Account.findOne({
where: {
name: chat.recipient
}
});
await models.Chat.sendCheckingPresence(ctx, recipient.id, chat.message);
}
async function sendMessage(chat) {
if (process.env.NODE_ENV !== 'production') { if (process.env.NODE_ENV !== 'production') {
return new Promise(resolve => { return new Promise(resolve => {
return resolve({ return resolve({
@ -49,6 +52,12 @@ module.exports = Self => {
}); });
} }
const sender = await models.Account.findOne({
where: {
name: chat.senderFk
}
});
const login = await Self.getServiceAuth(); const login = await Self.getServiceAuth();
const avatar = `${login.host}/avatar/${sender.name}`; const avatar = `${login.host}/avatar/${sender.name}`;
@ -60,10 +69,10 @@ module.exports = Self => {
}; };
return axios.post(`${login.api}/chat.postMessage`, { return axios.post(`${login.api}/chat.postMessage`, {
'channel': channel, 'channel': `@${chat.recipient}`,
'avatar': avatar, 'avatar': avatar,
'alias': sender.nickname, 'alias': sender.nickname,
'text': message 'text': chat.message
}, options); }, options);
} }
}; };

View File

@ -3,4 +3,5 @@ module.exports = 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);
require('../methods/chat/sendQueued')(Self);
}; };

View File

@ -1,6 +1,39 @@
{ {
"name": "Chat", "name": "Chat",
"base": "VnModel", "base": "VnModel",
"options": {
"mysql": {
"table": "chat"
}
},
"properties": {
"id": {
"id": true,
"type": "number",
"description": "Identifier"
},
"senderFk": {
"type": "number"
},
"recipient": {
"type": "string"
},
"dated": {
"type": "date"
},
"checkUserStatus": {
"type": "boolean"
},
"message": {
"type": "string"
},
"status": {
"type": "string"
},
"attempts": {
"type": "number"
}
},
"acls": [{ "acls": [{
"property": "validations", "property": "validations",
"accessType": "EXECUTE", "accessType": "EXECUTE",

View File

@ -1,10 +1,13 @@
CREATE TABLE `vn`.`chat` ( CREATE TABLE `vn`.`chat` (
`id` int(11) DEFAULT NULL, `id` int(11) NOT NULL AUTO_INCREMENT,
`senderFk` int(11) DEFAULT NULL, `senderFk` int(10) unsigned DEFAULT NULL,
`recipient` varchar(50) COLLATE utf8mb3_unicode_ci DEFAULT NULL, `recipient` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`dated` date DEFAULT NULL, `dated` date DEFAULT NULL,
`checkUserStatus` tinyint(1) DEFAULT NULL, `checkUserStatus` tinyint(1) DEFAULT NULL,
`message` varchar(200) COLLATE utf8mb3_unicode_ci DEFAULT NULL, `message` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
`status` tinyint(1) DEFAULT NULL, `status` tinyint(1) DEFAULT NULL,
`attempts` int(1) DEFAULT NULL `attempts` int(1) DEFAULT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci PRIMARY KEY (`id`),
KEY `chat_FK` (`senderFk`),
CONSTRAINT `chat_FK` FOREIGN KEY (`senderFk`) REFERENCES `account`.`user` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

View File

@ -2558,7 +2558,11 @@ INSERT INTO `vn`.`supplierAgencyTerm` (`agencyFk`, `supplierFk`, `minimumPackage
(2, 1, 60, 0.00, 0.00, NULL, 0, 5.00, 33), (2, 1, 60, 0.00, 0.00, NULL, 0, 5.00, 33),
(3, 2, 0, 15.00, 0.00, NULL, 0, 0.00, 0), (3, 2, 0, 15.00, 0.00, NULL, 0, 0.00, 0),
(4, 2, 0, 20.00, 0.00, NULL, 0, 0.00, 0), (4, 2, 0, 20.00, 0.00, NULL, 0, 0.00, 0),
(5, 442, 0, 0.00, 3.05, NULL, 0, 0.00, 0); (5, 442, 0, 0.00, 3.05, NULL, 0, 0.00, 0);
INSERT INTO `vn`.`chat` (`senderFk`, `recipient`, `dated`, `checkUserStatus`, `message`, `status`, `attempts`)
VALUES
(1101, 'PetterParker', '2022-06-02', 1, 'First test message', 0, 0);
INSERT INTO `vn`.`mobileAppVersionControl` (`appName`, `version`, `isVersionCritical`) INSERT INTO `vn`.`mobileAppVersionControl` (`appName`, `version`, `isVersionCritical`)
VALUES VALUES