2019-10-08 14:57:10 +00:00
|
|
|
import { mapInboxMessage, inboxModel as Inbox } from '../../models/message';
|
2019-10-10 18:11:50 +00:00
|
|
|
import { getUserInfo, sendTxn as sendTxnEmail } from '../email'; // eslint-disable-line import/no-cycle
|
2020-04-15 19:36:53 +00:00
|
|
|
import { sendNotification as sendPushNotification } from '../pushNotifications'; // eslint-disable-line import/no-cycle
|
2018-09-21 13:12:20 +00:00
|
|
|
|
2019-07-15 09:25:22 +00:00
|
|
|
export async function sentMessage (sender, receiver, message, translate) {
|
|
|
|
|
const messageSent = await sender.sendMessage(receiver, { receiverMsg: message });
|
2019-09-30 17:36:22 +00:00
|
|
|
const senderName = getUserInfo(sender, ['name']).name;
|
2019-07-15 09:25:22 +00:00
|
|
|
|
|
|
|
|
if (receiver.preferences.emailNotifications.newPM !== false) {
|
|
|
|
|
sendTxnEmail(receiver, 'new-pm', [
|
2019-10-08 14:57:10 +00:00
|
|
|
{ name: 'SENDER', content: senderName },
|
2019-07-15 09:25:22 +00:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-15 19:36:53 +00:00
|
|
|
if (receiver.preferences.pushNotifications.newPM !== false && messageSent.unformattedText) {
|
2024-06-11 18:19:03 +00:00
|
|
|
await sendPushNotification(
|
2019-07-15 09:25:22 +00:00
|
|
|
receiver,
|
|
|
|
|
{
|
2019-10-10 18:11:50 +00:00
|
|
|
title: translate(
|
|
|
|
|
'newPMNotificationTitle',
|
|
|
|
|
{ name: getUserInfo(sender, ['name']).name },
|
|
|
|
|
receiver.preferences.language,
|
|
|
|
|
),
|
2019-11-29 17:46:26 +00:00
|
|
|
message: messageSent.unformattedText,
|
2019-07-15 09:25:22 +00:00
|
|
|
identifier: 'newPM',
|
|
|
|
|
category: 'newPM',
|
2020-06-26 13:27:02 +00:00
|
|
|
payload: { replyTo: sender._id, senderName, message: messageSent.unformattedText },
|
2019-10-08 14:57:10 +00:00
|
|
|
},
|
2019-07-15 09:25:22 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return messageSent;
|
|
|
|
|
}
|
2020-04-20 14:32:54 +00:00
|
|
|
const PM_PER_PAGE = 10;
|
2019-07-15 09:25:22 +00:00
|
|
|
|
2020-04-20 14:32:54 +00:00
|
|
|
const getUserInboxDefaultOptions = {
|
2020-05-02 19:48:16 +00:00
|
|
|
asArray: true,
|
|
|
|
|
page: undefined,
|
|
|
|
|
conversation: null,
|
|
|
|
|
mapProps: false,
|
2020-04-20 14:32:54 +00:00
|
|
|
};
|
2019-03-31 18:52:53 +00:00
|
|
|
|
2020-04-20 14:32:54 +00:00
|
|
|
export async function getUserInbox (user, optionParams = getUserInboxDefaultOptions) {
|
|
|
|
|
// if not all properties are passed, fill the default values
|
|
|
|
|
const options = { ...getUserInboxDefaultOptions, ...optionParams };
|
2019-07-15 09:25:22 +00:00
|
|
|
|
2019-10-08 14:57:10 +00:00
|
|
|
const findObj = { ownerId: user._id };
|
2019-04-26 16:45:05 +00:00
|
|
|
|
|
|
|
|
if (options.conversation) {
|
|
|
|
|
findObj.uuid = options.conversation;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-31 18:52:53 +00:00
|
|
|
let query = Inbox
|
2019-04-26 16:45:05 +00:00
|
|
|
.find(findObj)
|
2019-10-08 14:57:10 +00:00
|
|
|
.sort({ timestamp: -1 });
|
2019-03-31 18:52:53 +00:00
|
|
|
|
|
|
|
|
if (typeof options.page !== 'undefined') {
|
|
|
|
|
query = query
|
2020-04-20 14:32:54 +00:00
|
|
|
.skip(PM_PER_PAGE * Number(options.page))
|
|
|
|
|
.limit(PM_PER_PAGE);
|
2019-03-31 18:52:53 +00:00
|
|
|
}
|
|
|
|
|
|
2019-07-15 09:25:22 +00:00
|
|
|
const messages = (await query.exec()).map(msg => {
|
|
|
|
|
const msgObj = msg.toJSON();
|
|
|
|
|
|
|
|
|
|
if (options.mapProps) {
|
|
|
|
|
mapInboxMessage(msgObj, user);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return msgObj;
|
|
|
|
|
});
|
2018-09-21 13:12:20 +00:00
|
|
|
|
2019-03-31 18:52:53 +00:00
|
|
|
if (options.asArray) {
|
2018-10-05 17:34:42 +00:00
|
|
|
return messages;
|
2018-09-21 13:12:20 +00:00
|
|
|
}
|
2019-10-08 14:57:10 +00:00
|
|
|
const messagesObj = {};
|
2019-10-10 18:11:50 +00:00
|
|
|
messages.forEach(msg => { messagesObj[msg._id] = msg; });
|
2019-10-08 14:57:10 +00:00
|
|
|
|
|
|
|
|
return messagesObj;
|
2018-09-21 13:12:20 +00:00
|
|
|
}
|
|
|
|
|
|
2018-11-08 21:04:55 +00:00
|
|
|
export async function getUserInboxMessage (user, messageId) {
|
2019-10-08 14:57:10 +00:00
|
|
|
return Inbox.findOne({ ownerId: user._id, _id: messageId }).exec();
|
2018-11-08 21:04:55 +00:00
|
|
|
}
|
|
|
|
|
|
2018-08-30 19:50:03 +00:00
|
|
|
export async function deleteMessage (user, messageId) {
|
2019-10-08 14:57:10 +00:00
|
|
|
const message = await Inbox.findOne({ _id: messageId, ownerId: user._id }).exec();
|
2018-10-05 17:34:42 +00:00
|
|
|
if (!message) return false;
|
2024-01-16 21:18:47 +00:00
|
|
|
await Inbox.deleteOne({ _id: message._id, ownerId: user._id }).exec();
|
2018-08-30 19:50:03 +00:00
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2018-09-21 13:12:20 +00:00
|
|
|
|
|
|
|
|
export async function clearPMs (user) {
|
|
|
|
|
user.inbox.newMessages = 0;
|
|
|
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
|
user.save(),
|
2024-01-16 21:18:47 +00:00
|
|
|
Inbox.deleteMany({ ownerId: user._id }).exec(),
|
2018-09-21 13:12:20 +00:00
|
|
|
]);
|
|
|
|
|
}
|