mirror of
https://github.com/discourse/discourse.git
synced 2025-05-24 01:14:12 +08:00
DEV: adds sendChatMessage client API (#21783)
Usage: ``` api.sendChatMessage(channelId, message, { threadId: 6 }); ```
This commit is contained in:
@ -114,12 +114,12 @@ export function chatComposerButtons(composer, position, context) {
|
|||||||
|
|
||||||
if (isFunction(button.action)) {
|
if (isFunction(button.action)) {
|
||||||
result.action = () => {
|
result.action = () => {
|
||||||
button.action.apply(composer);
|
button.action.apply(composer, [context]);
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
const actionName = button.action;
|
const actionName = button.action;
|
||||||
result.action = () => {
|
result.action = () => {
|
||||||
composer[actionName]();
|
composer[actionName](context);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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 {
|
export default {
|
||||||
name: "chat-plugin-api",
|
name: "chat-plugin-api",
|
||||||
after: "inject-discourse-objects",
|
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,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -170,6 +170,8 @@ export default class ChatApi extends Service {
|
|||||||
* @param {string} data.cooked - The cooked content of the message.
|
* @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.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.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.
|
* @param {Array.<number>} [data.upload_ids] - Array of upload ids linked to the message.
|
||||||
* @returns {Promise}
|
* @returns {Promise}
|
||||||
*/
|
*/
|
||||||
|
@ -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 });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
Reference in New Issue
Block a user