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', {
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'
}],
accepts: [],
returns: {
type: 'object',
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 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;
const sentStatus = 1;
const chats = await models.Chat.find({
where: {
status: {neq: sentStatus}
}
});
return false;
for (let chat of chats) {
if (chat.checkUserStatus)
await sendCheckingPresence(ctx, chat);
else
await sendMessage(chat);
}
};
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') {
return new Promise(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 avatar = `${login.host}/avatar/${sender.name}`;
@ -60,10 +69,10 @@ module.exports = Self => {
};
return axios.post(`${login.api}/chat.postMessage`, {
'channel': channel,
'channel': `@${chat.recipient}`,
'avatar': avatar,
'alias': sender.nickname,
'text': message
'text': chat.message
}, options);
}
};

View File

@ -3,4 +3,5 @@ module.exports = Self => {
require('../methods/chat/send')(Self);
require('../methods/chat/sendCheckingPresence')(Self);
require('../methods/chat/notifyIssues')(Self);
require('../methods/chat/sendQueued')(Self);
};

View File

@ -1,6 +1,39 @@
{
"name": "Chat",
"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": [{
"property": "validations",
"accessType": "EXECUTE",

View File

@ -1,10 +1,13 @@
CREATE TABLE `vn`.`chat` (
`id` int(11) DEFAULT NULL,
`senderFk` int(11) DEFAULT NULL,
`recipient` varchar(50) COLLATE utf8mb3_unicode_ci DEFAULT NULL,
`id` int(11) NOT NULL AUTO_INCREMENT,
`senderFk` int(10) unsigned DEFAULT NULL,
`recipient` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`dated` date 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,
`attempts` int(1) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci
`attempts` int(1) DEFAULT NULL,
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

@ -2560,6 +2560,10 @@ INSERT INTO `vn`.`supplierAgencyTerm` (`agencyFk`, `supplierFk`, `minimumPackage
(4, 2, 0, 20.00, 0.00, 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`)
VALUES
('delivery', '9.2', 0),