diff --git a/assets/javascripts/discourse/routes/transcript.js b/assets/javascripts/discourse/routes/transcript.js index 57b39de..f9ad13c 100644 --- a/assets/javascripts/discourse/routes/transcript.js +++ b/assets/javascripts/discourse/routes/transcript.js @@ -1,32 +1,31 @@ import { ajax } from "discourse/lib/ajax"; import { popupAjaxError } from "discourse/lib/ajax-error"; import DiscourseRoute from "discourse/routes/discourse"; -import { next } from "@ember/runloop"; +import { inject as service } from "@ember/service"; export default class Trascript extends DiscourseRoute { - model(params) { - if (this.currentUser) { - const secret = params.secret; + @service currentUser; + @service composer; + @service router; - this.replaceWith("discovery.latest").then((e) => { - if (this.controllerFor("navigation/default").get("canCreateTopic")) { - next(() => { - ajax(`chat-transcript/${secret}`).then((result) => { - e.send( - "createNewTopicViaParams", - null, - result["content"], - null, - null, - null - ); - }, popupAjaxError); - }); - } - }); - } else { + async model(params) { + if (!this.currentUser) { this.session.set("shouldRedirectToUrl", window.location.href); - this.replaceWith("login"); + this.router.replaceWith("login"); + return; + } + + const secret = params.secret; + + await this.router.replaceWith("discovery.latest").followRedirects(); + + try { + const result = await ajax(`/chat-transcript/${secret}`); + this.composer.openNewTopic({ + body: result.content, + }); + } catch (e) { + popupAjaxError(e); } } } diff --git a/plugin.rb b/plugin.rb index 4d20789..ec9dd7e 100644 --- a/plugin.rb +++ b/plugin.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true # name: discourse-chat-integration -# about: Allows integration with several external chat system providers +# about: Allows integration with several external chat system providers # meta_topic_id: 66522 # version: 0.1 # url: https://github.com/discourse/discourse-chat-integration diff --git a/test/javascripts/acceptance/transcript-test.js b/test/javascripts/acceptance/transcript-test.js new file mode 100644 index 0000000..6768cd3 --- /dev/null +++ b/test/javascripts/acceptance/transcript-test.js @@ -0,0 +1,37 @@ +import { settled, visit } from "@ember/test-helpers"; +import { test } from "qunit"; +import { acceptance } from "discourse/tests/helpers/qunit-helpers"; + +/** + * Workaround for https://github.com/tildeio/router.js/pull/335 + */ +async function visitWithRedirects(url) { + try { + await visit(url); + } catch (error) { + const { message } = error; + if (message !== "TransitionAborted") { + throw error; + } + await settled(); + } +} + +acceptance("Chat Integration - slack transcript", function (needs) { + needs.user({ + can_create_topic: true, + }); + + needs.pretender((server, helper) => { + server.get("/chat-transcript/abcde", () => { + return helper.response({ + content: "This is a chat transcript", + }); + }); + }); + + test("Can open composer with transcript", async function (assert) { + await visitWithRedirects("/chat-transcript/abcde"); + assert.dom(".d-editor-input").hasValue("This is a chat transcript"); + }); +});