A platform for community discussion. Free, open, simple.
DEV: Stop injecting a service result object in the caller object
Loïc Guitautf79dd5c8b5d0
diff --git a/app/controllers/admin/config/flags_controller.rb b/app/controllers/admin/config/flags_controller.rbindex 6e46a7e0857..0d83da70da9 100644--- a/app/controllers/admin/config/flags_controller.rb+++ b/app/controllers/admin/config/flags_controller.rb@@ -27,9 +27,9 @@ class Admin::Config::FlagsController < Admin::AdminControllerdef createFlags::CreateFlag.call(service_params) do- on_success do+ on_success do |flag:|Discourse.request_refresh!- render json: result.flag, serializer: FlagSerializer, used_flag_ids: Flag.used_flag_ids+ render json: flag, serializer: FlagSerializer, used_flag_ids: Flag.used_flag_idsendon_failure { render(json: failed_json, status: 422) }on_failed_policy(:invalid_access) { raise Discourse::InvalidAccess }@@ -42,9 +42,9 @@ class Admin::Config::FlagsController < Admin::AdminControllerdef updateFlags::UpdateFlag.call(service_params) do- on_success do+ on_success do |flag:|Discourse.request_refresh!- render json: result.flag, serializer: FlagSerializer, used_flag_ids: Flag.used_flag_ids+ render json: flag, serializer: FlagSerializer, used_flag_ids: Flag.used_flag_idsendon_failure { render(json: failed_json, status: 422) }on_model_not_found(:message) { raise Discourse::NotFound }diff --git a/app/controllers/admin/site_settings_controller.rb b/app/controllers/admin/site_settings_controller.rbindex eb86c9789d2..cd80e41d669 100644--- a/app/controllers/admin/site_settings_controller.rb+++ b/app/controllers/admin/site_settings_controller.rb@@ -40,9 +40,9 @@ class Admin::SiteSettingsController < Admin::AdminControllerprevious_value = value_or_default(SiteSetting.get(id)) if update_existing_usersSiteSetting::Update.call(service_params.merge(setting_name: id, new_value: value)) do- on_success do+ on_success do |contract:|if update_existing_users- SiteSettingUpdateExistingUsers.call(id, result.contract.new_value, previous_value)+ SiteSettingUpdateExistingUsers.call(id, contract.new_value, previous_value)endrender body: nilenddiff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rbindex 8a2f668414d..ca0223dd5b0 100644--- a/app/controllers/admin/users_controller.rb+++ b/app/controllers/admin/users_controller.rb@@ -121,13 +121,13 @@ class Admin::UsersController < Admin::StaffControllerdef suspendUser::Suspend.call(service_params) do- on_success do+ on_success do |contract:, user:, full_reason:|render_json_dump(suspension: {- suspend_reason: result.contract.reason,- full_suspend_reason: result.full_reason,- suspended_till: result.user.suspended_till,- suspended_at: result.user.suspended_at,+ suspend_reason: contract.reason,+ full_suspend_reason: full_reason,+ suspended_till: user.suspended_till,+ suspended_at: user.suspended_at,suspended_by: BasicUserSerializer.new(current_user, root: false).as_json,},)@@ -316,13 +316,13 @@ class Admin::UsersController < Admin::StaffControllerdef silenceUser::Silence.call(service_params) do- on_success do+ on_success do |full_reason:, user:|render_json_dump(silence: {silenced: true,- silence_reason: result.full_reason,- silenced_till: result.user.silenced_till,- silenced_at: result.user.silenced_at,+ silence_reason: full_reason,+ silenced_till: user.silenced_till,+ silenced_at: user.silenced_at,silenced_by: BasicUserSerializer.new(current_user, root: false).as_json,},)diff --git a/app/services/experiments/toggle.rb b/app/services/experiments/toggle.rbindex 3746b82f53a..84b6b709016 100644--- a/app/services/experiments/toggle.rb+++ b/app/services/experiments/toggle.rb@@ -4,14 +4,11 @@ class Experiments::Toggleinclude Service::Basepolicy :current_user_is_admin-contract doattribute :setting_name, :stringvalidates :setting_name, presence: trueend-policy :setting_is_available-transaction { step :toggle }privatediff --git a/lib/service/runner.rb b/lib/service/runner.rbindex 794a3cc4c36..768aa5192e4 100644--- a/lib/service/runner.rb+++ b/lib/service/runner.rb@@ -97,8 +97,6 @@ class Service::Runner# @!visibility privateattr_reader :service, :object, :dependencies- delegate :result, to: :object-# @!visibility privatedef initialize(service, object, dependencies)@service = service@@ -117,8 +115,7 @@ class Service::Runner# @!visibility privatedef call(&block)- instance_eval(&block)- setup_and_run_service+ instance_exec(result, &block)# Always have `on_failure` as the last action(actions@@ -132,12 +129,8 @@ class Service::Runnerattr_reader :actions- def setup_and_run_service- runner = self- object.instance_eval do- def result = @_result- @_result = runner.service.call(runner.dependencies)- end+ def result+ @result ||= service.call(dependencies)enddef failure_for?(key)@@ -151,6 +144,7 @@ class Service::Runner-> doobject.instance_exec(result[[*action[:key], args.first || action[:default_name]].join(".")],+ **result.slice(*block.parameters.filter_map { _1.last if _1.first == :keyreq }),&block)end,diff --git a/plugins/chat/app/controllers/chat/api/channel_messages_controller.rb b/plugins/chat/app/controllers/chat/api/channel_messages_controller.rbindex e706614ec55..d988ff628b7 100644--- a/plugins/chat/app/controllers/chat/api/channel_messages_controller.rb+++ b/plugins/chat/app/controllers/chat/api/channel_messages_controller.rb@@ -2,7 +2,7 @@class Chat::Api::ChannelMessagesController < Chat::ApiControllerdef index- ::Chat::ListChannelMessages.call(service_params) do+ ::Chat::ListChannelMessages.call(service_params) do |result|on_success { render_serialized(result, ::Chat::MessagesSerializer, root: false) }on_failure { render(json: failed_json, status: 422) }on_failed_policy(:can_view_channel) { raise Discourse::InvalidAccess }@@ -52,7 +52,7 @@ class Chat::Api::ChannelMessagesController < Chat::ApiControllerdef updateChat::UpdateMessage.call(service_params) do- on_success { render json: success_json.merge(message_id: result[:message].id) }+ on_success { |message:| render json: success_json.merge(message_id: message.id) }on_failure { render(json: failed_json, status: 422) }on_model_not_found(:message) { raise Discourse::NotFound }on_model_errors(:message) do |model|@@ -69,7 +69,9 @@ class Chat::Api::ChannelMessagesController < Chat::ApiController# users can't force a thread through JSON APIChat::CreateMessage.call(service_params.merge(force_thread: false)) do- on_success { render json: success_json.merge(message_id: result[:message_instance].id) }+ on_success do |message_instance:|+ render json: success_json.merge(message_id: message_instance.id)+ endon_failure { render(json: failed_json, status: 422) }on_failed_policy(:no_silenced_user) { raise Discourse::InvalidAccess }on_model_not_found(:channel) { raise Discourse::NotFound }diff --git a/plugins/chat/app/controllers/chat/api/channel_thread_messages_controller.rb b/plugins/chat/app/controllers/chat/api/channel_thread_messages_controller.rbindex 27fc14ce4b2..d5b4e5a4331 100644--- a/plugins/chat/app/controllers/chat/api/channel_thread_messages_controller.rb+++ b/plugins/chat/app/controllers/chat/api/channel_thread_messages_controller.rb@@ -2,7 +2,7 @@class Chat::Api::ChannelThreadMessagesController < Chat::ApiControllerdef index- ::Chat::ListChannelThreadMessages.call(service_params) do+ ::Chat::ListChannelThreadMessages.call(service_params) do |result|on_success dorender_serialized(result,diff --git a/plugins/chat/app/controllers/chat/api/channel_threads_controller.rb b/plugins/chat/app/controllers/chat/api/channel_threads_controller.rbindex 52d4daf97f7..491b14911da 100644--- a/plugins/chat/app/controllers/chat/api/channel_threads_controller.rb+++ b/plugins/chat/app/controllers/chat/api/channel_threads_controller.rb@@ -3,16 +3,16 @@class Chat::Api::ChannelThreadsController < Chat::ApiControllerdef index::Chat::LookupChannelThreads.call(service_params) do- on_success do+ on_success do |threads:, channel:, tracking:, memberships:, load_more_url:, participants:|render_serialized(::Chat::ThreadsView.new(user: guardian.user,- threads: result.threads,- channel: result.channel,- tracking: result.tracking,- memberships: result.memberships,- load_more_url: result.load_more_url,- threads_participants: result.participants,+ threads_participants: participants,+ threads:,+ channel:,+ tracking:,+ memberships:,+ load_more_url:,),::Chat::ThreadListSerializer,root: false,@@ -31,15 +31,15 @@ class Chat::Api::ChannelThreadsController < Chat::ApiControllerdef show::Chat::LookupThread.call(service_params) do- on_success do+ on_success do |thread:, membership:, participants:|render_serialized(- result.thread,+ thread,::Chat::ThreadSerializer,root: "thread",- membership: result.membership,include_thread_preview: true,include_thread_original_message: true,- participants: result.participants,+ membership:,+ participants:,)endon_failed_policy(:invalid_access) { raise Discourse::InvalidAccess }@@ -58,8 +58,8 @@ class Chat::Api::ChannelThreadsController < Chat::ApiControlleron_failed_policy(:can_view_channel) { raise Discourse::InvalidAccess }on_failed_policy(:can_edit_thread) { raise Discourse::InvalidAccess }on_model_not_found(:thread) { raise Discourse::NotFound }- on_failed_step(:update) do- render json: failed_json.merge(errors: [result["result.step.update"].error]), status: 422+ on_failed_step(:update) do |step|+ render json: failed_json.merge(errors: [step.error]), status: 422endon_success { render(json: success_json) }on_failure { render(json: failed_json, status: 422) }@@ -71,20 +71,19 @@ class Chat::Api::ChannelThreadsController < Chat::ApiControllerdef create::Chat::CreateThread.call(service_params) do- on_success do+ on_success do |thread:, membership:|render_serialized(- result.thread,+ thread,::Chat::ThreadSerializer,root: false,- membership: result.membership,include_thread_original_message: true,+ membership:,)endon_failed_policy(:threading_enabled_for_channel) { raise Discourse::NotFound }on_failed_policy(:can_view_channel) { raise Discourse::InvalidAccess }- on_failed_step(:create_thread) do- render json: failed_json.merge(errors: [result["result.step.create_thread"].error]),- status: 422+ on_failed_step(:create_thread) do |step|+ render json: failed_json.merge(errors: [step.error]), status: 422endon_failure { render(json: failed_json, status: 422) }on_failed_contract do |contract|diff --git a/plugins/chat/app/controllers/chat/api/channel_threads_current_user_notifications_settings_controller.rb b/plugins/chat/app/controllers/chat/api/channel_threads_current_user_notifications_settings_controller.rbindex 7880827d19b..abce511e4e4 100644--- a/plugins/chat/app/controllers/chat/api/channel_threads_current_user_notifications_settings_controller.rb+++ b/plugins/chat/app/controllers/chat/api/channel_threads_current_user_notifications_settings_controller.rb@@ -3,17 +3,13 @@class Chat::Api::ChannelThreadsCurrentUserNotificationsSettingsController < Chat::ApiControllerdef updateChat::UpdateThreadNotificationSettings.call(service_params) do+ on_success do |membership:|+ render_serialized(membership, Chat::BaseThreadMembershipSerializer, root: "membership")+ end+ on_failure { render(json: failed_json, status: 422) }on_failed_policy(:threading_enabled_for_channel) { raise Discourse::NotFound }on_failed_policy(:can_view_channel) { raise Discourse::InvalidAccess }on_model_not_found(:thread) { raise Discourse::NotFound }- on_success do- render_serialized(- result.membership,- Chat::BaseThreadMembershipSerializer,- root: "membership",- )- end- on_failure { render(json: failed_json, status: 422) }on_failed_contract do |contract|render(json: failed_json.merge(errors: contract.errors.full_messages), status: 400)enddiff --git a/plugins/chat/app/controllers/chat/api/channel_threads_current_user_title_prompt_seen_controller.rb b/plugins/chat/app/controllers/chat/api/channel_threads_current_user_title_prompt_seen_controller.rbindex b37fd1a4bc3..6a740e9a5b5 100644--- a/plugins/chat/app/controllers/chat/api/channel_threads_current_user_title_prompt_seen_controller.rb+++ b/plugins/chat/app/controllers/chat/api/channel_threads_current_user_title_prompt_seen_controller.rb@@ -3,17 +3,13 @@class Chat::Api::ChannelThreadsCurrentUserTitlePromptSeenController < Chat::ApiControllerdef updateChat::MarkThreadTitlePromptSeen.call(service_params) do+ on_success do |membership:|+ render_serialized(membership, Chat::BaseThreadMembershipSerializer, root: "membership")+ end+ on_failure { render(json: failed_json, status: 422) }on_failed_policy(:threading_enabled_for_channel) { raise Discourse::NotFound }on_failed_policy(:can_view_channel) { raise Discourse::InvalidAccess }on_model_not_found(:thread) { raise Discourse::NotFound }- on_success do- render_serialized(- result.membership,- Chat::BaseThreadMembershipSerializer,- root: "membership",- )- end- on_failure { render(json: failed_json, status: 422) }on_failed_contract do |contract|render(json: failed_json.merge(errors: contract.errors.full_messages), status: 400)enddiff --git a/plugins/chat/app/controllers/chat/api/channels_controller.rb b/plugins/chat/app/controllers/chat/api/channels_controller.rbindex 69a0058bf1b..cd5f203f769 100644--- a/plugins/chat/app/controllers/chat/api/channels_controller.rb+++ b/plugins/chat/app/controllers/chat/api/channels_controller.rb@@ -57,24 +57,19 @@ class Chat::Api::ChannelsController < Chat::ApiControllerChat::CreateCategoryChannel.call(service_params.merge(channel_params.merge(category_id: channel_params[:chatable_id])),) do- on_success do- render_serialized(- result.channel,- Chat::ChannelSerializer,- root: "channel",- membership: result.membership,- )+ on_success do |channel:, membership:|+ render_serialized(channel, Chat::ChannelSerializer, root: "channel", membership:)endon_model_not_found(:category) { raise ActiveRecord::RecordNotFound }on_failed_policy(:can_create_channel) { raise Discourse::InvalidAccess }on_failed_policy(:category_channel_does_not_exist) doraise Discourse::InvalidParameters.new(I18n.t("chat.errors.channel_exists_for_category"))end- on_model_errors(:channel) do- render_json_error(result.channel, type: :record_invalid, status: 422)+ on_model_errors(:channel) do |model|+ render_json_error(model, type: :record_invalid, status: 422)end- on_model_errors(:membership) do- render_json_error(result.membership, type: :record_invalid, status: 422)+ on_model_errors(:membership) do |model|+ render_json_error(model, type: :record_invalid, status: 422)endon_failure { render(json: failed_json, status: 422) }on_failed_contract do |contract|@@ -101,12 +96,12 @@ class Chat::Api::ChannelsController < Chat::ApiControllerendChat::UpdateChannel.call(service_params.merge(params_to_edit)) do- on_success do+ on_success do |channel:|render_serialized(- result.channel,+ channel,Chat::ChannelSerializer,root: "channel",- membership: result.channel.membership_for(current_user),+ membership: channel.membership_for(current_user),)endon_model_not_found(:channel) { raise ActiveRecord::RecordNotFound }diff --git a/plugins/chat/app/controllers/chat/api/channels_current_user_membership_follows_controller.rb b/plugins/chat/app/controllers/chat/api/channels_current_user_membership_follows_controller.rbindex b1da98b2773..bc17163fd73 100644--- a/plugins/chat/app/controllers/chat/api/channels_current_user_membership_follows_controller.rb+++ b/plugins/chat/app/controllers/chat/api/channels_current_user_membership_follows_controller.rb@@ -3,18 +3,14 @@class Chat::Api::ChannelsCurrentUserMembershipFollowsController < Chat::Api::ChannelsControllerdef destroyChat::UnfollowChannel.call(service_params) do- on_success do- render_serialized(- result.membership,- Chat::UserChannelMembershipSerializer,- root: "membership",- )+ on_success do |membership:|+ render_serialized(membership, Chat::UserChannelMembershipSerializer, root: "membership")end- on_model_not_found(:channel) { raise Discourse::NotFound }- on_failure { render(json: failed_json, status: 422) }on_failed_contract do |contract|render(json: failed_json.merge(errors: contract.errors.full_messages), status: 400)end+ on_model_not_found(:channel) { raise Discourse::NotFound }+ on_failure { render(json: failed_json, status: 422) }endendenddiff --git a/plugins/chat/app/controllers/chat/api/channels_read_controller.rb b/plugins/chat/app/controllers/chat/api/channels_read_controller.rbindex 2549d15a3e8..a02a21a77dc 100644--- a/plugins/chat/app/controllers/chat/api/channels_read_controller.rb+++ b/plugins/chat/app/controllers/chat/api/channels_read_controller.rb@@ -20,9 +20,7 @@ class Chat::Api::ChannelsReadController < Chat::ApiControllerdef update_allChat::MarkAllUserChannelsRead.call(service_params) do- on_success do- render(json: success_json.merge(updated_memberships: result.updated_memberships))- end+ on_success { |updated_memberships:| render(json: success_json.merge(updated_memberships:)) }on_failure { render(json: failed_json, status: 422) }endenddiff --git a/plugins/chat/app/controllers/chat/api/channels_status_controller.rb b/plugins/chat/app/controllers/chat/api/channels_status_controller.rbindex 6f83c3223e8..cd97271c6d0 100644--- a/plugins/chat/app/controllers/chat/api/channels_status_controller.rb+++ b/plugins/chat/app/controllers/chat/api/channels_status_controller.rb@@ -3,7 +3,7 @@class Chat::Api::ChannelsStatusController < Chat::Api::ChannelsControllerdef updateChat::UpdateChannelStatus.call(service_params) do- on_success { render_serialized(result.channel, Chat::ChannelSerializer, root: "channel") }+ on_success { |channel:| render_serialized(channel, Chat::ChannelSerializer, root: "channel") }on_model_not_found(:channel) { raise ActiveRecord::RecordNotFound }on_failed_policy(:check_channel_permission) { raise Discourse::InvalidAccess }on_failure { render(json: failed_json, status: 422) }diff --git a/plugins/chat/app/controllers/chat/api/chatables_controller.rb b/plugins/chat/app/controllers/chat/api/chatables_controller.rbindex e59a0bb729d..166f6b660b9 100644--- a/plugins/chat/app/controllers/chat/api/chatables_controller.rb+++ b/plugins/chat/app/controllers/chat/api/chatables_controller.rb@@ -4,7 +4,7 @@ class Chat::Api::ChatablesController < Chat::ApiControllerbefore_action :ensure_logged_indef index- ::Chat::SearchChatable.call(service_params) do+ ::Chat::SearchChatable.call(service_params) do |result|on_success { render_serialized(result, ::Chat::ChatablesSerializer, root: false) }on_failure { render(json: failed_json, status: 422) }on_failed_contract do |contract|diff --git a/plugins/chat/app/controllers/chat/api/current_user_channels_controller.rb b/plugins/chat/app/controllers/chat/api/current_user_channels_controller.rbindex 2e14301cf74..d87955a5c24 100644--- a/plugins/chat/app/controllers/chat/api/current_user_channels_controller.rb+++ b/plugins/chat/app/controllers/chat/api/current_user_channels_controller.rb@@ -3,12 +3,12 @@class Chat::Api::CurrentUserChannelsController < Chat::ApiControllerdef indexChat::ListUserChannels.call(service_params) do- on_success do+ on_success do |structured:, post_allowed_category_ids:|render_serialized(- result.structured,+ structured,Chat::ChannelIndexSerializer,root: false,- post_allowed_category_ids: result.post_allowed_category_ids,+ post_allowed_category_ids: post_allowed_category_ids,)endon_failure { render(json: failed_json, status: 422) }diff --git a/plugins/chat/app/controllers/chat/api/current_user_threads_controller.rb b/plugins/chat/app/controllers/chat/api/current_user_threads_controller.rbindex d781b526718..5fdaf983455 100644--- a/plugins/chat/app/controllers/chat/api/current_user_threads_controller.rb+++ b/plugins/chat/app/controllers/chat/api/current_user_threads_controller.rb@@ -3,26 +3,26 @@class Chat::Api::CurrentUserThreadsController < Chat::ApiControllerdef index::Chat::LookupUserThreads.call(service_params) do- on_success do+ on_success do |threads:, tracking:, memberships:, load_more_url:, participants:|render_serialized(::Chat::ThreadsView.new(user: guardian.user,- threads: result.threads,- channel: result.channel,- tracking: result.tracking,- memberships: result.memberships,- load_more_url: result.load_more_url,- threads_participants: result.participants,+ threads_participants: participants,+ channel: nil,+ threads:,+ tracking:,+ memberships:,+ load_more_url:,),::Chat::ThreadListSerializer,root: false,)end- on_model_not_found(:threads) { render json: success_json.merge(threads: []) }- on_failure { render(json: failed_json, status: 422) }on_failed_contract do |contract|render(json: failed_json.merge(errors: contract.errors.full_messages), status: 400)end+ on_model_not_found(:threads) { render json: success_json.merge(threads: []) }+ on_failure { render(json: failed_json, status: 422) }endendenddiff --git a/plugins/chat/app/controllers/chat/api/direct_messages_controller.rb b/plugins/chat/app/controllers/chat/api/direct_messages_controller.rbindex 63c98f973d6..66b756f1be8 100644--- a/plugins/chat/app/controllers/chat/api/direct_messages_controller.rb+++ b/plugins/chat/app/controllers/chat/api/direct_messages_controller.rb@@ -3,14 +3,7 @@class Chat::Api::DirectMessagesController < Chat::ApiControllerdef createChat::CreateDirectMessageChannel.call(service_params) do- on_success do- render_serialized(- result.channel,- Chat::ChannelSerializer,- root: "channel",- membership: result.membership,- )- end+ on_success { |channel:| render_serialized(channel, Chat::ChannelSerializer, root: "channel") }on_model_not_found(:target_users) { raise ActiveRecord::RecordNotFound }on_failed_policy(:satisfies_dms_max_users_limit) do |policy|render_json_dump({ error: policy.reason }, status: 400)diff --git a/plugins/chat/app/jobs/regular/chat/auto_join_channel_batch.rb b/plugins/chat/app/jobs/regular/chat/auto_join_channel_batch.rbindex fb3f7e2166a..c7c5d6014d0 100644--- a/plugins/chat/app/jobs/regular/chat/auto_join_channel_batch.rb+++ b/plugins/chat/app/jobs/regular/chat/auto_join_channel_batch.rb@@ -9,8 +9,8 @@ module Jobson_failed_contract do |contract|Rails.logger.error(contract.errors.full_messages.join(", "))end- on_model_not_found(:channel) do- Rails.logger.error("Channel not found (id=#{result.contract.channel_id})")+ on_model_not_found(:channel) do |contract:|+ Rails.logger.error("Channel not found (id=#{contract.channel_id})")endendenddiff --git a/plugins/chat/lib/chat_sdk/channel.rb b/plugins/chat/lib/chat_sdk/channel.rbindex 1bf85714a1f..5a0b914b3b2 100644--- a/plugins/chat/lib/chat_sdk/channel.rb+++ b/plugins/chat/lib/chat_sdk/channel.rb@@ -17,7 +17,7 @@ module ChatSDKdef messages(channel_id:, guardian:, **params)Chat::ListChannelMessages.call(channel_id:, guardian:, **params, direction: "future") do- on_success { result.messages }+ on_success { |messages:| messages }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" }diff --git a/plugins/chat/lib/chat_sdk/message.rb b/plugins/chat/lib/chat_sdk/message.rbindex 64079713239..952b9fe13ae 100644--- a/plugins/chat/lib/chat_sdk/message.rb+++ b/plugins/chat/lib/chat_sdk/message.rb@@ -90,7 +90,7 @@ module ChatSDKdef stop_stream(message_id:, guardian:)Chat::StopMessageStreaming.call(message_id:, guardian:) do- on_success { result.message }+ on_success { |message:| message }on_model_not_found(:message) { raise "Couldn't find message with id: `#{message_id}`" }on_model_not_found(:membership) doraise "Couldn't find membership for user with id: `#{guardian.user.id}`"@@ -145,7 +145,7 @@ module ChatSDKraise "User with id: `#{guardian.user.id}` can't join this channel"endon_failed_contract { |contract| raise contract.errors.full_messages.join(", ") }- on_success { result.message_instance }+ on_success { |message_instance:| message_instance }on_failure { raise "Unexpected error" }enddiff --git a/plugins/chat/lib/chat_sdk/thread.rb b/plugins/chat/lib/chat_sdk/thread.rbindex a8bfedd1cbf..9e038728a07 100644--- a/plugins/chat/lib/chat_sdk/thread.rb+++ b/plugins/chat/lib/chat_sdk/thread.rb@@ -75,7 +75,7 @@ module ChatSDKdef messages(thread_id:, guardian:, direction: "future", **params)Chat::ListChannelThreadMessages.call(thread_id:, guardian:, direction:, **params) do- on_success { result.messages }+ 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" }on_failure { raise "Unexpected error" }@@ -96,7 +96,7 @@ module ChatSDKraise "Threading is not enabled for this channel"endon_failed_contract { |contract| raise contract.errors.full_messages.join(", ") }- on_success { result.thread_instance }+ on_success { |thread:| thread }on_failure { raise "Unexpected error" }endenddiff --git a/plugins/chat/spec/plugin_helper.rb b/plugins/chat/spec/plugin_helper.rbindex 9c29fd94aec..a15918f73d7 100644--- a/plugins/chat/spec/plugin_helper.rb+++ b/plugins/chat/spec/plugin_helper.rb@@ -67,49 +67,49 @@ module ChatSpecHelpersenddef update_message!(message, text: nil, user: Discourse.system_user, upload_ids: nil)- result =- Chat::UpdateMessage.call(- guardian: user.guardian,- message_id: message.id,- upload_ids: upload_ids,- message: text,- process_inline: true,- )- service_failed!(result) if result.failure?- result.message_instance+ Chat::UpdateMessage.call(+ guardian: user.guardian,+ message_id: message.id,+ upload_ids: upload_ids,+ message: text,+ process_inline: true,+ ) do |result|+ on_success { result.message_instance }+ on_failure { service_failed!(result) }+ endenddef trash_message!(message, user: Discourse.system_user)- result =- Chat::TrashMessage.call(- message_id: message.id,- channel_id: message.chat_channel_id,- guardian: user.guardian,- )- service_failed!(result) if result.failure?- result+ Chat::TrashMessage.call(+ message_id: message.id,+ channel_id: message.chat_channel_id,+ guardian: user.guardian,+ ) do |result|+ on_success { result }+ on_failure { service_failed!(result) }+ endenddef restore_message!(message, user: Discourse.system_user)- result =- Chat::RestoreMessage.call(- message_id: message.id,- channel_id: message.chat_channel_id,- guardian: user.guardian,- )- service_failed!(result) if result.failure?- result+ Chat::RestoreMessage.call(+ message_id: message.id,+ channel_id: message.chat_channel_id,+ guardian: user.guardian,+ ) do |result|+ on_success { result }+ on_failure { service_failed!(result) }+ endenddef add_users_to_channel(users, channel, user: Discourse.system_user)- result =- ::Chat::AddUsersToChannel.call(- guardian: user.guardian,- channel_id: channel.id,- usernames: Array(users).map(&:username),- )- service_failed!(result) if result.failure?- result+ ::Chat::AddUsersToChannel.call(+ guardian: user.guardian,+ channel_id: channel.id,+ usernames: Array(users).map(&:username),+ ) do |result|+ on_success { result }+ on_failure { service_failed!(result) }+ endenddef create_draft(channel, thread: nil, user: Discourse.system_user, data: { message: "draft" })@@ -119,15 +119,15 @@ module ChatSpecHelpersendend- result =- ::Chat::UpsertDraft.call(- guardian: user.guardian,- channel_id: channel.id,- thread_id: thread&.id,- data: data.to_json,- )- service_failed!(result) if result.failure?- result+ ::Chat::UpsertDraft.call(+ guardian: user.guardian,+ channel_id: channel.id,+ thread_id: thread&.id,+ data: data.to_json,+ ) do |result|+ on_success { result }+ on_failure { service_failed!(result) }+ endendenddiff --git a/plugins/chat/spec/requests/chat/api/current_user_threads_spec.rb b/plugins/chat/spec/requests/chat/api/current_user_threads_spec.rbindex 0dc0230879e..02a1f5867df 100644--- a/plugins/chat/spec/requests/chat/api/current_user_threads_spec.rb+++ b/plugins/chat/spec/requests/chat/api/current_user_threads_spec.rb@@ -11,10 +11,20 @@ describe Chat::Api::CurrentUserThreadsController dodescribe "#index" dodescribe "success" do+ let!(:thread) do+ Fabricate(+ :chat_thread,+ original_message_user: current_user,+ with_replies: 2,+ use_service: true,+ )+ end+it "works" doget "/chat/api/me/threads"- expect(response.status).to eq(200)+ expect(response).to have_http_status :ok+ expect(response.parsed_body[:threads]).not_to be_emptyendenddiff --git a/spec/lib/service/runner_spec.rb b/spec/lib/service/runner_spec.rbindex b9d98c66b03..7128b82b11f 100644--- a/spec/lib/service/runner_spec.rb+++ b/spec/lib/service/runner_spec.rb@@ -150,10 +150,13 @@ RSpec.describe Service::Runner dodescribe ".call" dosubject(:runner) { described_class.call(service, &actions_block) }- let(:result) { object.result }let(:actions_block) { object.instance_eval(actions) }- let(:service) { SuccessService }- let(:actions) { "proc {}" }+ let(:service) { SuccessWithModelService }+ let(:actions) { <<-BLOCK }+ proc do |result|+ on_success { |fake_model:| [result, fake_model] }+ end+ BLOCKlet(:object) doClass.new(ApplicationController) do@@ -171,10 +174,12 @@ RSpec.describe Service::Runner do.newend- it "runs the provided service in the context of a controller" do- runner- expect(result).to be_a Service::Base::Context- expect(result).to be_a_success+ it "allows access to the result object" do+ expect(runner.first).to be_a Service::Base::Context+ end++ it "allows using keyword args in blocks" do+ expect(runner.last).to eq :model_foundendcontext "when using the on_success action" do@@ -241,7 +246,7 @@ RSpec.describe Service::Runner docontext "when using the block argument" dolet(:actions) { <<-BLOCK }- proc do+ proc do |result|on_failed_policy(:test) { |policy| policy == result["result.policy.test"] }endBLOCK@@ -279,7 +284,7 @@ RSpec.describe Service::Runner docontext "when using the block argument" dolet(:actions) { <<-BLOCK }- proc do+ proc do |result|on_failed_contract { |contract| contract == result["result.contract.default"] }endBLOCK@@ -301,8 +306,9 @@ RSpec.describe Service::Runner docontext "when using the on_model_not_found action" dolet(:actions) { <<-BLOCK }- proc do- on_model_not_found(:fake_model) { :no_model }+ proc do |result|+ on_success { [result] }+ on_model_not_found(:fake_model) { [:no_model, result] }endBLOCK@@ -311,7 +317,7 @@ RSpec.describe Service::Runner dolet(:service) { FailureWithOptionalModelService }it "does not run the provided block" do- expect(runner).not_to eq :no_model+ expect(runner).not_to include :no_modelendend@@ -320,13 +326,13 @@ RSpec.describe Service::Runner docontext "when not using the block argument" doit "runs the provided block" do- expect(runner).to eq :no_model+ expect(runner).to include :no_modelendendcontext "when using the block argument" dolet(:actions) { <<-BLOCK }- proc do+ proc do |result|on_model_not_found(:fake_model) { |model| model == result["result.model.fake_model"] }endBLOCK@@ -341,7 +347,7 @@ RSpec.describe Service::Runner dolet(:service) { SuccessWithModelService }it "does not run the provided block" do- expect(runner).not_to eq :no_model+ expect(runner).not_to include :no_modelendendend@@ -351,7 +357,7 @@ RSpec.describe Service::Runner dolet(:service) { FailureWithCollectionModelService }it "runs the provided block" do- expect(runner).to eq :no_model+ expect(runner).to include :no_modelendend@@ -359,7 +365,7 @@ RSpec.describe Service::Runner dolet(:service) { SuccessWithCollectionModelService }it "does not run the provided block" do- expect(runner).not_to eq :no_model+ expect(runner).not_to include :no_modelendendend@@ -371,23 +377,21 @@ RSpec.describe Service::Runner dobefore { Fabricate(:user) }it "does not run the provided block" do- expect(runner).not_to eq :no_model+ expect(runner).not_to include :no_modelendit "does not fetch records from the relation" do- runner- expect(result[:fake_model]).not_to be_loaded+ expect(runner.last[:fake_model]).not_to be_loadedendendcontext "when the service fails" doit "runs the provided block" do- expect(runner).to eq :no_model+ expect(runner).to include :no_modelendit "does not fetch records from the relation" do- runner- expect(result[:fake_model]).not_to be_loaded+ expect(runner.last[:fake_model]).not_to be_loadedendendend