2024-09-06 18:56:56 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class User::Silence
|
|
|
|
include Service::Base
|
|
|
|
|
DEV: Have `contract` take a block in services
Currently in services, the `contract` step is only used to define where
the contract will be called in the execution flow. Then, a `Contract`
class has to be defined with validations in it.
This patch allows the `contract` step to take a block containing
validations, attributes, etc. directly. No need to then open a
`Contract` class later in the service.
It also has a nice side effect, as it’s now easy to define multiples
contracts inside the same service. Before, we had the `class_name:`
option, but it wasn’t really useful as you had to redefine a complete
new contract class.
Now, when using a name for the contract other than `default`, a new
contract will be created automatically using the provided name.
Example:
```ruby
contract(:user) do
attribute :user_id, :integer
validates :user_id, presence: true
end
```
This will create a `UserContract` class and use it, also putting the
resulting contract in `context[:user_contract]`.
2024-10-01 23:17:14 +08:00
|
|
|
contract do
|
2024-09-06 18:56:56 +08:00
|
|
|
attribute :user_id, :integer
|
|
|
|
attribute :reason, :string
|
|
|
|
attribute :message, :string
|
|
|
|
attribute :silenced_till, :datetime
|
|
|
|
attribute :other_user_ids, :array
|
|
|
|
attribute :post_id, :integer
|
|
|
|
attribute :post_action, :string
|
|
|
|
attribute :post_edit, :string
|
|
|
|
|
|
|
|
validates :user_id, presence: true
|
|
|
|
validates :reason, presence: true, length: { maximum: 300 }
|
|
|
|
validates :silenced_till, presence: true
|
|
|
|
validates :other_user_ids, length: { maximum: User::MAX_SIMILAR_USERS }
|
|
|
|
validates :post_action, inclusion: { in: %w[delete delete_replies edit] }, allow_blank: true
|
|
|
|
end
|
DEV: Have `contract` take a block in services
Currently in services, the `contract` step is only used to define where
the contract will be called in the execution flow. Then, a `Contract`
class has to be defined with validations in it.
This patch allows the `contract` step to take a block containing
validations, attributes, etc. directly. No need to then open a
`Contract` class later in the service.
It also has a nice side effect, as it’s now easy to define multiples
contracts inside the same service. Before, we had the `class_name:`
option, but it wasn’t really useful as you had to redefine a complete
new contract class.
Now, when using a name for the contract other than `default`, a new
contract will be created automatically using the provided name.
Example:
```ruby
contract(:user) do
attribute :user_id, :integer
validates :user_id, presence: true
end
```
This will create a `UserContract` class and use it, also putting the
resulting contract in `context[:user_contract]`.
2024-10-01 23:17:14 +08:00
|
|
|
model :user
|
|
|
|
policy :not_silenced_already, class_name: User::Policy::NotAlreadySilenced
|
|
|
|
model :users
|
|
|
|
policy :can_silence_all_users
|
|
|
|
step :silence
|
|
|
|
model :post, optional: true
|
|
|
|
step :perform_post_action
|
2024-09-06 18:56:56 +08:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def fetch_user(contract:)
|
|
|
|
User.find_by(id: contract.user_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def fetch_users(user:, contract:)
|
|
|
|
[user, *User.where(id: contract.other_user_ids.to_a.uniq).to_a]
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_silence_all_users(guardian:, users:)
|
|
|
|
users.all? { guardian.can_silence_user?(_1) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def silence(guardian:, users:, contract:)
|
2024-09-12 21:09:10 +08:00
|
|
|
context[:full_reason] = User::Action::SilenceAll.call(users:, actor: guardian.user, contract:)
|
2024-09-06 18:56:56 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def fetch_post(contract:)
|
|
|
|
Post.find_by(id: contract.post_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def perform_post_action(guardian:, post:, contract:)
|
2024-09-12 21:09:10 +08:00
|
|
|
User::Action::TriggerPostAction.call(guardian:, post:, contract:)
|
2024-09-06 18:56:56 +08:00
|
|
|
end
|
|
|
|
end
|