DEV: allows stop/resume streaming on a message (#25774)

```ruby
ChatSDK::Message.start_stream(message_id: 1, guardian: guardian)
ChatSDK::Message.stream(raw: "foo", message_id: 1, guardian: guardian)
ChatSDK::Message.stream(raw: "bar", message_id: 1, guardian: guardian)
ChatSDK::Message.stop_stream(message_id: 1, guardian: guardian)
```

Generally speaking only admins or owners of the message can interact with a message.  Also note, Streaming to an existing message with a different user won't change the initial user of the message.
This commit is contained in:
Joffrey JAFFEUX
2024-02-26 14:16:29 +01:00
committed by GitHub
parent 794ef67268
commit 41790f7739
8 changed files with 195 additions and 36 deletions

View File

@ -26,10 +26,7 @@ module ChatSDK
direction: "future",
) do
on_success { result.messages }
on_failure do
p Chat::StepsInspector.new(result)
raise "Unexpected error"
end
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

View File

@ -17,13 +17,13 @@ module ChatSDK
# @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.
# @return [ChMessage] The created message object.
# @return [Chat::Message] The created message object.
#
# @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
# Message.create_with_stream(raw: "Streaming message", channel_id: 1, guardian: Guardian.new) do |helper, message|
# ChatSDK::Message.create_with_stream(raw: "Streaming message", channel_id: 1, guardian: Guardian.new) do |helper, message|
# helper.stream(raw: "Continuation of the message")
# end
def self.create(**params, &block)
@ -40,6 +40,70 @@ module ChatSDK
self.create(**params, streaming: true, &block)
end
# 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")
def self.stream(raw:, message_id:, guardian:, &block)
new.stream(raw: raw, message_id: message_id, guardian: guardian, &block)
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)
def self.start_stream(message_id:, guardian:)
new.start_stream(message_id: message_id, guardian: guardian)
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)
def self.stop_stream(message_id:, guardian:)
new.stop_stream(message_id: message_id, guardian: guardian)
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:)
with_service(Chat::StopMessageStreaming, message_id: message_id, guardian: guardian) do
on_success { result.message }
on_model_not_found(:message) { raise "Couldn't find message with id: `#{message_id}`" }
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
def create(
raw:,
channel_id:,
@ -75,14 +139,11 @@ module ChatSDK
end
on_failed_contract { |contract| raise contract.errors.full_messages.join(", ") }
on_success { result.message_instance }
on_failure do
p Chat::StepsInspector.new(result)
raise "Unexpected error"
end
on_failure { raise "Unexpected error" }
end
if streaming && block_given?
helper = Helper.new(message)
helper = StreamHelper.new(message, guardian)
block.call(helper, message)
end
@ -95,30 +156,28 @@ module ChatSDK
end
end
class Helper
class StreamHelper
include Chat::WithServiceHelper
attr_reader :message
attr_reader :guardian
def initialize(message)
@message = message
def initialize(message, guardian)
@message = message.reload
@guardian = guardian
end
def stream(raw: nil)
return false unless self.message.reload.streaming
return false if !self.message.streaming
return false if !raw
with_service(
Chat::UpdateMessage,
message_id: self.message.id,
message: raw ? self.message.reload.message + " " + raw : self.message.message,
guardian: self.message.user.guardian,
message: self.message.message + raw,
guardian: self.guardian,
streaming: true,
) do
on_failure do
p Chat::StepsInspector.new(result)
raise "Unexpected error"
end
end
) { on_failure { raise "Unexpected error" } }
self.message
end

View File

@ -48,10 +48,7 @@ module ChatSDK
on_failed_policy(:ensure_thread_enabled) do
raise "Threading is not enabled for this channel"
end
on_failure do
p Chat::StepsInspector.new(result)
raise "Unexpected error"
end
on_failure { raise "Unexpected error" }
end
end
@ -70,10 +67,7 @@ module ChatSDK
end
on_failed_contract { |contract| raise contract.errors.full_messages.join(", ") }
on_success { result.thread_instance }
on_failure do
p Chat::StepsInspector.new(result)
raise "Unexpected error"
end
on_failure { raise "Unexpected error" }
end
end
end