Files
habitica/website/server/libs/inbox/index.js
Sabe Jones 8b569e2136 PMs rebuild (#11360)
* feat(messages): big PMs refactor

* add private messages route

* move to page

* WIP - header + begin with the sidebar

* extract userLabel + style sidebar + extract converstation item

* correct conversation item style

* toggle switch style

* add contributor / backer to conversation user-label

* fix shadows

* fix the conversations list (ignoring own sent)

* selected conversation label

* faceAvatar component

* fix message / avatar height

* fix message list / empty messages height

* new message padding/styles/functionality - finished sidebar conversation styling -

* fix loading messages + perfect-scrollbar

* fix load more line

* fix loading label

* open new conversation from outside

* if the user doesn't have avatar-data inside the conversation and does not exist anymore, just load/set the user name

* search bar new icon / style

* block using from conversation context-menu

* fix lint

* fix merge / lint

* fix merge

* first separate page

* fix tooltips + full width private message + card max width + more responsive

* separate conversations methods, to prevent circular deps

* update eslint config

* fix open new private message

* remove unneeded close icon + fix toggle-switch layout

* same content height on empty conversations - remove border / box-shadow

* canLoadMore = false

* remove inbox conditions on chat components

* hide footer / fix empty sidebar

* floating shadow

* remove tooltip on selected conversation user + pm always full-size

* show avatar on empty conversation

* disable face-avatar

* fix faceAvatar + story

* fix loading conversation messages while switching the conversation

* refresh private-messages page when you are already on it

* add countbadge knob to change the example

* fix lint

* fix hide tooltip + align header correctly

* disable perfect scroll

* load messages on refresh event

* fix header label + conversation actions not breaking layout on hover

* add gifting banner to the max height calculation

* correct chunk name

Co-authored-by: negue <negue@users.noreply.github.com>
Co-authored-by: Matteo Pagliazzi <matteopagliazzi@gmail.com>
2020-01-12 19:34:40 +01:00

103 lines
2.6 KiB
JavaScript

import { mapInboxMessage, inboxModel as Inbox } from '../../models/message';
import { getUserInfo, sendTxn as sendTxnEmail } from '../email'; // eslint-disable-line import/no-cycle
import { sendNotification as sendPushNotification } from '../pushNotifications';
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', [
{ name: 'SENDER', content: senderName },
]);
}
if (receiver.preferences.pushNotifications.newPM !== false) {
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 },
},
);
}
return messageSent;
}
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;
}
const findObj = { ownerId: user._id };
if (options.conversation) {
findObj.uuid = options.conversation;
}
let query = Inbox
.find(findObj)
.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;
});
if (options.asArray) {
return messages;
}
const messagesObj = {};
messages.forEach(msg => { messagesObj[msg._id] = msg; });
return messagesObj;
}
export async function getUserInboxMessage (user, messageId) {
return Inbox.findOne({ ownerId: user._id, _id: messageId }).exec();
}
export async function deleteMessage (user, messageId) {
const message = await Inbox.findOne({ _id: messageId, ownerId: user._id }).exec();
if (!message) return false;
await Inbox.remove({ _id: message._id, ownerId: user._id }).exec();
return true;
}
export async function clearPMs (user) {
user.inbox.newMessages = 0;
await Promise.all([
user.save(),
Inbox.remove({ ownerId: user._id }).exec(),
]);
}