Merge pull request #7930 from crookedneighbor/flag_improvements

Flag improvements
This commit is contained in:
Blade Barringer 2016-08-24 22:28:26 -05:00 committed by GitHub
commit 7f50532e8b
2 changed files with 56 additions and 4 deletions

View file

@ -8,7 +8,7 @@ describe('POST /chat/:chatId/flag', () => {
let user, admin, anotherUser, group;
const TEST_MESSAGE = 'Test Message';
before(async () => {
beforeEach(async () => {
user = await generateUser({balance: 1});
admin = await generateUser({balance: 1, 'contributor.admin': true});
anotherUser = await generateUser();
@ -83,6 +83,41 @@ describe('POST /chat/:chatId/flag', () => {
expect(messageToCheck.flagCount).to.equal(5);
});
it('allows admin to flag a message in a private group', async () => {
let privateGroup = await user.post('/groups', {
name: 'Test party',
type: 'party',
privacy: 'private',
});
let { message } = await user.post(`/groups/${privateGroup._id}/chat`, {message: TEST_MESSAGE});
let flagResult = await admin.post(`/groups/${privateGroup._id}/chat/${message.id}/flag`);
expect(flagResult.flags[admin._id]).to.equal(true);
expect(flagResult.flagCount).to.equal(5);
let groupWithFlags = await user.get(`/groups/${privateGroup._id}`);
let messageToCheck = find(groupWithFlags.chat, {id: message.id});
expect(messageToCheck).to.not.exist;
});
it('does not allow non member to flag message in private group', async () => {
let privateGroup = await user.post('/groups', {
name: 'Test party',
type: 'party',
privacy: 'private',
});
let { message } = await user.post(`/groups/${privateGroup._id}/chat`, {message: TEST_MESSAGE});
await expect(anotherUser.post(`/groups/${privateGroup._id}/chat/${message.id}/flag`))
.to.eventually.be.rejected.and.eql({
code: 404,
error: 'NotFound',
message: t('groupNotFound'),
});
});
it('Returns an error when user tries to flag a message that is already flagged', async () => {
let { message } = await anotherUser.post(`/groups/${group._id}/chat`, {message: TEST_MESSAGE});

View file

@ -159,14 +159,27 @@ api.likeChat = {
/**
* @api {post} /api/v3/groups/:groupId/chat/:chatId/flag Flag a group chat message
* @apiVersion 3.0.0
* @apiDescription A message will be hidden from chat if two or more users flag a message. It will be hidden immediately if a moderator flags the message. An email is sent to the moderators about every flagged message.
* @apiName FlagChat
* @apiGroup Chat
*
* @apiParam {UUID} groupId The group _id ('party' for the user party and 'habitrpg' for tavern are accepted)
* @apiParam {UUID} groupId The group id ('party' for the user party and 'habitrpg' for tavern are accepted)
* @apiParam {UUID} chatId The chat message id
*
* @apiSuccess {Object} data The flagged chat message
* @apiSuccess {UUID} data.id The id of the message
* @apiSuccess {String} data.text The text of the message
* @apiSuccess {Number} data.timestamp The timestamp of the message in milliseconds
* @apiSuccess {Object} data.likes The likes of the message
* @apiSuccess {Object} data.flags The flags of the message
* @apiSuccess {Number} data.flagCount The number of flags the message has
* @apiSuccess {UUID} data.uuid The user id of the author of the message
* @apiSuccess {String} data.user The username of the author of the message
*
* @apiError GroupNotFound Group could not be found or you don't have access
* @apiError ChatNotFound Chat message with specified id could not be found
* @apiError FlagOwnMessage Chat messages cannot be flagged by the author of the message
* @apiError AlreadyFlagged Chat messages cannot be flagged more than once by a user
*/
api.flagChat = {
method: 'POST',
@ -182,7 +195,11 @@ api.flagChat = {
let validationErrors = req.validationErrors();
if (validationErrors) throw validationErrors;
let group = await Group.getGroup({user, groupId});
let group = await Group.getGroup({
user,
groupId,
optionalMembership: user.contributor.admin,
});
if (!group) throw new NotFound(res.t('groupNotFound'));
let message = _.find(group.chat, {id: req.params.chatId});