FIX: Avoid publishing a gigantic payload.

* Certain sites have way too many categories.
This commit is contained in:
Guo Xiang Tan 2017-08-16 11:38:30 +09:00
parent 996ce8c443
commit ed851dbfff
4 changed files with 33 additions and 5 deletions

View File

@ -95,6 +95,10 @@ export default {
bus.subscribe("/client_settings", data => Ember.set(siteSettings, data.name, data.value));
bus.subscribe("/refresh_client", data => {
Discourse.set("assetVersion", data);
});
if (!Ember.testing) {
if (!site.mobileView) {
bus.subscribe(alertChannel(user), data => onNotification(data, user));

View File

@ -414,12 +414,18 @@ class Group < ActiveRecord::Base
users.pluck(:username).join(",")
end
PUBLISH_CATEGORIES_LIMIT = 10
def add(user)
self.users.push(user) unless self.users.include?(user)
MessageBus.publish('/categories', {
categories: ActiveModel::ArraySerializer.new(self.categories).as_json
}, user_ids: [user.id])
if self.categories.count < PUBLISH_CATEGORIES_LIMIT
MessageBus.publish('/categories', {
categories: ActiveModel::ArraySerializer.new(self.categories).as_json
}, user_ids: [user.id])
else
Discourse.request_refresh!(user_ids: [user.id])
end
self
end

View File

@ -296,12 +296,16 @@ module Discourse
last_read_only[$redis.namespace] = nil
end
def self.request_refresh!
def self.request_refresh!(user_ids: nil)
# Causes refresh on next click for all clients
#
# This is better than `MessageBus.publish "/file-change", ["refresh"]` because
# it spreads the refreshes out over a time period
MessageBus.publish '/global/asset-version', 'clobber'
if user_ids
MessageBus.publish("/refresh_client", 'clobber', user_ids: user_id)
else
MessageBus.publish('/global/asset-version', 'clobber')
end
end
def self.git_version

View File

@ -539,6 +539,20 @@ describe Group do
expect(message.data[:categories].first[:id]).to eq(category.id)
expect(message.user_ids).to eq([user.id])
end
describe "when group belongs to more than #{Group::PUBLISH_CATEGORIES_LIMIT} categories" do
it "should publish a message to refresh the user's client" do
(Group::PUBLISH_CATEGORIES_LIMIT + 1).times do
group.categories << Fabricate(:category)
end
message = MessageBus.track_publish { group.add(user) }.first
expect(message.data).to eq('clobber')
expect(message.channel).to eq('/refresh_client')
expect(message.user_ids).to eq([user.id])
end
end
end
end