diff --git a/app/assets/javascripts/discourse/app/widgets/post.js b/app/assets/javascripts/discourse/app/widgets/post.js index 950d5f4f77b..fc1d83c7696 100644 --- a/app/assets/javascripts/discourse/app/widgets/post.js +++ b/app/assets/javascripts/discourse/app/widgets/post.js @@ -293,11 +293,22 @@ createWidget("post-meta-data", { let postInfo = []; if (attrs.isWhisper) { + const groups = this.site.get("whispers_allowed_groups_names"); + let title = ""; + + if (groups?.length > 0) { + title = I18n.t("post.whisper_groups", { + groupNames: groups.join(", "), + }); + } else { + title = I18n.t("post.whisper"); + } + postInfo.push( h( "div.post-info.whisper", { - attributes: { title: I18n.t("post.whisper") }, + attributes: { title }, }, iconNode("far-eye-slash") ) diff --git a/app/serializers/site_serializer.rb b/app/serializers/site_serializer.rb index 53c23bcf480..4b89c974c0e 100644 --- a/app/serializers/site_serializer.rb +++ b/app/serializers/site_serializer.rb @@ -39,7 +39,8 @@ class SiteSerializer < ApplicationSerializer :hashtag_icons, :displayed_about_plugin_stat_groups, :show_welcome_topic_banner, - :anonymous_default_sidebar_tags + :anonymous_default_sidebar_tags, + :whispers_allowed_groups_names ) has_many :archetypes, embed: :objects, serializer: ArchetypeSerializer @@ -246,6 +247,14 @@ class SiteSerializer < ApplicationSerializer scope.anonymous? && !SiteSetting.legacy_navigation_menu? && SiteSetting.tagging_enabled && SiteSetting.default_sidebar_tags.present? end + def whispers_allowed_groups_names + SiteSetting.whispers_allowed_groups_map&.map { |id| Group.where(id: id).pluck_first(:name) } + end + + def include_whispers_allowed_groups_names? + scope.can_see_whispers? + end + private def ordered_flags(flags) diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index dcb764fe97e..3990eb2b50d 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -3447,6 +3447,7 @@ en: via_email: "this post arrived via email" via_auto_generated_email: "this post arrived via an auto generated email" whisper: "this post is a private whisper for moderators" + whisper_groups: "this post is a private whisper only visible to %{groupNames}" wiki: about: "this post is a wiki" diff --git a/spec/requests/api/schemas/json/site_response.json b/spec/requests/api/schemas/json/site_response.json index 4ec85e646fa..09d239f4322 100644 --- a/spec/requests/api/schemas/json/site_response.json +++ b/spec/requests/api/schemas/json/site_response.json @@ -755,6 +755,9 @@ "items": { } + }, + "whispers_allowed_groups_names" : { + "type": "array" } }, "required": [ diff --git a/spec/serializers/site_serializer_spec.rb b/spec/serializers/site_serializer_spec.rb index eb8635b977c..7692cccae62 100644 --- a/spec/serializers/site_serializer_spec.rb +++ b/spec/serializers/site_serializer_spec.rb @@ -234,4 +234,44 @@ RSpec.describe SiteSerializer do end end end + + describe '#whispers_allowed_groups_names' do + fab!(:admin) { Fabricate(:admin) } + fab!(:allowed_user) { Fabricate(:user) } + fab!(:not_allowed_user) { Fabricate(:user) } + fab!(:group1) { Fabricate(:group, name: 'whisperers1', users: [allowed_user]) } + fab!(:group2) { Fabricate(:group, name: 'whisperers2', users: [allowed_user]) } + + it "returns correct group names for created groups" do + admin_guardian = Guardian.new(admin) + SiteSetting.whispers_allowed_groups = "#{group1.id}|#{group2.id}" + + serialized = described_class.new(Site.new(admin_guardian), scope: admin_guardian, root: false).as_json + expect(serialized[:whispers_allowed_groups_names]).to eq(["whisperers1", "whisperers2"]) + end + + it "returns correct group names for automatic groups" do + admin_guardian = Guardian.new(admin) + SiteSetting.whispers_allowed_groups = "#{Group::AUTO_GROUPS[:staff]}|#{Group::AUTO_GROUPS[:trust_level_4]}" + + serialized = described_class.new(Site.new(admin_guardian), scope: admin_guardian, root: false).as_json + expect(serialized[:whispers_allowed_groups_names]).to eq(["staff", "trust_level_4"]) + end + + it "returns group names when user is allowed to whisper" do + user_guardian = Guardian.new(allowed_user) + SiteSetting.whispers_allowed_groups = "#{group1.id}|#{group2.id}" + + serialized = described_class.new(Site.new(user_guardian), scope: user_guardian, root: false).as_json + expect(serialized[:whispers_allowed_groups_names]).to eq(["whisperers1", "whisperers2"]) + end + + it "returns nil when user is not allowed to whisper" do + user_guardian = Guardian.new(not_allowed_user) + SiteSetting.whispers_allowed_groups = "#{Group::AUTO_GROUPS[:staff]}|#{Group::AUTO_GROUPS[:trust_level_4]}" + + serialized = described_class.new(Site.new(user_guardian), scope: user_guardian, root: false).as_json + expect(serialized[:whispers_allowed_groups_names]).to eq(nil) + end + end end