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;
|
2023-01-23 14:24:00 +00:00
|
|
|
const sender = await models.VnUser.findById(accessToken.userId);
|
2020-01-20 06:33:24 +00:00
|
|
|
const recipient = to.replace('@', '');
|
|
|
|
|
2022-04-06 12:06:53 +00:00
|
|
|
if (sender.name != recipient) {
|
2023-02-06 09:11:41 +00:00
|
|
|
const chat = await models.Chat.create({
|
2022-06-03 10:22:07 +00:00
|
|
|
senderFk: sender.id,
|
|
|
|
recipient: to,
|
2023-01-16 14:18:24 +00:00
|
|
|
dated: Date.vnNew(),
|
2022-06-03 10:22:07 +00:00
|
|
|
checkUserStatus: 0,
|
|
|
|
message: message,
|
2023-02-06 09:11:41 +00:00
|
|
|
status: 'sending',
|
2022-06-03 10:22:07 +00:00
|
|
|
attempts: 0
|
|
|
|
});
|
2022-04-07 06:51:07 +00:00
|
|
|
|
2023-02-06 09:11:41 +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');
|
2023-02-06 09:11:41 +00:00
|
|
|
} catch (error) {
|
2023-02-08 14:15:26 +00:00
|
|
|
await Self.updateChat(chat, 'error', error);
|
2023-02-06 09:11:41 +00:00
|
|
|
}
|
|
|
|
|
2022-04-07 09:54:08 +00:00
|
|
|
return true;
|
2022-04-06 12:06:53 +00:00
|
|
|
}
|
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
|
|
|
};
|