2024-02-20 16:49:19 +08: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)
|
|
|
|
#
|
2024-11-04 05:14:35 +08:00
|
|
|
# @raise [RuntimeError] Raises an "Unexpected error" if the message retrieval fails for an unspecified reason.
|
|
|
|
# @raise [RuntimeError] Raises "Guardian can't view channel" if the user's permissions are insufficient to view the channel.
|
|
|
|
# @raise [RuntimeError] Raises "Target message doesn't exist" if the specified target message cannot be found in the channel.
|
DEV: Provide user input to services using `params` key
Currently in services, we don’t make a distinction between input
parameters, options and dependencies.
This can lead to user input modifying the service behavior, whereas it
was not the developer intention.
This patch addresses the issue by changing how data is provided to
services:
- `params` is now used to hold all data coming from outside (typically
user input from a controller) and a contract will take its values from
`params`.
- `options` is a new key to provide options to a service. This typically
allows changing a service behavior at runtime. It is, of course,
totally optional.
- `dependencies` is actually anything else provided to the service (like
`guardian`) and available directly from the context object.
The `service_params` helper in controllers has been updated to reflect
those changes, so most of the existing services didn’t need specific
changes.
The options block has the same DSL as contracts, as it’s also based on
`ActiveModel`. There aren’t any validations, though. Here’s an example:
```ruby
options do
attribute :allow_changing_hidden, :boolean, default: false
end
```
And here’s an example of how to call a service with the new keys:
```ruby
MyService.call(params: { key1: value1, … }, options: { my_option: true }, guardian:, …)
```
2024-10-18 23:45:47 +08:00
|
|
|
def self.messages(...)
|
|
|
|
new.messages(...)
|
2024-02-20 16:49:19 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def messages(channel_id:, guardian:, **params)
|
DEV: Provide user input to services using `params` key
Currently in services, we don’t make a distinction between input
parameters, options and dependencies.
This can lead to user input modifying the service behavior, whereas it
was not the developer intention.
This patch addresses the issue by changing how data is provided to
services:
- `params` is now used to hold all data coming from outside (typically
user input from a controller) and a contract will take its values from
`params`.
- `options` is a new key to provide options to a service. This typically
allows changing a service behavior at runtime. It is, of course,
totally optional.
- `dependencies` is actually anything else provided to the service (like
`guardian`) and available directly from the context object.
The `service_params` helper in controllers has been updated to reflect
those changes, so most of the existing services didn’t need specific
changes.
The options block has the same DSL as contracts, as it’s also based on
`ActiveModel`. There aren’t any validations, though. Here’s an example:
```ruby
options do
attribute :allow_changing_hidden, :boolean, default: false
end
```
And here’s an example of how to call a service with the new keys:
```ruby
MyService.call(params: { key1: value1, … }, options: { my_option: true }, guardian:, …)
```
2024-10-18 23:45:47 +08:00
|
|
|
Chat::ListChannelMessages.call(
|
|
|
|
guardian:,
|
|
|
|
params: {
|
|
|
|
channel_id:,
|
|
|
|
direction: "future",
|
|
|
|
**params,
|
|
|
|
},
|
|
|
|
) 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 21:37:02 +08:00
|
|
|
on_success { |messages:| messages }
|
2024-02-26 21:16:29 +08:00
|
|
|
on_failure { raise "Unexpected error" }
|
2024-02-20 16:49:19 +08: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
|
2024-11-04 05:14:35 +08:00
|
|
|
|
|
|
|
# Initiates a reply in a specified channel or thread.
|
|
|
|
#
|
|
|
|
# @param channel_id [Integer] The ID of the channel where the reply is started.
|
|
|
|
# @param thread_id [Integer, nil] (optional) The ID of the thread within the channel where the reply is started.
|
|
|
|
# @param guardian [Guardian] The guardian object representing the user's permissions.
|
|
|
|
# @return [String] The client ID associated with the initiated reply.
|
|
|
|
#
|
|
|
|
# @example Starting a reply in a channel
|
|
|
|
# ChatSDK::Channel.start_reply(channel_id: 1, guardian: Guardian.new)
|
|
|
|
#
|
|
|
|
# @example Starting a reply in a specific thread
|
|
|
|
# ChatSDK::Channel.start_reply(channel_id: 1, thread_id: 34, guardian: Guardian.new)
|
|
|
|
#
|
|
|
|
# @raise [RuntimeError] Raises an error if the specified channel or thread is not found.
|
|
|
|
def self.start_reply(...)
|
|
|
|
new.start_reply(...)
|
|
|
|
end
|
|
|
|
|
|
|
|
def start_reply(channel_id:, thread_id: nil, guardian:)
|
|
|
|
Chat::StartReply.call(
|
|
|
|
guardian: guardian,
|
|
|
|
params: {
|
|
|
|
channel_id: channel_id,
|
|
|
|
thread_id: thread_id,
|
|
|
|
},
|
|
|
|
) do
|
|
|
|
on_success { |client_id:| client_id }
|
|
|
|
on_model_not_found(:presence_channel) { raise "Chat::Channel or Chat::Thread not found." }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Ends an ongoing reply in a specified channel or thread.
|
|
|
|
#
|
|
|
|
# @param channel_id [Integer] The ID of the channel where the reply is being stopped.
|
|
|
|
# @param thread_id [Integer, nil] (optional) The ID of the thread within the channel where the reply is being stopped.
|
|
|
|
# @param client_id [String] The client ID associated with the reply to stop.
|
|
|
|
# @param guardian [Guardian] The guardian object representing the user's permissions.
|
|
|
|
#
|
|
|
|
# @example Stopping a reply in a channel
|
|
|
|
# ChatSDK::Channel.stop_reply(channel_id: 1, client_id: "abc123", guardian: Guardian.new)
|
|
|
|
#
|
|
|
|
# @example Stopping a reply in a specific thread
|
|
|
|
# ChatSDK::Channel.stop_reply(channel_id: 1, thread_id: 34, client_id: "abc123", guardian: Guardian.new)
|
|
|
|
#
|
|
|
|
# @raise [RuntimeError] Raises an error if the specified channel or thread is not found.
|
|
|
|
def self.stop_reply(...)
|
|
|
|
new.stop_reply(...)
|
|
|
|
end
|
|
|
|
|
|
|
|
def stop_reply(channel_id:, thread_id: nil, client_id:, guardian:)
|
|
|
|
Chat::StopReply.call(
|
|
|
|
guardian: guardian,
|
|
|
|
params: {
|
|
|
|
client_id: client_id,
|
|
|
|
channel_id: channel_id,
|
|
|
|
thread_id: thread_id,
|
|
|
|
},
|
|
|
|
) do
|
|
|
|
on_model_not_found(:presence_channel) { raise "Chat::Channel or Chat::Thread not found." }
|
|
|
|
end
|
|
|
|
end
|
2024-02-20 16:49:19 +08:00
|
|
|
end
|
|
|
|
end
|