2024-02-20 16:49:19 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module ChatSDK
|
|
|
|
class Message
|
|
|
|
# Creates a new message in a chat channel.
|
|
|
|
#
|
|
|
|
# @param raw [String] The content of the message.
|
|
|
|
# @param channel_id [Integer] The ID of the chat channel.
|
|
|
|
# @param guardian [Guardian] The user's guardian object, for policy enforcement.
|
|
|
|
# @param in_reply_to_id [Integer, nil] The ID of the message this is in reply to (optional).
|
|
|
|
# @param thread_id [Integer, nil] The ID of the thread this message belongs to (optional).
|
|
|
|
# @param upload_ids [Array<Integer>, nil] The IDs of any uploads associated with the message (optional).
|
|
|
|
# @param streaming [Boolean] Whether the message is part of a streaming operation (default: false).
|
|
|
|
# @param enforce_membership [Boolean] Allows to ensure the guardian will be allowed in the channel (default: false).
|
|
|
|
# @yield [helper, message] Offers a block with a helper and the message for streaming operations.
|
|
|
|
# @yieldparam helper [Helper] The helper object for streaming operations.
|
|
|
|
# @yieldparam message [Message] The newly created message object.
|
2024-02-26 21:16:29 +08:00
|
|
|
# @return [Chat::Message] The created message object.
|
2024-02-20 16:49:19 +08:00
|
|
|
#
|
|
|
|
# @example Creating a simple message
|
|
|
|
# ChatSDK::Message.create(raw: "Hello, world!", channel_id: 1, guardian: Guardian.new)
|
|
|
|
#
|
|
|
|
# @example Creating a message with a block for streaming
|
2024-02-26 21:16:29 +08:00
|
|
|
# ChatSDK::Message.create_with_stream(raw: "Streaming message", channel_id: 1, guardian: Guardian.new) do |helper, message|
|
2024-02-20 16:49:19 +08:00
|
|
|
# helper.stream(raw: "Continuation of the message")
|
|
|
|
# end
|
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.create(...)
|
|
|
|
new.create(...)
|
2024-02-20 16:49:19 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# Creates a new message with streaming enabled by default.
|
|
|
|
#
|
|
|
|
# This method is a convenience wrapper around `create` with `streaming: true` set by default.
|
|
|
|
# It supports all the same parameters and block usage as `create`.
|
|
|
|
#
|
|
|
|
# @see #create
|
|
|
|
def self.create_with_stream(**params, &block)
|
2024-05-02 17:59:18 +08:00
|
|
|
self.create(**params, streaming: true, strip_whitespaces: false, &block)
|
2024-02-20 16:49:19 +08:00
|
|
|
end
|
|
|
|
|
2024-02-26 21:16:29 +08:00
|
|
|
# Streams to a specific chat message.
|
|
|
|
#
|
|
|
|
# @param raw [String] text to append to the existing message.
|
|
|
|
# @param message_id [Integer] the ID of the message to stream.
|
|
|
|
# @param guardian [Guardian] an instance of the guardian class, representing the user's permissions.
|
|
|
|
# @return [Chat::Message] The message object.
|
|
|
|
# @example Streaming a message
|
|
|
|
# ChatSDK::Message.stream(message_id: 42, guardian: guardian, raw: "text")
|
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.stream(...)
|
|
|
|
new.stream(...)
|
2024-02-26 21:16:29 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# Starts streaming for a specific chat message.
|
|
|
|
#
|
|
|
|
# @param message_id [Integer] the ID of the message for which streaming should be stopped.
|
|
|
|
# @param guardian [Guardian] an instance of the guardian class, representing the user's permissions.
|
|
|
|
# @return [Chat::Message] The message object.
|
|
|
|
# @example Starting the streaming of a message
|
|
|
|
# ChatSDK::Message.start_stream(message_id: 42, guardian: guardian)
|
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.start_stream(...)
|
|
|
|
new.start_stream(...)
|
2024-02-26 21:16:29 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# Stops streaming for a specific chat message.
|
|
|
|
#
|
|
|
|
# @param message_id [Integer] the ID of the message for which streaming should be stopped.
|
|
|
|
# @param guardian [Guardian] an instance of the guardian class, representing the user's permissions.
|
|
|
|
# @return [Chat::Message] The message object.
|
|
|
|
# @example Stopping the streaming of a message
|
|
|
|
# ChatSDK::Message.stop_stream(message_id: 42, guardian: guardian)
|
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.stop_stream(...)
|
|
|
|
new.stop_stream(...)
|
2024-02-26 21:16:29 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def start_stream(message_id:, guardian:)
|
|
|
|
message = Chat::Message.find(message_id)
|
|
|
|
guardian.ensure_can_edit_chat!(message)
|
|
|
|
message.update!(streaming: true)
|
|
|
|
::Chat::Publisher.publish_edit!(message.chat_channel, message.reload)
|
|
|
|
message
|
|
|
|
end
|
|
|
|
|
|
|
|
def stream(message_id:, raw:, guardian:, &block)
|
|
|
|
message = Chat::Message.find(message_id)
|
|
|
|
helper = StreamHelper.new(message, guardian)
|
|
|
|
helper.stream(raw: raw)
|
|
|
|
::Chat::Publisher.publish_edit!(message.chat_channel, message.reload)
|
|
|
|
message
|
|
|
|
end
|
|
|
|
|
|
|
|
def stop_stream(message_id:, guardian:)
|
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::StopMessageStreaming.call(params: { message_id: }, guardian:) 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 { |message:| message }
|
2024-02-26 21:16:29 +08:00
|
|
|
on_model_not_found(:message) { raise "Couldn't find message with id: `#{message_id}`" }
|
2024-05-07 21:17:42 +08:00
|
|
|
on_model_not_found(:membership) do
|
|
|
|
raise "Couldn't find membership for user with id: `#{guardian.user.id}`"
|
|
|
|
end
|
2024-02-26 21:16:29 +08:00
|
|
|
on_failed_policy(:can_join_channel) do
|
|
|
|
raise "User with id: `#{guardian.user.id}` can't join this channel"
|
|
|
|
end
|
|
|
|
on_failed_policy(:can_stop_streaming) do
|
|
|
|
raise "User with id: `#{guardian.user.id}` can't stop streaming this message"
|
|
|
|
end
|
|
|
|
on_failure { raise "Unexpected error" }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-02-20 16:49:19 +08:00
|
|
|
def create(
|
|
|
|
raw:,
|
|
|
|
channel_id:,
|
|
|
|
guardian:,
|
|
|
|
in_reply_to_id: nil,
|
|
|
|
thread_id: nil,
|
|
|
|
upload_ids: nil,
|
|
|
|
streaming: false,
|
|
|
|
enforce_membership: false,
|
2024-03-06 19:03:42 +08:00
|
|
|
force_thread: false,
|
2024-05-02 17:59:18 +08:00
|
|
|
strip_whitespaces: true,
|
2024-11-20 15:06:48 +08:00
|
|
|
blocks: nil,
|
2024-10-16 18:22:55 +08:00
|
|
|
**params,
|
2024-02-20 16:49:19 +08:00
|
|
|
&block
|
|
|
|
)
|
|
|
|
message =
|
2024-09-04 00:30:22 +08:00
|
|
|
Chat::CreateMessage.call(
|
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
|
|
|
params: {
|
|
|
|
message: raw,
|
|
|
|
chat_channel_id: channel_id,
|
|
|
|
in_reply_to_id:,
|
|
|
|
thread_id:,
|
|
|
|
upload_ids:,
|
2024-11-20 15:06:48 +08:00
|
|
|
blocks:,
|
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
|
|
|
**params,
|
|
|
|
},
|
|
|
|
options: {
|
|
|
|
created_by_sdk: true,
|
|
|
|
streaming:,
|
|
|
|
enforce_membership:,
|
|
|
|
force_thread:,
|
|
|
|
strip_whitespaces:,
|
|
|
|
},
|
|
|
|
guardian:,
|
2024-02-20 16:49:19 +08:00
|
|
|
) do
|
|
|
|
on_model_not_found(:channel) { raise "Couldn't find channel with id: `#{channel_id}`" }
|
2024-05-07 21:17:42 +08:00
|
|
|
on_model_not_found(:membership) do
|
|
|
|
raise "Couldn't find membership for user with id: `#{guardian.user.id}`"
|
2024-02-20 16:49:19 +08:00
|
|
|
end
|
|
|
|
on_failed_policy(:ensure_valid_thread_for_channel) do
|
|
|
|
raise "Couldn't find thread with id: `#{thread_id}`"
|
|
|
|
end
|
2024-11-20 15:06:48 +08:00
|
|
|
on_failed_policy(:accept_blocks) { raise "Only bots can create messages with blocks" }
|
2024-02-20 16:49:19 +08:00
|
|
|
on_failed_policy(:allowed_to_join_channel) do
|
|
|
|
raise "User with id: `#{guardian.user.id}` can't join this channel"
|
|
|
|
end
|
|
|
|
on_failed_contract { |contract| raise contract.errors.full_messages.join(", ") }
|
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 { |message_instance:| message_instance }
|
2024-02-26 21:16:29 +08:00
|
|
|
on_failure { raise "Unexpected error" }
|
2024-02-20 16:49:19 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
if streaming && block_given?
|
2024-02-26 21:16:29 +08:00
|
|
|
helper = StreamHelper.new(message, guardian)
|
2024-02-20 16:49:19 +08:00
|
|
|
block.call(helper, message)
|
|
|
|
end
|
|
|
|
|
|
|
|
message
|
|
|
|
ensure
|
|
|
|
if message && streaming
|
|
|
|
message.update!(streaming: false)
|
|
|
|
::Chat::Publisher.publish_edit!(message.chat_channel, message.reload)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-02-26 21:16:29 +08:00
|
|
|
class StreamHelper
|
2024-02-20 16:49:19 +08:00
|
|
|
attr_reader :message
|
2024-02-26 21:16:29 +08:00
|
|
|
attr_reader :guardian
|
2024-02-20 16:49:19 +08:00
|
|
|
|
2024-02-26 21:16:29 +08:00
|
|
|
def initialize(message, guardian)
|
|
|
|
@message = message.reload
|
|
|
|
@guardian = guardian
|
2024-02-20 16:49:19 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def stream(raw: nil)
|
2024-09-04 00:30:22 +08:00
|
|
|
return false if !message.streaming || !raw
|
|
|
|
|
|
|
|
Chat::UpdateMessage.call(
|
|
|
|
guardian: guardian,
|
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
|
|
|
params: {
|
|
|
|
message_id: message.id,
|
|
|
|
message: message.message + raw,
|
|
|
|
},
|
|
|
|
options: {
|
|
|
|
strip_whitespaces: false,
|
|
|
|
},
|
2024-02-26 21:16:29 +08:00
|
|
|
) { on_failure { raise "Unexpected error" } }
|
2024-02-20 16:49:19 +08:00
|
|
|
|
2024-09-04 00:30:22 +08:00
|
|
|
message
|
2024-02-20 16:49:19 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|