From a2b9ac9a481ba5b7636b981132f4b127841f1af3 Mon Sep 17 00:00:00 2001 From: multiple creatures Date: Sat, 3 Aug 2019 06:30:54 -0500 Subject: rename `BangtagHelper` to `ModerationHelper` --- app/helpers/bangtag_helper.rb | 114 ----------------------------- app/helpers/moderation_helper.rb | 114 +++++++++++++++++++++++++++++ app/lib/bangtags.rb | 2 +- app/workers/scheduler/janitor_scheduler.rb | 2 +- 4 files changed, 116 insertions(+), 116 deletions(-) delete mode 100644 app/helpers/bangtag_helper.rb create mode 100644 app/helpers/moderation_helper.rb diff --git a/app/helpers/bangtag_helper.rb b/app/helpers/bangtag_helper.rb deleted file mode 100644 index 248f26113..000000000 --- a/app/helpers/bangtag_helper.rb +++ /dev/null @@ -1,114 +0,0 @@ -module BangtagHelper - include LogHelper - - POLICIES = %w(silence unsilence suspend unsuspend force_unlisted allow_public force_sensitive allow_nonsensitive reset) - EXCLUDED_DOMAINS = %w(tailma.ws monsterpit.net monsterpit.cloud monsterpit.gallery monsterpit.blog) - - def account_policy(username, domain, policy, reason = nil) - return if policy.blank? - policy = policy.to_s - return false unless policy.in?(POLICIES) - - username, domain = username.split('@')[1..2] if username.start_with?('@') - domain.downcase! unless domain.nil? - - acct = Account.find_by(username: username, domain: domain) - return false if acct.nil? - - if policy == 'reset' - Admin::ActionLog.create(account: @account, action: unsuspend, target: acct) - user_friendly_action_log(@account, :unsuspend, acct) - else - Admin::ActionLog.create(account: @account, action: policy, target: acct) - user_friendly_action_log(@account, policy.to_sym, acct) - end - - case policy - when 'silence' - acct.silence! - when 'unsilence' - acct.unsilence! - when 'suspend' - SuspendAccountService.new.call(acct, include_user: true) - return true - when 'unsuspend' - acct.unsuspend! - when 'force_unlisted' - acct.force_unlisted - when 'allow_public' - acct.allow_public! - when 'force_sensitive' - acct.force_sensitive! - when 'allow_nonsensitive' - acct.allow_nonsensitive! - when 'reset' - acct.unsuspend! - acct.unsilence! - acct.allow_public! - acct.allow_nonsensitive! - end - - acct.save - - return true unless reason && !reason.strip.blank? - - AccountModerationNote.create( - account_id: @account.id, - target_account_id: acct.id, - content: reason.strip - ) - - true - end - - def domain_exists(domain) - begin - code = Request.new(:head, "https://#{domain}").perform(&:code) - rescue - return false - end - return false if [404, 410].include?(code) - true - end - - def domain_policy(domain, policy, reason = nil, force_sensitive = false, reject_media = false, reject_reports = false) - return if policy.blank? - policy = policy.to_s - return false unless policy.in?(POLICIES) - return false unless domain.match?(/\A[\w\-]+\.[\w\-]+(?:\.[\w\-]+)*\Z/) - - domain.downcase! - - return false if domain.in?(EXCLUDED_DOMAINS) - - if policy == 'force_sensitive' - policy = 'noop' - force_sensitive = true - end - - if policy.in? %w(silence suspend force_unlisted) - return false unless domain_exists(domain) - - domain_block = DomainBlock.find_or_create_by(domain: domain) - domain_block.severity = policy - domain_block.force_sensitive = force_sensitive - domain_block.reject_media = reject_media - domain_block.reject_reports = reject_reports - domain_block.reason = reason.strip if reason && !reason.strip.blank? - domain_block.save - - 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? - - Admin::ActionLog.create(account: @account, action: :destroy, target: domain_block) - user_friendly_action_log(@account, :destroy, domain_block) - DomainUnblockWorker.perform_async(domain_block.id) - end - - true - end -end diff --git a/app/helpers/moderation_helper.rb b/app/helpers/moderation_helper.rb new file mode 100644 index 000000000..13966405e --- /dev/null +++ b/app/helpers/moderation_helper.rb @@ -0,0 +1,114 @@ +module ModerationHelper + include LogHelper + + POLICIES = %w(silence unsilence suspend unsuspend force_unlisted allow_public force_sensitive allow_nonsensitive reset) + EXCLUDED_DOMAINS = %w(tailma.ws monsterpit.net monsterpit.cloud monsterpit.gallery monsterpit.blog) + + def account_policy(username, domain, policy, reason = nil) + return if policy.blank? + policy = policy.to_s + return false unless policy.in?(POLICIES) + + username, domain = username.split('@')[1..2] if username.start_with?('@') + domain.downcase! unless domain.nil? + + acct = Account.find_by(username: username, domain: domain) + return false if acct.nil? + + if policy == 'reset' + Admin::ActionLog.create(account: @account, action: unsuspend, target: acct) + user_friendly_action_log(@account, :unsuspend, acct) + else + Admin::ActionLog.create(account: @account, action: policy, target: acct) + user_friendly_action_log(@account, policy.to_sym, acct) + end + + case policy + when 'silence' + acct.silence! + when 'unsilence' + acct.unsilence! + when 'suspend' + SuspendAccountService.new.call(acct, include_user: true) + return true + when 'unsuspend' + acct.unsuspend! + when 'force_unlisted' + acct.force_unlisted + when 'allow_public' + acct.allow_public! + when 'force_sensitive' + acct.force_sensitive! + when 'allow_nonsensitive' + acct.allow_nonsensitive! + when 'reset' + acct.unsuspend! + acct.unsilence! + acct.allow_public! + acct.allow_nonsensitive! + end + + acct.save + + return true unless reason && !reason.strip.blank? + + AccountModerationNote.create( + account_id: @account.id, + target_account_id: acct.id, + content: reason.strip + ) + + true + end + + def domain_exists(domain) + begin + code = Request.new(:head, "https://#{domain}").perform(&:code) + rescue + return false + end + return false if [404, 410].include?(code) + true + end + + def domain_policy(domain, policy, reason = nil, force_sensitive = false, reject_media = false, reject_reports = false) + return if policy.blank? + policy = policy.to_s + return false unless policy.in?(POLICIES) + return false unless domain.match?(/\A[\w\-]+\.[\w\-]+(?:\.[\w\-]+)*\Z/) + + domain.downcase! + + return false if domain.in?(EXCLUDED_DOMAINS) + + if policy == 'force_sensitive' + policy = 'noop' + force_sensitive = true + end + + if policy.in? %w(silence suspend force_unlisted) + return false unless domain_exists(domain) + + domain_block = DomainBlock.find_or_create_by(domain: domain) + domain_block.severity = policy + domain_block.force_sensitive = force_sensitive + domain_block.reject_media = reject_media + domain_block.reject_reports = reject_reports + domain_block.reason = reason.strip if reason && !reason.strip.blank? + domain_block.save + + 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? + + Admin::ActionLog.create(account: @account, action: :destroy, target: domain_block) + user_friendly_action_log(@account, :destroy, domain_block) + DomainUnblockWorker.perform_async(domain_block.id) + end + + true + end +end diff --git a/app/lib/bangtags.rb b/app/lib/bangtags.rb index e859b8f1b..6946bcf9e 100644 --- a/app/lib/bangtags.rb +++ b/app/lib/bangtags.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true class Bangtags - include BangtagHelper + include ModerationHelper attr_reader :status, :account def initialize(status) diff --git a/app/workers/scheduler/janitor_scheduler.rb b/app/workers/scheduler/janitor_scheduler.rb index 844e08638..97e11ddf2 100644 --- a/app/workers/scheduler/janitor_scheduler.rb +++ b/app/workers/scheduler/janitor_scheduler.rb @@ -3,7 +3,7 @@ class Scheduler::JanitorScheduler include Sidekiq::Worker include BlocklistHelper - include BangtagHelper + include ModerationHelper MIN_POSTS = 6 -- cgit