From 30a99b2297e5d7420c8175b58df14e3f0262ff5e Mon Sep 17 00:00:00 2001 From: Rodrigo Nascimento Date: Tue, 26 Jul 2016 14:51:40 -0300 Subject: [PATCH] Use cache to send subscription date to client --- .../server/startup/cache/Subscriptions.js | 5 +++++ server/publications/subscription.coffee | 14 +++++++------- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/packages/rocketchat-lib/server/startup/cache/Subscriptions.js b/packages/rocketchat-lib/server/startup/cache/Subscriptions.js index f8a48eae03c..72cbf7dae9f 100644 --- a/packages/rocketchat-lib/server/startup/cache/Subscriptions.js +++ b/packages/rocketchat-lib/server/startup/cache/Subscriptions.js @@ -5,12 +5,17 @@ RocketChat.cache.Subscriptions = new (class CacheUser extends RocketChat.cache._ super('Subscriptions'); this.ensureIndex('rid', 'array'); + this.ensureIndex('u._id', 'array'); this.ensureIndex(['rid', 'u._id'], 'unique'); } findByUserId(userId, options) { return this.findByIndex('u._id', userId, options); } + + findOneByRidAndUserId(rid, userId, options) { + return this.findByIndex('rid,u._id', [rid, userId], options).fetch(); + } }); RocketChat.cache.Rooms.hasMany('Subscriptions', { diff --git a/server/publications/subscription.coffee b/server/publications/subscription.coffee index 4cbbd6b859b..713b17e0941 100644 --- a/server/publications/subscription.coffee +++ b/server/publications/subscription.coffee @@ -29,14 +29,14 @@ Meteor.methods options = fields: fields - if updatedAt instanceof Date - return RocketChat.models.Subscriptions.dinamicFindChangesAfter('findByUserId', updatedAt, Meteor.userId(), options) + records = RocketChat.cache.Subscriptions.findByUserId(Meteor.userId(), options).fetch() - return RocketChat.models.Subscriptions.findByUserId(Meteor.userId(), options).fetch() + if updatedAt instanceof Date + return records.filter (record) -> + return record._updatedAt > updatedAt + return records -RocketChat.models.Subscriptions.on 'change', (type, args...) -> - records = RocketChat.models.Subscriptions.getChangedRecords type, args[0], fields - for record in records - RocketChat.Notifications.notifyUser record.u._id, 'subscriptions-changed', type, record +RocketChat.cache.Subscriptions.on 'changed', (type, subscription) -> + RocketChat.Notifications.notifyUser subscription.u._id, 'subscriptions-changed', type, RocketChat.cache.Subscriptions.processQueryOptionsOnResult(subscription, {fields: fields})