DEV: Make params explicit for services in controllers

This commit is contained in:
Loïc Guitaut 2024-10-02 16:07:57 +02:00
parent fc1c5f6a8d
commit 92911a1ac7
No known key found for this signature in database
28 changed files with 52 additions and 50 deletions

View File

@ -2,7 +2,7 @@
class Admin::AdminNoticesController < Admin::StaffController class Admin::AdminNoticesController < Admin::StaffController
def destroy def destroy
AdminNotices::Dismiss.call do AdminNotices::Dismiss.call(service_params) do
on_success { render(json: success_json) } on_success { render(json: success_json) }
on_failure { render(json: failed_json, status: 422) } on_failure { render(json: failed_json, status: 422) }
end end

View File

@ -2,7 +2,7 @@
class Admin::Config::FlagsController < Admin::AdminController class Admin::Config::FlagsController < Admin::AdminController
def toggle def toggle
Flags::ToggleFlag.call do Flags::ToggleFlag.call(service_params) do
on_success do on_success do
Discourse.request_refresh! Discourse.request_refresh!
render(json: success_json) render(json: success_json)
@ -26,7 +26,7 @@ class Admin::Config::FlagsController < Admin::AdminController
end end
def create def create
Flags::CreateFlag.call do Flags::CreateFlag.call(service_params) do
on_success do on_success do
Discourse.request_refresh! Discourse.request_refresh!
render json: result.flag, serializer: FlagSerializer, used_flag_ids: Flag.used_flag_ids render json: result.flag, serializer: FlagSerializer, used_flag_ids: Flag.used_flag_ids
@ -41,7 +41,7 @@ class Admin::Config::FlagsController < Admin::AdminController
end end
def update def update
Flags::UpdateFlag.call do Flags::UpdateFlag.call(service_params) do
on_success do on_success do
Discourse.request_refresh! Discourse.request_refresh!
render json: result.flag, serializer: FlagSerializer, used_flag_ids: Flag.used_flag_ids render json: result.flag, serializer: FlagSerializer, used_flag_ids: Flag.used_flag_ids
@ -59,7 +59,7 @@ class Admin::Config::FlagsController < Admin::AdminController
end end
def reorder def reorder
Flags::ReorderFlag.call do Flags::ReorderFlag.call(service_params) do
on_success do on_success do
Discourse.request_refresh! Discourse.request_refresh!
render(json: success_json) render(json: success_json)
@ -75,7 +75,7 @@ class Admin::Config::FlagsController < Admin::AdminController
end end
def destroy def destroy
Flags::DestroyFlag.call do Flags::DestroyFlag.call(service_params) do
on_success do on_success do
Discourse.request_refresh! Discourse.request_refresh!
render(json: success_json) render(json: success_json)

View File

@ -39,7 +39,7 @@ class Admin::SiteSettingsController < Admin::AdminController
previous_value = value_or_default(SiteSetting.get(id)) if update_existing_users previous_value = value_or_default(SiteSetting.get(id)) if update_existing_users
UpdateSiteSetting.call(setting_name: id, new_value: value) do UpdateSiteSetting.call(service_params.merge(setting_name: id, new_value: value)) do
on_success do on_success do
if update_existing_users if update_existing_users
SiteSettingUpdateExistingUsers.call(id, result.new_value, previous_value) SiteSettingUpdateExistingUsers.call(id, result.new_value, previous_value)

View File

