discourse/plugins/chat/lib/chat_sdk/channel.rb
Loïc Guitaut e94707acdf DEV: Drop WithServiceHelper
This patch removes the `with_service` helper from the code base.
Instead, we can pass a block with actions directly to the `.call` method
of a service.

This simplifies how to use services:
- use `.call` without a block to run the service and get its result
  object.
- use `.call` with a block of actions to run the service and execute
  arbitrary code depending on the service outcome.

It also means a service is now “self-contained” and can be used anywhere
without having to include a helper or whatever.
2024-09-05 09:58:20 +02:00

28 lines
1.1 KiB
Ruby

# frozen_string_literal: true
module ChatSDK
class Channel
# Retrieves messages from a specified channel.
#
# @param channel_id [Integer] The ID of the chat channel from which to fetch messages.
# @param guardian [Guardian] The guardian object representing the user's permissions.
# @return [Array<ChMessage>] An array of message objects from the specified channel.
#
# @example Fetching messages from a channel with additional parameters
# ChatSDK::Channel.messages(channel_id: 1, guardian: Guardian.new)
#
def self.messages(channel_id:, guardian:, **params)
new.messages(channel_id:, guardian:, **params)
end
def messages(channel_id:, guardian:, **params)
Chat::ListChannelMessages.call(channel_id:, guardian:, **params, direction: "future") do
on_success { result.messages }
on_failure { raise "Unexpected error" }
on_failed_policy(:can_view_channel) { raise "Guardian can't view channel" }
on_failed_policy(:target_message_exists) { raise "Target message doesn't exist" }
end
end
end
end