FEATURE: Live-update site settings

This commit is contained in:
Kane York 2015-08-27 15:55:19 -07:00
parent b8c3187a94
commit d4c68f2096
3 changed files with 38 additions and 25 deletions

View File

@ -7,7 +7,9 @@ export default {
// We don't use the message bus in testing
if (Discourse.testing) { return; }
const messageBus = container.lookup('message-bus:main');
const messageBus = container.lookup('message-bus:main'),
user = container.lookup('current-user:main'),
siteSettings = container.lookup('site-settings:main');
const deprecatedBus = {};
deprecatedBus.prototype = messageBus;
@ -20,5 +22,25 @@ export default {
messageBus.alwaysLongPoll = Discourse.Environment === "development";
messageBus.start();
Discourse.KeyValueStore.init("discourse_", messageBus);
messageBus.callbackInterval = siteSettings.anon_polling_interval;
messageBus.backgroundCallbackInterval = siteSettings.background_polling_interval;
messageBus.baseUrl = siteSettings.long_polling_base_url;
if (messageBus.baseUrl !== '/') {
// zepto compatible, 1 param only
messageBus.ajax = function(opts) {
opts.headers = opts.headers || {};
opts.headers['X-Shared-Session-Key'] = $('meta[name=shared_session_key]').attr('content');
return $.ajax(opts);
};
} else {
messageBus.baseUrl = Discourse.getURL('/');
}
if (user) {
messageBus.callbackInterval = siteSettings.polling_interval;
messageBus.enableLongPolling = true;
}
}
};

View File

@ -10,24 +10,7 @@ export default {
siteSettings = container.lookup('site-settings:main'),
bus = container.lookup('message-bus:main');
bus.callbackInterval = siteSettings.anon_polling_interval;
bus.backgroundCallbackInterval = siteSettings.background_polling_interval;
bus.baseUrl = siteSettings.long_polling_base_url;
if (bus.baseUrl !== '/') {
// zepto compatible, 1 param only
bus.ajax = function(opts) {
opts.headers = opts.headers || {};
opts.headers['X-Shared-Session-Key'] = $('meta[name=shared_session_key]').attr('content');
return $.ajax(opts);
};
} else {
bus.baseUrl = Discourse.getURL('/');
}
if (user) {
bus.callbackInterval = siteSettings.polling_interval;
bus.enableLongPolling = true;
if (user.get('staff')) {
bus.subscribe('/flagged_counts', (data) => {
@ -58,7 +41,7 @@ export default {
}, user.notification_channel_position);
bus.subscribe("/categories", function(data) {
_.each(data.categories,function(c) {
_.each(data.categories, function(c) {
site.updateCategory(c);
});
_.each(data.deleted_categories,function(id) {
@ -66,6 +49,10 @@ export default {
});
});
bus.subscribe("/client_settings", function(data) {
siteSettings[data.name] = data.value;
});
if (!Ember.testing) {
initDesktopNotifications(bus);
}

View File

@ -65,6 +65,10 @@ module SiteSettingExtension
@refresh_settings ||= []
end
def client_settings
@client_settings ||= []
end
def previews
@previews ||= {}
end
@ -136,12 +140,7 @@ module SiteSettingExtension
# just like a setting, except that it is available in javascript via DiscourseSession
def client_setting(name, default = nil, opts = {})
setting(name, default, opts)
@client_settings ||= []
@client_settings << name
end
def client_settings
@client_settings ||= []
client_settings << name
end
def settings_hash
@ -312,6 +311,7 @@ module SiteSettingExtension
provider.save(name, val, type)
current[name] = convert(val, type)
notify_clients!(name) if client_settings.include? name
clear_cache!
end
@ -319,6 +319,10 @@ module SiteSettingExtension
MessageBus.publish('/site_settings', {process: process_id})
end
def notify_clients!(name)
MessageBus.publish('/client_settings', {name: name, value: self.send(name)})
end
def has_setting?(name)
defaults.has_key?(name.to_sym) || defaults.has_key?("#{name}?".to_sym)
end