Commit 3fe91f9e authored by Jyrki's avatar Jyrki 🐾

Add SpambotValidator log view to admin panel

parent 883d9c83
# frozen_string_literal: true
module Admin
class SpambotFilterController < BaseController
def index
@filter_logs = find_filter_logs
end
private
FilterLog = Struct.new(:time, :reasons, :account) do
@account_cache = {}
def self.from_log_record(log_record)
timestamp_s, reasons_s, account_id_s = log_record.split('$$')
new(Time.at(timestamp_s.to_f).utc,
reasons_s.split(',').map(&:to_sym),
@account_cache[account_id_s.to_i] ||= begin
Account.find(account_id_s.to_i)
rescue ActiveRecord::RecordNotFound
account_id_s.to_i
end)
end
end
def find_filter_logs
Redis.current.lrange(SpambotValidator::REDIS_LOG_KEY, 0, -1).map do |raw_log_record|
FilterLog.from_log_record(raw_log_record)
end.sort_by(&:time).reverse
end
end
end
# frozen_string_literal: true
class SpambotValidator < ActiveModel::Validator
FILTER_MATCHERS = {
things_i_hate: /Things I hate: feminism, gays, blacks/.freeze,
was_blog_link: %r{(?:<a href=")?https?://womenare(?:stupid|dumb).site/blog/}.freeze,
}.freeze
REDIS_LOG_KEY = "nilsding:spambotvalidator:log"
def validate(status)
# allow local spammers and potential reblogs
return if status.local? || status.reblog?
return if (status.local? && !Rails.env.development?) || status.reblog?
@status = status
......@@ -16,11 +22,6 @@ class SpambotValidator < ActiveModel::Validator
private
FILTER_MATCHERS = {
things_i_hate: /Things I hate: feminism, gays, blacks/.freeze,
was_blog_link: %r{<a href="https?://womenare(?:stupid|dumb).site/blog/}.freeze
}.freeze
def filter_reasons
@filter_reasons ||= [].tap do |reasons|
FILTER_MATCHERS.each do |reason, regexp|
......@@ -28,6 +29,7 @@ class SpambotValidator < ActiveModel::Validator
end
next if reasons.empty?
Redis.current.lpush(REDIS_LOG_KEY, "#{Time.now.utc.to_f}$$#{reasons.join(',')}$$#{@status.account_id}")
end
end
......
- content_for :page_title do
SpambotValidator log
.flash-message.alert
%strong This is not part of vanilla Mastodon.
It only exists for
- acc = Account.where(username: "nilsding", domain: nil).first
- acc ||= Account.first
= admin_account_inline_link_to(acc)
to analyse stuffz.
%p
This is just a log of all accounts + reasons why a status was filtered.
%br
The newest record is on top.
%p
%details
%summary
At the moment there are #{SpambotValidator::FILTER_MATCHERS.size} reasons present. (click to expand)
.table-wrapper
%table.table
%thead
%tr
%th Reason
%th
%th Regexp
- SpambotValidator::FILTER_MATCHERS.each do |reason, regexp|
%tr
%td
%code= reason.inspect
%td= "=>"
%td
%code= regexp.inspect
New reasons can be added by modifying the
%code FILTER_MATCHERS
constant inside
= succeed "." do
%code /app/validators/spambot_validator.rb
%p
There are
%strong= @filter_logs.size
log records.
- if @filter_logs.size.zero?
Nice!
.table-wrapper
%table.table
%thead
%tr
%th Account
%th Timestamp
%th Reasons
%tbody
- @filter_logs.each do |log|
%tr
%td
- if log.account.is_a?(Account)
= admin_account_link_to(log.account)
- else
ID ##{log.account}
%td= log.time
%td= log.reasons.join(", ")
......@@ -51,6 +51,8 @@ SimpleNavigation::Configuration.run do |navigation|
s.item :subscriptions, safe_join([fa_icon('paper-plane-o fw'), t('admin.subscriptions.title')]), admin_subscriptions_url, if: -> { current_user.admin? }
s.item :sidekiq, safe_join([fa_icon('diamond fw'), 'Sidekiq']), sidekiq_url, link_html: { target: 'sidekiq' }, if: -> { current_user.admin? }
s.item :pghero, safe_join([fa_icon('database fw'), 'PgHero']), pghero_url, link_html: { target: 'pghero' }, if: -> { current_user.admin? }
s.item :spambot_filter, safe_join([fa_icon('hard-of-hearing fw'), 'SpambotValidator log']), admin_spambot_filter_index_url
end
n.item :logout, safe_join([fa_icon('sign-out fw'), t('auth.logout')]), destroy_user_session_url, link_html: { 'data-method' => 'delete' }
......
......@@ -252,6 +252,8 @@ Rails.application.routes.draw do
post :unhide
end
end
resources :spambot_filter, only: [:index]
end
get '/admin', to: redirect('/admin/dashboard', status: 302)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment