Files
habitica/website/server/libs/inbox/index.js
negue 8b15d94ae1 Merge remote-tracking branch 'origin/develop' into negue/flagpm
# Conflicts:
#	website/client/components/chat/chatCard.vue
#	website/client/components/chat/chatMessages.vue
#	website/common/locales/en/messages.json
2018-11-18 22:04:33 +01:00

45 lines
1.1 KiB
JavaScript

import { inboxModel as Inbox } from '../../models/message';
export async function getUserInbox (user, asArray = true) {
const messages = (await Inbox
.find({ownerId: user._id})
.sort({timestamp: -1})
.exec()).map(msg => msg.toJSON());
if (asArray) {
return messages;
} else {
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(),
]);
}
export async function updateMessage (message) {
await Inbox.update({
id: message._id,
}, message).exec();
}