@ -120,7 +120,7 @@ class Admin::UsersController < Admin::StaffController
end end
def suspend def suspend
User::Suspend.call do User::Suspend.call(service_params) do
on_success do on_success do
render_json_dump( render_json_dump(
suspension: { suspension: {
@ -315,7 +315,7 @@ class Admin::UsersController < Admin::StaffController
end end
def silence def silence
User::Silence.call do User::Silence.call(service_params) do
on_success do on_success do
render_json_dump( render_json_dump(
silence: { silence: {

View File

@ -1162,4 +1162,8 @@ class ApplicationController < ActionController::Base
def clean_xml def clean_xml
response.body.gsub!(XmlCleaner::INVALID_CHARACTERS, "") response.body.gsub!(XmlCleaner::INVALID_CHARACTERS, "")
end end
def service_params
params.to_unsafe_h.merge(guardian:)
end
end end

View File

@ -134,13 +134,9 @@ class Service::Runner
def setup_and_run_service def setup_and_run_service
runner = self runner = self
params = object.try(:params) || ActionController::Parameters.new
object.instance_eval do object.instance_eval do
def result = @_result def result = @_result
@_result = @_result = runner.service.call(runner.dependencies)
runner.service.call(
params.to_unsafe_h.merge(guardian: try(:guardian), **runner.dependencies),
)
end end
end end

View File

@ -2,7 +2,7 @@
class Chat::Api::ChannelMessagesController < Chat::ApiController class Chat::Api::ChannelMessagesController < Chat::ApiController
def index def index
::Chat::ListChannelMessages.call do ::Chat::ListChannelMessages.call(service_params) do
on_success { render_serialized(result, ::Chat::MessagesSerializer, root: false) } on_success { render_serialized(result, ::Chat::MessagesSerializer, root: false) }
on_failure { render(json: failed_json, status: 422) } on_failure { render(json: failed_json, status: 422) }
on_failed_policy(:can_view_channel) { raise Discourse::InvalidAccess } on_failed_policy(:can_view_channel) { raise Discourse::InvalidAccess }
@ -15,7 +15,7 @@ class Chat::Api::ChannelMessagesController < Chat::ApiController
end end
def destroy def destroy
Chat::TrashMessage.call do Chat::TrashMessage.call(service_params) do
on_success { render(json: success_json) } on_success { render(json: success_json) }
on_failure { render(json: failed_json, status: 422) } on_failure { render(json: failed_json, status: 422) }
on_model_not_found(:message) { raise Discourse::NotFound } on_model_not_found(:message) { raise Discourse::NotFound }
@ -27,7 +27,7 @@ class Chat::Api::ChannelMessagesController < Chat::ApiController
end end
def bulk_destroy def bulk_destroy
Chat::TrashMessages.call do Chat::TrashMessages.call(service_params) do
on_success { render(json: success_json) } on_success { render(json: success_json) }
on_failure { render(json: failed_json, status: 422) } on_failure { render(json: failed_json, status: 422) }
on_model_not_found(:messages) { raise Discourse::NotFound } on_model_not_found(:messages) { raise Discourse::NotFound }
@ -39,7 +39,7 @@ class Chat::Api::ChannelMessagesController < Chat::ApiController
end end
def restore def restore
Chat::RestoreMessage.call do Chat::RestoreMessage.call(service_params) do
on_success { render(json: success_json) } on_success { render(json: success_json) }
on_failure { render(json: failed_json, status: 422) } on_failure { render(json: failed_json, status: 422) }
on_failed_policy(:invalid_access) { raise Discourse::InvalidAccess } on_failed_policy(:invalid_access) { raise Discourse::InvalidAccess }
@ -51,7 +51,7 @@ class Chat::Api::ChannelMessagesController < Chat::ApiController
end end
def update def update
Chat::UpdateMessage.call do Chat::UpdateMessage.call(service_params) do
on_success { render json: success_json.merge(message_id: result[:message].id) } on_success { render json: success_json.merge(message_id: result[:message].id) }
on_failure { render(json: failed_json, status: 422) } on_failure { render(json: failed_json, status: 422) }
on_model_not_found(:message) { raise Discourse::NotFound } on_model_not_found(:message) { raise Discourse::NotFound }
@ -68,7 +68,7 @@ class Chat::Api::ChannelMessagesController < Chat::ApiController
Chat::MessageRateLimiter.run!(current_user) Chat::MessageRateLimiter.run!(current_user)
# users can't force a thread through JSON API # users can't force a thread through JSON API
Chat::CreateMessage.call(force_thread: false) do 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 { render json: success_json.merge(message_id: result[:message_instance].id) }
on_failure { render(json: failed_json, status: 422) } on_failure { render(json: failed_json, status: 422) }
on_failed_policy(:no_silenced_user) { raise Discourse::InvalidAccess } on_failed_policy(:no_silenced_user) { raise Discourse::InvalidAccess }

View File

@ -2,7 +2,7 @@
class Chat::Api::ChannelThreadMessagesController < Chat::ApiController class Chat::Api::ChannelThreadMessagesController < Chat::ApiController
def index def index
::Chat::ListChannelThreadMessages.call do ::Chat::ListChannelThreadMessages.call(service_params) do
on_success do on_success do
render_serialized( render_serialized(
result, result,

View File

@ -2,7 +2,7 @@
class Chat::Api::ChannelThreadsController < Chat::ApiController class Chat::Api::ChannelThreadsController < Chat::ApiController
def index def index
::Chat::LookupChannelThreads.call do ::Chat::LookupChannelThreads.call(service_params) do
on_success do on_success do
render_serialized( render_serialized(
::Chat::ThreadsView.new( ::Chat::ThreadsView.new(
@ -30,7 +30,7 @@ class Chat::Api::ChannelThreadsController < Chat::ApiController
end end
def show def show
::Chat::LookupThread.call do ::Chat::LookupThread.call(service_params) do
on_success do on_success do
render_serialized( render_serialized(
result.thread, result.thread,
@ -53,7 +53,7 @@ class Chat::Api::ChannelThreadsController < Chat::ApiController
end end
def update def update
::Chat::UpdateThread.call do ::Chat::UpdateThread.call(service_params) do
on_failed_policy(:threading_enabled_for_channel) { raise Discourse::NotFound } on_failed_policy(:threading_enabled_for_channel) { raise Discourse::NotFound }
on_failed_policy(:can_view_channel) { raise Discourse::InvalidAccess } on_failed_policy(:can_view_channel) { raise Discourse::InvalidAccess }
on_failed_policy(:can_edit_thread) { raise Discourse::InvalidAccess } on_failed_policy(:can_edit_thread) { raise Discourse::InvalidAccess }
@ -70,7 +70,7 @@ class Chat::Api::ChannelThreadsController < Chat::ApiController
end end
def create def create
::Chat::CreateThread.call do ::Chat::CreateThread.call(service_params) do
on_success do on_success do
render_serialized( render_serialized(
result.thread, result.thread,

View File

@ -2,7 +2,7 @@
class Chat::Api::ChannelThreadsCurrentUserNotificationsSettingsController < Chat::ApiController class Chat::Api::ChannelThreadsCurrentUserNotificationsSettingsController < Chat::ApiController
def update def update
Chat::UpdateThreadNotificationSettings.call do Chat::UpdateThreadNotificationSettings.call(service_params) do
on_failed_policy(:threading_enabled_for_channel) { raise Discourse::NotFound } on_failed_policy(:threading_enabled_for_channel) { raise Discourse::NotFound }
on_failed_policy(:can_view_channel) { raise Discourse::InvalidAccess } on_failed_policy(:can_view_channel) { raise Discourse::InvalidAccess }
on_model_not_found(:thread) { raise Discourse::NotFound } on_model_not_found(:thread) { raise Discourse::NotFound }

View File

@ -2,7 +2,7 @@
class Chat::Api::ChannelThreadsCurrentUserTitlePromptSeenController < Chat::ApiController class Chat::Api::ChannelThreadsCurrentUserTitlePromptSeenController < Chat::ApiController
def update def update
Chat::MarkThreadTitlePromptSeen.call do Chat::MarkThreadTitlePromptSeen.call(service_params) do
on_failed_policy(:threading_enabled_for_channel) { raise Discourse::NotFound } on_failed_policy(:threading_enabled_for_channel) { raise Discourse::NotFound }
on_failed_policy(:can_view_channel) { raise Discourse::InvalidAccess } on_failed_policy(:can_view_channel) { raise Discourse::InvalidAccess }
on_model_not_found(:thread) { raise Discourse::NotFound } on_model_not_found(:thread) { raise Discourse::NotFound }

View File

@ -33,7 +33,7 @@ class Chat::Api::ChannelsController < Chat::ApiController
end end
def destroy def destroy
Chat::TrashChannel.call do Chat::TrashChannel.call(service_params) do
on_failed_policy(:invalid_access) { raise Discourse::InvalidAccess } on_failed_policy(:invalid_access) { raise Discourse::InvalidAccess }
on_model_not_found(:channel) { raise ActiveRecord::RecordNotFound } on_model_not_found(:channel) { raise ActiveRecord::RecordNotFound }
on_success { render(json: success_json) } on_success { render(json: success_json) }
@ -59,7 +59,7 @@ class Chat::Api::ChannelsController < Chat::ApiController
# at the moment. This may change in future, at which point we will need to pass in # at the moment. This may change in future, at which point we will need to pass in
# a chatable_type param as well and switch to the correct service here. # a chatable_type param as well and switch to the correct service here.
Chat::CreateCategoryChannel.call( Chat::CreateCategoryChannel.call(
channel_params.merge(category_id: channel_params[:chatable_id]), service_params.merge(channel_params.merge(category_id: channel_params[:chatable_id])),
) do ) do
on_success do on_success do
render_serialized( render_serialized(
@ -104,7 +104,7 @@ class Chat::Api::ChannelsController < Chat::ApiController
auto_join_limiter(channel_from_params).performed! auto_join_limiter(channel_from_params).performed!
end end
Chat::UpdateChannel.call(params_to_edit) do Chat::UpdateChannel.call(service_params.merge(params_to_edit)) do
on_success do on_success do
render_serialized( render_serialized(
result.channel, result.channel,

View File

@ -12,7 +12,7 @@ class Chat::Api::ChannelsCurrentUserMembershipController < Chat::Api::ChannelsCo
end end
def destroy def destroy
Chat::LeaveChannel.call do Chat::LeaveChannel.call(service_params) do
on_success { render(json: success_json) } on_success { render(json: success_json) }
on_failure { render(json: failed_json, status: 422) } on_failure { render(json: failed_json, status: 422) }
on_model_not_found(:channel) { raise Discourse::NotFound } on_model_not_found(:channel) { raise Discourse::NotFound }

View File

@ -2,7 +2,7 @@
class Chat::Api::ChannelsCurrentUserMembershipFollowsController < Chat::Api::ChannelsController class Chat::Api::ChannelsCurrentUserMembershipFollowsController < Chat::Api::ChannelsController
def destroy def destroy
Chat::UnfollowChannel.call do Chat::UnfollowChannel.call(service_params) do
on_success do on_success do
render_serialized( render_serialized(
result.membership, result.membership,

View File

@ -2,7 +2,7 @@
class Chat::Api::ChannelsDraftsController < Chat::ApiController class Chat::Api::ChannelsDraftsController < Chat::ApiController
def create def create
Chat::UpsertDraft.call do Chat::UpsertDraft.call(service_params) do
on_success { render(json: success_json) } on_success { render(json: success_json) }
on_failure { render(json: failed_json, status: 422) } on_failure { render(json: failed_json, status: 422) }
on_model_not_found(:channel) { raise Discourse::NotFound } on_model_not_found(:channel) { raise Discourse::NotFound }

View File

@ -2,7 +2,7 @@
class Chat::Api::ChannelsInvitesController < Chat::ApiController class Chat::Api::ChannelsInvitesController < Chat::ApiController
def create def create
Chat::InviteUsersToChannel.call do Chat::InviteUsersToChannel.call(service_params) do
on_success { render(json: success_json) } on_success { render(json: success_json) }
on_failure { render(json: failed_json, status: 422) } on_failure { render(json: failed_json, status: 422) }
on_failed_policy(:can_view_channel) { raise Discourse::InvalidAccess } on_failed_policy(:can_view_channel) { raise Discourse::InvalidAccess }

View File

@ -30,7 +30,7 @@ class Chat::Api::ChannelsMembershipsController < Chat::Api::ChannelsController
end end
def create def create
Chat::AddUsersToChannel.call do Chat::AddUsersToChannel.call(service_params) do
on_success { render(json: success_json) } on_success { render(json: success_json) }
on_failure { render(json: failed_json, status: 422) } on_failure { render(json: failed_json, status: 422) }
on_failed_policy(:can_add_users_to_channel) do on_failed_policy(:can_add_users_to_channel) do

View File

@ -4,7 +4,7 @@ class Chat::Api::ChannelsMessagesFlagsController < Chat::ApiController
def create def create
RateLimiter.new(current_user, "flag_chat_message", 4, 1.minutes).performed! RateLimiter.new(current_user, "flag_chat_message", 4, 1.minutes).performed!
Chat::FlagMessage.call do Chat::FlagMessage.call(service_params) do
on_success { render(json: success_json) } on_success { render(json: success_json) }
on_failure { render(json: failed_json, status: 422) } on_failure { render(json: failed_json, status: 422) }
on_model_not_found(:message) { raise Discourse::NotFound } on_model_not_found(:message) { raise Discourse::NotFound }

View File

@ -2,7 +2,7 @@
class Chat::Api::ChannelsMessagesStreamingController < Chat::Api::ChannelsController class Chat::Api::ChannelsMessagesStreamingController < Chat::Api::ChannelsController
def destroy def destroy
Chat::StopMessageStreaming.call do Chat::StopMessageStreaming.call(service_params) do
on_success { render(json: success_json) } on_success { render(json: success_json) }
on_failure { render(json: failed_json, status: 422) } on_failure { render(json: failed_json, status: 422) }
on_model_not_found(:message) { raise Discourse::NotFound } on_model_not_found(:message) { raise Discourse::NotFound }

View File

@ -2,7 +2,7 @@
class Chat::Api::ChannelsReadController < Chat::ApiController class Chat::Api::ChannelsReadController < Chat::ApiController
def update def update
Chat::UpdateUserChannelLastRead.call do Chat::UpdateUserChannelLastRead.call(service_params) do
on_success { render(json: success_json) } on_success { render(json: success_json) }
on_failure { render(json: failed_json, status: 422) } on_failure { render(json: failed_json, status: 422) }
on_failed_policy(:ensure_message_id_recency) do on_failed_policy(:ensure_message_id_recency) do
@ -19,7 +19,7 @@ class Chat::Api::ChannelsReadController < Chat::ApiController
end end
def update_all def update_all
Chat::MarkAllUserChannelsRead.call do Chat::MarkAllUserChannelsRead.call(service_params) do
on_success do on_success do
render(json: success_json.merge(updated_memberships: result.updated_memberships)) render(json: success_json.merge(updated_memberships: result.updated_memberships))
end end

View File

@ -2,7 +2,7 @@
class Chat::Api::ChannelsStatusController < Chat::Api::ChannelsController class Chat::Api::ChannelsStatusController < Chat::Api::ChannelsController
def update def update
Chat::UpdateChannelStatus.call do Chat::UpdateChannelStatus.call(service_params) do
on_success { render_serialized(result.channel, Chat::ChannelSerializer, root: "channel") } on_success { render_serialized(result.channel, Chat::ChannelSerializer, root: "channel") }
on_model_not_found(:channel) { raise ActiveRecord::RecordNotFound } on_model_not_found(:channel) { raise ActiveRecord::RecordNotFound }
on_failed_policy(:check_channel_permission) { raise Discourse::InvalidAccess } on_failed_policy(:check_channel_permission) { raise Discourse::InvalidAccess }

View File

@ -2,7 +2,7 @@
class Chat::Api::ChannelsThreadsDraftsController < Chat::ApiController class Chat::Api::ChannelsThreadsDraftsController < Chat::ApiController
def create def create
Chat::UpsertDraft.call do Chat::UpsertDraft.call(service_params) do
on_success { render(json: success_json) } on_success { render(json: success_json) }
on_failure { render(json: failed_json, status: 422) } on_failure { render(json: failed_json, status: 422) }
on_model_not_found(:channel) { raise Discourse::NotFound } on_model_not_found(:channel) { raise Discourse::NotFound }

View File

@ -2,7 +2,7 @@
class Chat::Api::ChannelsThreadsReadController < Chat::ApiController class Chat::Api::ChannelsThreadsReadController < Chat::ApiController
def update def update
Chat::UpdateUserThreadLastRead.call do Chat::UpdateUserThreadLastRead.call(service_params) do
on_success { render(json: success_json) } on_success { render(json: success_json) }
on_failure { render(json: failed_json, status: 422) } on_failure { render(json: failed_json, status: 422) }
on_model_not_found(:thread) { raise Discourse::NotFound } on_model_not_found(:thread) { raise Discourse::NotFound }

View File

@ -4,7 +4,7 @@ class Chat::Api::ChatablesController < Chat::ApiController
before_action :ensure_logged_in before_action :ensure_logged_in
def index def index
::Chat::SearchChatable.call do ::Chat::SearchChatable.call(service_params) do
on_success { render_serialized(result, ::Chat::ChatablesSerializer, root: false) } on_success { render_serialized(result, ::Chat::ChatablesSerializer, root: false) }
on_failure { render(json: failed_json, status: 422) } on_failure { render(json: failed_json, status: 422) }
on_failed_contract do |contract| on_failed_contract do |contract|

View File

@ -2,7 +2,7 @@
class Chat::Api::CurrentUserChannelsController < Chat::ApiController class Chat::Api::CurrentUserChannelsController < Chat::ApiController
def index def index
Chat::ListUserChannels.call do Chat::ListUserChannels.call(service_params) do
on_success do on_success do
render_serialized( render_serialized(
result.structured, result.structured,

View File

@ -2,7 +2,7 @@
class Chat::Api::CurrentUserThreadsController < Chat::ApiController class Chat::Api::CurrentUserThreadsController < Chat::ApiController
def index def index
::Chat::LookupUserThreads.call do ::Chat::LookupUserThreads.call(service_params) do
on_success do on_success do
render_serialized( render_serialized(
::Chat::ThreadsView.new( ::Chat::ThreadsView.new(

View File

@ -2,7 +2,7 @@
class Chat::Api::DirectMessagesController < Chat::ApiController class Chat::Api::DirectMessagesController < Chat::ApiController
def create def create
Chat::CreateDirectMessageChannel.call do Chat::CreateDirectMessageChannel.call(service_params) do
on_success do on_success do
render_serialized( render_serialized(
result.channel, result.channel,

View File

@ -56,10 +56,12 @@ module Chat
webhook.chat_channel.add(Discourse.system_user) webhook.chat_channel.add(Discourse.system_user)
Chat::CreateMessage.call( Chat::CreateMessage.call(
service_params.merge(
chat_channel_id: webhook.chat_channel_id, chat_channel_id: webhook.chat_channel_id,
guardian: Discourse.system_user.guardian, guardian: Discourse.system_user.guardian,
message: text, message: text,
incoming_chat_webhook: webhook, incoming_chat_webhook: webhook,
),
) do ) do
on_success { render json: success_json } on_success { render json: success_json }
on_failure { render(json: failed_json, status: 422) } on_failure { render(json: failed_json, status: 422) }