DEV: adds sendChatMessage client API (#21783)

Usage:

```
api.sendChatMessage(channelId, message, { threadId: 6 });
```
This commit is contained in:
Joffrey JAFFEUX 2023-05-29 18:41:12 +02:00 committed by GitHub
parent 7218da7510
commit 4ce8ea7496
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 61 additions and 2 deletions

View File

@ -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);
};
}

View File

@ -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,
});
},
});
}
});
},

View File

@ -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}
*/

View File

@ -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 });
});
});
});