diff --git a/app/assets/javascripts/discourse/app/controllers/share-topic.js b/app/assets/javascripts/discourse/app/controllers/share-topic.js index c4fefc3a84b..def7fe308a8 100644 --- a/app/assets/javascripts/discourse/app/controllers/share-topic.js +++ b/app/assets/javascripts/discourse/app/controllers/share-topic.js @@ -28,13 +28,42 @@ export default Controller.extend( allowInvites: false, }); - if (this.model && this.model.read_restricted) { - this.restrictedGroupWarning(); - } + this.appEvents.on( + "modal:body-shown", + this, + this.showRestrictedGroupWarning + ); scheduleOnce("afterRender", this, this.selectUrl); }, + onClose() { + this.appEvents.off( + "modal:body-shown", + this, + this.showRestrictedGroupWarning + ); + }, + + showRestrictedGroupWarning() { + if (!this.model) { + return; + } + + Category.reloadBySlugPath(this.model.slug).then((result) => { + const groups = result.category.group_permissions.mapBy("group_name"); + if (groups && !groups.any((x) => x === "everyone")) { + this.flash( + I18n.t("topic.share.restricted_groups", { + count: groups.length, + groups: groups.join(", "), + }), + "warning" + ); + } + }); + }, + selectUrl() { const input = document.querySelector("input.invite-link"); if (input && !this.site.mobileView) { @@ -105,24 +134,5 @@ export default Controller.extend( topicController.actions.replyAsNewTopic.call(topicController, post); this.send("closeModal"); }, - - restrictedGroupWarning() { - this.appEvents.on("modal:body-shown", () => { - let restrictedGroups; - Category.reloadBySlugPath(this.model.slug).then((result) => { - restrictedGroups = result.category.group_permissions.map( - (g) => g.group_name - ); - - if (restrictedGroups) { - const message = I18n.t("topic.share.restricted_groups", { - count: restrictedGroups.length, - groupNames: restrictedGroups.join(", "), - }); - this.flash(message, "warning"); - } - }); - }); - }, } ); 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 4ad9c6f0aec..c1cb5afc9ee 100644 --- a/app/assets/javascripts/discourse/tests/acceptance/share-topic-test.js +++ b/app/assets/javascripts/discourse/tests/acceptance/share-topic-test.js @@ -1,3 +1,4 @@ +import CategoryFixtures from "discourse/tests/fixtures/category-fixtures"; import { click, visit } from "@ember/test-helpers"; import { acceptance, @@ -10,6 +11,12 @@ import { test } from "qunit"; acceptance("Share and Invite modal", function (needs) { needs.user(); + needs.pretender((server, helper) => { + server.get("/c/feature/find_by_slug.json", () => + helper.response(200, CategoryFixtures["/c/1/show.json"]) + ); + }); + test("Topic footer button", async function (assert) { await visit("/t/internationalization-localization/280"); diff --git a/app/assets/javascripts/discourse/tests/acceptance/topic-test.js b/app/assets/javascripts/discourse/tests/acceptance/topic-test.js index c24c6c7337a..d73ebf1fc31 100644 --- a/app/assets/javascripts/discourse/tests/acceptance/topic-test.js +++ b/app/assets/javascripts/discourse/tests/acceptance/topic-test.js @@ -21,10 +21,14 @@ import { test } from "qunit"; import { withPluginApi } from "discourse/lib/plugin-api"; import topicFixtures from "discourse/tests/fixtures/topic"; import { cloneJSON } from "discourse-common/lib/object"; +import CategoryFixtures from "discourse/tests/fixtures/category-fixtures"; acceptance("Topic", function (needs) { needs.user(); needs.pretender((server, helper) => { + server.get("/c/feature/find_by_slug.json", () => { + return helper.response(200, CategoryFixtures["/c/1/show.json"]); + }); server.put("/posts/398/wiki", () => { return helper.response({}); });