DEV: Add last_message_id to channel and thread (#22488)

Initial migration and changes to models as well as
changing the following services to update last_message_id:

* Chat::MessageCreator
* Chat::RestoreMessage
* Chat::TrashMessage

The data migration will set the `last_message_id` for all existing
threads and channels in the database.

When we query the thread list as well as the channel,
we look at the last message ID for the following:

* Channel - Sorting DM channels, and channel metadata for the list of channels
* Thread - Last reply details for thread indicators and thread list
This commit is contained in:
Martin Brennan
2023-07-13 10:28:11 +10:00
committed by GitHub
parent 4ae26bcaac
commit b1978e7ad8
53 changed files with 554 additions and 212 deletions

View File

@ -88,7 +88,7 @@ module Chat
def self.secured_public_channel_search(guardian, options = {})
allowed_channel_ids = generate_allowed_channel_ids_sql(guardian, exclude_dm_channels: true)
channels = Chat::Channel.includes(chatable: [:topic_only_relative_url])
channels = Chat::Channel.includes(:last_message, chatable: [:topic_only_relative_url])
channels = channels.includes(:chat_channel_archive) if options[:include_archives]
channels =
@ -179,10 +179,15 @@ module Chat
def self.secured_direct_message_channels_search(user_id, guardian, options = {})
query =
Chat::Channel.strict_loading.includes(
last_message: [:uploads],
chatable: [{ direct_message_users: [user: :user_option] }, :users],
)
query = query.includes(chatable: [{ users: :user_status }]) if SiteSetting.enable_user_status
query = query.joins(:user_chat_channel_memberships)
query =
query.joins(
"LEFT JOIN chat_messages last_message ON last_message.id = chat_channels.last_message_id",
)
scoped_channels =
Chat::Channel
@ -223,7 +228,7 @@ module Chat
query
.where(chatable_type: Chat::Channel.direct_channel_chatable_types)
.where(chat_channels: { id: scoped_channels })
.order(last_message_sent_at: :desc)
.order("last_message.created_at DESC NULLS LAST")
channels = query.to_a
preload_fields =

View File

@ -63,6 +63,7 @@ module Chat
@chat_message.attach_uploads(uploads)
Chat::Draft.where(user_id: @user.id, chat_channel_id: @chat_channel.id).destroy_all
post_process_resolved_thread
update_channel_last_message
Chat::Publisher.publish_new!(
@chat_channel,
@chat_message,
@ -71,7 +72,6 @@ module Chat
)
Jobs.enqueue(Jobs::Chat::ProcessMessage, { chat_message_id: @chat_message.id })
Chat::Notifier.notify_new(chat_message: @chat_message, timestamp: @chat_message.created_at)
@chat_channel.touch(:last_message_sent_at)
DiscourseEvent.trigger(:chat_message_created, @chat_message, @chat_channel, @user)
rescue => error
@error = error
@ -232,6 +232,7 @@ module Chat
def post_process_resolved_thread
return if resolved_thread.blank?
resolved_thread.update!(last_message: @chat_message)
resolved_thread.increment_replies_count_cache
current_user_thread_membership = resolved_thread.add(@user)
current_user_thread_membership.update!(last_read_message_id: @chat_message.id)
@ -240,5 +241,10 @@ module Chat
resolved_thread.add(resolved_thread.original_message_user)
end
end
def update_channel_last_message
return if @chat_message.thread_reply?
@chat_channel.update!(last_message: @chat_message)
end
end
end

View File

@ -73,7 +73,7 @@ module Chat
# Prioritize messages from regular channels over direct messages
if channels.any?
channel_notification_text(
channels.sort_by { |channel| [channel.last_message_sent_at, channel.created_at] },
channels.sort_by { |channel| [channel.last_message.created_at, channel.created_at] },
dm_users,
)
else