Files
habitica/website/server/libs/inbox/index.js
Kalista Payne af7114dee8 Squashed commit of the following:
commit cc6a35e61d
Author: Kalista Payne <kalista@habitica.com>
Date:   Fri Dec 12 17:27:50 2025 -0600

    fix(CSP): more Amazon domains

commit 985b86c29a
Author: Kalista Payne <kalista@habitica.com>
Date:   Fri Dec 12 17:18:08 2025 -0600

    fix(csp): more loggly allowance

commit 166bd31527
Author: Kalista Payne <kalista@habitica.com>
Date:   Fri Dec 12 17:12:00 2025 -0600

    fix(csp): data, inline, some refactoring

commit 1a0a6c1806
Author: Kalista Payne <kalista@habitica.com>
Date:   Fri Dec 12 17:05:44 2025 -0600

    fix(CSP): override default script-src

commit 023d9886c8
Author: Kalista Payne <kalista@habitica.com>
Date:   Fri Dec 12 16:56:24 2025 -0600

    fix(CSP): unsafe-eval in default-src

commit f51f0a0c93
Author: Kalista Payne <kalista@habitica.com>
Date:   Fri Dec 12 16:52:14 2025 -0600

    fix(CSP): move trusted list to default-src

commit 83b2ba7688
Author: Kalista Payne <kalista@habitica.com>
Date:   Fri Dec 12 16:38:05 2025 -0600

    fix(CSP): explicit habitica/aws in script-src

commit d5ca5172d5
Author: Kalista Payne <kalista@habitica.com>
Date:   Fri Dec 12 16:31:38 2025 -0600

    fix(CSP): need escaped single quotes

commit c677a1ffef
Author: Kalista Payne <kalista@habitica.com>
Date:   Fri Dec 12 16:27:46 2025 -0600

    fix(CSP): unsafe-eval

commit 6ef35c3f72
Author: Kalista Payne <kalista@habitica.com>
Date:   Fri Dec 12 16:15:07 2025 -0600

    fix(CSP): might need to skip entirely in dev but try no 'self'

commit 5759fb37d8
Author: Kalista Payne <kalista@habitica.com>
Date:   Fri Dec 12 15:51:26 2025 -0600

    fix(csp): permit AWS in default-src

commit 9f238abf93
Author: Kalista Payne <kalista@habitica.com>
Date:   Fri Dec 5 17:22:25 2025 -0600

    fix(csp): update helmet version to latest

commit 9462e90f4f
Author: Kalista Payne <kalista@habitica.com>
Date:   Tue Nov 25 09:27:05 2025 -0600

    feat(security): implement CSP

commit 72539f9ba3
Author: Kalista Payne <kalista@habitica.com>
Date:   Wed Dec 10 14:16:53 2025 -0600

    5.42.2

commit dabd466719
Author: Kalista Payne <kalista@habitica.com>
Date:   Wed Dec 10 14:16:48 2025 -0600

    Revert "Chat optimization (#15545)"

    This reverts commit 2917955ef0.

commit 8bf2304330
Author: Kalista Payne <kalista@habitica.com>
Date:   Wed Dec 10 14:15:48 2025 -0600

    chore(event): G1G1 date tweaks

commit 6937dc4e4e
Author: Kalista Payne <kalista@habitica.com>
Date:   Mon Dec 8 16:37:04 2025 -0600

    fix(subscription): couple more layout tweaks
2025-12-12 17:31:02 -06:00

146 lines
3.9 KiB
JavaScript

import { mapInboxMessage, inboxModel } from '../../models/message';
import { getUserInfo, sendTxn as sendTxnEmail } from '../email'; // eslint-disable-line import/no-cycle
import { sendNotification as sendPushNotification } from '../pushNotifications';
export async function sentMessage (sender, receiver, message, translate) {
const fakeSending = sender.flags.chatShadowMuted;
const messageSent = await sender.sendMessage(receiver, {
receiverMsg: message,
fakeSending,
});
const senderName = getUserInfo(sender, ['name']).name;
if (!fakeSending) {
if (receiver.preferences.emailNotifications.newPM !== false) {
sendTxnEmail(receiver, 'new-pm', [
{ name: 'SENDER', content: senderName },
]);
}
if (receiver.preferences.pushNotifications.newPM !== false && messageSent.unformattedText) {
await sendPushNotification(
receiver,
{
title: translate(
'newPMNotificationTitle',
{ name: getUserInfo(sender, ['name']).name },
receiver.preferences.language,
),
message: messageSent.unformattedText,
identifier: 'newPM',
category: 'newPM',
payload: { replyTo: sender._id, senderName, message: messageSent.unformattedText },
},
);
}
}
return messageSent;
}
const PM_PER_PAGE = 10;
const getUserInboxDefaultOptions = {
asArray: true,
page: undefined,
conversation: null,
mapProps: false,
};
export async function getUserInbox (user, optionParams = getUserInboxDefaultOptions) {
// if not all properties are passed, fill the default values
const options = { ...getUserInboxDefaultOptions, ...optionParams };
const findObj = { ownerId: user._id };
if (options.conversation) {
findObj.uuid = options.conversation;
}
let query = inboxModel
.find(findObj)
.sort({ timestamp: -1 });
if (typeof options.page !== 'undefined') {
query = query
.skip(PM_PER_PAGE * Number(options.page))
.limit(PM_PER_PAGE);
} else {
// Limit for legacy calls that are not paginated to prevent database issues
query = query.limit(200);
}
const messages = (await query.lean().exec()).map(msgObj => {
delete msgObj.__v;
if (options.mapProps) {
mapInboxMessage(msgObj, user);
}
return msgObj;
});
if (options.asArray) {
return messages;
}
const messagesObj = {};
messages.forEach(msg => { messagesObj[msg._id] = msg; });
return messagesObj;
}
export async function applyLikeToMessages (user, uniqueMessages) {
const bulkWriteOperations = [];
for (const message of uniqueMessages) {
if (!message.likes) {
message.likes = {};
}
message.likes[user._id] = !message.likes[user._id];
bulkWriteOperations.push({
updateOne: {
filter: { _id: message._id },
update: {
$set: {
likes: message.likes,
},
},
},
});
}
await inboxModel.bulkWrite(bulkWriteOperations, {});
}
export async function getInboxMessagesByUniqueId (uniqueMessageId) {
return inboxModel
.find({ uniqueMessageId })
// prevents creating the proxies, no .save() and other stuff
.lean()
// since there can be only 2 messages maximum for this uniqueMessageId,
// this might speed up the query
.limit(2)
.exec();
}
export async function getUserInboxMessage (user, messageId) {
return inboxModel.findOne({ ownerId: user._id, _id: messageId }).exec();
}
export async function deleteMessage (user, messageId) {
const message = await inboxModel.findOne({ _id: messageId, ownerId: user._id }).exec();
if (!message) return false;
await inboxModel.deleteOne({ _id: message._id, ownerId: user._id }).exec();
return true;
}
export async function clearPMs (user) {
user.inbox.newMessages = 0;
await Promise.all([
user.save(),
inboxModel.deleteMany({ ownerId: user._id }).exec(),
]);
}