From 62ad4737162adfe0a139d497efcebec55ddb6554 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Fri, 12 Jun 2020 09:54:05 +0800 Subject: [PATCH] FIX: Preload readonly mode attribute seperately. There are two problems I'm trying to tackle here. 1. The site json is cached for anonymous users so readonly mode can be cached for up to 30 minutes which makes it confusing. 2. We've already checked for readonly mode in the controller so having to check for readonly mode again in `SiteSerializer` is adding an extra Redis query on every request. --- app/assets/javascripts/discourse/app/models/site.js | 4 +++- app/controllers/application_controller.rb | 1 + app/serializers/site_serializer.rb | 5 ----- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/app/assets/javascripts/discourse/app/models/site.js b/app/assets/javascripts/discourse/app/models/site.js index 8e6b8f2d2c0..0481485a9f4 100644 --- a/app/assets/javascripts/discourse/app/models/site.js +++ b/app/assets/javascripts/discourse/app/models/site.js @@ -132,7 +132,9 @@ Site.reopenClass(Singleton, { // The current singleton will retrieve its attributes from the `PreloadStore`. createCurrent() { const store = Discourse.__container__.lookup("service:store"); - return store.createRecord("site", PreloadStore.get("site")); + const siteAttributes = PreloadStore.get("site"); + siteAttributes["isReadOnly"] = PreloadStore.get("isReadOnly"); + return store.createRecord("site", siteAttributes); }, create() { diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index c59266e1305..eaaa515d8ec 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -539,6 +539,7 @@ class ApplicationController < ActionController::Base store_preloaded("customHTML", custom_html_json) store_preloaded("banner", banner_json) store_preloaded("customEmoji", custom_emoji) + store_preloaded("isReadOnly", @readonly_mode.to_s) end def preload_current_user_data diff --git a/app/serializers/site_serializer.rb b/app/serializers/site_serializer.rb index d5bb9013460..96833f398e7 100644 --- a/app/serializers/site_serializer.rb +++ b/app/serializers/site_serializer.rb @@ -12,7 +12,6 @@ class SiteSerializer < ApplicationSerializer :top_menu_items, :anonymous_top_menu_items, :uncategorized_category_id, # this is hidden so putting it here - :is_readonly, :disabled_plugins, :user_field_max_length, :post_action_types, @@ -95,10 +94,6 @@ class SiteSerializer < ApplicationSerializer SiteSetting.uncategorized_category_id end - def is_readonly - Discourse.readonly_mode? - end - def disabled_plugins Discourse.disabled_plugin_names end