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
keyword_filters = NewsmastMastodon::KeywordFilter.all
= NewsmastMastodon::CommunityFilterKeyword.where(patchwork_community_id: nil, filter_type: 'filter_out')
if keyword_filters.empty? && .empty?
Rails.logger.info 'No keyword filters found. Exiting.'
return
end
Rails.logger.info "Found #{keyword_filters.count} keyword filters and #{.count} community keyword filters to check against"
combined_keywords = keyword_filters.pluck(:keyword) + .pluck(:keyword)
filter_keywords = Set.new(combined_keywords.compact.map { |k| k.to_s.downcase.gsub('#', '').strip })
banned_count = 0
error_count = 0
processed_count = 0
accounts_scope = Account.where(is_banned: [false, nil])
total_accounts = accounts_scope.size
Rails.logger.info "Checking #{total_accounts} non-banned accounts..."
accounts_scope.find_each do |account|
processed_count += 1
begin
account_matched = false
if account.domain.present?
domain_lower = account.domain.downcase.strip
if filter_keywords.include?(domain_lower)
account_matched = true
elsif filter_keywords.any? { |keyword| domain_lower.include?(keyword) }
account_matched = true
end
end
if !account_matched && account.username.present?
username_lower = account.username.downcase.strip
if filter_keywords.include?(username_lower)
account_matched = true
elsif filter_keywords.any? { |keyword| username_lower.include?(keyword) }
account_matched = true
end
end
if !account_matched && account.display_name.present?
if account.display_name.include?('<') && account.display_name.include?('>')
display_name_text = ActionView::Base.full_sanitizer.sanitize(account.display_name)
display_name_lower = display_name_text.downcase.strip
else
display_name_lower = account.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 '#{account.id}'"
account_matched = true
end
end
if !account_matched && account.note.present?
if account.note.include?('<') && account.note.include?('>')
note_text = ActionView::Base.full_sanitizer.sanitize(account.note)
note_lower = note_text.downcase.strip
else
note_lower = account.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 '#{account.id}'"
account_matched = true
end
end
if account_matched
Rails.logger.info "Found account to ban: '#{account.username}' (ID: #{account.id}, Display: '#{account.display_name}')"
begin
account.update(is_banned: true)
account.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 #{account.id}: #{e.message}"
end
end
rescue => e
error_count += 1
Rails.logger.error "Error processing account ID #{account.id}: #{e.message}"
Rails.logger.error "Error in account banned worker for account #{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
|