From 5866043c1e8fdbdf731c1aace53511a7e2741733 Mon Sep 17 00:00:00 2001
From: Gabriel Engel <gabriel.engel@fgsys.com>
Date: Fri, 20 Nov 2015 17:50:37 -0200
Subject: [PATCH] split server and client settings initial loading logic

---
 .../rocketchat-lib/client/lib/settings.coffee | 26 ++++++++++++++++---
 packages/rocketchat-lib/lib/settings.coffee   |  6 +++++
 .../lib/startup/settings.coffee               | 13 ----------
 packages/rocketchat-lib/package.js            |  2 --
 .../server/functions/settings.coffee          | 21 +++++++++++++++
 .../server/startup/settings.coffee            | 18 +------------
 .../startup/settingsOnLoadCdnPrefix.coffee    |  7 ++++-
 7 files changed, 56 insertions(+), 37 deletions(-)
 delete mode 100644 packages/rocketchat-lib/lib/startup/settings.coffee

diff --git a/packages/rocketchat-lib/client/lib/settings.coffee b/packages/rocketchat-lib/client/lib/settings.coffee
index 6db18f21080..fdbfa3c26f6 100644
--- a/packages/rocketchat-lib/client/lib/settings.coffee
+++ b/packages/rocketchat-lib/client/lib/settings.coffee
@@ -3,12 +3,30 @@
 # @namespace RocketChat.settings
 ###
 
-settingsDict = new ReactiveDict('settings')
+@Settings = new Meteor.Collection 'rocketchat_settings'
 
 RocketChat.settings.subscription = Meteor.subscribe 'settings'
 
+RocketChat.settings.dict = new ReactiveDict 'settings'
+
 RocketChat.settings.get = (_id) ->
-	return settingsDict.get(_id)
+	return RocketChat.settings.dict.get(_id)
+
+RocketChat.settings.init = ->
+	initialLoad = true
+	Settings.find().observe
+		added: (record) ->
+			Meteor.settings[record._id] = record.value
+			RocketChat.settings.dict.set record._id, record.value
+			RocketChat.settings.load record._id, record.value, initialLoad
+		changed: (record) ->
+			Meteor.settings[record._id] = record.value
+			RocketChat.settings.dict.set record._id, record.value
+			RocketChat.settings.load record._id, record.value, initialLoad
+		removed: (record) ->
+			delete Meteor.settings[record._id]
+			RocketChat.settings.dict.set record._id, undefined
+			RocketChat.settings.load record._id, undefined, initialLoad
+	initialLoad = false
 
-RocketChat.settings.onload '*', (key, value) ->
-	return settingsDict.set key, value
+RocketChat.settings.init()
diff --git a/packages/rocketchat-lib/lib/settings.coffee b/packages/rocketchat-lib/lib/settings.coffee
index e43641e8c1d..7f3694134de 100644
--- a/packages/rocketchat-lib/lib/settings.coffee
+++ b/packages/rocketchat-lib/lib/settings.coffee
@@ -35,5 +35,11 @@ RocketChat.settings =
 
 
 	onload: (key, callback) ->
+		# if key is '*'
+		# 	for key, value in Meteor.settings
+		# 		callback key, value, false
+		# else if Meteor.settings?[_id]?
+		# 	callback key, Meteor.settings[_id], false
+
 		RocketChat.settings.callbacks[key] ?= []
 		RocketChat.settings.callbacks[key].push callback
