From 36ce87089dbc537eb058c048cc525ef59c0a2b92 Mon Sep 17 00:00:00 2001 From: Eugene Bolshakov Date: Tue, 16 Jan 2018 23:22:24 +0300 Subject: [PATCH 1/4] [REST API] Add query parameter to channels.list.joined, groups.list, groups.listAll, im.list Signed-off-by: Eugene Bolshakov --- packages/rocketchat-api/server/v1/channels.js | 9 +++++++-- packages/rocketchat-api/server/v1/groups.js | 15 +++++++++++---- packages/rocketchat-api/server/v1/im.js | 9 +++++++-- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/packages/rocketchat-api/server/v1/channels.js b/packages/rocketchat-api/server/v1/channels.js index 087e0792de9c..c37bd89ca2a5 100644 --- a/packages/rocketchat-api/server/v1/channels.js +++ b/packages/rocketchat-api/server/v1/channels.js @@ -404,8 +404,13 @@ RocketChat.API.v1.addRoute('channels.list', { authRequired: true }, { RocketChat.API.v1.addRoute('channels.list.joined', { authRequired: true }, { get() { const { offset, count } = this.getPaginationItems(); - const { sort, fields } = this.parseJsonQuery(); - let rooms = _.pluck(RocketChat.models.Subscriptions.findByTypeAndUserId('c', this.userId).fetch(), '_room'); + const { sort, fields, query } = this.parseJsonQuery(); + const ourQuery = Object.assign({}, query, { + t: 'c', + 'u._id': this.userId + }); + + let rooms = _.pluck(RocketChat.models.Subscriptions.find(ourQuery).fetch(), '_room'); const totalCount = rooms.length; rooms = RocketChat.models.Rooms.processQueryOptionsOnResult(rooms, { diff --git a/packages/rocketchat-api/server/v1/groups.js b/packages/rocketchat-api/server/v1/groups.js index ccc7790978c5..6e12f4b4d225 100644 --- a/packages/rocketchat-api/server/v1/groups.js +++ b/packages/rocketchat-api/server/v1/groups.js @@ -317,8 +317,13 @@ RocketChat.API.v1.addRoute('groups.leave', { authRequired: true }, { RocketChat.API.v1.addRoute('groups.list', { authRequired: true }, { get() { const { offset, count } = this.getPaginationItems(); - const { sort, fields } = this.parseJsonQuery(); - let rooms = _.pluck(RocketChat.models.Subscriptions.findByTypeAndUserId('p', this.userId).fetch(), '_room'); + const { sort, fields, query } = this.parseJsonQuery(); + const ourQuery = Object.assign({}, query, { + t: 'p', + 'u._id': this.userId + }); + + let rooms = _.pluck(RocketChat.models.Subscriptions.find(ourQuery).fetch(), '_room'); const totalCount = rooms.length; rooms = RocketChat.models.Rooms.processQueryOptionsOnResult(rooms, { @@ -344,8 +349,10 @@ RocketChat.API.v1.addRoute('groups.listAll', { authRequired: true }, { return RocketChat.API.v1.unauthorized(); } const { offset, count } = this.getPaginationItems(); - const { sort, fields } = this.parseJsonQuery(); - let rooms = RocketChat.models.Rooms.findByType('p').fetch(); + const { sort, fields, query } = this.parseJsonQuery(); + const ourQuery = Object.assign({}, query, { t: 'p' }); + + let rooms = RocketChat.models.Rooms.find(ourQuery).fetch(); const totalCount = rooms.length; rooms = RocketChat.models.Rooms.processQueryOptionsOnResult(rooms, { diff --git a/packages/rocketchat-api/server/v1/im.js b/packages/rocketchat-api/server/v1/im.js index f5bff08b80c4..4a554f6f585a 100644 --- a/packages/rocketchat-api/server/v1/im.js +++ b/packages/rocketchat-api/server/v1/im.js @@ -217,8 +217,13 @@ RocketChat.API.v1.addRoute(['dm.messages.others', 'im.messages.others'], { authR RocketChat.API.v1.addRoute(['dm.list', 'im.list'], { authRequired: true }, { get() { const { offset, count } = this.getPaginationItems(); - const { sort, fields } = this.parseJsonQuery(); - let rooms = _.pluck(RocketChat.models.Subscriptions.findByTypeAndUserId('d', this.userId).fetch(), '_room'); + const { sort, fields, query } = this.parseJsonQuery(); + const ourQuery = Object.assign({}, query, { + t: 'd', + 'u._id': this.userId + }); + + let rooms = _.pluck(RocketChat.models.Subscriptions.find(ourQuery).fetch(), '_room'); const totalCount = rooms.length; rooms = RocketChat.models.Rooms.processQueryOptionsOnResult(rooms, { From b5b07943df81d2b4bb591d3d696a5cf3f43574e1 Mon Sep 17 00:00:00 2001 From: Eugene Bolshakov Date: Wed, 17 Jan 2018 03:13:25 +0300 Subject: [PATCH 2/4] Add customFields from Room to Subscription to enable query Signed-off-by: Eugene Bolshakov --- packages/rocketchat-lib/server/models/Subscriptions.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/rocketchat-lib/server/models/Subscriptions.js b/packages/rocketchat-lib/server/models/Subscriptions.js index 4c59e26add7f..f150093ac764 100644 --- a/packages/rocketchat-lib/server/models/Subscriptions.js +++ b/packages/rocketchat-lib/server/models/Subscriptions.js @@ -557,6 +557,7 @@ class ModelSubscriptions extends RocketChat.models._Base { rid: room._id, name: room.name, fname: room.fname, + customFields: room.customFields, t: room.t, u: { _id: user._id, From b16bde5879a2aba8d645c486a4f82c8e300401b7 Mon Sep 17 00:00:00 2001 From: Eugene Bolshakov Date: Thu, 18 Jan 2018 00:49:16 +0300 Subject: [PATCH 3/4] Add customFields for Direct Messages of the calling user Signed-off-by: Eugene Bolshakov --- server/methods/createDirectMessage.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/server/methods/createDirectMessage.js b/server/methods/createDirectMessage.js index 1dd43f293b02..95b8d634c965 100644 --- a/server/methods/createDirectMessage.js +++ b/server/methods/createDirectMessage.js @@ -68,6 +68,7 @@ Meteor.methods({ unread: 0, userMentions: 0, groupMentions: 0, + customFields: me.customFields, u: { _id: me._id, username: me.username @@ -96,6 +97,7 @@ Meteor.methods({ unread: 0, userMentions: 0, groupMentions: 0, + customFields: to.customFields, u: { _id: to._id, username: to.username From d9dfcd8f143bb32dcf1acc7f27603ab507d4a71e Mon Sep 17 00:00:00 2001 From: Eugene Bolshakov Date: Mon, 22 Jan 2018 01:52:34 +0300 Subject: [PATCH 4/4] Propogate user's customFields changes to Direct Messages' Rooms Signed-off-by: Eugene Bolshakov --- .../saveCustomFieldsWithoutValidation.js | 3 +++ .../server/models/Subscriptions.js | 16 ++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/packages/rocketchat-lib/server/functions/saveCustomFieldsWithoutValidation.js b/packages/rocketchat-lib/server/functions/saveCustomFieldsWithoutValidation.js index d0f6ec896d1d..4b76441089d1 100644 --- a/packages/rocketchat-lib/server/functions/saveCustomFieldsWithoutValidation.js +++ b/packages/rocketchat-lib/server/functions/saveCustomFieldsWithoutValidation.js @@ -13,6 +13,9 @@ RocketChat.saveCustomFieldsWithoutValidation = function(userId, formData) { Object.keys(customFieldsMeta).forEach(key => customFields[key] = formData[key]); RocketChat.models.Users.setCustomFields(userId, customFields); + // Update customFields of all Direct Messages' Rooms for userId + RocketChat.models.Subscriptions.setCustomFieldsDirectMessagesByUserId(userId, customFields); + Object.keys(customFields).forEach((fieldName) => { if (!customFieldsMeta[fieldName].modifyRecordField) { return; diff --git a/packages/rocketchat-lib/server/models/Subscriptions.js b/packages/rocketchat-lib/server/models/Subscriptions.js index f150093ac764..3f4840267ec3 100644 --- a/packages/rocketchat-lib/server/models/Subscriptions.js +++ b/packages/rocketchat-lib/server/models/Subscriptions.js @@ -281,6 +281,22 @@ class ModelSubscriptions extends RocketChat.models._Base { return this.update(query, update); } + setCustomFieldsDirectMessagesByUserId(userId, fields) { + const values = {}; + Object.keys(fields).forEach(key => { + values[`customFields.${ key }`] = fields[key]; + }); + + const query = { + 'u._id': userId, + 't': 'd' + }; + const update = { $set: values }; + const options = { 'multi': true }; + + return this.update(query, update, options); + } + setFavoriteByRoomIdAndUserId(roomId, userId, favorite) { if (favorite == null) { favorite = true; } const query = {