FIX: Don't store translated trust level names in anonymous cache (#13224)

FIX: Don’t store translated trust level names in anonymous cache (#13224)

Refactors TrustLevel and moves translations from server to client

Additional changes:

  • “staff” and “admin” wasn’t translatable in site settings
  • it replaces a concatenated string with a translation
  • uses translation for trust levels in users_by_trust_level report
  • adds a DB migration to rename keys of translation overrides affected by this commit
diff --git a/app/assets/javascripts/discourse/app/models/site.js b/app/assets/javascripts/discourse/app/models/site.js
index fa59e1c..9fc38c9 100644
--- a/app/assets/javascripts/discourse/app/models/site.js
+++ b/app/assets/javascripts/discourse/app/models/site.js
@@ -179,8 +179,10 @@ Site.reopenClass(Singleton, {
     }
 
     if (result.trust_levels) {
-      result.trustLevels = result.trust_levels.map((tl) =>
-        TrustLevel.create(tl)
+      result.trustLevels = Object.entries(result.trust_levels).map(
+        ([key, id]) => {
+          return new TrustLevel(id, key);
+        }
       );
       delete result.trust_levels;
     }
diff --git a/app/assets/javascripts/discourse/app/models/trust-level.js b/app/assets/javascripts/discourse/app/models/trust-level.js
index b793195..2ee5136 100644
--- a/app/assets/javascripts/discourse/app/models/trust-level.js
+++ b/app/assets/javascripts/discourse/app/models/trust-level.js
@@ -1,6 +1,22 @@
-import RestModel from "discourse/models/rest";
-import { fmt } from "discourse/lib/computed";
+import { computed } from "@ember/object";
+import I18n from "I18n";
 
-export default RestModel.extend({
-  detailedName: fmt("id", "name", "%@ - %@"),
-});
+export default class TrustLevel {
+  constructor(id, key) {
+    this.id = id;
+    this._key = key;
+  }
+
+  @computed
+  get name() {
+    return I18n.t(`trust_levels.names.${this._key}`);
+  }
+
+  @computed
+  get detailedName() {
+    return I18n.t("trust_levels.detailed_name", {
+      level: this.id,
+      name: this.name,
+    });
+  }
+}
diff --git a/app/assets/javascripts/discourse/tests/fixtures/site-fixtures.js b/app/assets/javascripts/discourse/tests/fixtures/site-fixtures.js
index c143a52..bdd982a 100644
--- a/app/assets/javascripts/discourse/tests/fixtures/site-fixtures.js
+++ b/app/assets/javascripts/discourse/tests/fixtures/site-fixtures.js
@@ -12,6 +12,13 @@ export default {
         small_action: 3,
         whisper: 4,
       },
+      trust_levels: {
+        newuser: 0,
+        basic: 1,
+        member: 2,
+        regular: 3,
+        leader: 4,
+      },
       groups: [
         { id: 0, name: "everyone" },
         { id: 1, name: "admins" },
@@ -535,28 +542,6 @@ export default {
           is_custom_flag: true,
         },
       ],
-      trust_levels: [
-        {
-          id: 0,
-          name: "new user",
-        },
-        {
-          id: 1,
-          name: "basic user",
-        },
-        {
-          id: 2,
-          name: "member",
-        },
-        {
-          id: 3,
-          name: "regular",
-        },
-        {
-          id: 4,
-          name: "leader",
-        },
-      ],
       archetypes: [
         {
           id: "regular",
diff --git a/app/assets/javascripts/discourse/tests/helpers/site.js b/app/assets/javascripts/discourse/tests/helpers/site.js
index c6a68e9..7d38afa 100644
--- a/app/assets/javascripts/discourse/tests/helpers/site.js
+++ b/app/assets/javascripts/discourse/tests/helpers/site.js
@@ -16,6 +16,13 @@ PreloadStore.store("site", {
     moved_post: 10,
   },
   post_types: { regular: 1, moderator_action: 2 },
+  trust_levels: {
+    newuser: 0,
+    basic: 1,
+    member: 2,
+    regular: 3,
+    leader: 4,
+  },
   groups: [
     { id: 0, name: "everyone" },
     { id: 1, name: "admins" },
@@ -349,12 +356,5 @@ PreloadStore.store("site", {
       is_custom_flag: true,
     },
   ],
-  trust_levels: [
-    { id: 0, name: "new user" },
-    { id: 1, name: "basic user" },
-    { id: 2, name: "member" },
-    { id: 3, name: "regular" },
-    { id: 4, name: "leader" },
-  ],
   archetypes: [{ id: "regular", name: "Regular Topic", options: [] }],
 });
diff --git a/app/models/concerns/reports/users_by_trust_level.rb b/app/models/concerns/reports/users_by_trust_level.rb
index a2b99e1..95cda80 100644
--- a/app/models/concerns/reports/users_by_trust_level.rb
+++ b/app/models/concerns/reports/users_by_trust_level.rb
@@ -24,7 +24,7 @@ module Reports::UsersByTrustLevel
       ]
 
       User.real.group('trust_level').count.sort.each do |level, count|
-        key = TrustLevel.levels[level.to_i]
+        key = TrustLevel.name(level.to_i)
         url = Proc.new { |k| "/admin/users/list/#{k}" }
         report.data << { url: url.call(key), key: key, x: level.to_i, y: count }
       end
diff --git a/app/models/site.rb b/app/models/site.rb
index c7c2d7a..f8c131e 100644
--- a/app/models/site.rb
+++ b/app/models/site.rb
@@ -21,7 +21,7 @@ class Site
   end
 
   def trust_levels
-    TrustLevel.all
+    TrustLevel.levels
   end
 
   def user_fields
diff --git a/app/models/trust_level_and_staff_setting.rb b/app/models/trust_level_and_staff_setting.rb
index ecab020..a72f634 100644
--- a/app/models/trust_level_and_staff_setting.rb
+++ b/app/models/trust_level_and_staff_setting.rb
@@ -18,4 +18,12 @@ class TrustLevelAndStaffSetting < TrustLevelSetting
   def self.special_groups
     ['staff', 'admin']
   end
+
+  def self.translation(value)
+    if special_group?(value)
+      I18n.t("trust_levels.#{value}")
+    else
+      super
+    end
+  end
 end
diff --git a/app/models/trust_level_setting.rb b/app/models/trust_level_setting.rb
index 68b7e2b..fc48bf8 100644
--- a/app/models/trust_level_setting.rb
+++ b/app/models/trust_level_setting.rb
@@ -8,18 +8,23 @@ class TrustLevelSetting < EnumSiteSetting
   end
 
   def self.values
-    levels = TrustLevel.all
-    valid_values.map { |x|
-      {
-        name: x.is_a?(Integer) ? "#{x}: #{levels[x.to_i].name}" : x,
-        value: x
-      }
-    }
+    valid_values.map do |value|
+      { name: translation(value), value: value }
+    end
   end
 
   def self.valid_values
     TrustLevel.valid_range.to_a
   end
 
+  def self.translation(value)
+    I18n.t(
+      "js.trust_levels.detailed_name",
+      level: value,
+      name: TrustLevel.name(value)
+    )
+  end
+
   private_class_method :valid_values
+  private_class_method :translation
 end
diff --git a/app/serializers/site_serializer.rb b/app/serializers/site_serializer.rb
index 6fe1d7c..e1d0982 100644
--- a/app/serializers/site_serializer.rb
+++ b/app/serializers/site_serializer.rb
@@ -6,6 +6,7 @@ class SiteSerializer < ApplicationSerializer
     :default_archetype,
     :notification_types,
     :post_types,
+    :trust_levels,
     :groups,
     :filters,
     :periods,
@@ -32,7 +33,6 @@ class SiteSerializer < ApplicationSerializer
   )
 
   has_many :categories, serializer: SiteCategorySerializer, embed: :objects
-  has_many :trust_levels, embed: :objects
   has_many :archetypes, embed: :objects, serializer: ArchetypeSerializer
   has_many :user_fields, embed: :objects, serializer: UserFieldSerializer
   has_many :auth_providers, embed: :objects, serializer: AuthProviderSerializer
diff --git a/app/serializers/trust_level_serializer.rb b/app/serializers/trust_level_serializer.rb
deleted file mode 100644
index c41495f..0000000
--- a/app/serializers/trust_level_serializer.rb
+++ /dev/null
@@ -1,7 +0,0 @@
-# frozen_string_literal: true
-
-class TrustLevelSerializer < ApplicationSerializer
-
-  attributes :id, :name
-
-end
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index d9930d2..502f893 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -702,7 +702,7 @@ en:
           smtp_instructions: "When you enable SMTP for the group, all outbound emails sent from the group's inbox will be sent via the SMTP settings specified here instead of the mail server configured for other emails sent by your forum."
           imap_title: "IMAP"
           imap_additional_settings: "Additional Settings"

[... diff too long, it was truncated ...]

GitHub sha: 41ee5b7c

This commit appears in #13224 which was approved by eviltrout. It was merged by gschlager.