discourse/plugins/chat/app/services/chat/stop_message_streaming.rb
Loïc Guitaut 2f334964f2 DEV: Remove hash-like access from service contracts
We decided to keep only one way to access values from a contract. This
patch thus removes the hash-like access from contracts.
2024-10-29 16:02:51 +01:00

57 lines
1.5 KiB
Ruby

# frozen_string_literal: true
module Chat
# Service responsible for stopping streaming of a message.
#
# @example
# Chat::StopMessageStreaming.call(params: { message_id: 3 }, guardian: guardian)
#
class StopMessageStreaming
include ::Service::Base
# @!method self.call(guardian:, params:)
# @param [Guardian] guardian
# @param [Hash] params
# @option params [Integer] :message_id
# @return [Service::Base::Context]
params do
attribute :message_id, :integer
validates :message_id, presence: true
end
model :message
step :enforce_membership
model :membership
policy :can_stop_streaming
step :stop_message_streaming
step :publish_message_streaming_state
private
def fetch_message(params:)
::Chat::Message.find_by(id: params.message_id)
end
def enforce_membership(guardian:, message:)
message.chat_channel.add(guardian.user) if guardian.user.bot?
end
def fetch_membership(guardian:, message:)
message.chat_channel.membership_for(guardian.user)
end
def can_stop_streaming(guardian:, message:)
guardian.user.bot? || guardian.is_admin? || message.user.id == guardian.user.id ||
message.in_reply_to && message.in_reply_to.user_id == guardian.user.id
end
def stop_message_streaming(message:)
message.update!(streaming: false)
end
def publish_message_streaming_state(guardian:, message:)
::Chat::Publisher.publish_edit!(message.chat_channel, message)
end
end
end