A platform for community discussion. Free, open, simple.
DEV: adds sendChatMessage client API (#21783)
Joffrey JAFFEUX4ce8ea7496fa
diff --git a/plugins/chat/assets/javascripts/discourse/lib/chat-composer-buttons.js b/plugins/chat/assets/javascripts/discourse/lib/chat-composer-buttons.jsindex 629683aff2f..9d9390a1df1 100644--- a/plugins/chat/assets/javascripts/discourse/lib/chat-composer-buttons.js+++ b/plugins/chat/assets/javascripts/discourse/lib/chat-composer-buttons.js@@ -114,12 +114,12 @@ export function chatComposerButtons(composer, position, context) {if (isFunction(button.action)) {result.action = () => {- button.action.apply(composer);+ button.action.apply(composer, [context]);};} else {const actionName = button.action;result.action = () => {- composer[actionName]();+ composer[actionName](context);};}diff --git a/plugins/chat/assets/javascripts/discourse/pre-initializers/chat-plugin-api.js b/plugins/chat/assets/javascripts/discourse/pre-initializers/chat-plugin-api.jsindex 25c57175b58..fcd0d26abbf 100644--- a/plugins/chat/assets/javascripts/discourse/pre-initializers/chat-plugin-api.js+++ b/plugins/chat/assets/javascripts/discourse/pre-initializers/chat-plugin-api.js@@ -91,6 +91,26 @@ import { addChatDrawerStateCallback } from "discourse/plugins/chat/discourse/ser* });*/+/**+ * Send a chat message, message or uploads must be provided+ *+ * @memberof PluginApi+ * @instance+ * @function sendChatMessage+ * @param {number} channelId - The id of the channel+ * @param {Object} options+ * @param {string} [options.message] - The content of the message to send+ * @param {string} [options.uploads] - A list of uploads to send+ * @param {number} [options.threadId] - The thread id where the message should be sent+ *+ * @example+ *+ * api.sendChatMessage(+ * 1,+ * { message: "Hello world", threadId: 2 }+ * );+ */+export default {name: "chat-plugin-api",after: "inject-discourse-objects",@@ -122,6 +142,20 @@ export default {},});}++ if (!apiPrototype.hasOwnProperty("sendChatMessage")) {+ Object.defineProperty(apiPrototype, "sendChatMessage", {+ async value(channelId, options = {}) {+ return this.container+ .lookup("service:chat-api")+ .sendMessage(channelId, {+ thread_id: options.threadId,+ message: options.message,+ uploads: options.uploads,+ });+ },+ });+ }});},diff --git a/plugins/chat/assets/javascripts/discourse/services/chat-api.js b/plugins/chat/assets/javascripts/discourse/services/chat-api.jsindex 86ac936a9aa..6420cfd5472 100644--- a/plugins/chat/assets/javascripts/discourse/services/chat-api.js+++ b/plugins/chat/assets/javascripts/discourse/services/chat-api.js@@ -170,6 +170,8 @@ export default class ChatApi extends Service {* @param {string} data.cooked - The cooked content of the message.* @param {number} [data.in_reply_to_id] - The ID of the replied-to message.* @param {number} [data.staged_id] - The staged ID of the message before it was persisted.+ * @param {number} [data.thread_id] - The ID of the thread where this message should be posted.+ * @param {number} [data.staged_thread_id] - The staged ID of the thread before it was persisted.* @param {Array.<number>} [data.upload_ids] - Array of upload ids linked to the message.* @returns {Promise}*/diff --git a/plugins/chat/test/javascripts/unit/utility/plugin-api-test.js b/plugins/chat/test/javascripts/unit/utility/plugin-api-test.jsnew file mode 100644index 00000000000..7de4ef09674--- /dev/null+++ b/plugins/chat/test/javascripts/unit/utility/plugin-api-test.js@@ -0,0 +1,23 @@+import { module, test } from "qunit";+import { withPluginApi } from "discourse/lib/plugin-api";+import { setupTest } from "ember-qunit";+import pretender from "discourse/tests/helpers/create-pretender";++module("Chat | Unit | Utility | plugin-api", function (hooks) {+ setupTest(hooks);++ test("#sendChatMessage", async function (assert) {+ const done = assert.async();++ pretender.post("/chat/1", (request) => {+ assert.strictEqual(request.url, "/chat/1");+ assert.strictEqual(request.requestBody, "thread_id=2&message=hello");+ done();+ return [200, {}, {}];+ });++ withPluginApi("1.1.0", async (api) => {+ await api.sendChatMessage(1, { message: "hello", threadId: 2 });+ });+ });+});