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.Account.findById(accessToken.userId);
|
|
|
|
const recipient = to.replace('@', '');
|
|
|
|
|
2022-04-06 12:06:53 +00:00
|
|
|
if (sender.name != recipient) {
|
2022-06-03 10:22:07 +00:00
|
|
|
await models.Chat.create({
|
|
|
|
senderFk: sender.id,
|
|
|
|
recipient: to,
|
|
|
|
dated: new Date(),
|
|
|
|
checkUserStatus: 0,
|
|
|
|
message: message,
|
|
|
|
status: 0,
|
|
|
|
attempts: 0
|
|
|
|
});
|
2022-04-07 06:51:07 +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
|
|
|
};
|