habitica-self-host/website/server/libs/inbox/index.js

171 lines
4.1 KiB
JavaScript
Raw Normal View History

2019-10-08 14:57:10 +00:00
import { mapInboxMessage, inboxModel as Inbox } from '../../models/message';
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';
Move inbox to its own model (#10428) * shared model for chat and inbox * disable inbox schema * inbox: use separate model * remove old code that used group.chat * add back chat field (not used) and remove old tests * remove inbox exclusions when loading user * add GET /api/v3/inbox/messages * add comment * implement DELETE /inbox/messages/:messageid in v4 * implement GET /inbox/messages in v4 and update tests * implement DELETE /api/v4/inbox/clear * fix url * fix doc * update /export/inbox.html * update other data exports * add back messages in user schema * add user.toJSONWithInbox * add compativility until migration is done * more compatibility * fix tojson called twice * add compatibility methods * fix common tests * fix v4 integration tests * v3 get user -> with inbox * start to fix tests * fix v3 integration tests * wip * wip, client use new route * update tests for members/send-private-message * tests for get user in v4 * add tests for DELETE /inbox/messages/:messageId * add tests for DELETE /inbox/clear in v4 * update docs * fix tests * initial migration * fix migration * fix migration * migration fixes * migrate api.enterCouponCode * migrate api.castSpell * migrate reset, reroll, rebirth * add routes to v4 version * fix tests * fixes * api.updateUser * remove .only * get user -> userLib * refactor inbox.vue to work with new data model * fix return message when messaging yourself * wip fix bug with new conversation * wip * fix remaining ui issues * move api.registerLocal, fixes * keep only v3 version of GET /inbox/messages
2018-09-21 13:12:20 +00:00
const PM_PER_PAGE = 10;
export async function sentMessage (sender, receiver, message, translate) {
const messageSent = await sender.sendMessage(receiver, { receiverMsg: message });
const senderName = getUserInfo(sender, ['name']).name;
if (receiver.preferences.emailNotifications.newPM !== false) {
sendTxnEmail(receiver, 'new-pm', [
2019-10-08 14:57:10 +00:00
{ name: 'SENDER', content: senderName },
]);
}
if (receiver.preferences.pushNotifications.newPM !== false) {
sendPushNotification(
receiver,
{
title: translate(
'newPMNotificationTitle',
{ name: getUserInfo(sender, ['name']).name },
receiver.preferences.language,
),
message,
identifier: 'newPM',
category: 'newPM',
2019-10-08 14:57:10 +00:00
payload: { replyTo: sender._id, senderName, message },
},
);
}
return messageSent;
}
2019-10-08 14:57:10 +00:00
export async function getUserInbox (user, options = {
asArray: true, page: 0, conversation: null, mapProps: false,
}) {
if (typeof options.asArray === 'undefined') {
options.asArray = true;
}
if (typeof options.mapProps === 'undefined') {
options.mapProps = false;
}
2019-10-08 14:57:10 +00:00
const findObj = { ownerId: user._id };
if (options.conversation) {
findObj.uuid = options.conversation;
}
let query = Inbox
.find(findObj)
2019-10-08 14:57:10 +00:00
.sort({ timestamp: -1 });
if (typeof options.page !== 'undefined') {
query = query
.limit(PM_PER_PAGE)
.skip(PM_PER_PAGE * Number(options.page));
}
const messages = (await query.exec()).map(msg => {
const msgObj = msg.toJSON();
if (options.mapProps) {
mapInboxMessage(msgObj, user);
}
return msgObj;
});
Move inbox to its own model (#10428) * shared model for chat and inbox * disable inbox schema * inbox: use separate model * remove old code that used group.chat * add back chat field (not used) and remove old tests * remove inbox exclusions when loading user * add GET /api/v3/inbox/messages * add comment * implement DELETE /inbox/messages/:messageid in v4 * implement GET /inbox/messages in v4 and update tests * implement DELETE /api/v4/inbox/clear * fix url * fix doc * update /export/inbox.html * update other data exports * add back messages in user schema * add user.toJSONWithInbox * add compativility until migration is done * more compatibility * fix tojson called twice * add compatibility methods * fix common tests * fix v4 integration tests * v3 get user -> with inbox * start to fix tests * fix v3 integration tests * wip * wip, client use new route * update tests for members/send-private-message * tests for get user in v4 * add tests for DELETE /inbox/messages/:messageId * add tests for DELETE /inbox/clear in v4 * update docs * fix tests * initial migration * fix migration * fix migration * migration fixes * migrate api.enterCouponCode * migrate api.castSpell * migrate reset, reroll, rebirth * add routes to v4 version * fix tests * fixes * api.updateUser * remove .only * get user -> userLib * refactor inbox.vue to work with new data model * fix return message when messaging yourself * wip fix bug with new conversation * wip * fix remaining ui issues * move api.registerLocal, fixes * keep only v3 version of GET /inbox/messages
2018-09-21 13:12:20 +00:00
if (options.asArray) {
2018-10-05 17:34:42 +00:00
return messages;
Move inbox to its own model (#10428) * shared model for chat and inbox * disable inbox schema * inbox: use separate model * remove old code that used group.chat * add back chat field (not used) and remove old tests * remove inbox exclusions when loading user * add GET /api/v3/inbox/messages * add comment * implement DELETE /inbox/messages/:messageid in v4 * implement GET /inbox/messages in v4 and update tests * implement DELETE /api/v4/inbox/clear * fix url * fix doc * update /export/inbox.html * update other data exports * add back messages in user schema * add user.toJSONWithInbox * add compativility until migration is done * more compatibility * fix tojson called twice * add compatibility methods * fix common tests * fix v4 integration tests * v3 get user -> with inbox * start to fix tests * fix v3 integration tests * wip * wip, client use new route * update tests for members/send-private-message * tests for get user in v4 * add tests for DELETE /inbox/messages/:messageId * add tests for DELETE /inbox/clear in v4 * update docs * fix tests * initial migration * fix migration * fix migration * migration fixes * migrate api.enterCouponCode * migrate api.castSpell * migrate reset, reroll, rebirth * add routes to v4 version * fix tests * fixes * api.updateUser * remove .only * get user -> userLib * refactor inbox.vue to work with new data model * fix return message when messaging yourself * wip fix bug with new conversation * wip * fix remaining ui issues * move api.registerLocal, fixes * keep only v3 version of GET /inbox/messages
2018-09-21 13:12:20 +00:00
}
2019-10-08 14:57:10 +00:00
const messagesObj = {};
messages.forEach(msg => { messagesObj[msg._id] = msg; });
2019-10-08 14:57:10 +00:00
return messagesObj;
Move inbox to its own model (#10428) * shared model for chat and inbox * disable inbox schema * inbox: use separate model * remove old code that used group.chat * add back chat field (not used) and remove old tests * remove inbox exclusions when loading user * add GET /api/v3/inbox/messages * add comment * implement DELETE /inbox/messages/:messageid in v4 * implement GET /inbox/messages in v4 and update tests * implement DELETE /api/v4/inbox/clear * fix url * fix doc * update /export/inbox.html * update other data exports * add back messages in user schema * add user.toJSONWithInbox * add compativility until migration is done * more compatibility * fix tojson called twice * add compatibility methods * fix common tests * fix v4 integration tests * v3 get user -> with inbox * start to fix tests * fix v3 integration tests * wip * wip, client use new route * update tests for members/send-private-message * tests for get user in v4 * add tests for DELETE /inbox/messages/:messageId * add tests for DELETE /inbox/clear in v4 * update docs * fix tests * initial migration * fix migration * fix migration * migration fixes * migrate api.enterCouponCode * migrate api.castSpell * migrate reset, reroll, rebirth * add routes to v4 version * fix tests * fixes * api.updateUser * remove .only * get user -> userLib * refactor inbox.vue to work with new data model * fix return message when messaging yourself * wip fix bug with new conversation * wip * fix remaining ui issues * move api.registerLocal, fixes * keep only v3 version of GET /inbox/messages
2018-09-21 13:12:20 +00:00
}
async function usersMapByConversations (owner, users) {
2019-10-08 14:57:10 +00:00
const query = Inbox
.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' },
},
},
]);
const usersAr = await query.exec();
const usersMap = {};
for (const usr of usersAr) {
usersMap[usr._id] = usr;
}
return usersMap;
}
export async function listConversations (owner) {
// group messages by user owned by logged-in user
2019-10-08 14:57:10 +00:00
const query = Inbox
.aggregate([
{
$match: {
ownerId: owner._id,
},
},
{
$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-10-08 14:57:10 +00:00
{ $sort: { timestamp: -1 } }, // sort by latest message
]);
const conversationsList = await query.exec();
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 => ({
uuid: res._id,
...res,
userStyles: usersMap[res._id].userStyles,
contributor: usersMap[res._id].contributor,
}));
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
}
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();
return true;
}
Move inbox to its own model (#10428) * shared model for chat and inbox * disable inbox schema * inbox: use separate model * remove old code that used group.chat * add back chat field (not used) and remove old tests * remove inbox exclusions when loading user * add GET /api/v3/inbox/messages * add comment * implement DELETE /inbox/messages/:messageid in v4 * implement GET /inbox/messages in v4 and update tests * implement DELETE /api/v4/inbox/clear * fix url * fix doc * update /export/inbox.html * update other data exports * add back messages in user schema * add user.toJSONWithInbox * add compativility until migration is done * more compatibility * fix tojson called twice * add compatibility methods * fix common tests * fix v4 integration tests * v3 get user -> with inbox * start to fix tests * fix v3 integration tests * wip * wip, client use new route * update tests for members/send-private-message * tests for get user in v4 * add tests for DELETE /inbox/messages/:messageId * add tests for DELETE /inbox/clear in v4 * update docs * fix tests * initial migration * fix migration * fix migration * migration fixes * migrate api.enterCouponCode * migrate api.castSpell * migrate reset, reroll, rebirth * add routes to v4 version * fix tests * fixes * api.updateUser * remove .only * get user -> userLib * refactor inbox.vue to work with new data model * fix return message when messaging yourself * wip fix bug with new conversation * wip * fix remaining ui issues * move api.registerLocal, fixes * keep only v3 version of GET /inbox/messages
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(),
Move inbox to its own model (#10428) * shared model for chat and inbox * disable inbox schema * inbox: use separate model * remove old code that used group.chat * add back chat field (not used) and remove old tests * remove inbox exclusions when loading user * add GET /api/v3/inbox/messages * add comment * implement DELETE /inbox/messages/:messageid in v4 * implement GET /inbox/messages in v4 and update tests * implement DELETE /api/v4/inbox/clear * fix url * fix doc * update /export/inbox.html * update other data exports * add back messages in user schema * add user.toJSONWithInbox * add compativility until migration is done * more compatibility * fix tojson called twice * add compatibility methods * fix common tests * fix v4 integration tests * v3 get user -> with inbox * start to fix tests * fix v3 integration tests * wip * wip, client use new route * update tests for members/send-private-message * tests for get user in v4 * add tests for DELETE /inbox/messages/:messageId * add tests for DELETE /inbox/clear in v4 * update docs * fix tests * initial migration * fix migration * fix migration * migration fixes * migrate api.enterCouponCode * migrate api.castSpell * migrate reset, reroll, rebirth * add routes to v4 version * fix tests * fixes * api.updateUser * remove .only * get user -> userLib * refactor inbox.vue to work with new data model * fix return message when messaging yourself * wip fix bug with new conversation * wip * fix remaining ui issues * move api.registerLocal, fixes * keep only v3 version of GET /inbox/messages
2018-09-21 13:12:20 +00:00
]);
}