diff --git a/test/api/v3/integration/chat/POST-chat_seen.test.js b/test/api/v3/integration/chat/POST-chat_seen.test.js new file mode 100644 index 0000000000..6073fc1204 --- /dev/null +++ b/test/api/v3/integration/chat/POST-chat_seen.test.js @@ -0,0 +1,32 @@ +import { + createAndPopulateGroup, +} from '../../../../helpers/api-v3-integration.helper'; + +describe('POST /groups/:id/chat/seen', () => { + let groupWithChat, message, author, member; + + before(async () => { + let { group, groupLeader, members } = await createAndPopulateGroup({ + groupDetails: { + type: 'guild', + privacy: 'public', + }, + members: 1, + }); + + groupWithChat = group; + author = groupLeader; + member = members[0]; + + message = await author.post(`/groups/${groupWithChat._id}/chat`, { message: 'Some message' }); + message = message.message; + }); + + it('clears new messages', async () => { + await member.post(`/groups/${groupWithChat._id}/chat/seen`); + + let userThatHasSeenChat = await member.get('/user'); + + expect(userThatHasSeenChat.newMessages).to.be.empty; + }); +}); diff --git a/website/src/controllers/api-v3/chat.js b/website/src/controllers/api-v3/chat.js index 212e064441..bfca7eb3b7 100644 --- a/website/src/controllers/api-v3/chat.js +++ b/website/src/controllers/api-v3/chat.js @@ -105,7 +105,7 @@ api.postChat = { * @apiSuccess {Array} chat An array of chat messages */ api.likeChat = { - method: 'Post', + method: 'POST', url: '/groups/:groupId/chat/:chatId/like', middlewares: [authWithHeaders(), cron], async handler (req, res) { @@ -152,7 +152,7 @@ api.likeChat = { * @apiSuccess {Array} chat An array of chat messages */ api.flagChat = { - method: 'Post', + method: 'POST', url: '/groups/:groupId/chat/:chatId/flag', middlewares: [authWithHeaders(), cron], async handler (req, res) { @@ -256,4 +256,40 @@ api.flagChat = { }, }; +/** + * @api {post} /groups/:groupId/chat/:chatId/seen Seen a group chat message + * @apiVersion 3.0.0 + * @apiName SeenChat + * @apiGroup Chat + * + * @apiParam {groupId} groupId The group _id + * + * @apiSuccess {None} + */ +api.seenChat = { + method: 'POST', + url: '/groups/:groupId/chat/seen', + middlewares: [authWithHeaders(), cron], + async handler (req, res) { + let user = res.locals.user; + let groupId = req.params.groupId; + + req.checkParams('groupId', res.t('groupIdRequired')).notEmpty(); + + let validationErrors = req.validationErrors(); + if (validationErrors) throw validationErrors; + + let group = await Group.getGroup({user, groupId}); + if (!group) throw new NotFound(res.t('groupNotFound')); + + // Skip the auth step, we want this to be fast. If !found with uuid/token, then it just doesn't save + let update = { $unset: {} }; + + update.$unset[`newMessages.${groupId}`] = ''; + await User.update({_id: user._id}, update).exec(); + + res.respond(200); + }, +}; + export default api; diff --git a/website/src/models/group.js b/website/src/models/group.js index af592bb332..7e7e9c102b 100644 --- a/website/src/models/group.js +++ b/website/src/models/group.js @@ -205,7 +205,8 @@ schema.methods.sendChat = function sendChat (message, user) { // User.update({'profile.name':{$in:profileNames}},lastSeenUpdate,{multi:true}).exec(); } else { User.update({ - _id: {$in: this.members, $ne: user ? user._id : ''}, + guilds: this._id, + _id: { $ne: user ? user._id : ''}, }, lastSeenUpdate, {multi: true}).exec(); } };