mirror of
https://github.com/discourse/discourse.git
synced 2025-06-03 19:39:30 +08:00
DEV: allows a context when creating a message (#25647)
The service `Chat::CreateMessage` will now accept `context_post_ids` and `context_topic_id` as params. These values represent the topic which might be visible when sending a message (for now, this is only possible when using the drawer). The `DiscourseEvent` `chat_message_created` will now have the following signature: ```ruby on(:chat_message_created) do | message, channel, user, meta| p meta[:context][:post_ids] end ```
This commit is contained in:
@ -35,6 +35,7 @@ import {
|
||||
checkMessageTopVisibility,
|
||||
} from "discourse/plugins/chat/discourse/lib/check-message-visibility";
|
||||
import DatesSeparatorsPositioner from "discourse/plugins/chat/discourse/lib/dates-separators-positioner";
|
||||
import { extractCurrentTopicInfo } from "discourse/plugins/chat/discourse/lib/extract-current-topic-info";
|
||||
import {
|
||||
scrollListToBottom,
|
||||
scrollListToMessage,
|
||||
@ -560,12 +561,17 @@ export default class ChatChannel extends Component {
|
||||
}
|
||||
|
||||
try {
|
||||
await this.chatApi.sendMessage(this.args.channel.id, {
|
||||
const params = {
|
||||
message: message.message,
|
||||
in_reply_to_id: message.inReplyTo?.id,
|
||||
staged_id: message.id,
|
||||
upload_ids: message.uploads.map((upload) => upload.id),
|
||||
});
|
||||
};
|
||||
|
||||
await this.chatApi.sendMessage(
|
||||
this.args.channel.id,
|
||||
Object.assign({}, params, extractCurrentTopicInfo(this))
|
||||
);
|
||||
|
||||
if (!this.capabilities.isIOS) {
|
||||
this.scrollToLatestMessage();
|
||||
|
@ -25,6 +25,7 @@ import {
|
||||
} from "discourse/plugins/chat/discourse/lib/chat-ios-hacks";
|
||||
import ChatMessagesLoader from "discourse/plugins/chat/discourse/lib/chat-messages-loader";
|
||||
import DatesSeparatorsPositioner from "discourse/plugins/chat/discourse/lib/dates-separators-positioner";
|
||||
import { extractCurrentTopicInfo } from "discourse/plugins/chat/discourse/lib/extract-current-topic-info";
|
||||
import {
|
||||
scrollListToBottom,
|
||||
scrollListToMessage,
|
||||
@ -429,15 +430,17 @@ export default class ChatThread extends Component {
|
||||
}
|
||||
|
||||
try {
|
||||
const params = {
|
||||
message: message.message,
|
||||
in_reply_to_id: null,
|
||||
staged_id: message.id,
|
||||
upload_ids: message.uploads.map((upload) => upload.id),
|
||||
thread_id: message.thread.id,
|
||||
};
|
||||
|
||||
const response = await this.chatApi.sendMessage(
|
||||
this.args.thread.channel.id,
|
||||
{
|
||||
message: message.message,
|
||||
in_reply_to_id: null,
|
||||
staged_id: message.id,
|
||||
upload_ids: message.uploads.map((upload) => upload.id),
|
||||
thread_id: message.thread.id,
|
||||
}
|
||||
Object.assign({}, params, extractCurrentTopicInfo(this))
|
||||
);
|
||||
|
||||
this.args.thread.currentUserMembership ??=
|
||||
|
@ -0,0 +1,29 @@
|
||||
import { getOwner } from "@ember/application";
|
||||
|
||||
export function extractCurrentTopicInfo(context) {
|
||||
const topic = getOwner(context).lookup("controller:topic")?.get("model");
|
||||
|
||||
if (!topic) {
|
||||
return;
|
||||
}
|
||||
|
||||
const info = { topic_id: topic.id };
|
||||
const currentPostNumber = parseInt(topic.current_post_number, 10);
|
||||
const posts = topic.postStream.posts;
|
||||
|
||||
const currentPost = posts.find(
|
||||
(post) => post.post_number === currentPostNumber
|
||||
);
|
||||
const previousPost = posts.findLast(
|
||||
(post) =>
|
||||
!post.hidden && !post.deleted_at && post.post_number < currentPostNumber
|
||||
);
|
||||
const nextPost = posts.find(
|
||||
(post) =>
|
||||
!post.hidden && !post.deleted_at && post.post_number > currentPostNumber
|
||||
);
|
||||
|
||||
info.post_ids = [previousPost?.id, currentPost?.id, nextPost?.id];
|
||||
|
||||
return info;
|
||||
}
|
@ -163,6 +163,8 @@ export default {
|
||||
.lookup("service:chat-api")
|
||||
.sendMessage(channelId, {
|
||||
thread_id: options.threadId,
|
||||
post_ids: options.postIds,
|
||||
topic_id: options.topicId,
|
||||
message: options.message,
|
||||
uploads: options.uploads,
|
||||
});
|
||||
|
@ -180,6 +180,8 @@ export default class ChatApi extends Service {
|
||||
* @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.topic_id] - The ID of the currently visible topic in drawer mode.
|
||||
* @param {number} [data.post_ids] - The ID of the currently visible posts in drawer mode.
|
||||
* @param {Array.<number>} [data.upload_ids] - Array of upload ids linked to the message.
|
||||
* @returns {Promise}
|
||||
*/
|
||||
|
Reference in New Issue
Block a user