A platform for community discussion. Free, open, simple.
DEV: Remove hash-like access from service contracts
Loïc Guitaut2f334964f29a
diff --git a/app/services/admin_notices/dismiss.rb b/app/services/admin_notices/dismiss.rbindex b4543f973ea..defcf52ba22 100644--- a/app/services/admin_notices/dismiss.rb+++ b/app/services/admin_notices/dismiss.rb@@ -4,6 +4,11 @@ class AdminNotices::Dismissinclude Service::Basepolicy :invalid_access+ params do+ attribute :id, :integer++ validates :id, presence: true+ endmodel :admin_notice, optional: truetransaction dostep :destroy@@ -17,7 +22,7 @@ class AdminNotices::Dismissenddef fetch_admin_notice(params:)- AdminNotice.find_by(id: params[:id])+ AdminNotice.find_by(id: params.id)enddef destroy(admin_notice:)diff --git a/app/services/experiments/toggle.rb b/app/services/experiments/toggle.rbindex bd2e38a372d..9c63cc1c883 100644--- a/app/services/experiments/toggle.rb+++ b/app/services/experiments/toggle.rb@@ -18,13 +18,13 @@ class Experiments::Toggleenddef setting_is_available(params:)- SiteSetting.respond_to?(params[:setting_name])+ SiteSetting.respond_to?(params.setting_name)enddef toggle(params:, guardian:)SiteSetting.set_and_log(- params[:setting_name],- !SiteSetting.public_send(params[:setting_name]),+ params.setting_name,+ !SiteSetting.public_send(params.setting_name),guardian.user,)enddiff --git a/app/services/flags/create_flag.rb b/app/services/flags/create_flag.rbindex f1f8075df23..98d3b50934e 100644--- a/app/services/flags/create_flag.rb+++ b/app/services/flags/create_flag.rb@@ -32,7 +32,7 @@ class Flags::CreateFlagenddef unique_name(params:)- !Flag.custom.where(name: params[:name]).exists?+ !Flag.custom.where(name: params.name).exists?enddef instantiate_flag(params:)diff --git a/app/services/flags/destroy_flag.rb b/app/services/flags/destroy_flag.rbindex 2ee02d75a17..abaf3dd0155 100644--- a/app/services/flags/destroy_flag.rb+++ b/app/services/flags/destroy_flag.rb@@ -3,6 +3,11 @@class Flags::DestroyFlaginclude Service::Base+ params do+ attribute :id, :integer++ validates :id, presence: true+ endmodel :flagpolicy :not_systempolicy :not_used@@ -15,7 +20,7 @@ class Flags::DestroyFlagprivatedef fetch_flag(params:)- Flag.find_by(id: params[:id])+ Flag.find_by(id: params.id)enddef not_system(flag:)diff --git a/app/services/flags/reorder_flag.rb b/app/services/flags/reorder_flag.rbindex df7384a44b5..c680d8ae55e 100644--- a/app/services/flags/reorder_flag.rb+++ b/app/services/flags/reorder_flag.rb@@ -22,7 +22,7 @@ class Flags::ReorderFlagprivatedef fetch_flag(params:)- Flag.find_by(id: params[:flag_id])+ Flag.find_by(id: params.flag_id)enddef invalid_access(guardian:, flag:)@@ -34,15 +34,15 @@ class Flags::ReorderFlagenddef invalid_move(flag:, params:, all_flags:)- return false if all_flags.first == flag && params[:direction] == "up"- return false if all_flags.last == flag && params[:direction] == "down"+ return false if all_flags.first == flag && params.direction == "up"+ return false if all_flags.last == flag && params.direction == "down"trueenddef move(flag:, params:, all_flags:)old_position = flag.positionindex = all_flags.index(flag)- target_flag = all_flags[params[:direction] == "up" ? index - 1 : index + 1]+ target_flag = all_flags[params.direction == "up" ? index - 1 : index + 1]flag.update!(position: target_flag.position)target_flag.update!(position: old_position)@@ -51,7 +51,7 @@ class Flags::ReorderFlagdef log(guardian:, flag:, params:)StaffActionLogger.new(guardian.user).log_custom("move_flag",- { flag: flag.name, direction: params[:direction] },+ { flag: flag.name, direction: params.direction },)endenddiff --git a/app/services/flags/toggle_flag.rb b/app/services/flags/toggle_flag.rbindex a8f8cb7211d..3b15f257717 100644--- a/app/services/flags/toggle_flag.rb+++ b/app/services/flags/toggle_flag.rb@@ -22,7 +22,7 @@ class Flags::ToggleFlagenddef fetch_flag(params:)- Flag.find_by(id: params[:flag_id])+ Flag.find_by(id: params.flag_id)enddef toggle(flag:)diff --git a/app/services/flags/update_flag.rb b/app/services/flags/update_flag.rbindex 49d457ec4b4..ccd682b65a9 100644--- a/app/services/flags/update_flag.rb+++ b/app/services/flags/update_flag.rb@@ -32,7 +32,7 @@ class Flags::UpdateFlagprivatedef fetch_flag(params:)- Flag.find_by(id: params[:id])+ Flag.find_by(id: params.id)enddef not_system(flag:)@@ -48,7 +48,7 @@ class Flags::UpdateFlagenddef unique_name(params:)- !Flag.custom.where(name: params[:name]).where.not(id: params[:id]).exists?+ !Flag.custom.where(name: params.name).where.not(id: params.id).exists?enddef update(flag:, params:)diff --git a/app/services/site_setting/update.rb b/app/services/site_setting/update.rbindex 95511e2a48a..89c834d9687 100644--- a/app/services/site_setting/update.rb+++ b/app/services/site_setting/update.rb@@ -45,16 +45,16 @@ class SiteSetting::Updateenddef setting_is_visible(params:, options:)- options.allow_changing_hidden || !SiteSetting.hidden_settings.include?(params[:setting_name])+ options.allow_changing_hidden || !SiteSetting.hidden_settings.include?(params.setting_name)enddef setting_is_configurable(params:)- return true if !SiteSetting.plugins[params[:setting_name]]+ return true if !SiteSetting.plugins[params.setting_name]- Discourse.plugins_by_name[SiteSetting.plugins[params[:setting_name]]].configurable?+ Discourse.plugins_by_name[SiteSetting.plugins[params.setting_name]].configurable?enddef save(params:, guardian:)- SiteSetting.set_and_log(params[:setting_name], params[:new_value], guardian.user)+ SiteSetting.set_and_log(params.setting_name, params.new_value, guardian.user)endenddiff --git a/app/services/user/silence.rb b/app/services/user/silence.rbindex ecf1fc153d3..28bbc6d0ade 100644--- a/app/services/user/silence.rb+++ b/app/services/user/silence.rb@@ -30,11 +30,11 @@ class User::Silenceprivatedef fetch_user(params:)- User.find_by(id: params[:user_id])+ User.find_by(id: params.user_id)enddef fetch_users(user:, params:)- [user, *User.where(id: params[:other_user_ids].to_a.uniq).to_a]+ [user, *User.where(id: params.other_user_ids.to_a.uniq).to_a]enddef can_silence_all_users(guardian:, users:)@@ -46,7 +46,7 @@ class User::Silenceenddef fetch_post(params:)- Post.find_by(id: params[:post_id])+ Post.find_by(id: params.post_id)enddef perform_post_action(guardian:, post:, params:)diff --git a/app/services/user/suspend.rb b/app/services/user/suspend.rbindex 63de8bde7bd..235fca39eb3 100644--- a/app/services/user/suspend.rb+++ b/app/services/user/suspend.rb@@ -30,11 +30,11 @@ class User::Suspendprivatedef fetch_user(params:)- User.find_by(id: params[:user_id])+ User.find_by(id: params.user_id)enddef fetch_users(user:, params:)- [user, *User.where(id: params[:other_user_ids].to_a.uniq).to_a]+ [user, *User.where(id: params.other_user_ids.to_a.uniq).to_a]enddef can_suspend_all_users(guardian:, users:)@@ -46,7 +46,7 @@ class User::Suspendenddef fetch_post(params:)- Post.find_by(id: params[:post_id])+ Post.find_by(id: params.post_id)enddef perform_post_action(guardian:, post:, params:)diff --git a/lib/service/contract_base.rb b/lib/service/contract_base.rbindex afab2bb335f..698d4b3d383 100644--- a/lib/service/contract_base.rb+++ b/lib/service/contract_base.rb@@ -17,10 +17,6 @@ class Service::ContractBase@__options__end- def [](key)- public_send(key)- end-def to_hashattributes.symbolize_keysenddiff --git a/plugins/chat/app/services/chat/add_users_to_channel.rb b/plugins/chat/app/services/chat/add_users_to_channel.rbindex 109b83e0520..628c999e746 100644--- a/plugins/chat/app/services/chat/add_users_to_channel.rb+++ b/plugins/chat/app/services/chat/add_users_to_channel.rb@@ -50,7 +50,7 @@ module Chatprivatedef fetch_channel(params:)- ::Chat::Channel.includes(:chatable).find_by(id: params[:channel_id])+ ::Chat::Channel.includes(:chatable).find_by(id: params.channel_id)enddef can_add_users_to_channel(guardian:, channel:)@@ -60,8 +60,8 @@ module Chatdef fetch_target_users(params:, channel:)::Chat::UsersFromUsernamesAndGroupsQuery.call(- usernames: params[:usernames],- groups: params[:groups],+ usernames: params.usernames,+ groups: params.groups,excluded_user_ids: channel.chatable.direct_message_users.pluck(:user_id),)enddiff --git a/plugins/chat/app/services/chat/auto_join_channel_batch.rb b/plugins/chat/app/services/chat/auto_join_channel_batch.rbindex 102d3f245f6..03d0e9c399f 100644--- a/plugins/chat/app/services/chat/auto_join_channel_batch.rb+++ b/plugins/chat/app/services/chat/auto_join_channel_batch.rb@@ -45,7 +45,7 @@ module Chatprivatedef fetch_channel(params:)- ::Chat::CategoryChannel.find_by(id: params[:channel_id], auto_join_users: true)+ ::Chat::CategoryChannel.find_by(id: params.channel_id, auto_join_users: true)enddef create_memberships(channel:, params:)diff --git a/plugins/chat/app/services/chat/auto_remove/handle_category_updated.rb b/plugins/chat/app/services/chat/auto_remove/handle_category_updated.rbindex 2a60cc9344c..0c547a041b3 100644--- a/plugins/chat/app/services/chat/auto_remove/handle_category_updated.rb+++ b/plugins/chat/app/services/chat/auto_remove/handle_category_updated.rb@@ -38,7 +38,7 @@ module Chatenddef fetch_category(params:)- Category.find_by(id: params[:category_id])+ Category.find_by(id: params.category_id)enddef fetch_category_channel_ids(category:)diff --git a/plugins/chat/app/services/chat/auto_remove/handle_chat_allowed_groups_change.rb b/plugins/chat/app/services/chat/auto_remove/handle_chat_allowed_groups_change.rbindex cacb00c2227..3c120262e58 100644--- a/plugins/chat/app/services/chat/auto_remove/handle_chat_allowed_groups_change.rb+++ b/plugins/chat/app/services/chat/auto_remove/handle_chat_allowed_groups_change.rb@@ -33,7 +33,7 @@ module Chatenddef not_everyone_allowed(params:)- params[:new_allowed_groups].exclude?(Group::AUTO_GROUPS[:everyone])+ params.new_allowed_groups.exclude?(Group::AUTO_GROUPS[:everyone])enddef fetch_users(params:)@@ -46,8 +46,8 @@ module Chat.joins(:user_chat_channel_memberships).distinct.then do |users|- break users if params[:new_allowed_groups].blank?- users.where(<<~SQL, params[:new_allowed_groups])+ break users if params.new_allowed_groups.blank?+ users.where(<<~SQL, params.new_allowed_groups)users.id NOT IN (SELECT DISTINCT group_users.user_idFROM group_usersdiff --git a/plugins/chat/app/services/chat/auto_remove/handle_destroyed_group.rb b/plugins/chat/app/services/chat/auto_remove/handle_destroyed_group.rbindex 16303071d1a..627e3818041 100644--- a/plugins/chat/app/services/chat/auto_remove/handle_destroyed_group.rb+++ b/plugins/chat/app/services/chat/auto_remove/handle_destroyed_group.rb@@ -56,7 +56,7 @@ module Chat.not_staged.includes(:group_users).where("NOT admin AND NOT moderator")- .where(id: params[:destroyed_group_user_ids])+ .where(id: params.destroyed_group_user_ids).joins(:user_chat_channel_memberships).distinctenddiff --git a/plugins/chat/app/services/chat/auto_remove/handle_user_removed_from_group.rb b/plugins/chat/app/services/chat/auto_remove/handle_user_removed_from_group.rbindex f9ebb7227d8..a50248d640a 100644--- a/plugins/chat/app/services/chat/auto_remove/handle_user_removed_from_group.rb+++ b/plugins/chat/app/services/chat/auto_remove/handle_user_removed_from_group.rb@@ -49,7 +49,7 @@ module Chatenddef fetch_user(params:)- User.find_by(id: params[:user_id])+ User.find_by(id: params.user_id)enddef user_not_staff(user:)diff --git a/plugins/chat/app/services/chat/create_category_channel.rb b/plugins/chat/app/services/chat/create_category_channel.rbindex bb9aaf018f5..3484f80f5a1 100644--- a/plugins/chat/app/services/chat/create_category_channel.rb+++ b/plugins/chat/app/services/chat/create_category_channel.rb@@ -66,11 +66,11 @@ module Chatenddef fetch_category(params:)- Category.find_by(id: params[:category_id])+ Category.find_by(id: params.category_id)enddef category_channel_does_not_exist(category:, params:)- !Chat::Channel.exists?(chatable: category, name: params[:name])+ !Chat::Channel.exists?(chatable: category, name: params.name)enddef create_channel(category:, params:)diff --git a/plugins/chat/app/services/chat/create_direct_message_channel.rb b/plugins/chat/app/services/chat/create_direct_message_channel.rbindex 0e8aa1f6686..06a412607dd 100644--- a/plugins/chat/app/services/chat/create_direct_message_channel.rb+++ b/plugins/chat/app/services/chat/create_direct_message_channel.rb@@ -64,8 +64,8 @@ module Chatdef fetch_target_users(guardian:, params:)::Chat::UsersFromUsernamesAndGroupsQuery.call(- usernames: [*params[:target_usernames], guardian.user.username],- groups: params[:target_groups],+ usernames: [*params.target_usernames, guardian.user.username],+ groups: params.target_groups,)end@@ -79,9 +79,9 @@ module Chatdef fetch_or_create_direct_message(target_users:, params:)ids = target_users.map(&:id)- is_group = ids.size > 2 || params[:name].present?+ is_group = ids.size > 2 || params.name.present?- if params[:upsert] || !is_group+ if params.upsert || !is_group::Chat::DirectMessage.for_user_ids(ids, group: is_group) ||::Chat::DirectMessage.create(user_ids: ids, group: is_group)else@@ -94,7 +94,7 @@ module Chatenddef set_optional_name(channel:, params:)- channel.update!(params.slice(:name)) if params[:name]&.size&.positive?+ channel.update!(params.slice(:name)) if params.name&.size&.positive?enddef update_memberships(channel:, target_users:)diff --git a/plugins/chat/app/services/chat/create_message.rb b/plugins/chat/app/services/chat/create_message.rbindex 05efac60f32..5a59872b82d 100644--- a/plugins/chat/app/services/chat/create_message.rb+++ b/plugins/chat/app/services/chat/create_message.rb@@ -90,7 +90,7 @@ module Chatenddef fetch_channel(params:)- Chat::Channel.find_by_id_or_slug(params[:chat_channel_id])+ Chat::Channel.find_by_id_or_slug(params.chat_channel_id)enddef enforce_membership(guardian:, channel:, options:)@@ -108,16 +108,16 @@ module Chatenddef fetch_reply(params:)- Chat::Message.find_by(id: params[:in_reply_to_id])+ Chat::Message.find_by(id: params.in_reply_to_id)enddef ensure_reply_consistency(channel:, params:, reply:)- return true if params[:in_reply_to_id].blank?+ return true if params.in_reply_to_id.blank?reply&.chat_channel == channelenddef fetch_thread(params:, reply:, channel:, options:)- return Chat::Thread.find_by(id: params[:thread_id]) if params[:thread_id].present?+ return Chat::Thread.find_by(id: params.thread_id) if params.thread_id.present?return unless replyreply.thread ||reply.build_thread(@@ -129,7 +129,7 @@ module Chatenddef ensure_valid_thread_for_channel(thread:, params:, channel:)- return true if params[:thread_id].blank?+ return true if params.thread_id.blank?thread&.channel == channelend@@ -140,7 +140,7 @@ module Chatdef fetch_uploads(params:, guardian:)return [] if !SiteSetting.chat_allow_uploads- guardian.user.uploads.where(id: params[:upload_ids])+ guardian.user.uploads.where(id: params.upload_ids)enddef instantiate_message(channel:, guardian:, params:, uploads:, thread:, reply:, options:)@@ -148,10 +148,10 @@ module Chatuser: guardian.user,last_editor: guardian.user,in_reply_to: reply,- message: params[:message],+ message: params.message,uploads: uploads,thread: thread,- cooked: ::Chat::Message.cook(params[:message], user_id: guardian.user.id),+ cooked: ::Chat::Message.cook(params.message, user_id: guardian.user.id),cooked_version: ::Chat::Message::BAKED_VERSION,streaming: options.streaming,)@@ -206,7 +206,7 @@ module Chatenddef process(channel:, message_instance:, params:, thread:, options:)- ::Chat::Publisher.publish_new!(channel, message_instance, params[:staged_id])+ ::Chat::Publisher.publish_new!(channel, message_instance, params.staged_id)DiscourseEvent.trigger(:chat_message_created,@@ -217,20 +217,20 @@ module Chatthread: thread,thread_replies_count: thread&.replies_count_cache || 0,context: {- post_ids: params[:context_post_ids],- topic_id: params[:context_topic_id],+ post_ids: params.context_post_ids,+ topic_id: params.context_topic_id,},},)if options.process_inlineJobs::Chat::ProcessMessage.new.execute(- { chat_message_id: message_instance.id, staged_id: params[:staged_id] },+ { chat_message_id: message_instance.id, staged_id: params.staged_id },)elseJobs.enqueue(Jobs::Chat::ProcessMessage,- { chat_message_id: message_instance.id, staged_id: params[:staged_id] },+ { chat_message_id: message_instance.id, staged_id: params.staged_id },)endenddiff --git a/plugins/chat/app/services/chat/create_thread.rb b/plugins/chat/app/services/chat/create_thread.rbindex bc3f930aeb4..e5167478524 100644--- a/plugins/chat/app/services/chat/create_thread.rb+++ b/plugins/chat/app/services/chat/create_thread.rb@@ -40,14 +40,11 @@ module Chatprivatedef fetch_channel(params:)- ::Chat::Channel.find_by(id: params[:channel_id])+ ::Chat::Channel.find_by(id: params.channel_id)enddef fetch_original_message(channel:, params:)- ::Chat::Message.find_by(- id: params[:original_message_id],- chat_channel_id: params[:channel_id],- )+ ::Chat::Message.find_by(id: params.original_message_id, chat_channel_id: params.channel_id)enddef can_view_channel(guardian:, channel:)@@ -64,7 +61,7 @@ module Chatendcontext[:thread] = channel.threads.create(- title: params[:title],+ title: params.title,original_message: original_message,original_message_user: original_message.user,)diff --git a/plugins/chat/app/services/chat/flag_message.rb b/plugins/chat/app/services/chat/flag_message.rbindex 567dd864e20..cf6cfe52876 100644--- a/plugins/chat/app/services/chat/flag_message.rb+++ b/plugins/chat/app/services/chat/flag_message.rb@@ -48,8 +48,8 @@ module Chatdef fetch_message(params:)Chat::Message.includes(:chat_channel, :revisions).find_by(- id: params[:message_id],- chat_channel_id: params[:channel_id],+ id: params.message_id,+ chat_channel_id: params.channel_id,)end@@ -58,7 +58,7 @@ module Chatguardian.can_flag_chat_message?(message) &&guardian.can_flag_message_as?(message,- params[:flag_type_id],+ params.flag_type_id,params.slice(:queue_for_review, :take_action, :is_warning),)end@@ -67,7 +67,7 @@ module ChatChat::ReviewQueue.new.flag_message(message,guardian,- params[:flag_type_id],+ params.flag_type_id,**params.slice(:message, :is_warning, :take_action, :queue_for_review),)enddiff --git a/plugins/chat/app/services/chat/invite_users_to_channel.rb b/plugins/chat/app/services/chat/invite_users_to_channel.rbindex 9effedd3c8f..06e8c183872 100644--- a/plugins/chat/app/services/chat/invite_users_to_channel.rb+++ b/plugins/chat/app/services/chat/invite_users_to_channel.rb@@ -33,7 +33,7 @@ module Chatprivatedef fetch_channel(params:)- ::Chat::Channel.find_by(id: params[:channel_id])+ ::Chat::Channel.find_by(id: params.channel_id)enddef can_view_channel(guardian:, channel:)@@ -45,7 +45,7 @@ module Chat.joins(:user_option).where(user_options: { chat_enabled: true }).not_suspended- .where(id: params[:user_ids])+ .where(id: params.user_ids).limit(50)end@@ -59,7 +59,7 @@ module Chatchat_channel_title: channel.title(invited_user),chat_channel_slug: channel.slug,invited_by_username: guardian.user.username,- chat_message_id: params[:message_id],+ chat_message_id: params.message_id,}.compactinvited_user.notifications.create(diff --git a/plugins/chat/app/services/chat/leave_channel.rb b/plugins/chat/app/services/chat/leave_channel.rbindex e68ac4ddf52..83f49017e3d 100644--- a/plugins/chat/app/services/chat/leave_channel.rb+++ b/plugins/chat/app/services/chat/leave_channel.rb@@ -32,7 +32,7 @@ module Chatprivatedef fetch_channel(params:)- Chat::Channel.find_by(id: params[:channel_id])+ Chat::Channel.find_by(id: params.channel_id)enddef leave(channel:, guardian:)diff --git a/plugins/chat/app/services/chat/list_channel_messages.rb b/plugins/chat/app/services/chat/list_channel_messages.rbindex 0f39a26ecf7..391f2c8cfec 100644--- a/plugins/chat/app/services/chat/list_channel_messages.rb+++ b/plugins/chat/app/services/chat/list_channel_messages.rb@@ -57,7 +57,7 @@ module Chatprivatedef fetch_channel(params:)- ::Chat::Channel.includes(:chatable).find_by(id: params[:channel_id])+ ::Chat::Channel.includes(:chatable).find_by(id: params.channel_id)enddef fetch_membership(channel:, guardian:)@@ -73,10 +73,10 @@ module Chatenddef determine_target_message_id(params:, membership:)- if params[:fetch_from_last_read]+ if params.fetch_from_last_readcontext[:target_message_id] = membership&.last_read_message_idelse- context[:target_message_id] = params[:target_message_id]+ context[:target_message_id] = params.target_message_idendend@@ -103,9 +103,9 @@ module Chatguardian:,target_message_id:,include_thread_messages: !enabled_threads,- page_size: params[:page_size] || Chat::MessagesQuery::MAX_PAGE_SIZE,- direction: params[:direction],- target_date: params[:target_date],+ page_size: params.page_size || Chat::MessagesQuery::MAX_PAGE_SIZE,+ direction: params.direction,+ target_date: params.target_date,)context[:can_load_more_past] = messages_data[:can_load_more_past]diff --git a/plugins/chat/app/services/chat/list_channel_thread_messages.rb b/plugins/chat/app/services/chat/list_channel_thread_messages.rbindex b9a31e2b603..c3bacfa8964 100644--- a/plugins/chat/app/services/chat/list_channel_thread_messages.rb+++ b/plugins/chat/app/services/chat/list_channel_thread_messages.rb@@ -51,7 +51,7 @@ module Chatprivatedef fetch_thread(params:)- ::Chat::Thread.strict_loading.includes(channel: :chatable).find_by(id: params[:thread_id])+ ::Chat::Thread.strict_loading.includes(channel: :chatable).find_by(id: params.thread_id)enddef can_view_thread(guardian:, thread:)@@ -63,14 +63,14 @@ module Chatenddef determine_target_message_id(params:, membership:, guardian:, thread:)- if params[:fetch_from_last_message]+ if params.fetch_from_last_messagecontext[:target_message_id] = thread.last_message_id- elsif params[:fetch_from_first_message]+ elsif params.fetch_from_first_messagecontext[:target_message_id] = thread.original_message_id- elsif params[:fetch_from_last_read] || !params[:target_message_id]+ elsif params.fetch_from_last_read || !params.target_message_idcontext[:target_message_id] = membership&.last_read_message_id- elsif params[:target_message_id]- context[:target_message_id] = params[:target_message_id]+ elsif params.target_message_id+ context[:target_message_id] = params.target_message_idendend@@ -79,7 +79,7 @@ module Chattarget_message =::Chat::Message.with_deleted.find_by(id: context.target_message_id,- thread_id: params[:thread_id],+ thread_id: params.thread_id,)return false if target_message.blank?return true if !target_message.trashed?@@ -93,11 +93,11 @@ module Chatguardian: guardian,target_message_id: context.target_message_id,thread_id: thread.id,- page_size: params[:page_size] || Chat::MessagesQuery::MAX_PAGE_SIZE,- direction: params[:direction],- target_date: params[:target_date],+ page_size: params.page_size || Chat::MessagesQuery::MAX_PAGE_SIZE,+ direction: params.direction,+ target_date: params.target_date,include_target_message_id:- params[:fetch_from_first_message] || params[:fetch_from_last_message],+ params.fetch_from_first_message || params.fetch_from_last_message,)context[:can_load_more_past] = messages_data[:can_load_more_past]diff --git a/plugins/chat/app/services/chat/lookup_channel_threads.rb b/plugins/chat/app/services/chat/lookup_channel_threads.rbindex bc1a3894826..f8c34b2c5f8 100644--- a/plugins/chat/app/services/chat/lookup_channel_threads.rb+++ b/plugins/chat/app/services/chat/lookup_channel_threads.rb@@ -37,9 +37,12 @@ module Chatonly_integer: true,},allow_nil: true++ after_validation do+ self.limit = (limit || THREADS_LIMIT).to_i.clamp(1, THREADS_LIMIT)+ self.offset = [offset || 0, 0].max+ endend- step :set_limit- step :set_offsetmodel :channelpolicy :threading_enabled_for_channelpolicy :can_view_channel@@ -51,16 +54,8 @@ module Chatprivate- def set_limit(params:)- context[:limit] = (params[:limit] || THREADS_LIMIT).to_i.clamp(1, THREADS_LIMIT)- end-- def set_offset(params:)- context[:offset] = [params[:offset] || 0, 0].max- end-def fetch_channel(params:)- ::Chat::Channel.strict_loading.includes(:chatable).find_by(id: params[:channel_id])+ ::Chat::Channel.strict_loading.includes(:chatable).find_by(id: params.channel_id)enddef threading_enabled_for_channel(channel:)@@ -71,7 +66,7 @@ module Chatguardian.can_preview_chat_channel?(channel)end- def fetch_threads(guardian:, channel:)+ def fetch_threads(guardian:, channel:, params:)::Chat::Thread.includes(:channel,@@ -114,8 +109,8 @@ module Chat.order("CASE WHEN user_chat_thread_memberships.last_read_message_id IS NULL OR user_chat_thread_memberships.last_read_message_id < chat_threads.last_message_id THEN true ELSE false END DESC, last_message.created_at DESC",)- .limit(context.limit)- .offset(context.offset)+ .limit(params.limit)+ .offset(params.offset)enddef fetch_tracking(guardian:, threads:)@@ -137,8 +132,8 @@ module Chatcontext[:participants] = ::Chat::ThreadParticipantQuery.call(thread_ids: threads.map(&:id))end- def build_load_more_url(channel:)- load_more_params = { offset: context.offset + context.limit }.to_query+ def build_load_more_url(channel:, params:)+ load_more_params = { offset: params.offset + params.limit }.to_querycontext[:load_more_url] = ::URI::HTTP.build(path: "/chat/api/channels/#{channel.id}/threads",query: load_more_params,diff --git a/plugins/chat/app/services/chat/lookup_thread.rb b/plugins/chat/app/services/chat/lookup_thread.rbindex 52411333bdb..18738bbcef0 100644--- a/plugins/chat/app/services/chat/lookup_thread.rb+++ b/plugins/chat/app/services/chat/lookup_thread.rb@@ -36,7 +36,7 @@ module Chat:channel,original_message_user: :user_status,original_message: :chat_webhook_event,- ).find_by(id: params[:thread_id], channel_id: params[:channel_id])+ ).find_by(id: params.thread_id, channel_id: params.channel_id)enddef invalid_access(guardian:, thread:)diff --git a/plugins/chat/app/services/chat/lookup_user_threads.rb b/plugins/chat/app/services/chat/lookup_user_threads.rbindex c2c21e386fd..863758e661c 100644--- a/plugins/chat/app/services/chat/lookup_user_threads.rb+++ b/plugins/chat/app/services/chat/lookup_user_threads.rb@@ -24,9 +24,12 @@ module Chatparams doattribute :limit, :integerattribute :offset, :integer++ after_validation do+ self.limit = (limit || THREADS_LIMIT).to_i.clamp(1, THREADS_LIMIT)+ self.offset = [offset || 0, 0].max+ endend- step :set_limit- step :set_offsetmodel :threadsstep :fetch_trackingstep :fetch_memberships@@ -35,15 +38,7 @@ module Chatprivate- def set_limit(params:)- context[:limit] = (params[:limit] || THREADS_LIMIT).to_i.clamp(1, THREADS_LIMIT)- end-- def set_offset(params:)- context[:offset] = [params[:offset] || 0, 0].max- end-- def fetch_threads(guardian:)+ def fetch_threads(guardian:, params:)::Chat::Thread.includes(:channel,@@ -108,8 +103,8 @@ module Chat.order("CASE WHEN user_chat_thread_memberships.last_read_message_id IS NULL OR user_chat_thread_memberships.last_read_message_id < chat_threads.last_message_id THEN true ELSE false END DESC, last_message.created_at DESC",)- .limit(context.limit)- .offset(context.offset)+ .limit(params.limit)+ .offset(params.offset)enddef fetch_tracking(guardian:, threads:)@@ -131,8 +126,8 @@ module Chatcontext[:participants] = ::Chat::ThreadParticipantQuery.call(thread_ids: threads.map(&:id))end- def build_load_more_url- load_more_params = { limit: context.limit, offset: context.offset + context.limit }.to_query+ def build_load_more_url(params:)+ load_more_params = { limit: params.limit, offset: params.offset + params.limit }.to_querycontext[:load_more_url] = ::URI::HTTP.build(path: "/chat/api/me/threads",diff --git a/plugins/chat/app/services/chat/mark_thread_title_prompt_seen.rb b/plugins/chat/app/services/chat/mark_thread_title_prompt_seen.rbindex 90f75069aa5..2402a23a1d9 100644--- a/plugins/chat/app/services/chat/mark_thread_title_prompt_seen.rb+++ b/plugins/chat/app/services/chat/mark_thread_title_prompt_seen.rb@@ -38,7 +38,7 @@ module Chatprivatedef fetch_thread(params:)- Chat::Thread.find_by(id: params[:thread_id], channel_id: params[:channel_id])+ Chat::Thread.find_by(id: params.thread_id, channel_id: params.channel_id)enddef can_view_channel(guardian:, thread:)diff --git a/plugins/chat/app/services/chat/restore_message.rb b/plugins/chat/app/services/chat/restore_message.rbindex d20735082e3..65fac0aa9b8 100644--- a/plugins/chat/app/services/chat/restore_message.rb+++ b/plugins/chat/app/services/chat/restore_message.rb@@ -40,7 +40,7 @@ module ChatChat::Message.with_deleted.includes(chat_channel: :chatable)- .find_by(id: params[:message_id], chat_channel_id: params[:channel_id])+ .find_by(id: params.message_id, chat_channel_id: params.channel_id)enddef invalid_access(guardian:, message:)diff --git a/plugins/chat/app/services/chat/search_chatable.rb b/plugins/chat/app/services/chat/search_chatable.rbindex a7e806b6a5e..0349b094208 100644--- a/plugins/chat/app/services/chat/search_chatable.rb+++ b/plugins/chat/app/services/chat/search_chatable.rb@@ -40,33 +40,33 @@ module Chatenddef fetch_users(guardian:, params:)- return unless params[:include_users]+ return unless params.include_usersreturn unless guardian.can_create_direct_message?search_users(params, guardian)enddef fetch_groups(guardian:, params:)- return unless params[:include_groups]+ return unless params.include_groupsreturn unless guardian.can_create_direct_message?search_groups(params, guardian)enddef fetch_category_channels(guardian:, params:)- return unless params[:include_category_channels]+ return unless params.include_category_channelsreturn unless SiteSetting.enable_public_channelssearch_category_channels(params, guardian)enddef fetch_direct_message_channels(guardian:, params:, users:)- return unless params[:include_direct_message_channels]+ return unless params.include_direct_message_channelsreturn unless guardian.can_create_direct_message?search_direct_message_channels(guardian, params, users)enddef search_users(params, guardian)- user_search = ::UserSearch.new(params[:term], limit: SEARCH_RESULT_LIMIT)+ user_search = ::UserSearch.new(params.term, limit: SEARCH_RESULT_LIMIT)- if params[:term].blank?+ if params.term.blank?user_search = user_search.scoped_userselseuser_search = user_search.search@@ -78,11 +78,11 @@ module Chatuser_search = user_search.real(allowed_bot_user_ids: allowed_bot_user_ids)user_search = user_search.includes(:user_option)- if params[:excluded_memberships_channel_id]+ if params.excluded_memberships_channel_iduser_search =user_search.where("NOT EXISTS (SELECT 1 FROM user_chat_channel_memberships WHERE user_id = users.id AND chat_channel_id = ?)",- params[:excluded_memberships_channel_id],+ params.excluded_memberships_channel_id,)end@@ -95,7 +95,7 @@ module Chat.includes(users: :user_option).where("groups.name ILIKE :term_like OR groups.full_name ILIKE :term_like",- term_like: "%#{params[:term]}%",+ term_like: "%#{params.term}%",).limit(SEARCH_RESULT_LIMIT)end@@ -104,7 +104,7 @@ module Chat::Chat::ChannelFetcher.secured_public_channel_search(guardian,status: :open,- filter: params[:term],+ filter: params.term,filter_on_category_name: false,match_filter_on_starts_with: false,limit: SEARCH_RESULT_LIMIT,@@ -116,13 +116,13 @@ module Chat::Chat::ChannelFetcher.secured_direct_message_channels_search(guardian.user.id,guardian,- filter: params[:term],+ filter: params.term,match_filter_on_starts_with: false,limit: SEARCH_RESULT_LIMIT,) || []# skip 1:1s when search returns users- if params[:include_users] && users.present?+ if params.include_users && users.present?channels.reject! do |channel|other_user_ids = channel.allowed_user_ids - [guardian.user.id]other_user_ids.size <= 1diff --git a/plugins/chat/app/services/chat/stop_message_streaming.rb b/plugins/chat/app/services/chat/stop_message_streaming.rbindex cdad91954fb..b9f0bd50d6e 100644--- a/plugins/chat/app/services/chat/stop_message_streaming.rb+++ b/plugins/chat/app/services/chat/stop_message_streaming.rb@@ -29,7 +29,7 @@ module Chatprivatedef fetch_message(params:)- ::Chat::Message.find_by(id: params[:message_id])+ ::Chat::Message.find_by(id: params.message_id)enddef enforce_membership(guardian:, message:)diff --git a/plugins/chat/app/services/chat/trash_channel.rb b/plugins/chat/app/services/chat/trash_channel.rbindex 6a094962ab3..7bb88a75050 100644--- a/plugins/chat/app/services/chat/trash_channel.rb+++ b/plugins/chat/app/services/chat/trash_channel.rb@@ -18,7 +18,12 @@ module ChatDELETE_CHANNEL_LOG_KEY = "chat_channel_delete"- model :channel, :fetch_channel+ params do+ attribute :channel_id, :integer++ validates :channel_id, presence: true+ end+ model :channelpolicy :invalid_accesstransaction dostep :prevents_slug_collision@@ -30,7 +35,7 @@ module Chatprivatedef fetch_channel(params:)- Chat::Channel.find_by(id: params[:channel_id])+ Chat::Channel.find_by(id: params.channel_id)enddef invalid_access(guardian:, channel:)diff --git a/plugins/chat/app/services/chat/trash_message.rb b/plugins/chat/app/services/chat/trash_message.rbindex b157413f50b..ac3edc4a250 100644--- a/plugins/chat/app/services/chat/trash_message.rb+++ b/plugins/chat/app/services/chat/trash_message.rb@@ -40,8 +40,8 @@ module Chatdef fetch_message(params:)Chat::Message.includes(chat_channel: :chatable).find_by(- id: params[:message_id],- chat_channel_id: params[:channel_id],+ id: params.message_id,+ chat_channel_id: params.channel_id,)enddiff --git a/plugins/chat/app/services/chat/trash_messages.rb b/plugins/chat/app/services/chat/trash_messages.rbindex 7abf4479b7c..3f1e21aaadb 100644--- a/plugins/chat/app/services/chat/trash_messages.rb+++ b/plugins/chat/app/services/chat/trash_messages.rb@@ -40,8 +40,8 @@ module Chatdef fetch_messages(params:)Chat::Message.includes(chat_channel: :chatable).where(- id: params[:message_ids],- chat_channel_id: params[:channel_id],+ id: params.message_ids,+ chat_channel_id: params.channel_id,)end@@ -90,7 +90,7 @@ module Chatmessages.each do |message|DiscourseEvent.trigger(:chat_message_trashed, message, message.chat_channel, guardian.user)end- Chat::Publisher.publish_bulk_delete!(messages.first.chat_channel, params[:message_ids])+ Chat::Publisher.publish_bulk_delete!(messages.first.chat_channel, params.message_ids)endendenddiff --git a/plugins/chat/app/services/chat/unfollow_channel.rb b/plugins/chat/app/services/chat/unfollow_channel.rbindex c23cd44536d..49fddd0107d 100644--- a/plugins/chat/app/services/chat/unfollow_channel.rb+++ b/plugins/chat/app/services/chat/unfollow_channel.rb@@ -31,7 +31,7 @@ module Chatprivatedef fetch_channel(params:)- Chat::Channel.find_by(id: params[:channel_id])+ Chat::Channel.find_by(id: params.channel_id)enddef unfollow(channel:, guardian:)diff --git a/plugins/chat/app/services/chat/update_channel_status.rb b/plugins/chat/app/services/chat/update_channel_status.rbindex 309b4649f4c..fbd84f38e1d 100644--- a/plugins/chat/app/services/chat/update_channel_status.rb+++ b/plugins/chat/app/services/chat/update_channel_status.rb@@ -16,28 +16,30 @@ module Chat# @option params [String] :status# @return [Service::Base::Context]- model :channelparams do+ attribute :channel_id, :integerattribute :status, :string+ validates :channel_id, presence: truevalidates :status, inclusion: { in: Chat::Channel.editable_statuses.keys }end+ model :channelpolicy :check_channel_permissionstep :change_statusprivatedef fetch_channel(params:)- Chat::Channel.find_by(id: params[:channel_id])+ Chat::Channel.find_by(id: params.channel_id)enddef check_channel_permission(guardian:, channel:, params:)guardian.can_preview_chat_channel?(channel) &&- guardian.can_change_channel_status?(channel, params[:status].to_sym)+ guardian.can_change_channel_status?(channel, params.status.to_sym)enddef change_status(channel:, params:, guardian:)- channel.public_send("#{params[:status]}!", guardian.user)+ channel.public_send("#{params.status}!", guardian.user)endendenddiff --git a/plugins/chat/app/services/chat/update_message.rb b/plugins/chat/app/services/chat/update_message.rbindex b58a459abf3..fc4c42f80e3 100644--- a/plugins/chat/app/services/chat/update_message.rb+++ b/plugins/chat/app/services/chat/update_message.rb@@ -79,7 +79,7 @@ module Chatchatable: [:topic_only_relative_url, direct_message_users: [user: :user_option]],],user: :user_status,- ).find_by(id: params[:message_id])+ ).find_by(id: params.message_id)enddef fetch_membership(guardian:, message:)@@ -88,7 +88,7 @@ module Chatdef fetch_uploads(params:, guardian:)return if !SiteSetting.chat_allow_uploads- guardian.user.uploads.where(id: params[:upload_ids])+ guardian.user.uploads.where(id: params.upload_ids)enddef can_modify_channel_message(guardian:, message:)@@ -100,11 +100,11 @@ module Chatenddef modify_message(params:, message:, guardian:, uploads:)- message.message = params[:message]+ message.message = params.messagemessage.last_editor_id = guardian.user.idmessage.cook- return if uploads&.size != params[:upload_ids].to_a.size+ return if uploads&.size != params.upload_ids.to_a.sizenew_upload_ids = uploads.map(&:id)existing_upload_ids = message.upload_idsdiff --git a/plugins/chat/app/services/chat/update_thread.rb b/plugins/chat/app/services/chat/update_thread.rbindex c42c9ee0c92..256d57c5a97 100644--- a/plugins/chat/app/services/chat/update_thread.rb+++ b/plugins/chat/app/services/chat/update_thread.rb@@ -36,7 +36,7 @@ module Chatprivatedef fetch_thread(params:)- Chat::Thread.find_by(id: params[:thread_id])+ Chat::Thread.find_by(id: params.thread_id)enddef can_view_channel(guardian:, thread:)diff --git a/plugins/chat/app/services/chat/update_thread_notification_settings.rb b/plugins/chat/app/services/chat/update_thread_notification_settings.rbindex 57c3caaf4bd..0c776877a8b 100644--- a/plugins/chat/app/services/chat/update_thread_notification_settings.rb+++ b/plugins/chat/app/services/chat/update_thread_notification_settings.rb@@ -45,7 +45,7 @@ module Chatprivatedef fetch_thread(params:)- Chat::Thread.find_by(id: params[:thread_id], channel_id: params[:channel_id])+ Chat::Thread.find_by(id: params.thread_id, channel_id: params.channel_id)enddef can_view_channel(guardian:, thread:)@@ -62,7 +62,7 @@ module Chatmembership = thread.add(guardian.user)membership.update!(last_read_message_id: thread.last_message_id)end- membership.update!(notification_level: params[:notification_level])+ membership.update!(notification_level: params.notification_level)context[:membership] = membershipendenddiff --git a/plugins/chat/app/services/chat/update_user_channel_last_read.rb b/plugins/chat/app/services/chat/update_user_channel_last_read.rbindex 580f2679fc0..871808b14bb 100644--- a/plugins/chat/app/services/chat/update_user_channel_last_read.rb+++ b/plugins/chat/app/services/chat/update_user_channel_last_read.rb@@ -36,7 +36,7 @@ module Chatprivatedef fetch_channel(params:)- ::Chat::Channel.find_by(id: params[:channel_id])+ ::Chat::Channel.find_by(id: params.channel_id)enddef fetch_membership(guardian:, channel:)@@ -48,7 +48,7 @@ module Chatenddef fetch_message(channel:, params:)- ::Chat::Message.with_deleted.find_by(chat_channel_id: channel.id, id: params[:message_id])+ ::Chat::Message.with_deleted.find_by(chat_channel_id: channel.id, id: params.message_id)enddef ensure_message_id_recency(message:, membership:)diff --git a/plugins/chat/app/services/chat/update_user_thread_last_read.rb b/plugins/chat/app/services/chat/update_user_thread_last_read.rbindex 8e10dd0b44c..e76e7ef757c 100644--- a/plugins/chat/app/services/chat/update_user_thread_last_read.rb+++ b/plugins/chat/app/services/chat/update_user_thread_last_read.rb@@ -37,7 +37,7 @@ module Chatprivatedef fetch_thread(params:)- ::Chat::Thread.find_by(id: params[:thread_id], channel_id: params[:channel_id])+ ::Chat::Thread.find_by(id: params.thread_id, channel_id: params.channel_id)enddef invalid_access(guardian:, thread:)@@ -50,7 +50,7 @@ module Chatdef fetch_message(params:, thread:)::Chat::Message.with_deleted.find_by(- id: params[:message_id] || thread.last_message_id,+ id: params.message_id || thread.last_message_id,thread: thread,chat_channel: thread.channel,)diff --git a/plugins/chat/app/services/chat/upsert_draft.rb b/plugins/chat/app/services/chat/upsert_draft.rbindex c47313d1fa3..0beae366d74 100644--- a/plugins/chat/app/services/chat/upsert_draft.rb+++ b/plugins/chat/app/services/chat/upsert_draft.rb@@ -39,7 +39,7 @@ module Chatprivatedef fetch_channel(params:)- Chat::Channel.find_by(id: params[:channel_id])+ Chat::Channel.find_by(id: params.channel_id)enddef can_upsert_draft(guardian:, channel:)@@ -47,26 +47,26 @@ module Chatenddef check_thread_exists(params:, channel:)- return if params[:thread_id].blank?- fail!("Thread not found") if !channel.threads.exists?(id: params[:thread_id])+ return if params.thread_id.blank?+ fail!("Thread not found") if !channel.threads.exists?(id: params.thread_id)enddef upsert_draft(params:, guardian:)- if params[:data].present?+ if params.data.present?draft =Chat::Draft.find_or_initialize_by(user_id: guardian.user.id,- chat_channel_id: params[:channel_id],- thread_id: params[:thread_id],+ chat_channel_id: params.channel_id,+ thread_id: params.thread_id,)- draft.data = params[:data]+ draft.data = params.datadraft.save!else# when data is empty, we destroy the draftChat::Draft.where(user: guardian.user,- chat_channel_id: params[:channel_id],- thread_id: params[:thread_id],+ chat_channel_id: params.channel_id,+ thread_id: params.thread_id,).destroy_allendenddiff --git a/plugins/chat/spec/services/chat/lookup_channel_threads_spec.rb b/plugins/chat/spec/services/chat/lookup_channel_threads_spec.rbindex 40926884e80..6076b209558 100644--- a/plugins/chat/spec/services/chat/lookup_channel_threads_spec.rb+++ b/plugins/chat/spec/services/chat/lookup_channel_threads_spec.rb@@ -30,7 +30,7 @@ RSpec.describe ::Chat::LookupChannelThreads dolet(:limit) { nil }it "defaults to a max value" do- expect(result.limit).to eq(described_class::THREADS_LIMIT)+ expect(result.params.limit).to eq(described_class::THREADS_LIMIT)endend@@ -44,7 +44,7 @@ RSpec.describe ::Chat::LookupChannelThreads dolet(:limit) { 0 }it "defaults to a max value" do- expect(result.limit).to eq(1)+ expect(result.params.limit).to eq(1)endendend@@ -57,7 +57,7 @@ RSpec.describe ::Chat::LookupChannelThreads dolet(:offset) { nil }it "defaults to zero" do- expect(result.offset).to eq(0)+ expect(result.params.offset).to eq(0)endend@@ -65,7 +65,7 @@ RSpec.describe ::Chat::LookupChannelThreads dolet(:offset) { -99 }it "defaults to a min value" do- expect(result.offset).to eq(0)+ expect(result.params.offset).to eq(0)endendenddiff --git a/plugins/chat/spec/services/chat/search_chatable_spec.rb b/plugins/chat/spec/services/chat/search_chatable_spec.rbindex 87f16bdd1ce..4344788d8e7 100644--- a/plugins/chat/spec/services/chat/search_chatable_spec.rb+++ b/plugins/chat/spec/services/chat/search_chatable_spec.rb@@ -47,10 +47,10 @@ RSpec.describe Chat::SearchChatable doit "cleans the term" doparams[:term] = "#bob"- expect(result.params[:term]).to eq("bob")+ expect(result.params.term).to eq("bob")params[:term] = "@bob"- expect(result.params[:term]).to eq("bob")+ expect(result.params.term).to eq("bob")endit "fetches user memberships" dodiff --git a/plugins/chat/spec/services/chat/trash_channel_spec.rb b/plugins/chat/spec/services/chat/trash_channel_spec.rbindex 097f1824dd1..6ab5e3e5b9b 100644--- a/plugins/chat/spec/services/chat/trash_channel_spec.rb+++ b/plugins/chat/spec/services/chat/trash_channel_spec.rb@@ -1,30 +1,40 @@# frozen_string_literal: trueRSpec.describe Chat::TrashChannel do- subject(:result) { described_class.call(params:, **dependencies) }+ describe described_class::Contract, type: :model do+ it { is_expected.to validate_presence_of(:channel_id) }+ end- fab!(:current_user) { Fabricate(:admin) }- fab!(:channel) { Fabricate(:chat_channel) }+ describe ".call" do+ subject(:result) { described_class.call(params:, **dependencies) }- let(:params) { { channel_id: } }- let(:dependencies) { { guardian: } }- let(:guardian) { Guardian.new(current_user) }- let(:channel_id) { channel.id }+ fab!(:current_user) { Fabricate(:admin) }+ fab!(:channel) { Fabricate(:chat_channel) }- context "when channel_id is not provided" do- let(:channel_id) { nil }+ let(:params) { { channel_id: } }+ let(:dependencies) { { guardian: } }+ let(:guardian) { Guardian.new(current_user) }+ let(:channel_id) { channel.id }- it { is_expected.to fail_to_find_a_model(:channel) }- end+ context "when data is invalid" do+ let(:channel_id) { nil }++ it { is_expected.to fail_a_contract }+ end++ context "when model is not found" do+ let(:channel_id) { 0 }++ it { is_expected.to fail_to_find_a_model(:channel) }+ end- context "when channel_id is provided" docontext "when user is not allowed to perform the action" dolet!(:current_user) { Fabricate(:user) }it { is_expected.to fail_a_policy(:invalid_access) }end- context "when user is allowed to perform the action" do+ context "when everything’s ok" doit { is_expected.to run_successfully }it "trashes the channel" dodiff --git a/plugins/chat/spec/services/chat/update_channel_status_spec.rb b/plugins/chat/spec/services/chat/update_channel_status_spec.rbindex e7a6e143fb6..50a3a0f8ead 100644--- a/plugins/chat/spec/services/chat/update_channel_status_spec.rb+++ b/plugins/chat/spec/services/chat/update_channel_status_spec.rb@@ -2,6 +2,7 @@RSpec.describe(Chat::UpdateChannelStatus) dodescribe described_class::Contract, type: :model do+ it { is_expected.to validate_presence_of(:channel_id) }it dois_expected.to validate_inclusion_of(:status).in_array(Chat::Channel.editable_statuses.keys)end@@ -19,8 +20,8 @@ RSpec.describe(Chat::UpdateChannelStatus) dolet(:status) { "open" }let(:channel_id) { channel.id }- context "when no channel_id is given" do- let(:channel_id) { nil }+ context "when model is not found" do+ let(:channel_id) { 0 }it { is_expected.to fail_to_find_a_model(:channel) }enddiff --git a/spec/services/admin_notices/dismiss_spec.rb b/spec/services/admin_notices/dismiss_spec.rbindex c2606acb11d..028f3acc76b 100644--- a/spec/services/admin_notices/dismiss_spec.rb+++ b/spec/services/admin_notices/dismiss_spec.rb@@ -1,36 +1,49 @@# frozen_string_literal: trueRSpec.describe(AdminNotices::Dismiss) do- subject(:result) { described_class.call(params:, **dependencies) }+ describe described_class::Contract, type: :model do+ it { is_expected.to validate_presence_of(:id) }+ end- fab!(:current_user) { Fabricate(:admin) }- fab!(:admin_notice) { Fabricate(:admin_notice, identifier: "problem.test") }- fab!(:problem_check) { Fabricate(:problem_check_tracker, identifier: "problem.test", blips: 3) }+ describe ".call" do+ subject(:result) { described_class.call(params:, **dependencies) }- let(:params) { { id: admin_notice.id } }- let(:dependencies) { { guardian: current_user.guardian } }+ fab!(:current_user) { Fabricate(:admin) }+ fab!(:admin_notice) { Fabricate(:admin_notice, identifier: "problem.test") }+ fab!(:problem_check) { Fabricate(:problem_check_tracker, identifier: "problem.test", blips: 3) }- context "when user is not allowed to perform the action" do- fab!(:current_user) { Fabricate(:user) }+ let(:params) { { id: notice_id } }+ let(:notice_id) { admin_notice.id }+ let(:dependencies) { { guardian: current_user.guardian } }- it { is_expected.to fail_a_policy(:invalid_access) }- end+ context "when user is not allowed to perform the action" do+ fab!(:current_user) { Fabricate(:user) }- context "when the admin notice has already been dismissed" do- before { admin_notice.destroy! }+ it { is_expected.to fail_a_policy(:invalid_access) }+ end- it { is_expected.to run_successfully }- end+ context "when data is invalid" do+ let(:notice_id) { nil }- context "when everything's ok" do- it { is_expected.to run_successfully }+ it { is_expected.to fail_a_contract }+ end++ context "when the admin notice has already been dismissed" do+ before { admin_notice.destroy! }- it "destroys the admin notice" do- expect { result }.to change { AdminNotice.count }.from(1).to(0)+ it { is_expected.to run_successfully }end- it "resets any associated problem check" do- expect { result }.to change { problem_check.reload.blips }.from(3).to(0)+ context "when everything's ok" do+ it { is_expected.to run_successfully }++ it "destroys the admin notice" do+ expect { result }.to change { AdminNotice.count }.from(1).to(0)+ end++ it "resets any associated problem check" do+ expect { result }.to change { problem_check.reload.blips }.from(3).to(0)+ endendendenddiff --git a/spec/services/flags/destroy_flag_spec.rb b/spec/services/flags/destroy_flag_spec.rbindex 1fc39df27aa..2936eadfd00 100644--- a/spec/services/flags/destroy_flag_spec.rb+++ b/spec/services/flags/destroy_flag_spec.rb@@ -1,57 +1,69 @@# frozen_string_literal: trueRSpec.describe(Flags::DestroyFlag) do- subject(:result) { described_class.call(params:, **dependencies) }+ describe described_class::Contract, type: :model do+ it { is_expected.to validate_presence_of(:id) }+ end- fab!(:current_user) { Fabricate(:admin) }- fab!(:flag)+ describe ".call" do+ subject(:result) { described_class.call(params:, **dependencies) }- let(:params) { { id: flag_id } }- let(:dependencies) { { guardian: current_user.guardian } }- let(:flag_id) { flag.id }+ fab!(:current_user) { Fabricate(:admin) }+ fab!(:flag)- # DO NOT REMOVE: flags have side effects and their state will leak to- # other examples otherwise.- after { flag.destroy! }+ let(:params) { { id: flag_id } }+ let(:dependencies) { { guardian: current_user.guardian } }+ let(:flag_id) { flag.id }- context "when model is not found" do- let(:flag_id) { 0 }+ # DO NOT REMOVE: flags have side effects and their state will leak to+ # other examples otherwise.+ after { flag.destroy! }- it { is_expected.to fail_to_find_a_model(:flag) }- end+ context "when data is invalid" do+ let(:flag_id) { nil }- context "when the flag is a system one" do- let(:flag) { Flag.first }+ it { is_expected.to fail_a_contract }+ end- it { is_expected.to fail_a_policy(:not_system) }- end+ context "when model is not found" do+ let(:flag_id) { 0 }- context "when the flag has been used" do- let!(:post_action) { Fabricate(:post_action, post_action_type_id: flag.id) }+ it { is_expected.to fail_to_find_a_model(:flag) }+ end- it { is_expected.to fail_a_policy(:not_used) }- end+ context "when the flag is a system one" do+ let(:flag) { Flag.first }- context "when user is not allowed to perform the action" do- fab!(:current_user) { Fabricate(:user) }+ it { is_expected.to fail_a_policy(:not_system) }+ end- it { is_expected.to fail_a_policy(:invalid_access) }- end+ context "when the flag has been used" do+ let!(:post_action) { Fabricate(:post_action, post_action_type_id: flag.id) }++ it { is_expected.to fail_a_policy(:not_used) }+ end- context "when everything's ok" do- it { is_expected.to run_successfully }+ context "when user is not allowed to perform the action" do+ fab!(:current_user) { Fabricate(:user) }- it "destroys the flag" do- expect { result }.to change { Flag.where(id: flag).count }.by(-1)+ it { is_expected.to fail_a_policy(:invalid_access) }end- it "logs the action" do- expect { result }.to change { UserHistory.count }.by(1)- expect(UserHistory.last).to have_attributes(- custom_type: "delete_flag",- details:- "name: offtopic\ndescription: \napplies_to: [\"Post\", \"Chat::Message\"]\nenabled: true",- )+ context "when everything's ok" do+ it { is_expected.to run_successfully }++ it "destroys the flag" do+ expect { result }.to change { Flag.where(id: flag).count }.by(-1)+ end++ it "logs the action" do+ expect { result }.to change { UserHistory.count }.by(1)+ expect(UserHistory.last).to have_attributes(+ custom_type: "delete_flag",+ details:+ "name: offtopic\ndescription: \napplies_to: [\"Post\", \"Chat::Message\"]\nenabled: true",+ )+ endendendend