diff --git a/packages/rocketchat-github-enterprise/common.coffee b/packages/rocketchat-github-enterprise/common.coffee index fe44a29b7f9..d18f56a5bf9 100644 --- a/packages/rocketchat-github-enterprise/common.coffee +++ b/packages/rocketchat-github-enterprise/common.coffee @@ -13,7 +13,7 @@ GitHubEnterprise = new CustomOAuth 'github_enterprise', config if Meteor.isServer Meteor.startup -> - RocketChat.models.Settings.find({ _id: 'API_GitHub_Enterprise_URL' }).observe + RocketChat.models.Settings.findById('API_GitHub_Enterprise_URL').observe added: (record) -> config.serverURL = RocketChat.settings.get 'API_GitHub_Enterprise_URL' GitHubEnterprise.configure config diff --git a/packages/rocketchat-gitlab/common.coffee b/packages/rocketchat-gitlab/common.coffee index d9256e7e21b..7d6cd560398 100644 --- a/packages/rocketchat-gitlab/common.coffee +++ b/packages/rocketchat-gitlab/common.coffee @@ -9,7 +9,7 @@ Gitlab = new CustomOAuth 'gitlab', config if Meteor.isServer Meteor.startup -> - RocketChat.models.Settings.find({ _id: 'API_Gitlab_URL' }).observe + RocketChat.models.Settings.findById('API_Gitlab_URL').observe added: (record) -> config.serverURL = RocketChat.settings.get 'API_Gitlab_URL' Gitlab.configure config diff --git a/packages/rocketchat-hubot/hubot.coffee b/packages/rocketchat-hubot/hubot.coffee index 5851315dad1..e65cac42df7 100644 --- a/packages/rocketchat-hubot/hubot.coffee +++ b/packages/rocketchat-hubot/hubot.coffee @@ -339,7 +339,7 @@ init = => # username: "rocketbot" # action: true -RocketChat.models.Settings.find({ _id: { $in: [ 'RocketBot_Name', 'RocketBot_Enabled'] } }).observe +RocketChat.models.Settings.findByIds([ 'RocketBot_Name', 'RocketBot_Enabled']).observe added: -> init() changed: -> diff --git a/packages/rocketchat-lib/server/models/Settings.coffee b/packages/rocketchat-lib/server/models/Settings.coffee index eff7591e658..50b5648ea1a 100644 --- a/packages/rocketchat-lib/server/models/Settings.coffee +++ b/packages/rocketchat-lib/server/models/Settings.coffee @@ -2,6 +2,7 @@ RocketChat.models.Settings = new class extends RocketChat.models._Base constructor: -> @_initModel 'settings' + @tryEnsureIndex { 'hidden': 1 }, { sparse: 1 } # FIND ONE findOneById: (_id, options) -> @@ -12,6 +13,21 @@ RocketChat.models.Settings = new class extends RocketChat.models._Base # FIND + findById: (_id) -> + query = + _id: _id + + return @find query + + findByIds: (_id = []) -> + _id = [].concat _id + + query = + _id: + $in: _id + + return @find query + findByRole: (role, options) -> query = role: role @@ -24,6 +40,19 @@ RocketChat.models.Settings = new class extends RocketChat.models._Base return @find query, options + findNotHiddenPublic: (ids = [])-> + filter = + hidden: { $ne: true } + public: true + + if ids.length > 0 + filter._id = + $in: ids + + return @find filter, { fields: _id: 1, value: 1 } + + findNotHidden: -> + return @find { hidden: { $ne: true } } # UPDATE updateValueById: (_id, value) -> diff --git a/packages/rocketchat-lib/server/publications/settings.coffee b/packages/rocketchat-lib/server/publications/settings.coffee index f5454f90107..f7c16f1725b 100644 --- a/packages/rocketchat-lib/server/publications/settings.coffee +++ b/packages/rocketchat-lib/server/publications/settings.coffee @@ -1,20 +1,12 @@ Meteor.publish 'settings', (ids = []) -> - filter = - hidden: { $ne: true } - public: true - - if ids.length > 0 - filter._id = - $in: ids - - return RocketChat.models.Settings.find filter, { fields: _id: 1, value: 1 } + return RocketChat.models.Settings.findNotHiddenPublic(ids) Meteor.publish 'admin-settings', -> unless @userId return @ready() if RocketChat.authz.hasPermission( @userId, 'view-privileged-setting') - return RocketChat.models.Settings.find({ hidden: { $ne: true } }) + return RocketChat.models.Settings.findNotHidden() else return @ready()