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
|
2019-10-08 14:57:10 +00:00
|
|
|
import { sendNotification as sendPushNotification } from '../pushNotifications';
|
2018-09-21 13:12:20 +00:00
|
|
|
|
2019-03-31 18:52:53 +00:00
|
|
|
const PM_PER_PAGE = 10;
|
|
|
|
|
|
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
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (receiver.preferences.pushNotifications.newPM !== false) {
|
|
|
|
|
sendPushNotification(
|
|
|
|
|
receiver,
|
|
|
|
|
{
|
2019-10-10 18:11:50 +00:00
|
|
|
title: translate(
|
|
|
|
|
'newPMNotificationTitle',
|
|
|
|
|
{ name: getUserInfo(sender, ['name']).name },
|
|
|
|
|
receiver.preferences.language,
|
|
|
|
|
),
|
2019-09-29 16:45:30 +00:00
|
|
|
message,
|
2019-07-15 09:25:22 +00:00
|
|
|
identifier: 'newPM',
|
|
|
|
|
category: 'newPM',
|
2019-10-08 14:57:10 +00:00
|
|
|
payload: { replyTo: sender._id, senderName, message },
|
|
|
|
|
},
|
2019-07-15 09:25:22 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return messageSent;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-08 14:57:10 +00:00
|
|
|
export async function getUserInbox (user, options = {
|
|
|
|
|
asArray: true, page: 0, conversation: null, mapProps: false,
|
|
|
|
|
}) {
|
2019-03-31 18:52:53 +00:00
|
|
|
if (typeof options.asArray === 'undefined') {
|
|
|
|
|
options.asArray = true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-15 09:25:22 +00:00
|
|
|
if (typeof options.mapProps === 'undefined') {
|
|
|
|
|
options.mapProps = false;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
.limit(PM_PER_PAGE)
|
|
|
|
|
.skip(PM_PER_PAGE * Number(options.page));
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2019-09-29 16:45:46 +00:00
|
|
|
async function usersMapByConversations (owner, users) {
|
2019-10-08 14:57:10 +00:00
|
|
|
const query = Inbox
|
2019-09-29 16:45:46 +00:00
|
|
|
.aggregate([
|
|
|
|
|
{
|
|
|
|
|
$match: {
|
|
|
|
|
ownerId: owner._id,
|
|
|
|
|
uuid: { $in: users },
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
$group: {
|
|
|
|
|
_id: '$uuid',
|
2019-10-08 14:57:10 +00:00
|
|
|
userStyles: { $last: '$userStyles' },
|
|
|
|
|
contributor: { $last: '$contributor' },
|
2019-09-29 16:45:46 +00:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const usersAr = await query.exec();
|
|
|
|
|
const usersMap = {};
|
|
|
|
|
|
|
|
|
|
for (const usr of usersAr) {
|
|
|
|
|
usersMap[usr._id] = usr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return usersMap;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-15 09:25:22 +00:00
|
|
|
export async function listConversations (owner) {
|
2019-09-29 16:45:46 +00:00
|
|
|
// group messages by user owned by logged-in user
|
2019-10-08 14:57:10 +00:00
|
|
|
const query = Inbox
|
2019-04-26 16:45:05 +00:00
|
|
|
.aggregate([
|
|
|
|
|
{
|
|
|
|
|
$match: {
|
2019-07-15 09:25:22 +00:00
|
|
|
ownerId: owner._id,
|
2019-04-26 16:45:05 +00:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
$group: {
|
|
|
|
|
_id: '$uuid',
|
2019-10-08 14:57:10 +00:00
|
|
|
user: { $last: '$user' },
|
|
|
|
|
username: { $last: '$username' },
|
|
|
|
|
timestamp: { $last: '$timestamp' },
|
|
|
|
|
text: { $last: '$text' },
|
|
|
|
|
count: { $sum: 1 },
|
2019-04-26 16:45:05 +00:00
|
|
|
},
|
|
|
|
|
},
|
2019-10-08 14:57:10 +00:00
|
|
|
{ $sort: { timestamp: -1 } }, // sort by latest message
|
2019-04-26 16:45:05 +00:00
|
|
|
]);
|
|
|
|
|
|
2019-08-25 16:16:27 +00:00
|
|
|
const conversationsList = await query.exec();
|
2019-04-26 16:45:05 +00:00
|
|
|
|
2019-09-29 16:45:46 +00:00
|
|
|
const userIdList = conversationsList.map(c => c._id);
|
|
|
|
|
|
|
|
|
|
// get user-info based on conversations
|
|
|
|
|
const usersMap = await usersMapByConversations(owner, userIdList);
|
|
|
|
|
|
2019-10-08 14:57:10 +00:00
|
|
|
const conversations = conversationsList.map(res => ({
|
2019-08-25 16:16:27 +00:00
|
|
|
uuid: res._id,
|
|
|
|
|
...res,
|
2019-09-29 16:45:46 +00:00
|
|
|
userStyles: usersMap[res._id].userStyles,
|
|
|
|
|
contributor: usersMap[res._id].contributor,
|
2019-04-26 16:45:05 +00:00
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
return conversations;
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
2019-10-08 14:57:10 +00:00
|
|
|
await Inbox.remove({ _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(),
|
2019-10-08 14:57:10 +00:00
|
|
|
Inbox.remove({ ownerId: user._id }).exec(),
|
2018-09-21 13:12:20 +00:00
|
|
|
]);
|
|
|
|
|
}
|