mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 22:57:21 +01:00
Cleanup after inbox migration (#10487)
This commit is contained in:
@@ -20,8 +20,8 @@ describe('DELETE user message', () => {
|
||||
|
||||
messagesId = Object.keys(userRes.inbox.messages);
|
||||
expect(messagesId.length).to.eql(2);
|
||||
expect(userRes.inbox.messages[messagesId[0]].text).to.eql('first');
|
||||
expect(userRes.inbox.messages[messagesId[1]].text).to.eql('second');
|
||||
expect(userRes.inbox.messages[messagesId[0]].text).to.eql('second');
|
||||
expect(userRes.inbox.messages[messagesId[1]].text).to.eql('first');
|
||||
});
|
||||
|
||||
it('one message', async () => {
|
||||
@@ -31,7 +31,7 @@ describe('DELETE user message', () => {
|
||||
|
||||
let userRes = await user.get('/user');
|
||||
expect(Object.keys(userRes.inbox.messages).length).to.eql(1);
|
||||
expect(userRes.inbox.messages[messagesId[0]].text).to.eql('second');
|
||||
expect(userRes.inbox.messages[messagesId[0]].text).to.eql('first');
|
||||
});
|
||||
|
||||
it('clear all', async () => {
|
||||
|
||||
@@ -53,6 +53,6 @@ describe('GET /user', () => {
|
||||
let returnedUser = await user.get('/user');
|
||||
|
||||
expect(returnedUser._id).to.equal(user._id);
|
||||
expect(returnedUser.inbox.messages).to.be.empty;
|
||||
expect(returnedUser.inbox.messages).to.be.undefined;
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,18 +1,15 @@
|
||||
import { inboxModel as Inbox } from '../../models/message';
|
||||
import { toArray, orderBy } from 'lodash';
|
||||
|
||||
export async function getUserInbox (user, asArray = true) {
|
||||
const messages = (await Inbox
|
||||
.find({ownerId: user._id})
|
||||
.sort({timestamp: -1})
|
||||
.exec()).map(msg => msg.toJSON());
|
||||
|
||||
const messagesObj = Object.assign({}, user.inbox.messages); // copy, shallow clone
|
||||
|
||||
if (asArray) {
|
||||
messages.push(...toArray(messagesObj));
|
||||
|
||||
return orderBy(messages, ['timestamp'], ['desc']);
|
||||
return messages;
|
||||
} else {
|
||||
const messagesObj = {};
|
||||
messages.forEach(msg => messagesObj[msg._id] = msg);
|
||||
|
||||
return messagesObj;
|
||||
@@ -20,15 +17,9 @@ export async function getUserInbox (user, asArray = true) {
|
||||
}
|
||||
|
||||
export async function deleteMessage (user, messageId) {
|
||||
if (user.inbox.messages[messageId]) { // compatibility
|
||||
delete user.inbox.messages[messageId];
|
||||
user.markModified(`inbox.messages.${messageId}`);
|
||||
await user.save();
|
||||
} else {
|
||||
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();
|
||||
}
|
||||
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;
|
||||
}
|
||||
@@ -36,10 +27,6 @@ export async function deleteMessage (user, messageId) {
|
||||
export async function clearPMs (user) {
|
||||
user.inbox.newMessages = 0;
|
||||
|
||||
// compatibility
|
||||
user.inbox.messages = {};
|
||||
user.markModified('inbox.messages');
|
||||
|
||||
await Promise.all([
|
||||
user.save(),
|
||||
Inbox.remove({ownerId: user._id}).exec(),
|
||||
|
||||
@@ -559,11 +559,7 @@ let schema = new Schema({
|
||||
tags: [TagSchema],
|
||||
|
||||
inbox: {
|
||||
// messages are stored in the Inbox collection, this path will be removed
|
||||
// as soon as the migration has run and all the messages have been removed from here
|
||||
messages: {type: Schema.Types.Mixed, default: () => {
|
||||
return {};
|
||||
}},
|
||||
// messages are stored in the Inbox collection
|
||||
newMessages: {type: Number, default: 0},
|
||||
blocks: {type: Array, default: () => []},
|
||||
optOut: {type: Boolean, default: false},
|
||||
|
||||
Reference in New Issue
Block a user