2024-02-20 03:49:19 -05:00
|
|
|
# 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)
|
2024-09-03 12:30:22 -04:00
|
|
|
new.messages(channel_id:, guardian:, **params)
|
2024-02-20 03:49:19 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def messages(channel_id:, guardian:, **params)
|
2024-09-03 12:30:22 -04:00
|
|
|
Chat::ListChannelMessages.call(channel_id:, guardian:, **params, direction: "future") do
|
DEV: Stop injecting a service result object in the caller object
Currently, when calling a service with its block form, a `#result`
method is automatically created on the caller object. Even if it never
clashed so far, this could happen.
This patch removes that method, and instead use a more classical way of
doing things: the result object is now provided as an argument to the
main block. This means if we need to access the result object in an
outcome block, it will be done like this from now on:
```ruby
MyService.call(params) do |result|
on_success do
# do something with the result object
do_something(result)
end
end
```
In the same vein, this patch introduces the ability to match keys from
the result object in the outcome blocks, like we already do with step
definitions in a service. For example:
```ruby
on_success do |model:, contract:|
do_something(model, contract)
end
```
Instead of
```ruby
on_success do
do_something(result.model, result.contract)
end
```
2024-10-21 09:37:02 -04:00
|
|
|
on_success { |messages:| messages }
|
2024-02-26 08:16:29 -05:00
|
|
|
on_failure { raise "Unexpected error" }
|
2024-02-20 03:49:19 -05: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
|