salix/back/methods/chat/send.js

49 lines
1.3 KiB
JavaScript

module.exports = Self => {
Self.remoteMethodCtx('send', {
description: 'Creates a direct message in the chat model for a user or a channel',
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'
}],
returns: {
type: 'object',
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('@', '');
if (sender.name != recipient) {
await models.Chat.create({
senderFk: sender.id,
recipient: to,
dated: Date.vnNew(),
checkUserStatus: 0,
message: message,
status: 0,
attempts: 0
});
return true;
}
return false;
};
};