mirror of
https://github.com/discourse/discourse.git
synced 2026-08-13 06:25:11 -05:00
FEATURE: Thread list initial UI (#21412)
This commit adds an initial thread list UI. There are several limitations with this that will be addressed in future PRs: * There is no MessageBus reactivity, so e.g. if someone edits the original message of the thread it will not be reflected in the list. However if the thread title is updated the original message indicator will be updated. * There is no unread functionality for threads in the list, if new messages come into the thread there is no indicator in the UI. * There is no unread indicator on the actual button to open the thread list. * No pagination. In saying that, this is the functionality so far: * We show a list of the 50 threads that the user has most recently participated in (i.e. sent a message) for the channel in descending order. * Each thread we show a rich excerpt, the title, and the user who is the OM creator. * The title is editable by staff and by the OM creator. * Thread indicators show a title. We also replace emojis in the titles. * Thread list works in the drawer/mobile.
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Chat
|
||||
# Gets a list of threads for a channel to be shown in an index.
|
||||
# In future pagination and filtering will be added -- for now
|
||||
# we just want to return N threads ordered by the latest
|
||||
# message that the user has sent in a thread.
|
||||
#
|
||||
# @example
|
||||
# Chat::LookupChannelThreads.call(channel_id: 2, guardian: guardian)
|
||||
#
|
||||
class LookupChannelThreads
|
||||
include Service::Base
|
||||
|
||||
# @!method call(channel_id:, guardian:)
|
||||
# @param [Integer] channel_id
|
||||
# @param [Guardian] guardian
|
||||
# @return [Service::Base::Context]
|
||||
|
||||
policy :threaded_discussions_enabled
|
||||
contract
|
||||
model :channel
|
||||
policy :threading_enabled_for_channel
|
||||
policy :can_view_channel
|
||||
model :threads
|
||||
|
||||
# @!visibility private
|
||||
class Contract
|
||||
attribute :channel_id, :integer
|
||||
validates :channel_id, presence: true
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def threaded_discussions_enabled
|
||||
SiteSetting.enable_experimental_chat_threaded_discussions
|
||||
end
|
||||
|
||||
def fetch_channel(contract:, **)
|
||||
Chat::Channel.find_by(id: contract.channel_id)
|
||||
end
|
||||
|
||||
def threading_enabled_for_channel(channel:, **)
|
||||
channel.threading_enabled
|
||||
end
|
||||
|
||||
def can_view_channel(guardian:, channel:, **)
|
||||
guardian.can_preview_chat_channel?(channel)
|
||||
end
|
||||
|
||||
def fetch_threads(guardian:, channel:, **)
|
||||
Chat::Thread
|
||||
.includes(
|
||||
:channel,
|
||||
original_message_user: :user_status,
|
||||
original_message: :chat_webhook_event,
|
||||
)
|
||||
.select("chat_threads.*, MAX(chat_messages.created_at) AS last_posted_at")
|
||||
.joins(
|
||||
"LEFT JOIN chat_messages ON chat_threads.id = chat_messages.thread_id AND chat_messages.chat_channel_id = #{channel.id}",
|
||||
)
|
||||
.joins(
|
||||
"LEFT JOIN chat_messages original_messages ON chat_threads.original_message_id = original_messages.id",
|
||||
)
|
||||
.where("chat_messages.user_id = ? OR chat_messages.user_id IS NULL", guardian.user.id)
|
||||
.where(channel_id: channel.id)
|
||||
.where("original_messages.deleted_at IS NULL AND chat_messages.deleted_at IS NULL")
|
||||
.group("chat_threads.id")
|
||||
.order("last_posted_at DESC NULLS LAST")
|
||||
.limit(50)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -72,6 +72,7 @@ module Chat
|
||||
type: :update_thread_original_message,
|
||||
original_message_id: thread.original_message_id,
|
||||
replies_count: thread.replies_count_cache,
|
||||
title: thread.title,
|
||||
},
|
||||
)
|
||||
end
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Chat
|
||||
# Updates a thread. The thread_id and channel_id must
|
||||
# match. For now we do not want to allow updating threads if the
|
||||
# enable_experimental_chat_threaded_discussions hidden site setting
|
||||
# is not turned on, and the channel must specifically have threading
|
||||
# enabled.
|
||||
#
|
||||
# Only the thread title can be updated.
|
||||
#
|
||||
# @example
|
||||
# Chat::UpdateThread.call(thread_id: 88, channel_id: 2, guardian: guardian, title: "Restaurant for Saturday")
|
||||
#
|
||||
class UpdateThread
|
||||
include Service::Base
|
||||
|
||||
# @!method call(thread_id:, channel_id:, guardian:, **params_to_edit)
|
||||
# @param [Integer] thread_id
|
||||
# @param [Integer] channel_id
|
||||
# @param [Guardian] guardian
|
||||
# @option params_to_edit [String,nil] title
|
||||
# @return [Service::Base::Context]
|
||||
|
||||
policy :threaded_discussions_enabled
|
||||
contract
|
||||
model :thread, :fetch_thread
|
||||
policy :can_view_channel
|
||||
policy :can_edit_thread
|
||||
policy :threading_enabled_for_channel
|
||||
step :update
|
||||
step :publish_metadata
|
||||
|
||||
# @!visibility private
|
||||
class Contract
|
||||
attribute :thread_id, :integer
|
||||
attribute :channel_id, :integer
|
||||
attribute :title, :string
|
||||
|
||||
validates :thread_id, :channel_id, presence: true
|
||||
validates :title, length: { maximum: Chat::Thread::MAX_TITLE_LENGTH }
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def threaded_discussions_enabled
|
||||
SiteSetting.enable_experimental_chat_threaded_discussions
|
||||
end
|
||||
|
||||
def fetch_thread(contract:, **)
|
||||
Chat::Thread.find_by(id: contract.thread_id, channel_id: contract.channel_id)
|
||||
end
|
||||
|
||||
def can_view_channel(guardian:, thread:, **)
|
||||
guardian.can_preview_chat_channel?(thread.channel)
|
||||
end
|
||||
|
||||
def can_edit_thread(guardian:, thread:, **)
|
||||
guardian.can_edit_thread?(thread)
|
||||
end
|
||||
|
||||
def threading_enabled_for_channel(thread:, **)
|
||||
thread.channel.threading_enabled
|
||||
end
|
||||
|
||||
def update(thread:, contract:, **)
|
||||
thread.update(title: contract.title)
|
||||
fail!(thread.errors.full_messages.join(", ")) if thread.invalid?
|
||||
end
|
||||
|
||||
def publish_metadata(thread:, **)
|
||||
Chat::Publisher.publish_thread_original_message_metadata!(thread)
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user