mirror of
https://github.com/discourse/discourse.git
synced 2024-11-23 22:26:26 +08:00
abcaa1a961
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.
56 lines
1.6 KiB
Ruby
56 lines
1.6 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
class Chat::DirectMessagesController < Chat::ChatBaseController
|
|
# NOTE: For V1 of chat channel archiving and deleting we are not doing
|
|
# anything for DM channels, their behaviour will stay as is.
|
|
def create
|
|
guardian.ensure_can_chat!(current_user)
|
|
users = users_from_usernames(current_user, params)
|
|
|
|
begin
|
|
chat_channel =
|
|
Chat::DirectMessageChannelCreator.create!(acting_user: current_user, target_users: users)
|
|
render_serialized(
|
|
chat_channel,
|
|
ChatChannelSerializer,
|
|
root: "chat_channel",
|
|
membership: chat_channel.membership_for(current_user),
|
|
)
|
|
rescue Chat::DirectMessageChannelCreator::NotAllowed => err
|
|
render_json_error(err.message)
|
|
end
|
|
end
|
|
|
|
def index
|
|
guardian.ensure_can_chat!(current_user)
|
|
users = users_from_usernames(current_user, params)
|
|
|
|
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,
|
|
root: "chat_channel",
|
|
membership: chat_channel.membership_for(current_user),
|
|
)
|
|
else
|
|
render body: nil, status: 404
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def users_from_usernames(current_user, params)
|
|
params.require(:usernames)
|
|
|
|
usernames =
|
|
(params[:usernames].is_a?(String) ? params[:usernames].split(",") : params[:usernames])
|
|
|
|
users = [current_user]
|
|
other_usernames = usernames - [current_user.username]
|
|
users.concat(User.where(username: other_usernames).to_a) if other_usernames.any?
|
|
users
|
|
end
|
|
end
|