diff --git a/.github/workflows/discourse-plugin.yml b/.github/workflows/discourse-plugin.yml index f5cf62e..9a022b0 100644 --- a/.github/workflows/discourse-plugin.yml +++ b/.github/workflows/discourse-plugin.yml @@ -3,7 +3,7 @@ name: Discourse Plugin on: push: branches: - - main + - master pull_request: jobs: diff --git a/assets/javascripts/discourse/initializers/retort-init.js.es6 b/assets/javascripts/discourse/initializers/retort-init.js.es6 index d2a41c4..9e00258 100644 --- a/assets/javascripts/discourse/initializers/retort-init.js.es6 +++ b/assets/javascripts/discourse/initializers/retort-init.js.es6 @@ -22,29 +22,35 @@ function initializePlugin(api) { } api.decorateWidget("post-menu:before-extra-controls", (helper) => { - let retort = api.container.lookup("service:retort"); - let postId = helper.getModel().id; - let post = retort.postFor(postId); + let currentUser = api.container.lookup("service:currentUser"); - if (retort.disabledFor(postId)) { - return; - } + if (currentUser) { + let retort = api.container.lookup("service:retort"); + let postId = helper.getModel().id; + let post = retort.postFor(postId); - retort.storeWidget(helper); + if (retort.disabledFor(postId)) { + return; + } - if (!post.retorts) { - return; - } + retort.storeWidget(helper); + + if (!post.retorts) { + return; + } - const retorts = post.retorts.map(({ usernames, emoji }) => { - return helper.attach("retort-toggle", { - post, - usernames, - emoji, + const retorts = post.retorts.map(({ usernames, emoji }) => { + return helper.attach("retort-toggle", { + post, + usernames, + emoji, + }); }); - }); - return helper.h("div.post-retort-container", retorts); + return helper.h("div.post-retort-container", retorts); + } else { + return ""; + } }); api.addPostClassesCallback((attrs) => { diff --git a/assets/javascripts/discourse/services/retort.js b/assets/javascripts/discourse/services/retort.js index 13d350d..77254d2 100644 --- a/assets/javascripts/discourse/services/retort.js +++ b/assets/javascripts/discourse/services/retort.js @@ -8,6 +8,7 @@ export default class RetortService extends Service { @service siteSettings; @service messageBus; @service router; + @service currentUser; @tracked picker; @tracked model; @tracked widgets; @@ -20,19 +21,20 @@ export default class RetortService extends Service { if (this.model.id) { this.messageBus.unsubscribe(`/retort/topics/${this.model.id}`); } + if (this.currentUser) { + this.messageBus.subscribe( + `/retort/topics/${this.model.id}`, + ({ id, retorts }) => { + const post = this.postFor(id); + if (!post) { + return; + } - this.messageBus.subscribe( - `/retort/topics/${this.model.id}`, - ({ id, retorts }) => { - const post = this.postFor(id); - if (!post) { - return; + post.setProperties({ retorts }); + this.get(`widgets.${id}`).scheduleRerender(); } - - post.setProperties({ retorts }); - this.get(`widgets.${id}`).scheduleRerender(); - } - ); + ); + } } postFor(id) { @@ -59,6 +61,9 @@ export default class RetortService extends Service { } disabledFor(postId) { + if (!this.currentUser) { + return true; + } const post = this.postFor(postId); if (!post) { return true; diff --git a/test/javascripts/acceptance/retort-anon-test.js b/test/javascripts/acceptance/retort-anon-test.js new file mode 100644 index 0000000..ee407fb --- /dev/null +++ b/test/javascripts/acceptance/retort-anon-test.js @@ -0,0 +1,21 @@ +import { visit } from "@ember/test-helpers"; +import { test } from "qunit"; +import { acceptance, exists } from "discourse/tests/helpers/qunit-helpers"; +import { default as RetortTopics } from "../fixtures/retort-topic-fixtures"; + +acceptance("Retort - Anon", function (needs) { + needs.settings({ + retort_enabled: true, + }); + + needs.pretender((server, helper) => { + const topicPath = "/t/56.json"; + server.get(topicPath, () => helper.response(RetortTopics[topicPath])); + }); + + test("It does not show retort controls", async (assert) => { + await visit("/t/slug/56"); + + assert.notOk(exists("button.retort"), "retort controls are not available"); + }); +});