mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-18 07:07:35 +01:00
fix: prevent 500 error when unflagging a system message
This commit is contained in:
@@ -73,6 +73,36 @@ describe('POST /groups/:id/chat/:id/clearflags', () => {
|
||||
let messages = await members[0].get(`/groups/${group._id}/chat`);
|
||||
expect(messages[0].flagCount).to.eql(0);
|
||||
});
|
||||
|
||||
it('can unflag a system message', async () => {
|
||||
let { group, members } = await createAndPopulateGroup({
|
||||
groupDetails: {
|
||||
type: 'party',
|
||||
privacy: 'private',
|
||||
},
|
||||
members: 1,
|
||||
});
|
||||
|
||||
let member = members[0];
|
||||
|
||||
// make member that can use skills
|
||||
await member.update({
|
||||
'stats.lvl': 100,
|
||||
'stats.mp': 400,
|
||||
'stats.class': 'wizard',
|
||||
});
|
||||
|
||||
await member.post('/user/class/cast/mpheal');
|
||||
|
||||
let [skillMsg] = await member.get(`/groups/${group.id}/chat`);
|
||||
|
||||
await member.post(`/groups/${group._id}/chat/${skillMsg.id}/flag`);
|
||||
await admin.post(`/groups/${group._id}/chat/${skillMsg.id}/clearflags`);
|
||||
|
||||
let messages = await members[0].get(`/groups/${group._id}/chat`);
|
||||
expect(messages[0].id).to.eql(skillMsg.id);
|
||||
expect(messages[0].flagCount).to.eql(0);
|
||||
});
|
||||
});
|
||||
|
||||
context('admin user, group with multiple messages', () => {
|
||||
|
||||
@@ -19,6 +19,22 @@ const FLAG_REPORT_EMAILS = nconf.get('FLAG_REPORT_EMAIL').split(',').map((email)
|
||||
|
||||
let api = {};
|
||||
|
||||
async function getAuthorEmailFromMessage (message) {
|
||||
let authorId = message.uuid;
|
||||
|
||||
if (authorId === 'system') {
|
||||
return 'system';
|
||||
}
|
||||
|
||||
let author = await User.findOne({_id: authorId}, {auth: 1});
|
||||
|
||||
if (author) {
|
||||
return getUserInfo(author, ['email']).email;
|
||||
} else {
|
||||
return 'Author Account Deleted';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @api {get} /api/v3/groups/:groupId/chat Get chat messages from a group
|
||||
* @apiVersion 3.0.0
|
||||
@@ -208,8 +224,6 @@ api.flagChat = {
|
||||
|
||||
if (message.uuid === user._id) throw new NotFound(res.t('messageGroupChatFlagOwnMessage'));
|
||||
|
||||
let author = await User.findOne({_id: message.uuid}, {auth: 1});
|
||||
|
||||
let update = {$set: {}};
|
||||
|
||||
// Log user ids that have flagged the message
|
||||
@@ -234,16 +248,7 @@ api.flagChat = {
|
||||
);
|
||||
|
||||
let reporterEmailContent = getUserInfo(user, ['email']).email;
|
||||
|
||||
let authorEmailContent;
|
||||
if (author) {
|
||||
authorEmailContent = getUserInfo(author, ['email']).email;
|
||||
} else if (message.uuid === 'system') {
|
||||
authorEmailContent = 'system';
|
||||
} else {
|
||||
authorEmailContent = 'Author Account Deleted';
|
||||
}
|
||||
|
||||
let authorEmail = await getAuthorEmailFromMessage(message);
|
||||
let groupUrl = getGroupUrl(group);
|
||||
|
||||
sendTxn(FLAG_REPORT_EMAILS, 'flag-report-to-mods', [
|
||||
@@ -257,7 +262,7 @@ api.flagChat = {
|
||||
|
||||
{name: 'AUTHOR_USERNAME', content: message.user},
|
||||
{name: 'AUTHOR_UUID', content: message.uuid},
|
||||
{name: 'AUTHOR_EMAIL', content: authorEmailContent},
|
||||
{name: 'AUTHOR_EMAIL', content: authorEmail},
|
||||
{name: 'AUTHOR_MODAL_URL', content: `/static/front/#?memberId=${message.uuid}`},
|
||||
|
||||
{name: 'GROUP_NAME', content: group.name},
|
||||
@@ -329,11 +334,7 @@ api.clearChatFlags = {
|
||||
);
|
||||
|
||||
let adminEmailContent = getUserInfo(user, ['email']).email;
|
||||
|
||||
let author = await User.findOne({_id: message.uuid}, {auth: 1});
|
||||
|
||||
let authorEmailContent = getUserInfo(author, ['email']).email;
|
||||
|
||||
let authorEmail = getAuthorEmailFromMessage(message);
|
||||
let groupUrl = getGroupUrl(group);
|
||||
|
||||
sendTxn(FLAG_REPORT_EMAILS, 'unflag-report-to-mods', [
|
||||
@@ -347,7 +348,7 @@ api.clearChatFlags = {
|
||||
|
||||
{name: 'AUTHOR_USERNAME', content: message.user},
|
||||
{name: 'AUTHOR_UUID', content: message.uuid},
|
||||
{name: 'AUTHOR_EMAIL', content: authorEmailContent},
|
||||
{name: 'AUTHOR_EMAIL', content: authorEmail},
|
||||
{name: 'AUTHOR_MODAL_URL', content: `/static/front/#?memberId=${message.uuid}`},
|
||||
|
||||
{name: 'GROUP_NAME', content: group.name},
|
||||
|
||||
Reference in New Issue
Block a user