salix/back/methods/chat/send.js

56 lines
1.6 KiB
JavaScript
Raw Permalink Normal View History

2020-01-20 06:33:24 +00:00
module.exports = Self => {
Self.remoteMethodCtx('send', {
2022-06-08 11:08:53 +00:00
description: 'Creates a direct message in the chat model for a user or a channel',
2020-01-20 06:33:24 +00:00
accessType: 'WRITE',
accepts: [{
arg: 'to',
2021-03-15 11:26:11 +00:00
type: 'string',
2020-01-20 06:33:24 +00:00
required: true,
2020-01-23 12:31:07 +00:00
description: 'User (@) or channel (#) to send the message'
2020-01-20 06:33:24 +00:00
}, {
arg: 'message',
2021-03-15 11:26:11 +00:00
type: 'string',
2020-01-20 06:33:24 +00:00
required: true,
description: 'The message'
}],
returns: {
2021-03-15 11:26:11 +00:00
type: 'object',
2020-01-20 06:33:24 +00:00
root: true
},
http: {
path: `/send`,
verb: 'POST'
}
});
Self.send = async(ctx, to, message) => {
const models = Self.app.models;
const accessToken = ctx.req.accessToken;
const sender = await models.VnUser.findById(accessToken.userId);
2020-01-20 06:33:24 +00:00
const recipient = to.replace('@', '');
if (sender.name != recipient) {
const chat = await models.Chat.create({
senderFk: sender.id,
recipient: to,
2023-01-16 14:18:24 +00:00
dated: Date.vnNew(),
checkUserStatus: 0,
message: message,
status: 'sending',
attempts: 0
});
2022-04-07 06:51:07 +00:00
try {
await Self.sendMessage(chat.senderFk, chat.recipient, chat.message);
2023-02-08 14:15:26 +00:00
await Self.updateChat(chat, 'sent');
} catch (error) {
2023-02-08 14:15:26 +00:00
await Self.updateChat(chat, 'error', error);
}
2022-04-07 09:54:08 +00:00
return true;
}
2020-01-20 06:33:24 +00:00
2022-06-03 11:36:21 +00:00
return false;
};
2020-01-20 06:33:24 +00:00
};