From 6fe7bc4e238716e384d0003a0a6f779e0118cd03 Mon Sep 17 00:00:00 2001 From: Rodrigo Nascimento Date: Tue, 31 May 2016 17:17:16 -0300 Subject: [PATCH] Use events insted of observers for streams --- .../server/publications/permissions.js | 23 ++------ .../client/lib/cachedCollection.js | 2 +- .../server/publications/settings.coffee | 24 +++------ server/publications/subscription.coffee | 53 +++++++++---------- 4 files changed, 35 insertions(+), 67 deletions(-) diff --git a/packages/rocketchat-authorization/server/publications/permissions.js b/packages/rocketchat-authorization/server/publications/permissions.js index 6043100ddb1..ca0f64c3153 100644 --- a/packages/rocketchat-authorization/server/publications/permissions.js +++ b/packages/rocketchat-authorization/server/publications/permissions.js @@ -7,25 +7,10 @@ Meteor.methods({ }); -let subscriptionsReady = false; -RocketChat.models.Settings.findNotHidden().observe({ - added(record) { - if (subscriptionsReady) { - RocketChat.Notifications.notifyAll('permissions-changed', 'added', record); - } - }, +RocketChat.models.Permissions.on('change', (type, ...args) => { + const records = RocketChat.models.Permissions.getChangedRecords(type, args[0]); - changed(record) { - if (subscriptionsReady) { - RocketChat.Notifications.notifyAll('permissions-changed', 'changed', record); - } - }, - - removed(record) { - if (subscriptionsReady) { - RocketChat.Notifications.notifyAll('permissions-changed', 'removed', { _id: record._id }); - } + for (const record of records) { + RocketChat.Notifications.notifyAll('permissions-changed', type, record); } }); - -subscriptionsReady = true; diff --git a/packages/rocketchat-lib/client/lib/cachedCollection.js b/packages/rocketchat-lib/client/lib/cachedCollection.js index 553d96ab6d4..87c8b98617e 100644 --- a/packages/rocketchat-lib/client/lib/cachedCollection.js +++ b/packages/rocketchat-lib/client/lib/cachedCollection.js @@ -82,7 +82,7 @@ class CachedCollection { setupListener(eventType, eventName) { RocketChat.Notifications[eventType || this.eventType](eventName || this.eventName, (t, record) => { - if (t === 'removed') { + if (t === 'remove') { this.collection.remove(record._id); } else { const _id = record._id; diff --git a/packages/rocketchat-lib/server/publications/settings.coffee b/packages/rocketchat-lib/server/publications/settings.coffee index 1f6f5caaaf5..aab76092443 100644 --- a/packages/rocketchat-lib/server/publications/settings.coffee +++ b/packages/rocketchat-lib/server/publications/settings.coffee @@ -16,24 +16,12 @@ Meteor.methods return RocketChat.models.Settings.findNotHidden().fetch() -subscriptionsReady = false -RocketChat.models.Settings.findNotHidden().observe - added: (record) -> - if subscriptionsReady - e = if record.public is true then 'public-settings-changed' else 'private-settings-changed' - RocketChat.Notifications.notifyAll e, 'added', record - - changed: (record) -> - if subscriptionsReady - e = if record.public is true then 'public-settings-changed' else 'private-settings-changed' - RocketChat.Notifications.notifyAll e, 'changed', record - - removed: (record) -> - if subscriptionsReady - e = if record.public is true then 'public-settings-changed' else 'private-settings-changed' - RocketChat.Notifications.notifyAll e, 'removed', { _id: record._id } - -subscriptionsReady = true +RocketChat.models.Settings.on 'change', (type, args...) -> + records = RocketChat.models.Settings.getChangedRecords type, args[0] + + for record in records + e = if record.public is true then 'public-settings-changed' else 'private-settings-changed' + RocketChat.Notifications.notifyAll e, type, record RocketChat.Notifications.streamAll.allowRead 'private-settings-changed', -> diff --git a/server/publications/subscription.coffee b/server/publications/subscription.coffee index 029fe700a63..dc1a2e00499 100644 --- a/server/publications/subscription.coffee +++ b/server/publications/subscription.coffee @@ -1,3 +1,22 @@ +fields = + t: 1 + ts: 1 + ls: 1 + name: 1 + rid: 1 + code: 1 + f: 1 + u: 1 + open: 1 + alert: 1 + roles: 1 + unread: 1 + archived: 1 + desktopNotifications: 1 + mobilePushNotifications: 1 + emailNotifications: 1 + + Meteor.methods subscriptions: -> unless Meteor.userId() @@ -6,37 +25,13 @@ Meteor.methods this.unblock() options = - fields: - t: 1 - ts: 1 - ls: 1 - name: 1 - rid: 1 - code: 1 - f: 1 - open: 1 - alert: 1 - roles: 1 - unread: 1 - archived: 1 - desktopNotifications: 1 - mobilePushNotifications: 1 - emailNotifications: 1 + fields: fields return RocketChat.models.Subscriptions.findByUserId(Meteor.userId(), options).fetch() -subscriptionsReady = false -RocketChat.models.Subscriptions.find().observe - added: (record) -> - if subscriptionsReady - RocketChat.Notifications.notifyUser record.u._id, 'subscription-change', 'added', record - - changed: (record) -> - if subscriptionsReady - RocketChat.Notifications.notifyUser record.u._id, 'subscription-change', 'changed', record - removed: (record) -> - if subscriptionsReady - RocketChat.Notifications.notifyUser record.u._id, 'subscription-change', 'removed', {_id: record._id} +RocketChat.models.Subscriptions.on 'change', (type, args...) -> + records = RocketChat.models.Subscriptions.getChangedRecords type, args[0], fields -subscriptionsReady = true + for record in records + RocketChat.Notifications.notifyUser record.u._id, 'subscription-change', type, record