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