FEATURE: display warning when sharing a topic in a restricted category (#14820)

FEATURE: display warning when sharing a topic in a restricted category (#14820)

  • FEATURE: display warning when sharing a topic in a restricted category

If a topic belongs to a category that is not readable by everyone, display a text warning of “Only visible to members of groups: [group_a], [group_b]”

  • DEV: Adding a new category means we need to bump this value

  • DEV: pass category to showModal

diff --git a/app/assets/javascripts/discourse/app/components/composer-messages.js b/app/assets/javascripts/discourse/app/components/composer-messages.js
index a888c28..ad323bc 100644
--- a/app/assets/javascripts/discourse/app/components/composer-messages.js
+++ b/app/assets/javascripts/discourse/app/components/composer-messages.js
@@ -76,7 +76,7 @@ export default Component.extend({
 
     shareModal() {
       const { topic } = this.composer;
-      const controller = showModal("share-topic");
+      const controller = showModal("share-topic", { model: topic.category });
       controller.setProperties({
         allowInvites:
           topic.details.can_invite_to &&
diff --git a/app/assets/javascripts/discourse/app/controllers/share-topic.js b/app/assets/javascripts/discourse/app/controllers/share-topic.js
index 7f8d3e2..d1c3657 100644
--- a/app/assets/javascripts/discourse/app/controllers/share-topic.js
+++ b/app/assets/javascripts/discourse/app/controllers/share-topic.js
@@ -9,13 +9,29 @@ import showModal from "discourse/lib/show-modal";
 import { bufferedProperty } from "discourse/mixins/buffered-content";
 import ModalFunctionality from "discourse/mixins/modal-functionality";
 import I18n from "I18n";
+import Category from "discourse/models/category";
 
 export default Controller.extend(
   ModalFunctionality,
   bufferedProperty("invite"),
   {
+    topic: null,
+    restrictedGroups: null,
+
     onShow() {
       this.set("showNotifyUsers", false);
+
+      if (this.model && this.model.read_restricted) {
+        Category.reloadBySlugPath(this.model.slug).then((result) => {
+          this.setProperties({
+            restrictedGroups: result.category.group_permissions.map(
+              (g) => g.group_name
+            ),
+          });
+        });
+      } else {
+        this.setProperties({ restrictedGroups: null });
+      }
     },
 
     @discourseComputed("topic.shareUrl")
@@ -39,6 +55,21 @@ export default Controller.extend(
       );
     },
 
+    @discourseComputed("restrictedGroups")
+    hasRestrictedGroups(groups) {
+      return !!groups;
+    },
+
+    @discourseComputed("restrictedGroups")
+    restrictedGroupsCount(groups) {
+      return groups.length;
+    },
+
+    @discourseComputed("restrictedGroups")
+    restrictedGroupsDisplayText(groups) {
+      return groups.join(", ");
+    },
+
     @action
     onChangeUsers(usernames) {
       this.set("users", usernames.uniq());
diff --git a/app/assets/javascripts/discourse/app/initializers/topic-footer-buttons.js b/app/assets/javascripts/discourse/app/initializers/topic-footer-buttons.js
index a8aa789..a5a5227 100644
--- a/app/assets/javascripts/discourse/app/initializers/topic-footer-buttons.js
+++ b/app/assets/javascripts/discourse/app/initializers/topic-footer-buttons.js
@@ -23,7 +23,9 @@ export default {
       },
       title: "topic.share.help",
       action() {
-        const controller = showModal("share-topic");
+        const controller = showModal("share-topic", {
+          model: this.topic.category,
+        });
         controller.setProperties({
           allowInvites: this.canInviteTo && !this.inviteDisabled,
           topic: this.topic,
diff --git a/app/assets/javascripts/discourse/app/templates/modal/share-topic.hbs b/app/assets/javascripts/discourse/app/templates/modal/share-topic.hbs
index cbdac56..ff2de50 100644
--- a/app/assets/javascripts/discourse/app/templates/modal/share-topic.hbs
+++ b/app/assets/javascripts/discourse/app/templates/modal/share-topic.hbs
@@ -12,6 +12,13 @@
         {{copy-button selector="input.invite-link"}}
       </div>
     </div>
+
+    {{#if hasRestrictedGroups}}
+      <div class="link-share-restricted-groups">
+        {{i18n "topic.share.restricted_groups" count=restrictedGroupsCount}}
+        {{restrictedGroupsDisplayText}}
+      </div>
+    {{/if}}
     <div class="link-share-actions">
       <div class="sources">
         {{#each sources as |s|}}
diff --git a/app/assets/javascripts/discourse/tests/acceptance/share-topic-test.js b/app/assets/javascripts/discourse/tests/acceptance/share-topic-test.js
index 0b952d6..3446b53 100644
--- a/app/assets/javascripts/discourse/tests/acceptance/share-topic-test.js
+++ b/app/assets/javascripts/discourse/tests/acceptance/share-topic-test.js
@@ -48,6 +48,23 @@ acceptance("Share and Invite modal", function (needs) {
 
     assert.ok(exists("#share-link"), "it shows the share modal");
   });
+
+  test("Share topic in a restricted category", async function (assert) {
+    await visit("/t/topic-in-restricted-group/2481");
+
+    assert.ok(
+      exists("#topic-footer-button-share-and-invite"),
+      "the button exists"
+    );
+
+    await click("#topic-footer-button-share-and-invite");
+
+    assert.ok(exists(".share-topic-modal"), "it shows the modal");
+    assert.ok(
+      exists(".link-share-restricted-groups"),
+      "it shows restricted warning"
+    );
+  });
 });
 
 acceptance("Share and Invite modal - mobile", function (needs) {
diff --git a/app/assets/javascripts/discourse/tests/fixtures/category-fixtures.js b/app/assets/javascripts/discourse/tests/fixtures/category-fixtures.js
index 6edf9ea..44ed93d 100644
--- a/app/assets/javascripts/discourse/tests/fixtures/category-fixtures.js
+++ b/app/assets/javascripts/discourse/tests/fixtures/category-fixtures.js
@@ -48,5 +48,18 @@ export default {
       slug: "testing",
       can_edit: true
     }
-  }
+  },
+  "/c/2481/show.json": {
+    category: {
+      id: 2481,
+      name: "restricted-group",
+      color: "e9dd00",
+      text_color: "000000",
+      slug: "restricted-group",
+      read_restricted: true,
+      permission: null,
+      group_permissions: [{ permission_type: 1, group_name: "moderators" }],
+    }
+  },
+
 };
diff --git a/app/assets/javascripts/discourse/tests/fixtures/site-fixtures.js b/app/assets/javascripts/discourse/tests/fixtures/site-fixtures.js
index 68488ba..1151e2e 100644
--- a/app/assets/javascripts/discourse/tests/fixtures/site-fixtures.js
+++ b/app/assets/javascripts/discourse/tests/fixtures/site-fixtures.js
@@ -438,6 +438,23 @@ export default {
           default_view: "latest",
           subcategory_list_style: "boxes",
         },
+        {
+          id: 2481,
+          name: "Restricted Group",
+          color: "0E78BD",
+          text_color: "FFFFFF",
+          slug: "restricted-group",
+          topic_count: 137,
+          post_count: 1142,
+          description: "A restricted group",
+          topic_url: "/t/category-definition-for-restricted-group/11",
+          read_restricted: true,
+          permission: 1,
+          notification_level: null,
+          show_subcategory_list: true,
+          default_view: "latest",
+          subcategory_list_style: "boxes",
+        },
       ],
       post_action_types: [
         {
diff --git a/app/assets/javascripts/discourse/tests/fixtures/topic.js b/app/assets/javascripts/discourse/tests/fixtures/topic.js
index a4db6fe..35437db 100644
--- a/app/assets/javascripts/discourse/tests/fixtures/topic.js
+++ b/app/assets/javascripts/discourse/tests/fixtures/topic.js
@@ -5855,4 +5855,347 @@ export default {
       },
     },
   },
+  "/t/2481/1.json": {
+    post_stream: {
+      posts: [
+        {
+          id: 2441,
+          name: "",
+          username: "group_moderator",
+          avatar_template: "/images/avatar.png",
+          created_at: "2020-07-24T17:48:55.419Z",
+          cooked:
+            "<p>Here is my new topic. I am a group category moderator!</p>",
+          post_number: 1,
+          post_type: 1,
+          updated_at: "2020-07-24T17:48:55.419Z",
+          reply_count: 0,
+          reply_to_post_number: null,
+          quote_count: 0,
+          incoming_link_count: 0,
+          reads: 2,
+          readers_count: 1,
+          score: 0,
+          yours: true,
+          topic_id: 2481,
+          topic_slug: "topic-in-restricted-group",
+          display_username: "",

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

GitHub sha: e306d84c564b5674fe37bd0fa29c630a75228921

This commit appears in #14820 which was approved by eviltrout. It was merged by jbrw.