2024-02-20 02:49:19 -06:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module ChatSDK
|
|
|
|
class Channel
|
2024-04-04 08:57:41 -05:00
|
|
|
include WithServiceHelper
|
2024-02-20 02:49:19 -06:00
|
|
|
|
|
|
|
# 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: channel_id, guardian: guardian, **params)
|
|
|
|
end
|
|
|
|
|
|
|
|
def messages(channel_id:, guardian:, **params)
|
|
|
|
with_service(
|
|
|
|
Chat::ListChannelMessages,
|
|
|
|
channel_id: channel_id,
|
|
|
|
guardian: guardian,
|
|
|
|
**params,
|
|
|
|
direction: "future",
|
|
|
|
) do
|
|
|
|
on_success { result.messages }
|
2024-02-26 07:16:29 -06:00
|
|
|
on_failure { raise "Unexpected error" }
|
2024-02-20 02:49:19 -06:00
|
|
|
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
|