3 var unreadItemService = require('gitter-web-unread-items');
4 var recentRoomService = require('gitter-web-rooms/lib/recent-room-service');
5 var StatusError = require('statuserror');
6 var uniqueIds = require('mongodb-unique-ids');
7 var mongoUtils = require('gitter-web-persistence-utils/lib/mongo-utils');
11 index: function(req) {
12 var userId = req.resourceUser.id;
14 return unreadItemService.getUnreadItemsForUser(userId, req.params.userTroupeId);
17 create: function(req) {
18 var unreadItems = req.body;
19 if (!unreadItems) throw new StatusError(400, 'No body');
23 /* TODO: remove mentions in February 2015 */
24 if (Array.isArray(unreadItems.mention)) allIds = allIds.concat(unreadItems.mention);
25 if (Array.isArray(unreadItems.chat)) allIds = allIds.concat(unreadItems.chat);
27 if (Array.isArray(unreadItems.mention) && Array.isArray(unreadItems.chat)) {
28 allIds = uniqueIds(allIds);
31 if (!allIds.length) throw new StatusError(400); /* You comin at me bro? */
33 return unreadItemService
34 .markItemsRead(req.resourceUser.id, req.params.userTroupeId, allIds)
36 return { success: true };
40 update: function(req) {
41 var lastSeenItem = req.unreadItem;
42 var lastAccessTime = mongoUtils.getDateFromObjectId(lastSeenItem);
43 if (lastAccessTime > new Date()) {
44 throw new StatusError(400, 'Invalid last seen item');
47 return recentRoomService.saveLastVisitedTroupeforUserId(
49 req.params.userTroupeId,
51 lastAccessTime: lastAccessTime
56 destroy: function(req) {
57 if (req.params.unreadItem.toLowerCase() !== 'all') throw new StatusError(404);
59 return unreadItemService
60 .markAllChatsRead(req.resourceUser.id, req.params.userTroupeId, { member: true })
62 return { success: true };