From 788b1e6b7c5df0848c6796bf0b7932801ed324e6 Mon Sep 17 00:00:00 2001 From: Eugene Bolshakov Date: Sun, 11 Feb 2018 16:25:12 +0300 Subject: [PATCH 01/12] Add countVisibleByRoomIdBetweenTimestampsInclusive Signed-off-by: Eugene Bolshakov --- packages/rocketchat-lib/server/models/Messages.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/packages/rocketchat-lib/server/models/Messages.js b/packages/rocketchat-lib/server/models/Messages.js index 90f8bf068765..8f20aa122066 100644 --- a/packages/rocketchat-lib/server/models/Messages.js +++ b/packages/rocketchat-lib/server/models/Messages.js @@ -20,6 +20,21 @@ RocketChat.models.Messages = new class extends RocketChat.models._Base { this.tryEnsureIndex({ 'slackBotId': 1, 'slackTs': 1 }, { sparse: 1 }); } + countVisibleByRoomIdBetweenTimestampsInclusive(roomId, afterTimestamp, beforeTimestamp, options) { + const query = { + _hidden: { + $ne: true + }, + rid: roomId, + ts: { + $gte: afterTimestamp, + $lte: beforeTimestamp + } + }; + + return this.find(query, options).count(); + } + // FIND findByMention(username, options) { const query = {'mentions.username': username}; From 30a57d410e74b09eb19fa06651aa9cc2aa1eec32 Mon Sep 17 00:00:00 2001 From: Eugene Bolshakov Date: Sun, 11 Feb 2018 16:26:00 +0300 Subject: [PATCH 02/12] Add channels.counters, groups.counters, im.counters Signed-off-by: Eugene Bolshakov --- packages/rocketchat-api/server/v1/channels.js | 50 +++++++++++++++ packages/rocketchat-api/server/v1/groups.js | 62 +++++++++++++++++++ packages/rocketchat-api/server/v1/im.js | 48 ++++++++++++++ 3 files changed, 160 insertions(+) diff --git a/packages/rocketchat-api/server/v1/channels.js b/packages/rocketchat-api/server/v1/channels.js index 3c150d14ccd4..fcb9319c7702 100644 --- a/packages/rocketchat-api/server/v1/channels.js +++ b/packages/rocketchat-api/server/v1/channels.js @@ -133,6 +133,56 @@ RocketChat.API.v1.addRoute('channels.close', { authRequired: true }, { } }); +RocketChat.API.v1.addRoute('channels.counters', { authRequired: true }, { + get() { + const access = RocketChat.authz.hasPermission(this.userId, 'view-room-administration'); + const ruserId = this.requestParams().userId; + let user = this.userId; + let unreads = null; + let userMentions = null; + let unreadsFrom = null; + let joined = false; + let msgs = null; + let latest = null; + let members = null; + + if(ruserId) { + if (!access) { + return RocketChat.API.v1.unauthorized(); + } + user = ruserId; + } + const room = findChannelByIdOrName({ + params: this.requestParams(), + returnUsernames: true + }); + const channel = RocketChat.models.Subscriptions.findOneByRoomIdAndUserId(room._id, user); + + if(typeof channel !== 'undefined' && channel.open) { + unreads = RocketChat.models.Messages.countVisibleByRoomIdBetweenTimestampsInclusive(channel.rid, channel.ls, channel._room.lm); + userMentions = channel.userMentions; + unreadsFrom = channel.ls; + joined = true; + } + + if(access || joined) { + msgs = room.msgs; + latest = room.lm; + members = room.usernames.length; + } + + return RocketChat.API.v1.success({ + joined: joined, + members: members, + unreads: unreads, + unreadsFrom: unreadsFrom, + msgs: msgs, + latest: latest, + userMentions: userMentions + }); + } +}); + // Channel -> create function createChannelValidator(params) { diff --git a/packages/rocketchat-api/server/v1/groups.js b/packages/rocketchat-api/server/v1/groups.js index ccc7790978c5..4d3cb694356b 100644 --- a/packages/rocketchat-api/server/v1/groups.js +++ b/packages/rocketchat-api/server/v1/groups.js @@ -107,6 +107,68 @@ RocketChat.API.v1.addRoute('groups.close', { authRequired: true }, { } }); +RocketChat.API.v1.addRoute('groups.counters', { authRequired: true }, { + get() { + const access = RocketChat.authz.hasPermission(this.userId, 'view-room-administration'); + const params = this.requestParams(); + let user = this.userId; + let room; + let unreads = null; + let userMentions = null; + let unreadsFrom = null; + let joined = false; + + if ((!params.roomId || !params.roomId.trim()) && (!params.roomName || !params.roomName.trim())) { + throw new Meteor.Error('error-room-param-not-provided', 'The parameter "roomId" or "roomName" is required'); + } + + if (params.roomId) { + room = RocketChat.models.Rooms.findOneById(params.roomId); + } else if (params.roomName) { + room = RocketChat.models.Rooms.findOneByName(params.roomName); + } + + if (!room || room.t !== 'p') { + throw new Meteor.Error('error-room-not-found', 'The required "roomId" or "roomName" param provided does not match any group'); + } + + if (room.archived) { + throw new Meteor.Error('error-room-archived', `The private group, ${ room.name }, is archived`); + } + + if(params.userId) { + if (!access) { + return RocketChat.API.v1.unauthorized(); + } + user = params.userId; + } + const group = RocketChat.models.Subscriptions.findOneByRoomIdAndUserId(room._id, user); + + if(typeof group !== 'undefined' && group.open) { + unreads = RocketChat.models.Messages.countVisibleByRoomIdBetweenTimestampsInclusive(group.rid, group.ls, group._room.lm); + userMentions = group.userMentions; + unreadsFrom = group.ls; + joined = true; + } + + if(access || joined) { + msgs = room.msgs; + latest = room.lm; + members = room.usernames.length; + } + + return RocketChat.API.v1.success({ + joined: joined, + members: members, + unreads: unreads, + unreadsFrom: unreadsFrom, + msgs: msgs, + latest: latest, + userMentions: userMentions + }); + } +}); + //Create Private Group RocketChat.API.v1.addRoute('groups.create', { authRequired: true }, { post() { diff --git a/packages/rocketchat-api/server/v1/im.js b/packages/rocketchat-api/server/v1/im.js index f5bff08b80c4..15f46239089b 100644 --- a/packages/rocketchat-api/server/v1/im.js +++ b/packages/rocketchat-api/server/v1/im.js @@ -49,6 +49,54 @@ RocketChat.API.v1.addRoute(['dm.close', 'im.close'], { authRequired: true }, { } }); +RocketChat.API.v1.addRoute(['dm.counters', 'im.counters'], { authRequired: true }, { + get() { + const access = RocketChat.authz.hasPermission(this.userId, 'view-room-administration'); + const ruserId = this.requestParams().userId; + let user = this.userId; + let unreads = null; + let userMentions = null; + let unreadsFrom = null; + let joined = false; + let msgs = null; + let latest = null; + let members = null; + + if(ruserId) { + if (!access) { + return RocketChat.API.v1.unauthorized(); + } + user = ruserId; + } + const rs = findDirectMessageRoom(this.requestParams(), { '_id': user} ); + const room = rs.room; + const dm = rs.subscription; + + if(typeof dm !== 'undefined' && dm.open) { + unreads = dm.unread; + userMentions = dm.userMentions; + unreadsFrom = dm.ls; + joined = true; + } + + if(access || joined) { + msgs = room.msgs; + latest = room.lm; + members = room.usernames.length; + } + + return RocketChat.API.v1.success({ + joined: joined, + members: members, + unreads: unreads, + unreadsFrom: unreadsFrom, + msgs: msgs, + latest: latest, + userMentions: userMentions + }); + } +}); + RocketChat.API.v1.addRoute(['dm.files', 'im.files'], { authRequired: true }, { get() { const findResult = findDirectMessageRoom(this.requestParams(), this.user); From 032071b6773d29c048d7a2796964193b9afc1119 Mon Sep 17 00:00:00 2001 From: Eugene Bolshakov Date: Sun, 11 Feb 2018 17:19:35 +0300 Subject: [PATCH 03/12] Fix spaces Signed-off-by: Eugene Bolshakov --- packages/rocketchat-api/server/v1/channels.js | 14 +++++++------- packages/rocketchat-api/server/v1/groups.js | 10 +++++----- packages/rocketchat-api/server/v1/im.js | 18 +++++++++--------- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/packages/rocketchat-api/server/v1/channels.js b/packages/rocketchat-api/server/v1/channels.js index fcb9319c7702..8718041820fa 100644 --- a/packages/rocketchat-api/server/v1/channels.js +++ b/packages/rocketchat-api/server/v1/channels.js @@ -146,7 +146,7 @@ RocketChat.API.v1.addRoute('channels.counters', { authRequired: true }, { let latest = null; let members = null; - if(ruserId) { + if (ruserId) { if (!access) { return RocketChat.API.v1.unauthorized(); } @@ -158,19 +158,19 @@ RocketChat.API.v1.addRoute('channels.counters', { authRequired: true }, { }); const channel = RocketChat.models.Subscriptions.findOneByRoomIdAndUserId(room._id, user); - if(typeof channel !== 'undefined' && channel.open) { + if (typeof channel !== 'undefined' && channel.open) { unreads = RocketChat.models.Messages.countVisibleByRoomIdBetweenTimestampsInclusive(channel.rid, channel.ls, channel._room.lm); - userMentions = channel.userMentions; + userMentions = channel.userMentions; unreadsFrom = channel.ls; joined = true; } - - if(access || joined) { + + if (access || joined) { msgs = room.msgs; latest = room.lm; - members = room.usernames.length; + members = room.usernames.length; } - + return RocketChat.API.v1.success({ joined: joined, members: members, diff --git a/packages/rocketchat-api/server/v1/groups.js b/packages/rocketchat-api/server/v1/groups.js index 4d3cb694356b..fca532ca3774 100644 --- a/packages/rocketchat-api/server/v1/groups.js +++ b/packages/rocketchat-api/server/v1/groups.js @@ -136,7 +136,7 @@ RocketChat.API.v1.addRoute('groups.counters', { authRequired: true }, { throw new Meteor.Error('error-room-archived', `The private group, ${ room.name }, is archived`); } - if(params.userId) { + if (params.userId) { if (!access) { return RocketChat.API.v1.unauthorized(); } @@ -144,17 +144,17 @@ RocketChat.API.v1.addRoute('groups.counters', { authRequired: true }, { } const group = RocketChat.models.Subscriptions.findOneByRoomIdAndUserId(room._id, user); - if(typeof group !== 'undefined' && group.open) { + if (typeof group !== 'undefined' && group.open) { unreads = RocketChat.models.Messages.countVisibleByRoomIdBetweenTimestampsInclusive(group.rid, group.ls, group._room.lm); - userMentions = group.userMentions; + userMentions = group.userMentions; unreadsFrom = group.ls; joined = true; } - if(access || joined) { + if (access || joined) { msgs = room.msgs; latest = room.lm; - members = room.usernames.length; + members = room.usernames.length; } return RocketChat.API.v1.success({ diff --git a/packages/rocketchat-api/server/v1/im.js b/packages/rocketchat-api/server/v1/im.js index 15f46239089b..50345c017957 100644 --- a/packages/rocketchat-api/server/v1/im.js +++ b/packages/rocketchat-api/server/v1/im.js @@ -51,7 +51,7 @@ RocketChat.API.v1.addRoute(['dm.close', 'im.close'], { authRequired: true }, { RocketChat.API.v1.addRoute(['dm.counters', 'im.counters'], { authRequired: true }, { get() { - const access = RocketChat.authz.hasPermission(this.userId, 'view-room-administration'); + const access = RocketChat.authz.hasPermission(this.userId, 'view-room-administration'); const ruserId = this.requestParams().userId; let user = this.userId; let unreads = null; @@ -61,8 +61,8 @@ RocketChat.API.v1.addRoute(['dm.counters', 'im.counters'], { authRequired: true let msgs = null; let latest = null; let members = null; - - if(ruserId) { + + if (ruserId) { if (!access) { return RocketChat.API.v1.unauthorized(); } @@ -71,18 +71,18 @@ RocketChat.API.v1.addRoute(['dm.counters', 'im.counters'], { authRequired: true const rs = findDirectMessageRoom(this.requestParams(), { '_id': user} ); const room = rs.room; const dm = rs.subscription; - - if(typeof dm !== 'undefined' && dm.open) { + + if (typeof dm !== 'undefined' && dm.open) { unreads = dm.unread; - userMentions = dm.userMentions; + userMentions = dm.userMentions; unreadsFrom = dm.ls; joined = true; } - - if(access || joined) { + + if (access || joined) { msgs = room.msgs; latest = room.lm; - members = room.usernames.length; + members = room.usernames.length; } return RocketChat.API.v1.success({ From ee40cbc78a3bd72dcd52f340a22649364d0f336e Mon Sep 17 00:00:00 2001 From: Eugene Bolshakov Date: Sun, 11 Feb 2018 19:55:27 +0300 Subject: [PATCH 04/12] Fixes Signed-off-by: Eugene Bolshakov --- packages/rocketchat-api/server/v1/channels.js | 16 ++++++++-------- packages/rocketchat-api/server/v1/groups.js | 19 +++++++++++-------- packages/rocketchat-api/server/v1/im.js | 14 +++++++------- 3 files changed, 26 insertions(+), 23 deletions(-) diff --git a/packages/rocketchat-api/server/v1/channels.js b/packages/rocketchat-api/server/v1/channels.js index 8718041820fa..8a702c2e27b2 100644 --- a/packages/rocketchat-api/server/v1/channels.js +++ b/packages/rocketchat-api/server/v1/channels.js @@ -145,7 +145,7 @@ RocketChat.API.v1.addRoute('channels.counters', { authRequired: true }, { let msgs = null; let latest = null; let members = null; - + if (ruserId) { if (!access) { return RocketChat.API.v1.unauthorized(); @@ -172,13 +172,13 @@ RocketChat.API.v1.addRoute('channels.counters', { authRequired: true }, { } return RocketChat.API.v1.success({ - joined: joined, - members: members, - unreads: unreads, - unreadsFrom: unreadsFrom, - msgs: msgs, - latest: latest, - userMentions: userMentions + joined, + members, + unreads, + unreadsFrom, + msgs, + latest, + userMentions }); } }); diff --git a/packages/rocketchat-api/server/v1/groups.js b/packages/rocketchat-api/server/v1/groups.js index fca532ca3774..579c0229eb65 100644 --- a/packages/rocketchat-api/server/v1/groups.js +++ b/packages/rocketchat-api/server/v1/groups.js @@ -117,6 +117,9 @@ RocketChat.API.v1.addRoute('groups.counters', { authRequired: true }, { let userMentions = null; let unreadsFrom = null; let joined = false; + let msgs = null; + let latest = null; + let members = null; if ((!params.roomId || !params.roomId.trim()) && (!params.roomName || !params.roomName.trim())) { throw new Meteor.Error('error-room-param-not-provided', 'The parameter "roomId" or "roomName" is required'); @@ -150,7 +153,7 @@ RocketChat.API.v1.addRoute('groups.counters', { authRequired: true }, { unreadsFrom = group.ls; joined = true; } - + if (access || joined) { msgs = room.msgs; latest = room.lm; @@ -158,13 +161,13 @@ RocketChat.API.v1.addRoute('groups.counters', { authRequired: true }, { } return RocketChat.API.v1.success({ - joined: joined, - members: members, - unreads: unreads, - unreadsFrom: unreadsFrom, - msgs: msgs, - latest: latest, - userMentions: userMentions + joined, + members, + unreads, + unreadsFrom, + msgs, + latest, + userMentions }); } }); diff --git a/packages/rocketchat-api/server/v1/im.js b/packages/rocketchat-api/server/v1/im.js index 50345c017957..4040e299111e 100644 --- a/packages/rocketchat-api/server/v1/im.js +++ b/packages/rocketchat-api/server/v1/im.js @@ -86,13 +86,13 @@ RocketChat.API.v1.addRoute(['dm.counters', 'im.counters'], { authRequired: true } return RocketChat.API.v1.success({ - joined: joined, - members: members, - unreads: unreads, - unreadsFrom: unreadsFrom, - msgs: msgs, - latest: latest, - userMentions: userMentions + joined, + members, + unreads, + unreadsFrom, + msgs, + latest, + userMentions }); } }); From bab013b6ab04388d7da0e80334716d564c4ba4ff Mon Sep 17 00:00:00 2001 From: Eugene Bolshakov Date: Sun, 11 Feb 2018 20:04:22 +0300 Subject: [PATCH 05/12] Small fix Signed-off-by: Eugene Bolshakov --- packages/rocketchat-api/server/v1/im.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/rocketchat-api/server/v1/im.js b/packages/rocketchat-api/server/v1/im.js index 4040e299111e..105469dccbf6 100644 --- a/packages/rocketchat-api/server/v1/im.js +++ b/packages/rocketchat-api/server/v1/im.js @@ -68,7 +68,7 @@ RocketChat.API.v1.addRoute(['dm.counters', 'im.counters'], { authRequired: true } user = ruserId; } - const rs = findDirectMessageRoom(this.requestParams(), { '_id': user} ); + const rs = findDirectMessageRoom(this.requestParams(), { '_id': user } ); const room = rs.room; const dm = rs.subscription; From 8c014fcff75d12411eb22420081edab8208fe659 Mon Sep 17 00:00:00 2001 From: Eugene Bolshakov Date: Sun, 11 Feb 2018 20:12:38 +0300 Subject: [PATCH 06/12] Small fix #2 Signed-off-by: Eugene Bolshakov --- packages/rocketchat-api/server/v1/im.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/rocketchat-api/server/v1/im.js b/packages/rocketchat-api/server/v1/im.js index 105469dccbf6..c4d3df89aa7c 100644 --- a/packages/rocketchat-api/server/v1/im.js +++ b/packages/rocketchat-api/server/v1/im.js @@ -68,7 +68,7 @@ RocketChat.API.v1.addRoute(['dm.counters', 'im.counters'], { authRequired: true } user = ruserId; } - const rs = findDirectMessageRoom(this.requestParams(), { '_id': user } ); + const rs = findDirectMessageRoom( this.requestParams(), { '_id': user } ); const room = rs.room; const dm = rs.subscription; From 4caed2d228d3fd39d78e54d2a1f91bf0f8e73d25 Mon Sep 17 00:00:00 2001 From: Eugene Bolshakov Date: Sun, 11 Feb 2018 20:18:15 +0300 Subject: [PATCH 07/12] Small fix #3 Signed-off-by: Eugene Bolshakov --- packages/rocketchat-api/server/v1/im.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/rocketchat-api/server/v1/im.js b/packages/rocketchat-api/server/v1/im.js index c4d3df89aa7c..18aedb586285 100644 --- a/packages/rocketchat-api/server/v1/im.js +++ b/packages/rocketchat-api/server/v1/im.js @@ -68,7 +68,7 @@ RocketChat.API.v1.addRoute(['dm.counters', 'im.counters'], { authRequired: true } user = ruserId; } - const rs = findDirectMessageRoom( this.requestParams(), { '_id': user } ); + const rs = findDirectMessageRoom(this.requestParams(), {'_id': user}); const room = rs.room; const dm = rs.subscription; From 4d6bea4c1a418c79039d52480a636d566c64debb Mon Sep 17 00:00:00 2001 From: Eugene Bolshakov Date: Fri, 23 Feb 2018 18:10:43 +0300 Subject: [PATCH 08/12] Add channels.couters and groups.couters tests Signed-off-by: Eugene Bolshakov --- tests/end-to-end/api/02-channels.js | 21 +++++++++++++++++++++ tests/end-to-end/api/03-groups.js | 23 ++++++++++++++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/tests/end-to-end/api/02-channels.js b/tests/end-to-end/api/02-channels.js index 784263e7b8ce..d3ccc217e783 100644 --- a/tests/end-to-end/api/02-channels.js +++ b/tests/end-to-end/api/02-channels.js @@ -381,6 +381,27 @@ describe('[Channels]', function() { .end(done); }); + it('/channels.couters', (done) => { + request.get(api('channels.couters')) + .set(credentials) + .query({ + roomId: channel._id + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + expect(res.body).to.have.property('joined', true); + expect(res.body).to.have.property('members'); + expect(res.body).to.have.property('unreads'); + expect(res.body).to.have.property('unreadsFrom'); + expect(res.body).to.have.property('msgs'); + expect(res.body).to.have.property('latest'); + expect(res.body).to.have.property('userMentions'); + }) + .end(done); + }); + it('/channels.rename', async(done) => { const roomInfo = await getRoomInfo(channel._id); diff --git a/tests/end-to-end/api/03-groups.js b/tests/end-to-end/api/03-groups.js index 8ce141a0ac32..a3a227a54f63 100644 --- a/tests/end-to-end/api/03-groups.js +++ b/tests/end-to-end/api/03-groups.js @@ -19,7 +19,7 @@ function getRoomInfo(roomId) { }); } -describe('groups', function() { +describe('[Groups]', function() { this.retries(0); before(done => getCredentials(done)); @@ -343,6 +343,27 @@ describe('groups', function() { .end(done); }); + it('/groups.couters', (done) => { + request.get(api('groups.couters')) + .set(credentials) + .query({ + roomId: group._id + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + expect(res.body).to.have.property('joined', true); + expect(res.body).to.have.property('members'); + expect(res.body).to.have.property('unreads'); + expect(res.body).to.have.property('unreadsFrom'); + expect(res.body).to.have.property('msgs'); + expect(res.body).to.have.property('latest'); + expect(res.body).to.have.property('userMentions'); + }) + .end(done); + }); + it('/groups.rename', async(done) => { const roomInfo = await getRoomInfo(group._id); From 9679b6a625b19dbe46496e2788534e90a9c05664 Mon Sep 17 00:00:00 2001 From: Eugene Bolshakov Date: Fri, 23 Feb 2018 19:19:29 +0300 Subject: [PATCH 09/12] Fix tests, last message and unread message times Signed-off-by: Eugene Bolshakov --- packages/rocketchat-api/server/v1/channels.js | 10 +++++++--- packages/rocketchat-api/server/v1/groups.js | 10 +++++++--- tests/end-to-end/api/02-channels.js | 4 ++-- tests/end-to-end/api/03-groups.js | 4 ++-- 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/packages/rocketchat-api/server/v1/channels.js b/packages/rocketchat-api/server/v1/channels.js index 8a702c2e27b2..8081a2dc7988 100644 --- a/packages/rocketchat-api/server/v1/channels.js +++ b/packages/rocketchat-api/server/v1/channels.js @@ -145,6 +145,7 @@ RocketChat.API.v1.addRoute('channels.counters', { authRequired: true }, { let msgs = null; let latest = null; let members = null; + let lm = null; if (ruserId) { if (!access) { @@ -157,17 +158,20 @@ RocketChat.API.v1.addRoute('channels.counters', { authRequired: true }, { returnUsernames: true }); const channel = RocketChat.models.Subscriptions.findOneByRoomIdAndUserId(room._id, user); + lm = channel._room.lm ? channel._room.lm : channel._room._updatedAt; if (typeof channel !== 'undefined' && channel.open) { - unreads = RocketChat.models.Messages.countVisibleByRoomIdBetweenTimestampsInclusive(channel.rid, channel.ls, channel._room.lm); + if (channel.ls) { + unreads = RocketChat.models.Messages.countVisibleByRoomIdBetweenTimestampsInclusive(channel.rid, channel.ls, lm); + unreadsFrom = channel.ls; + } userMentions = channel.userMentions; - unreadsFrom = channel.ls; joined = true; } if (access || joined) { msgs = room.msgs; - latest = room.lm; + latest = lm; members = room.usernames.length; } diff --git a/packages/rocketchat-api/server/v1/groups.js b/packages/rocketchat-api/server/v1/groups.js index 579c0229eb65..988d6db904e9 100644 --- a/packages/rocketchat-api/server/v1/groups.js +++ b/packages/rocketchat-api/server/v1/groups.js @@ -120,6 +120,7 @@ RocketChat.API.v1.addRoute('groups.counters', { authRequired: true }, { let msgs = null; let latest = null; let members = null; + let lm = null; if ((!params.roomId || !params.roomId.trim()) && (!params.roomName || !params.roomName.trim())) { throw new Meteor.Error('error-room-param-not-provided', 'The parameter "roomId" or "roomName" is required'); @@ -146,17 +147,20 @@ RocketChat.API.v1.addRoute('groups.counters', { authRequired: true }, { user = params.userId; } const group = RocketChat.models.Subscriptions.findOneByRoomIdAndUserId(room._id, user); + lm = group._room.lm ? group._room.lm : group._room._updatedAt; if (typeof group !== 'undefined' && group.open) { - unreads = RocketChat.models.Messages.countVisibleByRoomIdBetweenTimestampsInclusive(group.rid, group.ls, group._room.lm); + if (group.ls) { + unreads = RocketChat.models.Messages.countVisibleByRoomIdBetweenTimestampsInclusive(group.rid, group.ls, lm); + unreadsFrom = group.ls; + } userMentions = group.userMentions; - unreadsFrom = group.ls; joined = true; } if (access || joined) { msgs = room.msgs; - latest = room.lm; + latest = lm; members = room.usernames.length; } diff --git a/tests/end-to-end/api/02-channels.js b/tests/end-to-end/api/02-channels.js index d3ccc217e783..9c4eeba38a37 100644 --- a/tests/end-to-end/api/02-channels.js +++ b/tests/end-to-end/api/02-channels.js @@ -381,8 +381,8 @@ describe('[Channels]', function() { .end(done); }); - it('/channels.couters', (done) => { - request.get(api('channels.couters')) + it('/channels.counters', (done) => { + request.get(api('channels.counters')) .set(credentials) .query({ roomId: channel._id diff --git a/tests/end-to-end/api/03-groups.js b/tests/end-to-end/api/03-groups.js index a3a227a54f63..83fb50a16df4 100644 --- a/tests/end-to-end/api/03-groups.js +++ b/tests/end-to-end/api/03-groups.js @@ -343,8 +343,8 @@ describe('[Groups]', function() { .end(done); }); - it('/groups.couters', (done) => { - request.get(api('groups.couters')) + it('/groups.counters', (done) => { + request.get(api('groups.counters')) .set(credentials) .query({ roomId: group._id From 2b064aa93da2645e2c34bc9cb02415a2ad8d500e Mon Sep 17 00:00:00 2001 From: Eugene Bolshakov Date: Fri, 23 Feb 2018 20:50:10 +0300 Subject: [PATCH 10/12] Fix last message and unread message times for IM Signed-off-by: Eugene Bolshakov --- packages/rocketchat-api/server/v1/im.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/rocketchat-api/server/v1/im.js b/packages/rocketchat-api/server/v1/im.js index 18aedb586285..e5053fcdd0eb 100644 --- a/packages/rocketchat-api/server/v1/im.js +++ b/packages/rocketchat-api/server/v1/im.js @@ -61,6 +61,7 @@ RocketChat.API.v1.addRoute(['dm.counters', 'im.counters'], { authRequired: true let msgs = null; let latest = null; let members = null; + let lm = null; if (ruserId) { if (!access) { @@ -71,17 +72,20 @@ RocketChat.API.v1.addRoute(['dm.counters', 'im.counters'], { authRequired: true const rs = findDirectMessageRoom(this.requestParams(), {'_id': user}); const room = rs.room; const dm = rs.subscription; + lm = room.lm ? room.lm : room._updatedAt; if (typeof dm !== 'undefined' && dm.open) { - unreads = dm.unread; + if (dm.ls) { + unreads = dm.unread; + unreadsFrom = dm.ls; + } userMentions = dm.userMentions; - unreadsFrom = dm.ls; joined = true; } if (access || joined) { msgs = room.msgs; - latest = room.lm; + latest = lm; members = room.usernames.length; } From 7bcf1634fa6c18b86cf619930dae3d068ad95a81 Mon Sep 17 00:00:00 2001 From: Eugene Bolshakov Date: Fri, 23 Feb 2018 20:59:37 +0300 Subject: [PATCH 11/12] Add im.counters test Signed-off-by: Eugene Bolshakov --- tests/end-to-end/api/04-direct-message.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/end-to-end/api/04-direct-message.js b/tests/end-to-end/api/04-direct-message.js index 5bf8a8154b48..84ee72527554 100644 --- a/tests/end-to-end/api/04-direct-message.js +++ b/tests/end-to-end/api/04-direct-message.js @@ -101,6 +101,27 @@ describe('[Direct Messages]', function() { .end(done); }); + it('/im.counters', (done) => { + request.get(api('im.counters')) + .set(credentials) + .query({ + roomId: directMessage._id + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + expect(res.body).to.have.property('joined', true); + expect(res.body).to.have.property('members'); + expect(res.body).to.have.property('unreads'); + expect(res.body).to.have.property('unreadsFrom'); + expect(res.body).to.have.property('msgs'); + expect(res.body).to.have.property('latest'); + expect(res.body).to.have.property('userMentions'); + }) + .end(done); + }); + it('/im.close', (done) => { request.post(api('im.close')) .set(credentials) From 83c7c3001c127db160245b032f7a390318d3e04b Mon Sep 17 00:00:00 2001 From: Eugene Bolshakov Date: Fri, 23 Feb 2018 21:17:14 +0300 Subject: [PATCH 12/12] Fix for msgs=0 Signed-off-by: Eugene Bolshakov --- packages/rocketchat-api/server/v1/im.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/rocketchat-api/server/v1/im.js b/packages/rocketchat-api/server/v1/im.js index e5053fcdd0eb..de977c9db7e8 100644 --- a/packages/rocketchat-api/server/v1/im.js +++ b/packages/rocketchat-api/server/v1/im.js @@ -75,7 +75,7 @@ RocketChat.API.v1.addRoute(['dm.counters', 'im.counters'], { authRequired: true lm = room.lm ? room.lm : room._updatedAt; if (typeof dm !== 'undefined' && dm.open) { - if (dm.ls) { + if (dm.ls && room.msgs) { unreads = dm.unread; unreadsFrom = dm.ls; }