Moderation API

From neuromatch

Up to: Moderation

Moderating with the mastodon API!

Some bulk moderation actions aren't available in the web interface, so we can use lil scripts to handle them.

Note: Do not leave these credentials lying around, you usually want to delete them after you are finished using them unless there is a need to persistent moderation. Only request the access scopes that you need for a given action

I am going to demo these using Mastodon.py but the general idea is transportable across different api interfaces.

Creating an App

API access needs an app, so creating one comes first! This happens in two steps: first we request generic app credentials with a set of requested scopes, and then we have to log in with an account that has access to those scopes.

In this case we'll request the admin:read:accounts and admin:write:accounts scopes - see the rest of the scopes here. We save the creds in both cases to files for reuse, and then will delete them later.

from mastodon import Mastodon

scopes = [
  'read', 'write', 'follow', 'push', 
  'admin:read:accounts', 'admin:write:accounts'
]

# create app
client_id, client_secret = Mastodon.create_app(
  'manual_api', 
  api_base_url='https://neuromatch.social', 
  to_file='tmp_masto_app.secret', 
  scopes=scopes
)

# instantiate api object
masto = Mastodon('tmp_masto_app.secret')

# log in!
access_token = masto.log_in(
  'my@email.example.com', 
  'MYPASSWORD', 
  to_file='tmp_masto_app_user.secret', 
  scopes=scopes
)

Examples

Bulk Unmute

Apparently, when you un-silence an instance, masto doesn't un-silence all the users on that instance. To fix that, we can do a bulk unsilence!

In this case, we are unmuting all the users from qoto.org

# give ourselves a lil progress bar as a treat
from tqdm import tqdm

# Get the first page of 100 silenced accounts 
qoto = masto.admin_accounts_v2(
  by_domain='qoto.org', 
  status='silenced'
)

# Get the rest of the silenced accounts (which also includes the first page)
qoto = masto.fetch_remaining(qoto)

# iterate through each account, unsilencing
for account in tqdm(qoto):
    masto.admin_account_unsilence(account['id'])

## or if you don't have tqdm
# for account in qoto:
#    masto.admin_account_unsilence(account['id'])

and there you have it!

References