Skip to content

A platform for community discussion. Free, open, simple.

DEV: Apply new Rubocop linting on services

Loïc Guitautcf2b4d993428
diff --git a/Gemfile.lock b/Gemfile.lock
index 4797847e1e9..391f6db80f1 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -465,7 +465,7 @@ GEM
parser (>= 3.3.1.0)
rubocop-capybara (2.21.0)
rubocop (~> 1.41)
- rubocop-discourse (3.8.6)
+ rubocop-discourse (3.9.0)
activesupport (>= 6.1)
rubocop (>= 1.59.0)
rubocop-capybara (>= 2.0.0)
diff --git a/app/services/admin_notices/dismiss.rb b/app/services/admin_notices/dismiss.rb
index defcf52ba22..69567ea1cde 100644
--- a/app/services/admin_notices/dismiss.rb
+++ b/app/services/admin_notices/dismiss.rb
@@ -4,12 +4,15 @@ class AdminNotices::Dismiss
include Service::Base
policy :invalid_access
+
params do
attribute :id, :integer
validates :id, presence: true
end
+
model :admin_notice, optional: true
+
transaction do
step :destroy
step :reset_problem_check
diff --git a/app/services/experiments/toggle.rb b/app/services/experiments/toggle.rb
index 9c63cc1c883..cfb3899ec2b 100644
--- a/app/services/experiments/toggle.rb
+++ b/app/services/experiments/toggle.rb
@@ -4,10 +4,12 @@ class Experiments::Toggle
include Service::Base
policy :current_user_is_admin
+
params do
attribute :setting_name, :string
validates :setting_name, presence: true
end
+
policy :setting_is_available
transaction { step :toggle }
diff --git a/app/services/flags/create_flag.rb b/app/services/flags/create_flag.rb
index 98d3b50934e..6f23b396692 100644
--- a/app/services/flags/create_flag.rb
+++ b/app/services/flags/create_flag.rb
@@ -4,6 +4,7 @@ class Flags::CreateFlag
include Service::Base
policy :invalid_access
+
params do
attribute :name, :string
attribute :description, :string
@@ -18,8 +19,10 @@ class Flags::CreateFlag
validates :description, length: { maximum: Flag::MAX_DESCRIPTION_LENGTH }
validates :applies_to, inclusion: { in: -> { Flag.valid_applies_to_types } }, allow_nil: false
end
+
policy :unique_name
model :flag, :instantiate_flag
+
transaction do
step :create
step :log
diff --git a/app/services/flags/destroy_flag.rb b/app/services/flags/destroy_flag.rb
index abaf3dd0155..6f0153e57b7 100644
--- a/app/services/flags/destroy_flag.rb
+++ b/app/services/flags/destroy_flag.rb
@@ -8,10 +8,12 @@ class Flags::DestroyFlag
validates :id, presence: true
end
+
model :flag
policy :not_system
policy :not_used
policy :invalid_access
+
transaction do
step :destroy
step :log
diff --git a/app/services/flags/reorder_flag.rb b/app/services/flags/reorder_flag.rb
index c680d8ae55e..b1fa5a2405a 100644
--- a/app/services/flags/reorder_flag.rb
+++ b/app/services/flags/reorder_flag.rb
@@ -10,10 +10,12 @@ class Flags::ReorderFlag
validates :flag_id, presence: true
validates :direction, inclusion: { in: %w[up down] }
end
+
model :flag
policy :invalid_access
model :all_flags
policy :invalid_move
+
transaction do
step :move
step :log
diff --git a/app/services/flags/toggle_flag.rb b/app/services/flags/toggle_flag.rb
index 3b15f257717..06112b3b575 100644
--- a/app/services/flags/toggle_flag.rb
+++ b/app/services/flags/toggle_flag.rb
@@ -4,12 +4,15 @@ class Flags::ToggleFlag
include Service::Base
policy :invalid_access
+
params do
attribute :flag_id, :integer
validates :flag_id, presence: true
end
+
model :flag
+
transaction do
step :toggle
step :log
diff --git a/app/services/flags/update_flag.rb b/app/services/flags/update_flag.rb
index ccd682b65a9..625243dda57 100644
--- a/app/services/flags/update_flag.rb
+++ b/app/services/flags/update_flag.rb
@@ -19,11 +19,13 @@ class Flags::UpdateFlag
validates :description, length: { maximum: Flag::MAX_DESCRIPTION_LENGTH }
validates :applies_to, inclusion: { in: -> { Flag.valid_applies_to_types } }, allow_nil: false
end
+
model :flag
policy :not_system
policy :not_used
policy :invalid_access
policy :unique_name
+
transaction do
step :update
step :log
diff --git a/app/services/site_setting/update.rb b/app/services/site_setting/update.rb
index 89c834d9687..c337b2f78f9 100644
--- a/app/services/site_setting/update.rb
+++ b/app/services/site_setting/update.rb
@@ -6,6 +6,7 @@ class SiteSetting::Update
options { attribute :allow_changing_hidden, :boolean, default: false }
policy :current_user_is_admin
+
params do
attribute :setting_name
attribute :new_value
@@ -34,6 +35,7 @@ class SiteSetting::Update
end
end
end
+
policy :setting_is_visible
policy :setting_is_configurable
step :save
diff --git a/app/services/user/silence.rb b/app/services/user/silence.rb
index 28bbc6d0ade..13b70112c0d 100644
--- a/app/services/user/silence.rb
+++ b/app/services/user/silence.rb
@@ -19,6 +19,7 @@ class User::Silence
validates :other_user_ids, length: { maximum: User::MAX_SIMILAR_USERS }
validates :post_action, inclusion: { in: %w[delete delete_replies edit] }, allow_blank: true
end
+
model :user
policy :not_silenced_already, class_name: User::Policy::NotAlreadySilenced
model :users
diff --git a/app/services/user/suspend.rb b/app/services/user/suspend.rb
index 235fca39eb3..1802b1c056b 100644
--- a/app/services/user/suspend.rb
+++ b/app/services/user/suspend.rb
@@ -19,6 +19,7 @@ class User::Suspend
validates :other_user_ids, length: { maximum: User::MAX_SIMILAR_USERS }
validates :post_action, inclusion: { in: %w[delete delete_replies edit] }, allow_blank: true
end
+
model :user
policy :not_suspended_already, class_name: User::Policy::NotAlreadySuspended
model :users
diff --git a/plugins/chat/app/services/chat/add_users_to_channel.rb b/plugins/chat/app/services/chat/add_users_to_channel.rb
index 628c999e746..1e70f1656fa 100644
--- a/plugins/chat/app/services/chat/add_users_to_channel.rb
+++ b/plugins/chat/app/services/chat/add_users_to_channel.rb
@@ -24,6 +24,7 @@ module Chat
# @option params [Array<String>] :usernames
# @option params [Array<String>] :groups
# @return [Service::Base::Context]
+
params do
attribute :usernames, :array
attribute :groups, :array
@@ -36,11 +37,13 @@ module Chat
usernames.present? || groups.present?
end
end
+
model :channel
policy :can_add_users_to_channel
model :target_users, optional: true
policy :satisfies_dms_max_users_limit,
class_name: Chat::DirectMessageChannel::Policy::MaxUsersExcess
+
transaction do
step :upsert_memberships
step :recompute_users_count
diff --git a/plugins/chat/app/services/chat/create_category_channel.rb b/plugins/chat/app/services/chat/create_category_channel.rb
index 64e8c7b3f32..e6dfa61cf8b 100644
--- a/plugins/chat/app/services/chat/create_category_channel.rb
+++ b/plugins/chat/app/services/chat/create_category_channel.rb
@@ -31,6 +31,7 @@ module Chat
policy :public_channels_enabled
policy :can_create_channel
+
params do
attribute :name, :string
attribute :description, :string
@@ -48,12 +49,15 @@ module Chat
validates :category_id, presence: true
validates :name, length: { maximum: SiteSetting.max_topic_title_length }
end
+
model :category
policy :category_channel_does_not_exist
+
transaction do
model :channel, :create_channel
model :membership, :create_membership
end
+
step :auto_join_users_if_needed
private
diff --git a/plugins/chat/app/services/chat/create_direct_message_channel.rb b/plugins/chat/app/services/chat/create_direct_message_channel.rb
index 79c28e56a15..6ea4e94475b 100644
--- a/plugins/chat/app/services/chat/create_direct_message_channel.rb
+++ b/plugins/chat/app/services/chat/create_direct_message_channel.rb
@@ -38,6 +38,7 @@ module Chat
target_usernames.present? || target_groups.present?
end
end
+
model :target_users
policy :can_create_direct_message
policy :satisfies_dms_max_users_limit,
diff --git a/plugins/chat/app/services/chat/create_message.rb b/plugins/chat/app/services/chat/create_message.rb
index 21c6ae80d77..2d4c2aad2a4 100644
--- a/plugins/chat/app/services/chat/create_message.rb
+++ b/plugins/chat/app/services/chat/create_message.rb
@@ -72,6 +72,7 @@ module Chat
policy :ensure_thread_matches_parent
model :uploads, optional: true
model :message_instance, :instantiate_message
+
transaction do
step :create_excerpt
step :update_created_by_sdk
@@ -83,6 +84,7 @@ module Chat
step :update_membership_last_read
step :process_direct_message_channel
end
+
step :publish_new_thread
step :process
step :publish_user_tracking_state
diff --git a/plugins/chat/app/services/chat/create_thread.rb b/plugins/chat/app/services/chat/create_thread.rb
index 138154c579a..889a9a71482 100644
--- a/plugins/chat/app/services/chat/create_thread.rb
+++ b/plugins/chat/app/services/chat/create_thread.rb
@@ -25,14 +25,17 @@ module Chat
validates :original_message_id, :channel_id, presence: true
validates :title, length: { maximum: Chat::Thread::MAX_TITLE_LENGTH }
end
+
model :channel
policy :can_view_channel
policy :threading_enabled_for_channel
model :original_message
+
transaction do
model :thread, :find_or_create_thread
step :associate_thread_to_message
end
+
step :publish_new_thread
step :trigger_chat_thread_created_event
diff --git a/plugins/chat/app/services/chat/flag_message.rb b/plugins/chat/app/services/chat/flag_message.rb
index cf6cfe52876..7817ca62f64 100644
--- a/plugins/chat/app/services/chat/flag_message.rb
+++ b/plugins/chat/app/services/chat/flag_message.rb
@@ -40,6 +40,7 @@ module Chat
validates :channel_id, presence: true
validates :flag_type_id, inclusion: { in: -> { ::ReviewableScore.types.values } }
end
+
model :message
policy :can_flag_message_in_channel
step :flag_message
diff --git a/plugins/chat/app/services/chat/invite_users_to_channel.rb b/plugins/chat/app/services/chat/invite_users_to_channel.rb
index 06e8c183872..61f6178e9a0 100644
--- a/plugins/chat/app/services/chat/invite_users_to_channel.rb
+++ b/plugins/chat/app/services/chat/invite_users_to_channel.rb
@@ -25,6 +25,7 @@ module Chat
validates :user_ids, presence: true
validates :channel_id, presence: true
end
+
model :channel
policy :can_view_channel
model :users, optional: true
diff --git a/plugins/chat/app/services/chat/leave_channel.rb b/plugins/chat/app/services/chat/leave_channel.rb
index 83f49017e3d..908d5e528cb 100644
--- a/plugins/chat/app/services/chat/leave_channel.rb
+++ b/plugins/chat/app/services/chat/leave_channel.rb
@@ -25,6 +25,7 @@ module Chat
validates :channel_id, presence: true
end
+
model :channel
step :leave
step :recompute_users_count
diff --git a/plugins/chat/app/services/chat/list_channel_messages.rb b/plugins/chat/app/services/chat/list_channel_messages.rb
index 391f2c8cfec..def99e82d8e 100644
--- a/plugins/chat/app/services/chat/list_channel_messages.rb
+++ b/plugins/chat/app/services/chat/list_channel_messages.rb
@@ -40,6 +40,7 @@ module Chat
},
allow_nil: true
end
+
model :channel
policy :can_view_channel
model :membership, optional: true
diff --git a/plugins/chat/app/services/chat/list_channel_thread_messages.rb b/plugins/chat/app/services/chat/list_channel_thread_messages.rb
index c3bacfa8964..4b32e1da752 100644
--- a/plugins/chat/app/services/chat/list_channel_thread_messages.rb
+++ b/plugins/chat/app/services/chat/list_channel_thread_messages.rb
@@ -41,6 +41,7 @@ module Chat
},
allow_nil: true
end
+
model :thread
policy :can_view_thread
model :membership, optional: true
diff --git a/plugins/chat/app/services/chat/lookup_channel_threads.rb b/plugins/chat/app/services/chat/lookup_channel_threads.rb
index f8c34b2c5f8..ce2e8c02452 100644
--- a/plugins/chat/app/services/chat/lookup_channel_threads.rb
+++ b/plugins/chat/app/services/chat/lookup_channel_threads.rb
@@ -43,6 +43,7 @@ module Chat
self.offset = [offset || 0, 0].max
end
end
+
model :channel
policy :threading_enabled_for_channel
policy :can_view_channel
diff --git a/plugins/chat/app/services/chat/lookup_thread.rb b/plugins/chat/app/services/chat/lookup_thread.rb
index 18738bbcef0..01b9c6eeaf4 100644
--- a/plugins/chat/app/services/chat/lookup_thread.rb
+++ b/plugins/chat/app/services/chat/lookup_thread.rb
@@ -23,6 +23,7 @@ module Chat
validates :thread_id, :channel_id, presence: true
end
+
model :thread
policy :invalid_access
policy :threading_enabled_for_channel
diff --git a/plugins/chat/app/services/chat/lookup_user_threads.rb b/plugins/chat/app/services/chat/lookup_user_threads.rb
index eb5a2c10a78..a78a41672cd 100644
--- a/plugins/chat/app/services/chat/lookup_user_threads.rb
+++ b/plugins/chat/app/services/chat/lookup_user_threads.rb
@@ -30,6 +30,7 @@ module Chat
self.offset = [offset || 0, 0].max
end
end
+
model :threads
step :fetch_tracking
step :fetch_memberships
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.rb
index 2402a23a1d9..bcf57d14b73 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
@@ -30,6 +30,7 @@ module Chat
validates :thread_id, :channel_id, presence: true
end
+
model :thread
policy :threading_enabled_for_channel
policy :can_view_channel
diff --git a/plugins/chat/app/services/chat/restore_message.rb b/plugins/chat/app/services/chat/restore_message.rb
index 65fac0aa9b8..4f171e36c18 100644
--- a/plugins/chat/app/services/chat/restore_message.rb
+++ b/plugins/chat/app/services/chat/restore_message.rb
@@ -25,13 +25,16 @@ module Chat
validates :message_id, presence: true
validates :channel_id, presence: true
end
+
model :message
policy :invalid_access
+
transaction do
step :restore_message
step :update_last_message_ids
step :update_thread_reply_cache
end
+
step :publish_events
private
diff --git a/plugins/chat/app/services/chat/search_chatable.rb b/plugins/chat/app/services/chat/search_chatable.rb
index abd6a0786a6..bc8c4b24773 100644
--- a/plugins/chat/app/services/chat/search_chatable.rb
+++ b/plugins/chat/app/services/chat/search_chatable.rb
@@ -27,6 +27,7 @@ module Chat
after_validation { self.term = term&.downcase&.strip&.gsub(/^[@#]+/, "") }
end
+
model :memberships, optional: true
model :users, optional: true
model :groups, optional: true
diff --git a/plugins/chat/app/services/chat/tracking_state.rb b/plugins/chat/app/services/chat/tracking_state.rb
index b4e0b6375bc..0bb711aa88f 100644
--- a/plugins/chat/app/services/chat/tracking_state.rb
+++ b/plugins/chat/app/services/chat/tracking_state.rb
@@ -41,6 +41,7 @@ module Chat
attribute :include_threads, default: false
attribute :include_read, default: true
end
+
model :report
private
diff --git a/plugins/chat/app/services/chat/trash_channel.rb b/plugins/chat/app/services/chat/trash_channel.rb
index 7bb88a75050..e69d341b81e 100644
--- a/plugins/chat/app/services/chat/trash_channel.rb
+++ b/plugins/chat/app/services/chat/trash_channel.rb
@@ -23,13 +23,16 @@ module Chat
validates :channel_id, presence: true
end
+
model :channel
policy :invalid_access
+
transaction do
step :prevents_slug_collision
step :soft_delete_channel
step :log_channel_deletion
end
+
step :enqueue_delete_channel_relations_job
private
diff --git a/plugins/chat/app/services/chat/trash_message.rb b/plugins/chat/app/services/chat/trash_message.rb
index ac3edc4a250..03365150f9f 100644
--- a/plugins/chat/app/services/chat/trash_message.rb
+++ b/plugins/chat/app/services/chat/trash_message.rb
@@ -25,8 +25,10 @@ module Chat
validates :message_id, presence: true
validates :channel_id, presence: true
end
+
model :message
policy :invalid_access
+
transaction do
step :trash_message
step :destroy_notifications
@@ -34,6 +36,7 @@ module Chat
step :update_tracking_state
step :update_thread_reply_cache
end
+
step :publish_events
private
diff --git a/plugins/chat/app/services/chat/trash_messages.rb b/plugins/chat/app/services/chat/trash_messages.rb
index 3f1e21aaadb..0a6bc6cbd7d 100644
--- a/plugins/chat/app/services/chat/trash_messages.rb
+++ b/plugins/chat/app/services/chat/trash_messages.rb
@@ -25,8 +25,10 @@ module Chat
validates :channel_id, presence: true
validates :message_ids, length: { minimum: 1, maximum: 200 }
end
+
model :messages
policy :can_delete_all_chat_messages
+
transaction do
step :trash_messages
step :destroy_notifications
@@ -34,6 +36,7 @@ module Chat
step :update_tracking_states
step :update_thread_reply_cache
end
+
step :publish_events
private
diff --git a/plugins/chat/app/services/chat/unfollow_channel.rb b/plugins/chat/app/services/chat/unfollow_channel.rb
index 49fddd0107d..eb979e98a98 100644
--- a/plugins/chat/app/services/chat/unfollow_channel.rb
+++ b/plugins/chat/app/services/chat/unfollow_channel.rb
@@ -25,6 +25,7 @@ module Chat
validates :channel_id, presence: true
end
+
model :channel
step :unfollow
diff --git a/plugins/chat/app/services/chat/update_channel_status.rb b/plugins/chat/app/services/chat/update_channel_status.rb
index fbd84f38e1d..1050b34e4ed 100644
--- a/plugins/chat/app/services/chat/update_channel_status.rb
+++ b/plugins/chat/app/services/chat/update_channel_status.rb
@@ -23,6 +23,7 @@ module Chat
validates :channel_id, presence: true
validates :status, inclusion: { in: Chat::Channel.editable_statuses.keys }
end
+
model :channel
policy :check_channel_permission
step :change_status
diff --git a/plugins/chat/app/services/chat/update_message.rb b/plugins/chat/app/services/chat/update_message.rb
index fc4c42f80e3..938e5c1b7a6 100644
--- a/plugins/chat/app/services/chat/update_message.rb
+++ b/plugins/chat/app/services/chat/update_message.rb
@@ -44,12 +44,14 @@ module Chat
)
end
end
+
model :message
model :uploads, optional: true
step :enforce_membership
model :membership
policy :can_modify_channel_message
policy :can_modify_message
+
transaction do
step :modify_message
step :update_excerpt
diff --git a/plugins/chat/app/services/chat/update_thread.rb b/plugins/chat/app/services/chat/update_thread.rb
index 256d57c5a97..46f88cd8b7f 100644
--- a/plugins/chat/app/services/chat/update_thread.rb
+++ b/plugins/chat/app/services/chat/update_thread.rb
@@ -26,6 +26,7 @@ module Chat
validates :thread_id, presence: true
validates :title, length: { maximum: Chat::Thread::MAX_TITLE_LENGTH }
end
+
model :thread
policy :can_view_channel
policy :can_edit_thread
diff --git a/plugins/chat/app/services/chat/update_thread_notification_settings.rb b/plugins/chat/app/services/chat/update_thread_notification_settings.rb
index 0c776877a8b..049021ed533 100644
--- a/plugins/chat/app/services/chat/update_thread_notification_settings.rb
+++ b/plugins/chat/app/services/chat/update_thread_notification_settings.rb
@@ -37,6 +37,7 @@ module Chat
in: Chat::UserChatThreadMembership.notification_levels.values,
}
end
+
model :thread, :fetch_thread
policy :can_view_channel
policy :threading_enabled_for_channel
diff --git a/plugins/chat/app/services/chat/upsert_draft.rb b/plugins/chat/app/services/chat/upsert_draft.rb
index 0beae366d74..0930808c596 100644
--- a/plugins/chat/app/services/chat/upsert_draft.rb
+++ b/plugins/chat/app/services/chat/upsert_draft.rb
@@ -31,6 +31,7 @@ module Chat
validates :channel_id, presence: true
end
+
model :channel
policy :can_upsert_draft
step :check_thread_exists
diff --git a/spec/lib/service/steps_inspector_spec.rb b/spec/lib/service/steps_inspector_spec.rb
index 142ad36c579..26a185b7836 100644
--- a/spec/lib/service/steps_inspector_spec.rb
+++ b/spec/lib/service/steps_inspector_spec.rb
@@ -11,15 +11,18 @@ RSpec.describe Service::StepsInspector do
model :model
policy :policy
+
params do
attribute :parameter
validates :parameter, presence: true
end
+
transaction do
step :in_transaction_step_1
step :in_transaction_step_2
end
+
try { step :might_raise }
step :final_step
end