mirror of
https://github.com/discourse/discourse.git
synced 2025-05-21 18:12:32 +08:00
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 ```
This commit is contained in:

committed by
Loïc Guitaut

parent
07ff21d045
commit
f79dd5c8b5
@ -2,7 +2,7 @@
|
||||
|
||||
class Chat::Api::ChannelMessagesController < Chat::ApiController
|
||||
def 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::ApiController
|
||||
|
||||
def update
|
||||
Chat::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 API
|
||||
Chat::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)
|
||||
end
|
||||
on_failure { render(json: failed_json, status: 422) }
|
||||
on_failed_policy(:no_silenced_user) { raise Discourse::InvalidAccess }
|
||||
on_model_not_found(:channel) { raise Discourse::NotFound }
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
class Chat::Api::ChannelThreadMessagesController < Chat::ApiController
|
||||
def index
|
||||
::Chat::ListChannelThreadMessages.call(service_params) do
|
||||
::Chat::ListChannelThreadMessages.call(service_params) do |result|
|
||||
on_success do
|
||||
render_serialized(
|
||||
result,
|
||||
|
@ -3,16 +3,16 @@
|
||||
class Chat::Api::ChannelThreadsController < Chat::ApiController
|
||||
def 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::ApiController
|
||||
|
||||
def 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:,
|
||||
)
|
||||
end
|
||||
on_failed_policy(:invalid_access) { raise Discourse::InvalidAccess }
|
||||
@ -58,8 +58,8 @@ class Chat::Api::ChannelThreadsController < Chat::ApiController
|
||||
on_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: 422
|
||||
end
|
||||
on_success { render(json: success_json) }
|
||||
on_failure { render(json: failed_json, status: 422) }
|
||||
@ -71,20 +71,19 @@ class Chat::Api::ChannelThreadsController < Chat::ApiController
|
||||
|
||||
def 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:,
|
||||
)
|
||||
end
|
||||
on_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: 422
|
||||
end
|
||||
on_failure { render(json: failed_json, status: 422) }
|
||||
on_failed_contract do |contract|
|
||||
|
@ -3,17 +3,13 @@
|
||||
class Chat::Api::ChannelThreadsCurrentUserNotificationsSettingsController < Chat::ApiController
|
||||
def update
|
||||
Chat::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)
|
||||
end
|
||||
|
@ -3,17 +3,13 @@
|
||||
class Chat::Api::ChannelThreadsCurrentUserTitlePromptSeenController < Chat::ApiController
|
||||
def update
|
||||
Chat::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)
|
||||
end
|
||||
|
@ -57,24 +57,19 @@ class Chat::Api::ChannelsController < Chat::ApiController
|
||||
Chat::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:)
|
||||
end
|
||||
on_model_not_found(:category) { raise ActiveRecord::RecordNotFound }
|
||||
on_failed_policy(:can_create_channel) { raise Discourse::InvalidAccess }
|
||||
on_failed_policy(:category_channel_does_not_exist) do
|
||||
raise 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)
|
||||
end
|
||||
on_failure { render(json: failed_json, status: 422) }
|
||||
on_failed_contract do |contract|
|
||||
@ -101,12 +96,12 @@ class Chat::Api::ChannelsController < Chat::ApiController
|
||||
end
|
||||
|
||||
Chat::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),
|
||||
)
|
||||
end
|
||||
on_model_not_found(:channel) { raise ActiveRecord::RecordNotFound }
|
||||
|
@ -3,18 +3,14 @@
|
||||
class Chat::Api::ChannelsCurrentUserMembershipFollowsController < Chat::Api::ChannelsController
|
||||
def destroy
|
||||
Chat::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) }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -20,9 +20,7 @@ class Chat::Api::ChannelsReadController < Chat::ApiController
|
||||
|
||||
def update_all
|
||||
Chat::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) }
|
||||
end
|
||||
end
|
||||
|
@ -3,7 +3,7 @@
|
||||
class Chat::Api::ChannelsStatusController < Chat::Api::ChannelsController
|
||||
def update
|
||||
Chat::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) }
|
||||
|
@ -4,7 +4,7 @@ class Chat::Api::ChatablesController < Chat::ApiController
|
||||
before_action :ensure_logged_in
|
||||
|
||||
def 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|
|
||||
|
@ -3,12 +3,12 @@
|
||||
class Chat::Api::CurrentUserChannelsController < Chat::ApiController
|
||||
def index
|
||||
Chat::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,
|
||||
)
|
||||
end
|
||||
on_failure { render(json: failed_json, status: 422) }
|
||||
|
@ -3,26 +3,26 @@
|
||||
class Chat::Api::CurrentUserThreadsController < Chat::ApiController
|
||||
def 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) }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -3,14 +3,7 @@
|
||||
class Chat::Api::DirectMessagesController < Chat::ApiController
|
||||
def create
|
||||
Chat::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)
|
||||
|
@ -9,8 +9,8 @@ module Jobs
|
||||
on_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})")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user