diff --git a/packages/rocketchat-lib/lib/startup/settings.coffee b/packages/rocketchat-lib/lib/startup/settings.coffee
deleted file mode 100644
index 2d485274080..00000000000
--- a/packages/rocketchat-lib/lib/startup/settings.coffee
+++ /dev/null
@@ -1,13 +0,0 @@
-@Settings = Settings = new Meteor.Collection 'rocketchat_settings'
-initialLoad = true
-Settings.find().observe
-	added: (record) ->
-		Meteor.settings[record._id] = record.value
-		RocketChat.settings.load record._id, record.value, initialLoad
-	changed: (record) ->
-		Meteor.settings[record._id] = record.value
-		RocketChat.settings.load record._id, record.value, initialLoad
-	removed: (record) ->
-		delete Meteor.settings[record._id]
-		RocketChat.settings.load record._id, undefined, initialLoad
-initialLoad = false
diff --git a/packages/rocketchat-lib/package.js b/packages/rocketchat-lib/package.js
index 98b1adcf584..e103586f041 100644
--- a/packages/rocketchat-lib/package.js
+++ b/packages/rocketchat-lib/package.js
@@ -67,7 +67,6 @@ Package.onUse(function(api) {
 	api.addFiles('server/startup/settings.coffee', 'server');
 
 	// COMMON STARTUP
-	api.addFiles('lib/startup/settings.coffee');
 	api.addFiles('lib/startup/settingsOnLoadSiteUrl.coffee');
 
 	// CLIENT LIB
@@ -77,7 +76,6 @@ Package.onUse(function(api) {
 
 	// CLIENT METHODS
 	api.addFiles('client/methods/sendMessage.coffee', 'client');
-
 	api.addFiles('client/AdminBox.coffee', 'client');
 	api.addFiles('client/Notifications.coffee', 'client');
 	api.addFiles('client/TabBar.coffee', 'client');
diff --git a/packages/rocketchat-lib/server/functions/settings.coffee b/packages/rocketchat-lib/server/functions/settings.coffee
index 18bb2f6548e..eede3324732 100644
--- a/packages/rocketchat-lib/server/functions/settings.coffee
+++ b/packages/rocketchat-lib/server/functions/settings.coffee
@@ -87,3 +87,24 @@ RocketChat.settings.updateById = (_id, value) ->
 		return false
 
 	return RocketChat.models.Settings.updateValueById _id, value
+
+
+###
+# Update a setting by id
+###
+RocketChat.settings.init = ->
+	initialLoad = true
+	RocketChat.models.Settings.find().observe
+		added: (record) ->
+			Meteor.settings[record._id] = record.value
+			process.env[record._id] = record.value
+			RocketChat.settings.load record._id, record.value, initialLoad
+		changed: (record) ->
+			Meteor.settings[record._id] = record.value
+			process.env[record._id] = record.value
+			RocketChat.settings.load record._id, record.value, initialLoad
+		removed: (record) ->
+			delete Meteor.settings[record._id]
+			delete process.env[record._id]
+			RocketChat.settings.load record._id, undefined, initialLoad
+	initialLoad = false
diff --git a/packages/rocketchat-lib/server/startup/settings.coffee b/packages/rocketchat-lib/server/startup/settings.coffee
index 687fb6df216..f1985261ece 100644
--- a/packages/rocketchat-lib/server/startup/settings.coffee
+++ b/packages/rocketchat-lib/server/startup/settings.coffee
@@ -113,23 +113,7 @@ RocketChat.settings.add 'Layout_Login_Terms', 'By proceeding to create your acco
 
 RocketChat.settings.add 'Statistics_opt_out', false, { type: 'boolean', group: false }
 
-
-initialLoad = true
-RocketChat.models.Settings.find().observe
-	added: (record) ->
-		Meteor.settings[record._id] = record.value
-		process.env[record._id] = record.value
-		RocketChat.settings.load record._id, record.value, initialLoad
-	changed: (record) ->
-		Meteor.settings[record._id] = record.value
-		process.env[record._id] = record.value
-		RocketChat.settings.load record._id, record.value, initialLoad
-	removed: (record) ->
-		delete Meteor.settings[record._id]
-		delete process.env[record._id]
-		RocketChat.settings.load record._id, undefined, initialLoad
-initialLoad = false
-
+RocketChat.settings.init()
 
 Meteor.startup ->
 	if process?.env? and not process.env['MAIL_URL']? and RocketChat.settings.get('SMTP_Host') and RocketChat.settings.get('SMTP_Username') and RocketChat.settings.get('SMTP_Password')
diff --git a/packages/rocketchat-lib/server/startup/settingsOnLoadCdnPrefix.coffee b/packages/rocketchat-lib/server/startup/settingsOnLoadCdnPrefix.coffee
index f8cf5970cb5..98100d83666 100644
--- a/packages/rocketchat-lib/server/startup/settingsOnLoadCdnPrefix.coffee
+++ b/packages/rocketchat-lib/server/startup/settingsOnLoadCdnPrefix.coffee
@@ -1,3 +1,8 @@
 RocketChat.settings.onload 'CDN_PREFIX', (key, value, initialLoad) ->
-	if value?.trim() isnt ''
+	if _.isString value
+		WebAppInternals?.setBundledJsCssPrefix value
+
+Meteor.startup ->
+	value = RocketChat.settings.get 'CDN_PREFIX'
+	if _.isString value
 		WebAppInternals?.setBundledJsCssPrefix value
-- 
GitLab