implement opt-out

This commit is contained in:
Kay Faraday 2023-02-06 08:20:34 +00:00
parent 872d7f4827
commit 40fad78407
6 changed files with 102 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*.toml
!*.example.toml

View File

@ -9,6 +9,20 @@ This is a simple database-based block bot for Mastodon.
Database admin is required to create the Python extension and function.
For example: `sudo -u postgres psql mastodon < blockbot.sql`.
3. Insert a row into blockbot_config with your Mastodon hostname and a token for the account you want the blockbot to use. `INSERT INTO blockbot_config (hostname, token) VALUES ('freak.university', '...');`
### Opter outer
This is a python fedi bot that processes opt out/opt in requests.
By default, people are opted in, and they can run @blockbot opt out to opt out.
1. Copy opter_outer.example.toml to opter_outer.toml
2. Edit the `db` section as needed.
3. `python -m venv .venv`
4. `. .venv/bin/activate`
5. `pip install -r requirements.txt`
Then set your service manager (such as systemd) to run `.venv/bin/python opter_outer.py`.
## License
AGPLv3, see LICENSE.md.

View File

@ -5,6 +5,10 @@ CREATE TABLE blockbot_config (
token TEXT
);
CREATE TABLE blockbot_opt_out (
account_id BIGINT
);
CREATE FUNCTION blockbot_notify(
blocker TEXT,
blocked TEXT,
@ -47,6 +51,11 @@ SELECT blockbot_notify(
unblock,
(SELECT hostname FROM blockbot_config),
(SELECT token FROM blockbot_config)
)
WHERE blocker NOT IN (
SELECT blocker
FROM blockbot_opt_out
WHERE account_id = $1
);
$$ LANGUAGE SQL;

6
opter_outer.example.toml Normal file
View File

@ -0,0 +1,6 @@
[db]
# Accepts all parameters accepted by https://magicstack.github.io/asyncpg/current/api/index.html#connection.
# Or leave this section blank to use the defaults.
username='mastodon'
password='...'
database='...'

68
opter_outer.py Executable file
View File

@ -0,0 +1,68 @@
#!/usr/bin/env python
from pleroma import Pleroma
import qtoml as toml
import asyncpg
import anyio
with open('opter_outer.toml') as f:
config = toml.load(f)
async def main():
db = await asyncpg.connect(**config['db'])
hostname, access_token = await db.fetchrow('SELECT hostname, token FROM blockbot_config')
async with (
Pleroma(
api_base_url='https://' + hostname,
access_token=access_token,
) as pleroma,
anyio.create_task_group() as nursery,
):
async for notif in pleroma.stream_notifications():
if 'opt out' in notif['status']['content']:
nursery.start_soon(opt_out, db, pleroma, notif['status'])
elif 'opt in' in notif['status']['content']:
nursery.start_soon(opt_in, db, pleroma, notif['status'])
elif 'status' in notif['status']['content']:
nursery.start_soon(status, db, pleroma, notif['status'])
elif 'help' in notif['status']['content']:
nursery.start_soon(help, db, pleroma, notif['status'])
async def opt_out(db, pleroma, status):
await db.execute(
'INSERT INTO blockbot_opt_out (account_id) VALUES ($1)',
int(status['account']['id']),
)
await pleroma.reply(
status,
'You have successfully opted out. Your blocks will no longer be sent to the people you block.',
)
async def opt_in(db, pleroma, status):
await db.execute(
'DELETE FROM blockbot_opt_out WHERE account_id = $1',
int(status['account']['id']),
)
await pleroma.reply(
status,
'You have successfully opted in. Your blocks will now be sent to the people you block.'
)
async def status(db, pleroma, status):
if await db.fetchval(
'SELECT 1 FROM blockbot_opt_out WHERE account_id = $1',
int(status['account']['id']),
):
await pleroma.reply(status, 'You are opted out.')
else:
await pleroma.reply(status, 'You are opted in.')
async def help(db, pleroma, status):
await pleroma.reply(status, 'Available commands: opt in, opt out, status, and help.')
if __name__ == '__main__':
anyio.run(main)

3
requirements.txt Normal file
View File

@ -0,0 +1,3 @@
pleroma.py
asyncpg
qtoml