mirror of
https://github.com/discourse/discourse.git
synced 2025-06-04 23:36:11 +08:00
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:

committed by
Loïc Guitaut

parent
7e992cb299
commit
abcaa1a961
@ -129,7 +129,7 @@ class Chat::ChatChannelsController < Chat::ChatBaseController
|
||||
if users.count > 0
|
||||
ChatChannel
|
||||
.includes(chatable: :users)
|
||||
.joins(direct_message_channel: :direct_message_users)
|
||||
.joins(direct_message: :direct_message_users)
|
||||
.group(1)
|
||||
.having(
|
||||
"ARRAY[?] <@ ARRAY_AGG(user_id) AND ARRAY[?] && ARRAY_AGG(user_id)",
|
||||
|
@ -25,13 +25,9 @@ class Chat::DirectMessagesController < Chat::ChatBaseController
|
||||
guardian.ensure_can_chat!(current_user)
|
||||
users = users_from_usernames(current_user, params)
|
||||
|
||||
direct_message_channel = DirectMessageChannel.for_user_ids(users.map(&:id).uniq)
|
||||
if direct_message_channel
|
||||
chat_channel =
|
||||
ChatChannel.find_by(
|
||||
chatable_id: direct_message_channel.id,
|
||||
chatable_type: "DirectMessageChannel",
|
||||
)
|
||||
direct_message = DirectMessage.for_user_ids(users.map(&:id).uniq)
|
||||
if direct_message
|
||||
chat_channel = ChatChannel.find_by(chatable: direct_message)
|
||||
render_serialized(
|
||||
chat_channel,
|
||||
ChatChannelSerializer,
|
||||
|
@ -4,8 +4,8 @@ class ChatChannel < ActiveRecord::Base
|
||||
include Trashable
|
||||
|
||||
belongs_to :chatable, polymorphic: true
|
||||
belongs_to :direct_message_channel,
|
||||
-> { where(chat_channels: { chatable_type: "DirectMessageChannel" }) },
|
||||
belongs_to :direct_message,
|
||||
-> { where(chat_channels: { chatable_type: "DirectMessage" }) },
|
||||
foreign_key: "chatable_id"
|
||||
|
||||
has_many :chat_messages
|
||||
@ -37,7 +37,7 @@ class ChatChannel < ActiveRecord::Base
|
||||
end
|
||||
|
||||
def chatable_types
|
||||
public_channel_chatable_types << "DirectMessageChannel"
|
||||
public_channel_chatable_types << "DirectMessage"
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -28,7 +28,7 @@ class ChatMessage < ActiveRecord::Base
|
||||
}
|
||||
|
||||
scope :in_dm_channel,
|
||||
-> { joins(:chat_channel).where(chat_channel: { chatable_type: "DirectMessageChannel" }) }
|
||||
-> { joins(:chat_channel).where(chat_channel: { chatable_type: "DirectMessage" }) }
|
||||
|
||||
scope :created_before, ->(date) { where("chat_messages.created_at < ?", date) }
|
||||
|
||||
|
@ -14,13 +14,6 @@ module Chatable
|
||||
private
|
||||
|
||||
def channel_class
|
||||
case self
|
||||
when Category
|
||||
CategoryChannel
|
||||
when DirectMessageChannel
|
||||
DMChannel
|
||||
else
|
||||
raise "Unknown chatable #{self}"
|
||||
end
|
||||
"#{self.class}Channel".safe_constantize || raise("Unknown chatable #{self}")
|
||||
end
|
||||
end
|
||||
|
@ -1,22 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# TODO: merge DMChannel and DirectMessageChannel models together
|
||||
class DMChannel < ChatChannel
|
||||
alias_attribute :direct_message_channel, :chatable
|
||||
|
||||
def direct_message_channel?
|
||||
true
|
||||
end
|
||||
|
||||
def allowed_user_ids
|
||||
direct_message_channel.user_ids
|
||||
end
|
||||
|
||||
def read_restricted?
|
||||
true
|
||||
end
|
||||
|
||||
def title(user)
|
||||
direct_message_channel.chat_channel_title_for_user(self, user)
|
||||
end
|
||||
end
|
56
plugins/chat/app/models/direct_message.rb
Normal file
56
plugins/chat/app/models/direct_message.rb
Normal file
@ -0,0 +1,56 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class DirectMessage < ActiveRecord::Base
|
||||
self.table_name = "direct_message_channels"
|
||||
|
||||
include Chatable
|
||||
|
||||
has_many :direct_message_users, foreign_key: :direct_message_channel_id
|
||||
has_many :users, through: :direct_message_users
|
||||
|
||||
def self.for_user_ids(user_ids)
|
||||
joins(:users)
|
||||
.group("direct_message_channels.id")
|
||||
.having("ARRAY[?] = ARRAY_AGG(users.id ORDER BY users.id)", user_ids.sort)
|
||||
&.first
|
||||
end
|
||||
|
||||
def user_can_access?(user)
|
||||
users.include?(user)
|
||||
end
|
||||
|
||||
def chat_channel_title_for_user(chat_channel, acting_user)
|
||||
users =
|
||||
(direct_message_users.map(&:user) - [acting_user]).map { |user| user || DeletedChatUser.new }
|
||||
|
||||
# direct message to self
|
||||
if users.empty?
|
||||
return I18n.t("chat.channel.dm_title.single_user", user: "@#{acting_user.username}")
|
||||
end
|
||||
|
||||
# all users deleted
|
||||
return chat_channel.id if !users.first
|
||||
|
||||
usernames_formatted = users.sort_by(&:username).map { |u| "@#{u.username}" }
|
||||
if usernames_formatted.size > 5
|
||||
return(
|
||||
I18n.t(
|
||||
"chat.channel.dm_title.multi_user_truncated",
|
||||
users: usernames_formatted[0..4].join(", "),
|
||||
leftover: usernames_formatted.length - 5,
|
||||
)
|
||||
)
|
||||
end
|
||||
|
||||
I18n.t("chat.channel.dm_title.multi_user", users: usernames_formatted.join(", "))
|
||||
end
|
||||
end
|
||||
|
||||
# == Schema Information
|
||||
#
|
||||
# Table name: direct_message_channels
|
||||
#
|
||||
# id :bigint not null, primary key
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
#
|
@ -1,54 +1,21 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class DirectMessageChannel < ActiveRecord::Base
|
||||
include Chatable
|
||||
class DirectMessageChannel < ChatChannel
|
||||
alias_attribute :direct_message, :chatable
|
||||
|
||||
has_many :direct_message_users
|
||||
has_many :users, through: :direct_message_users
|
||||
|
||||
def self.for_user_ids(user_ids)
|
||||
joins(:users)
|
||||
.group("direct_message_channels.id")
|
||||
.having("ARRAY[?] = ARRAY_AGG(users.id ORDER BY users.id)", user_ids.sort)
|
||||
&.first
|
||||
def direct_message_channel?
|
||||
true
|
||||
end
|
||||
|
||||
def user_can_access?(user)
|
||||
users.include?(user)
|
||||
def allowed_user_ids
|
||||
direct_message.user_ids
|
||||
end
|
||||
|
||||
def chat_channel_title_for_user(chat_channel, acting_user)
|
||||
users =
|
||||
(direct_message_users.map(&:user) - [acting_user]).map { |user| user || DeletedChatUser.new }
|
||||
def read_restricted?
|
||||
true
|
||||
end
|
||||
|
||||
# direct message to self
|
||||
if users.empty?
|
||||
return I18n.t("chat.channel.dm_title.single_user", user: "@#{acting_user.username}")
|
||||
end
|
||||
|
||||
# all users deleted
|
||||
return chat_channel.id if !users.first
|
||||
|
||||
usernames_formatted = users.sort_by(&:username).map { |u| "@#{u.username}" }
|
||||
if usernames_formatted.size > 5
|
||||
return(
|
||||
I18n.t(
|
||||
"chat.channel.dm_title.multi_user_truncated",
|
||||
users: usernames_formatted[0..4].join(", "),
|
||||
leftover: usernames_formatted.length - 5,
|
||||
)
|
||||
)
|
||||
end
|
||||
|
||||
I18n.t("chat.channel.dm_title.multi_user", users: usernames_formatted.join(", "))
|
||||
def title(user)
|
||||
direct_message.chat_channel_title_for_user(self, user)
|
||||
end
|
||||
end
|
||||
|
||||
# == Schema Information
|
||||
#
|
||||
# Table name: direct_message_channels
|
||||
#
|
||||
# id :bigint not null, primary key
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
#
|
||||
|
@ -1,7 +1,7 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class DirectMessageUser < ActiveRecord::Base
|
||||
belongs_to :direct_message_channel
|
||||
belongs_to :direct_message, foreign_key: :direct_message_channel_id
|
||||
belongs_to :user
|
||||
end
|
||||
|
||||
|
@ -44,8 +44,8 @@ class ChatChannelSerializer < ApplicationSerializer
|
||||
case object.chatable_type
|
||||
when "Category"
|
||||
BasicCategorySerializer.new(object.chatable, root: false).as_json
|
||||
when "DirectMessageChannel"
|
||||
DirectMessageChannelSerializer.new(object.chatable, scope: scope, root: false).as_json
|
||||
when "DirectMessage"
|
||||
DirectMessageSerializer.new(object.chatable, scope: scope, root: false).as_json
|
||||
when "Site"
|
||||
nil
|
||||
end
|
||||
|
@ -1,6 +1,6 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class DirectMessageChannelSerializer < ApplicationSerializer
|
||||
class DirectMessageSerializer < ApplicationSerializer
|
||||
has_many :users, serializer: UserWithCustomFieldsAndStatusSerializer, embed: :objects
|
||||
|
||||
def users
|
Reference in New Issue
Block a user