DEV: Migrate Chat::MessageCreator to a service (#22390)

Currently, the logic for creating a new chat message is scattered
between a controller and an “old” service.

This patch address this issue by creating a new service (using the “new”
sevice object system) encapsulating all the necessary logic.
(authorization, publishing events, etc.)
This commit is contained in:
Loïc Guitaut
2023-09-07 08:57:29 +02:00
committed by GitHub
parent 1f0a78fb82
commit 243793ec6e
27 changed files with 1229 additions and 1877 deletions

View File

@@ -0,0 +1,45 @@
# frozen_string_literal: true
class Chat::Channel::MessageCreationPolicy < PolicyBase
class DirectMessageStrategy
class << self
def call(guardian, channel)
guardian.can_create_channel_message?(channel) || guardian.can_create_direct_message?
end
def reason(*)
I18n.t("chat.errors.user_cannot_send_direct_messages")
end
end
end
class CategoryStrategy
class << self
def call(guardian, channel)
guardian.can_create_channel_message?(channel)
end
def reason(_, channel)
I18n.t("chat.errors.channel_new_message_disallowed.#{channel.status}")
end
end
end
attr_reader :strategy
delegate :channel, to: :context
def initialize(*)
super
@strategy = CategoryStrategy
@strategy = DirectMessageStrategy if channel.direct_message_channel?
end
def call
strategy.call(guardian, channel)
end
def reason
strategy.reason(guardian, channel)
end
end