DEV: Rename direct message related models

This is a followup of the previous refactor where we created two new
models to handle all the dedicated logic that was present in the
`ChatChannel` model.

For the sake of consistency, `DMChannel` has been renamed to
`DirectMessageChannel` and the previous `DirectMessageChannel` model is
now named `DirectMessage`. This should help reasoning about direct
messages.
This commit is contained in:
Loïc Guitaut
2022-11-02 15:53:36 +01:00
committed by Loïc Guitaut
parent 7e992cb299
commit abcaa1a961
57 changed files with 378 additions and 455 deletions

View File

@ -53,7 +53,7 @@ module Chat::ChatChannelFetcher
.select(:id)
.joins(
"INNER JOIN direct_message_channels ON direct_message_channels.id = chat_channels.chatable_id
AND chat_channels.chatable_type = 'DirectMessageChannel'
AND chat_channels.chatable_type = 'DirectMessage'
INNER JOIN direct_message_users ON direct_message_users.direct_message_channel_id = direct_message_channels.id",
)
.where("direct_message_users.user_id = :user_id", user_id: guardian.user.id)
@ -134,7 +134,7 @@ module Chat::ChatChannelFetcher
query
.joins(:user_chat_channel_memberships)
.where(user_chat_channel_memberships: { user_id: user_id, following: true })
.where(chatable_type: "DirectMessageChannel")
.where(chatable_type: "DirectMessage")
.where("chat_channels.id IN (#{generate_allowed_channel_ids_sql(guardian)})")
.order(last_message_sent_at: :desc)
.to_a

View File

@ -50,7 +50,7 @@ class Chat::ChatMailer
(uccm.last_unread_mention_when_emailed_id IS NULL OR c_msg.id > uccm.last_unread_mention_when_emailed_id) AND
(
(uccm.user_id = c_mentions.user_id AND uccm.following IS true AND cc.chatable_type = 'Category') OR
(cc.chatable_type = 'DirectMessageChannel')
(cc.chatable_type = 'DirectMessage')
)
SQL
.group("users.id, uccm.user_id")

View File

@ -7,13 +7,13 @@ module Chat::DirectMessageChannelCreator
def self.create!(acting_user:, target_users:)
Guardian.new(acting_user).ensure_can_create_direct_message!
target_users.uniq!
direct_messages_channel = DirectMessageChannel.for_user_ids(target_users.map(&:id))
if direct_messages_channel
chat_channel = ChatChannel.find_by!(chatable: direct_messages_channel)
direct_message = DirectMessage.for_user_ids(target_users.map(&:id))
if direct_message
chat_channel = ChatChannel.find_by!(chatable: direct_message)
else
ensure_actor_can_communicate!(acting_user, target_users)
direct_messages_channel = DirectMessageChannel.create!(user_ids: target_users.map(&:id))
chat_channel = direct_messages_channel.create_chat_channel!
direct_message = DirectMessage.create!(user_ids: target_users.map(&:id))
chat_channel = direct_message.create_chat_channel!
end
update_memberships(acting_user, target_users, chat_channel.id)

View File

@ -6,7 +6,7 @@ require "faker"
module DiscourseDev
class DirectChannel < Record
def initialize
super(::DirectMessageChannel, 5)
super(::DirectMessage, 5)
end
def data

View File

@ -11,7 +11,7 @@ module DiscourseDev
def data
if Faker::Boolean.boolean(true_ratio: 0.5)
channel = ::ChatChannel.where(chatable_type: "DirectMessageChannel").order("RANDOM()").first
channel = ::ChatChannel.where(chatable_type: "DirectMessage").order("RANDOM()").first
channel.user_chat_channel_memberships.update_all(following: true)
user = channel.chatable.users.order("RANDOM()").first
else

View File

@ -20,7 +20,7 @@ module Chat::UserNotificationsExtension
(uccm.last_unread_mention_when_emailed_id IS NULL OR chat_messages.id > uccm.last_unread_mention_when_emailed_id) AND
(
(cm.user_id = :user_id AND uccm.following IS true AND chat_channels.chatable_type = 'Category') OR
(chat_channels.chatable_type = 'DirectMessageChannel')
(chat_channels.chatable_type = 'DirectMessage')
)
SQL
.to_a

View File

@ -153,10 +153,10 @@ module Chat::GuardianExtensions
return false if !can_modify_channel_message?(message.chat_channel)
if message.user_id == current_user.id
case chatable.class.name
when "Category"
case chatable
when Category
return can_see_category?(chatable)
when "DirectMessageChannel"
when DirectMessage
return true
end
end