Skip to content

Instantly share code, notes, and snippets.

@sammy8806
Last active October 9, 2024 08:09
Show Gist options
  • Save sammy8806/c58816a69bf6074ffaace1aa1ddb480f to your computer and use it in GitHub Desktop.
Save sammy8806/c58816a69bf6074ffaace1aa1ddb480f to your computer and use it in GitHub Desktop.

For every Mastodon Admin, that wants to ban a big wave of users. (f. ex. spam wave).

Here a dump of commands for you.

Find Accounts in DB

Modify it for your need and execute in your DB

SELECT DISTINCT ON (a.domain, a.username) a.domain, a.username, s.uri, s.text, s.created_at, a.suspended_at
FROM statuses_tags t
JOIN statuses s on s.id = t.status_id
JOIN tags on t.tag_id = tags.id
JOIN public.accounts a on s.account_id = a.id
WHERE tags.display_name IN('as215935', '鈴木哲哉', '唐澤貴洋')
AND length(a.username) = 10
AND a.suspended_at is null
ORDER BY a.domain, a.username

Open Rails Console

DB_PORT=5432 RAILS_ENV=production bundle exec rails c

Define yourself the ban method:

def ban_csv(csv_data)
  CSV.parse(csv_data, headers: true) do |row|
    account_name = row['account']
    domain_name = row['domain']

    full_account = "#{account_name}@#{domain_name}"
    puts "Suspending account: #{full_account}"

    user_account = Account.find_by username: account_name, domain: domain_name
    if user_account
      if user_account.suspended?
        puts "Account #{full_account} ist bereits suspendiert"
      else
        # Benutzer suspendieren, falls vorhanden
        user_account.suspend!
        puts "Account #{full_account} wurde erfolgreich suspendiert."
      end
    else
      puts "Account #{full_account} wurde nicht gefunden."
    end
  end
end

Now grab the first two columns, build a list and suspend them

csv_data = <<ENO
domain,account
some.domain,vol9ktomk2
some.other.domain,wgcssayzhs
ENO

ban_csv csv_data
@verymilan
Copy link

verymilan commented Oct 9, 2024

Quick nooby note: I'd prefer to directly parse from psql output, so I've changed the following:

  • wrapping the postgres query into: Copy (spamquery) To '/tmp/spamusers.csv' With CSV DELIMITER ',' HEADER; (this seems not to work in the mastodon postgres user directly, unless permissions are elevated)
  • account_name = row['account'] -> account_name = row['username']
  • csv_data = <<ENO ... is skipped and replaced by:
    • file = File.open("/tmp/spamusers.csv", "r")
  • function is now called like this: ban_csv file

Other than that, thank you, thats quite some help. Need something like this for other federated services^^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment