Class: NewsmastMastodon::AccountBannedWorker

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::Worker
Defined in:
app/workers/newsmast_mastodon/account_banned_worker.rb

Instance Method Summary collapse

Instance Method Details

#performObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'app/workers/newsmast_mastodon/account_banned_worker.rb', line 9

def perform
  start_time = Time.current
  Rails.logger.info "Starting to check accounts against keyword filters at #{start_time}..."

  begin
    # Get all keyword filters (all types for accounts)
    keyword_filters = NewsmastMastodon::KeywordFilter.all
    # Community filters scoped to global (patchwork_community_id: nil) and filter_out
    community_keyword_filters = NewsmastMastodon::CommunityFilterKeyword.where(patchwork_community_id: nil, filter_type: 'filter_out')

    if keyword_filters.empty? && community_keyword_filters.empty?
      Rails.logger.info 'No keyword filters found. Exiting.'
      return
    end

    # Report counts for visibility
    Rails.logger.info "Found #{keyword_filters.count} keyword filters and #{community_keyword_filters.count} community keyword filters to check against"

    # Combine keywords from both sources and remove duplicates
    combined_keywords = keyword_filters.pluck(:keyword) + community_keyword_filters.pluck(:keyword)

    # Normalize, remove leading '#', downcase, strip and deduplicate using Set for O(1) lookup
    filter_keywords = Set.new(combined_keywords.compact.map { |k| k.to_s.downcase.gsub('#', '').strip })

    banned_count = 0
    error_count = 0
    processed_count = 0

    # Use select to only load necessary columns for better performance
    # Only check accounts that are not already banned
    accounts_scope = Account.where(is_banned: [false, nil])
    total_accounts = accounts_scope.size
    Rails.logger.info "Checking #{total_accounts} non-banned accounts..."

    # Iterate all non-banned accounts
    accounts_scope.find_each do ||
      processed_count += 1

      begin
         = false

        if .domain.present?
          domain_lower = .domain.downcase.strip

          # Check if any filter keyword is included in the domain (substring match)
          if filter_keywords.include?(domain_lower)
             = true
          elsif filter_keywords.any? { |keyword| domain_lower.include?(keyword) }
             = true
          end
        end

        # Check username
        if ! && .username.present?
          username_lower = .username.downcase.strip

          if filter_keywords.include?(username_lower)
             = true
          elsif filter_keywords.any? { |keyword| username_lower.include?(keyword) }
             = true
          end
        end

        # Check display_name if not already matched
        if ! && .display_name.present?
          if .display_name.include?('<') && .display_name.include?('>')
            display_name_text = ActionView::Base.full_sanitizer.sanitize(.display_name)
            display_name_lower = display_name_text.downcase.strip
          else
            display_name_lower = .display_name.downcase.strip
          end

          matched_keyword = filter_keywords.find do |keyword|
            regex = /\b#{Regexp.escape(keyword)}\b/i
            display_name_lower.match?(regex)
          end

          if matched_keyword
            Rails.logger.info "Exact word match '#{matched_keyword}' found in display_name: '#{display_name_lower}' for account '#{.id}'"
             = true
          end
        end

        # Check note if not already matched
        if ! && .note.present?
          if .note.include?('<') && .note.include?('>')
            note_text = ActionView::Base.full_sanitizer.sanitize(.note)
            note_lower = note_text.downcase.strip
          else
            note_lower = .note.downcase.strip
          end

          matched_keyword = filter_keywords.find do |keyword|
            regex = /\b#{Regexp.escape(keyword)}\b/i
            note_lower.match?(regex)
          end

          if matched_keyword
            Rails.logger.info "Exact word match '#{matched_keyword}' found in note: '#{note_lower}' for account '#{.id}'"
             = true
          end
        end

        if 
          Rails.logger.info "Found account to ban: '#{.username}' (ID: #{.id}, Display: '#{.display_name}')"

          begin
            .update(is_banned: true)

            .statuses.each do |status|
              status.update!(
                is_banned: true,
                updated_at: Time.current
              )

              if status.local?
                status.update!(
                  sensitive: true,
                  spoiler_text: 'Sensitive content!!!'
                )
              end
            end

            banned_count += 1
          rescue => e
            error_count += 1
            Rails.logger.error "Error updating account ID #{.id}: #{e.message}"
          end
        end
      rescue => e
        error_count += 1
        Rails.logger.error "Error processing account ID #{.id}: #{e.message}"
        Rails.logger.error "Error in account banned worker for account #{.id}: #{e.message}\n#{e.backtrace.join("\n")}"
      end

      if (processed_count % 1000).zero?
        Rails.logger.info "Processed #{processed_count}/#{total_accounts} accounts (#{(processed_count.to_f / total_accounts * 100).round(2)}%)"
      end
    end

    end_time = Time.current
    duration = (end_time - start_time).round(2)

    Rails.logger.info "\n" + '=' * 50
    Rails.logger.info 'SUMMARY:'
    Rails.logger.info "Total accounts processed: #{processed_count}"
    Rails.logger.info "Accounts banned: #{banned_count}"
    Rails.logger.info "Errors encountered: #{error_count}"
    Rails.logger.info "Duration: #{duration} seconds"
    Rails.logger.info "Average: #{(processed_count.to_f / duration).round(2)} accounts/second"
    Rails.logger.info '=' * 50
  rescue => e
    Rails.logger.error "Fatal error in account banned worker: #{e.message}"
    Rails.logger.error "Fatal error in account banned worker: #{e.message}\n#{e.backtrace.join("\n")}"
    raise
  end
end