From 3b276d268f8e49b897f16f490c78a5a5880c6216 Mon Sep 17 00:00:00 2001 From: multiple creatures Date: Fri, 30 Aug 2019 22:03:46 -0500 Subject: use a scheduled task to batch domain policy changes + revert `29643fd6` to avoid accidentally dosing servers when multiple users block a domain --- app/controllers/admin/domain_blocks_controller.rb | 5 ++--- app/controllers/api/v1/domain_blocks_controller.rb | 1 - app/helpers/moderation_helper.rb | 1 - app/models/domain_block.rb | 2 ++ app/models/status.rb | 1 + app/services/activitypub/process_account_service.rb | 6 ------ .../after_block_domain_from_account_service.rb | 7 ------- .../after_unblock_domain_from_account_service.rb | 18 ------------------ app/services/block_domain_service.rb | 2 ++ app/services/update_account_domain_blocks_service.rb | 19 ------------------- app/workers/after_account_domain_unblock_worker.rb | 11 ----------- app/workers/scheduler/domain_policy_scheduler.rb | 11 +++++++++++ app/workers/scheduler/janitor_scheduler.rb | 1 - app/workers/update_account_domain_blocks_worker.rb | 11 ----------- config/sidekiq.yml | 3 +++ .../20190831022432_add_processing_to_domain_blocks.rb | 5 +++++ db/schema.rb | 3 ++- 17 files changed, 28 insertions(+), 79 deletions(-) delete mode 100644 app/services/after_unblock_domain_from_account_service.rb delete mode 100644 app/services/update_account_domain_blocks_service.rb delete mode 100644 app/workers/after_account_domain_unblock_worker.rb create mode 100644 app/workers/scheduler/domain_policy_scheduler.rb delete mode 100644 app/workers/update_account_domain_blocks_worker.rb create mode 100644 db/migrate/20190831022432_add_processing_to_domain_blocks.rb diff --git a/app/controllers/admin/domain_blocks_controller.rb b/app/controllers/admin/domain_blocks_controller.rb index 587c2c8c2..593b91455 100644 --- a/app/controllers/admin/domain_blocks_controller.rb +++ b/app/controllers/admin/domain_blocks_controller.rb @@ -14,6 +14,7 @@ module Admin resource_params[:domain].strip! if resource_params[:domain].present? resource_params[:reason].strip! if resource_params[:reason].present? + resource_pararms[:processing] = true @domain_block = DomainBlock.new(resource_params) existing_domain_block = resource_params[:domain].present? ? DomainBlock.find_by(domain: resource_params[:domain].strip) : nil @@ -23,7 +24,6 @@ module Admin end if @domain_block.save - DomainBlockWorker.perform_async(@domain_block.id) log_action :create, @domain_block redirect_to admin_instance_path(id: @domain_block.domain, limited: '1'), notice: I18n.t('admin.domain_blocks.created_msg') else @@ -46,11 +46,10 @@ module Admin def update return destroy unless resource_params[:undo].to_i.zero? resource_params[:reason].strip! if resource_params[:reason].present? + resource_pararms[:processing] = true authorize @domain_block, :update? @domain_block.update(resource_params.except(:domain, :undo)) - changed = @domain_block.changed if @domain_block.save - DomainBlockWorker.perform_async(@domain_block.id) if (changed & %w(severity force_sensitive reject_media reject_unknown)).any? log_action :update, @domain_block flash[:notice] = I18n.t('admin.domain_blocks.updated_msg') else diff --git a/app/controllers/api/v1/domain_blocks_controller.rb b/app/controllers/api/v1/domain_blocks_controller.rb index 21c9d3aae..af9e7a20f 100644 --- a/app/controllers/api/v1/domain_blocks_controller.rb +++ b/app/controllers/api/v1/domain_blocks_controller.rb @@ -23,7 +23,6 @@ class Api::V1::DomainBlocksController < Api::BaseController def destroy current_account.unblock_domain!(domain_block_params[:domain]) - AfterAccountDomainUnblockWorker.perform_async(current_account.id, domain_block_params[:domain]) render_empty end diff --git a/app/helpers/moderation_helper.rb b/app/helpers/moderation_helper.rb index 2f1561772..9d12dc858 100644 --- a/app/helpers/moderation_helper.rb +++ b/app/helpers/moderation_helper.rb @@ -110,7 +110,6 @@ module ModerationHelper Admin::ActionLog.create(account: @account, action: :create, target: domain_block) user_friendly_action_log(@account, :create, domain_block) - DomainBlockWorker.perform_async(domain_block.id) else domain_block = DomainBlock.find_by(domain: domain) return false if domain_block.nil? diff --git a/app/models/domain_block.rb b/app/models/domain_block.rb index 7d3b65720..478095cb0 100644 --- a/app/models/domain_block.rb +++ b/app/models/domain_block.rb @@ -13,6 +13,7 @@ # force_sensitive :boolean default(FALSE), not null # reason :text # reject_unknown :boolean default(FALSE), not null +# processing :boolean default(TRUE), not null # class DomainBlock < ApplicationRecord @@ -26,6 +27,7 @@ class DomainBlock < ApplicationRecord delegate :count, to: :accounts, prefix: true scope :matches_domain, ->(value) { where(arel_table[:domain].matches("%#{value}%")) } + scope :unprocessed, -> { where(processing: true) } def self.blocked?(domain) where(domain: domain, severity: :suspend).exists? diff --git a/app/models/status.rb b/app/models/status.rb index 0be747b00..afd25c808 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -31,6 +31,7 @@ # edited :boolean # imported :boolean # origin :string +# tsv :tsvector # boostable :boolean # reject_replies :boolean # diff --git a/app/services/activitypub/process_account_service.rb b/app/services/activitypub/process_account_service.rb index dd19db485..c1c643ff0 100644 --- a/app/services/activitypub/process_account_service.rb +++ b/app/services/activitypub/process_account_service.rb @@ -27,7 +27,6 @@ class ActivityPub::ProcessAccountService < BaseService create_account end update_account - update_account_domain_blocks if is_new_account process_tags process_attachments else @@ -121,11 +120,6 @@ class ActivityPub::ProcessAccountService < BaseService VerifyAccountLinksWorker.perform_async(@account.id) end - def update_account_domain_blocks - return if @account.domain.nil? || @account.local? - UpdateAccountDomainBlocksWorker.perform_async(@account.id) - end - def actor_type if @json['type'].is_a?(Array) @json['type'].find { |type| ActivityPub::FetchRemoteAccountService::SUPPORTED_TYPES.include?(type) } diff --git a/app/services/after_block_domain_from_account_service.rb b/app/services/after_block_domain_from_account_service.rb index 762a8baea..f12f18319 100644 --- a/app/services/after_block_domain_from_account_service.rb +++ b/app/services/after_block_domain_from_account_service.rb @@ -10,7 +10,6 @@ class AfterBlockDomainFromAccountService < BaseService reject_existing_followers! reject_pending_follow_requests! - block_accounts! end private @@ -27,12 +26,6 @@ class AfterBlockDomainFromAccountService < BaseService end end - def block_accounts! - Account.where(domain: @domain).find_each do |blocked_account| - BlockService.new.call(@account, blocked_account) - end - end - def reject_follow!(follow) follow.destroy diff --git a/app/services/after_unblock_domain_from_account_service.rb b/app/services/after_unblock_domain_from_account_service.rb deleted file mode 100644 index a2d10bcf0..000000000 --- a/app/services/after_unblock_domain_from_account_service.rb +++ /dev/null @@ -1,18 +0,0 @@ -# frozen_string_literal: true - -class AfterUnblockDomainFromAccountService < BaseService - def call(account, domain) - @account = account - @domain = domain - - unblock_accounts! - end - - private - - def unblock_accounts! - @account.blocking.where(domain: @domain).find_each do |blocked_account| - UnblockService.new.call(@account, blocked_account) - end - end -end diff --git a/app/services/block_domain_service.rb b/app/services/block_domain_service.rb index 5a6f4a6ce..3fd55cf3b 100644 --- a/app/services/block_domain_service.rb +++ b/app/services/block_domain_service.rb @@ -10,6 +10,8 @@ class BlockDomainService < BaseService remove_existing_block! process_domain_block! invalidate_association_caches! + + @domain_block.update(processing: false) end private diff --git a/app/services/update_account_domain_blocks_service.rb b/app/services/update_account_domain_blocks_service.rb deleted file mode 100644 index d14cdbdda..000000000 --- a/app/services/update_account_domain_blocks_service.rb +++ /dev/null @@ -1,19 +0,0 @@ -# frozen_string_literal: true - -class UpdateAccountDomainBlocksService < BaseService - def call(account) - @account = account - @domain = account.domain - - block_where_domain_blocked! - end - - private - - def block_where_domain_blocked! - account_ids = AccountDomainBlock.distinct.where(domain: @domain).pluck(:account_id) - Account.where(id: account_ids).find_each do |blocked_by| - BlockService.new.call(blocked_by, @account) - end - end -end diff --git a/app/workers/after_account_domain_unblock_worker.rb b/app/workers/after_account_domain_unblock_worker.rb deleted file mode 100644 index 6268f0819..000000000 --- a/app/workers/after_account_domain_unblock_worker.rb +++ /dev/null @@ -1,11 +0,0 @@ -# frozen_string_literal: true - -class AfterAccountDomainUnblockWorker - include Sidekiq::Worker - - def perform(account_id, domain) - AfterUnblockDomainFromAccountService.new.call(Account.find(account_id), domain) - rescue ActiveRecord::RecordNotFound - true - end -end diff --git a/app/workers/scheduler/domain_policy_scheduler.rb b/app/workers/scheduler/domain_policy_scheduler.rb new file mode 100644 index 000000000..c7c0b5e62 --- /dev/null +++ b/app/workers/scheduler/domain_policy_scheduler.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +class Scheduler::DomainPolicyScheduler + include Sidekiq::Worker + + sidekiq_options unique: :until_executed + + def perform + DomainBlock.unprocessed.find_each { |policy| BlockDomainService.new.call(policy) } + end +end diff --git a/app/workers/scheduler/janitor_scheduler.rb b/app/workers/scheduler/janitor_scheduler.rb index 6a386b30a..3411ec26b 100644 --- a/app/workers/scheduler/janitor_scheduler.rb +++ b/app/workers/scheduler/janitor_scheduler.rb @@ -59,7 +59,6 @@ class Scheduler::JanitorScheduler blocks.each do |entry| next unless domain_exists?(entry[:domain]) block = DomainBlock.create!(entry) - DomainBlockWorker.perform_async(block) Admin::ActionLog.create(account: @account, action: :create, target: block) user_friendly_action_log(@account, :create, block) end diff --git a/app/workers/update_account_domain_blocks_worker.rb b/app/workers/update_account_domain_blocks_worker.rb deleted file mode 100644 index 14d4e8516..000000000 --- a/app/workers/update_account_domain_blocks_worker.rb +++ /dev/null @@ -1,11 +0,0 @@ -# frozen_string_literal: true - -class UpdateAccountDomainBlocksWorker - include Sidekiq::Worker - - def perform(account_id) - UpdateAccountDomainBlocksService.new.call(Account.find(account_id)) - rescue ActiveRecord::RecordNotFound - true - end -end diff --git a/config/sidekiq.yml b/config/sidekiq.yml index 82cc6cce7..145106f0f 100644 --- a/config/sidekiq.yml +++ b/config/sidekiq.yml @@ -15,6 +15,9 @@ boosts_scheduler: every: '1m' class: Scheduler::BoostsScheduler + domain_policy_scheduler: + every: '15m' + class: Scheduler::DomainPolicyScheduler janitor_scheduler: every: '1h' class: Scheduler::JanitorScheduler diff --git a/db/migrate/20190831022432_add_processing_to_domain_blocks.rb b/db/migrate/20190831022432_add_processing_to_domain_blocks.rb new file mode 100644 index 000000000..1a3b3c89c --- /dev/null +++ b/db/migrate/20190831022432_add_processing_to_domain_blocks.rb @@ -0,0 +1,5 @@ +class AddProcessingToDomainBlocks < ActiveRecord::Migration[5.2] + def change + safety_assured { add_column :domain_blocks, :processing, :boolean, null: false, default: true } + end +end diff --git a/db/schema.rb b/db/schema.rb index 16b284b58..4a5115285 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2019_08_15_232125) do +ActiveRecord::Schema.define(version: 2019_08_31_022432) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -278,6 +278,7 @@ ActiveRecord::Schema.define(version: 2019_08_15_232125) do t.boolean "force_sensitive", default: false, null: false t.text "reason" t.boolean "reject_unknown", default: false, null: false + t.boolean "processing", default: true, null: false t.index ["domain"], name: "index_domain_blocks_on_domain", unique: true end -- cgit