mirror of
https://github.com/discourse/discourse.git
synced 2025-05-30 06:28:11 +08:00
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:, …) ```
This commit is contained in:

committed by
Loïc Guitaut

parent
a89767913d
commit
41584ab40c
@ -11,12 +11,19 @@ module ChatSDK
|
||||
# @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:, guardian:, **params)
|
||||
def self.messages(...)
|
||||
new.messages(...)
|
||||
end
|
||||
|
||||
def messages(channel_id:, guardian:, **params)
|
||||
Chat::ListChannelMessages.call(channel_id:, guardian:, **params, direction: "future") do
|
||||
Chat::ListChannelMessages.call(
|
||||
guardian:,
|
||||
params: {
|
||||
channel_id:,
|
||||
direction: "future",
|
||||
**params,
|
||||
},
|
||||
) do
|
||||
on_success { |messages:| messages }
|
||||
on_failure { raise "Unexpected error" }
|
||||
on_failed_policy(:can_view_channel) { raise "Guardian can't view channel" }
|
||||
|
@ -24,8 +24,8 @@ module ChatSDK
|
||||
# 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)
|
||||
new.create(**params, &block)
|
||||
def self.create(...)
|
||||
new.create(...)
|
||||
end
|
||||
|
||||
# Creates a new message with streaming enabled by default.
|
||||
@ -46,8 +46,8 @@ module ChatSDK
|
||||
# @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)
|
||||
def self.stream(...)
|
||||
new.stream(...)
|
||||
end
|
||||
|
||||
# Starts streaming for a specific chat message.
|
||||
@ -57,8 +57,8 @@ module ChatSDK
|
||||
# @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)
|
||||
def self.start_stream(...)
|
||||
new.start_stream(...)
|
||||
end
|
||||
|
||||
# Stops streaming for a specific chat message.
|
||||
@ -68,8 +68,8 @@ module ChatSDK
|
||||
# @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)
|
||||
def self.stop_stream(...)
|
||||
new.stop_stream(...)
|
||||
end
|
||||
|
||||
def start_stream(message_id:, guardian:)
|
||||
@ -89,7 +89,7 @@ module ChatSDK
|
||||
end
|
||||
|
||||
def stop_stream(message_id:, guardian:)
|
||||
Chat::StopMessageStreaming.call(message_id:, guardian:) do
|
||||
Chat::StopMessageStreaming.call(params: { message_id: }, guardian:) do
|
||||
on_success { |message:| message }
|
||||
on_model_not_found(:message) { raise "Couldn't find message with id: `#{message_id}`" }
|
||||
on_model_not_found(:membership) do
|
||||
@ -121,18 +121,22 @@ module ChatSDK
|
||||
)
|
||||
message =
|
||||
Chat::CreateMessage.call(
|
||||
message: raw,
|
||||
guardian: guardian,
|
||||
chat_channel_id: channel_id,
|
||||
in_reply_to_id: in_reply_to_id,
|
||||
thread_id: thread_id,
|
||||
upload_ids: upload_ids,
|
||||
streaming: streaming,
|
||||
enforce_membership: enforce_membership,
|
||||
force_thread: force_thread,
|
||||
strip_whitespaces: strip_whitespaces,
|
||||
created_by_sdk: true,
|
||||
**params,
|
||||
params: {
|
||||
message: raw,
|
||||
chat_channel_id: channel_id,
|
||||
in_reply_to_id:,
|
||||
thread_id:,
|
||||
upload_ids:,
|
||||
**params,
|
||||
},
|
||||
options: {
|
||||
created_by_sdk: true,
|
||||
streaming:,
|
||||
enforce_membership:,
|
||||
force_thread:,
|
||||
strip_whitespaces:,
|
||||
},
|
||||
guardian:,
|
||||
) do
|
||||
on_model_not_found(:channel) { raise "Couldn't find channel with id: `#{channel_id}`" }
|
||||
on_model_not_found(:membership) do
|
||||
@ -176,11 +180,14 @@ module ChatSDK
|
||||
return false if !message.streaming || !raw
|
||||
|
||||
Chat::UpdateMessage.call(
|
||||
message_id: message.id,
|
||||
message: message.message + raw,
|
||||
guardian: guardian,
|
||||
streaming: true,
|
||||
strip_whitespaces: false,
|
||||
params: {
|
||||
message_id: message.id,
|
||||
message: message.message + raw,
|
||||
},
|
||||
options: {
|
||||
strip_whitespaces: false,
|
||||
},
|
||||
) { on_failure { raise "Unexpected error" } }
|
||||
|
||||
message
|
||||
|
@ -13,7 +13,7 @@ module ChatSDK
|
||||
# ChatSDK::Thread.update_title(title: "New Thread Title", thread_id: 1, guardian: Guardian.new)
|
||||
#
|
||||
def self.update_title(thread_id:, guardian:, title:)
|
||||
new.update(title: title, thread_id: thread_id, guardian: guardian)
|
||||
new.update(thread_id:, guardian:, title:)
|
||||
end
|
||||
|
||||
# Retrieves messages from a specified thread.
|
||||
@ -25,8 +25,8 @@ module ChatSDK
|
||||
# @example Fetching messages from a thread with additional parameters
|
||||
# ChatSDK::Thread.messages(thread_id: 1, guardian: Guardian.new)
|
||||
#
|
||||
def self.messages(thread_id:, guardian:, **params)
|
||||
new.messages(thread_id: thread_id, guardian: guardian, **params)
|
||||
def self.messages(...)
|
||||
new.messages(...)
|
||||
end
|
||||
|
||||
# Fetches the first messages from a specified chat thread, starting from the first available message.
|
||||
@ -41,9 +41,9 @@ module ChatSDK
|
||||
#
|
||||
def self.first_messages(thread_id:, guardian:, page_size: 10)
|
||||
new.messages(
|
||||
thread_id: thread_id,
|
||||
guardian: guardian,
|
||||
page_size: page_size,
|
||||
thread_id:,
|
||||
guardian:,
|
||||
page_size:,
|
||||
direction: "future",
|
||||
fetch_from_first_message: true,
|
||||
)
|
||||
@ -61,20 +61,27 @@ module ChatSDK
|
||||
#
|
||||
def self.last_messages(thread_id:, guardian:, page_size: 10)
|
||||
new.messages(
|
||||
thread_id: thread_id,
|
||||
guardian: guardian,
|
||||
page_size: page_size,
|
||||
thread_id:,
|
||||
guardian:,
|
||||
page_size:,
|
||||
direction: "past",
|
||||
fetch_from_last_message: true,
|
||||
)
|
||||
end
|
||||
|
||||
def self.update(**params)
|
||||
new.update(**params)
|
||||
def self.update(...)
|
||||
new.update(...)
|
||||
end
|
||||
|
||||
def messages(thread_id:, guardian:, direction: "future", **params)
|
||||
Chat::ListChannelThreadMessages.call(thread_id:, guardian:, direction:, **params) do
|
||||
Chat::ListChannelThreadMessages.call(
|
||||
guardian:,
|
||||
params: {
|
||||
thread_id:,
|
||||
direction:,
|
||||
**params,
|
||||
},
|
||||
) do
|
||||
on_success { |messages:| messages }
|
||||
on_failed_policy(:can_view_thread) { raise "Guardian can't view thread" }
|
||||
on_failed_policy(:target_message_exists) { raise "Target message doesn't exist" }
|
||||
@ -82,8 +89,8 @@ module ChatSDK
|
||||
end
|
||||
end
|
||||
|
||||
def update(**params)
|
||||
Chat::UpdateThread.call(params) do
|
||||
def update(guardian:, **params)
|
||||
Chat::UpdateThread.call(guardian:, params:) do
|
||||
on_model_not_found(:channel) do
|
||||
raise "Couldn’t find channel with id: `#{params[:channel_id]}`"
|
||||
end
|
||||
|
Reference in New Issue
Block a user