mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
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:
@@ -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
|
||||
Reference in New Issue
Block a user