From ded8fac097849a04b79b19de53722671dd8b1360 Mon Sep 17 00:00:00 2001 From: Lynne Date: Thu, 19 Sep 2019 14:40:41 +1000 Subject: [PATCH] send status to users when they are added to a bot's learning list --- app/pages/bot/accounts_add.py | 23 +++++++++++++++- app/setup.py | 51 +++++++++++++++++++++++++++++++++++ app/webui.py | 2 +- 3 files changed, 74 insertions(+), 2 deletions(-) create mode 100755 app/setup.py diff --git a/app/pages/bot/accounts_add.py b/app/pages/bot/accounts_add.py index 0fa543c..5469616 100644 --- a/app/pages/bot/accounts_add.py +++ b/app/pages/bot/accounts_add.py @@ -2,7 +2,7 @@ from flask import session, render_template, request, redirect import requests from mastodon import Mastodon -def bot_accounts_add(mysql): +def bot_accounts_add(mysql, cfg): if request.method == 'POST': if session['step'] == 1: if request.form['account'] == session['bot']: @@ -79,6 +79,27 @@ def bot_accounts_add(mysql): c.execute("INSERT INTO `bot_learned_accounts` (`bot_id`, `fedi_id`) VALUES (%s, %s)", (session['bot'], request.form['account'])) c.close() mysql.connection.commit() + + if 'account' in cfg: + client = Mastodon( + cfg['account']['client_id'], + cfg['account']['client_secret'], + cfg['account']['secret'], + "https://{}".format(cfg['account']['instance']) + ) + status = """ +Hi, {user}. Someone has created a FediBooks bot that learns from your posts. The bot's username is {bot}. Your public posts are now being downloaded and stored for use by this bot. +If you do not want {bot} to learn from your posts, click here: {overview} +If you want to ensure that nobody can use {home} to create bots that use your post history, click here: {blacklist} + """.format( + user = request.form['account'], + bot = session['bot'].replace("@", "@\u200B"), + overview = "{}/overview/{}".format(cfg['base_uri'], request.form['account']), + blacklist = "{}/blacklist".format(cfg['base_uri']), + home = cfg['base_uri'] + ) + + client.status_post(status) return redirect("/bot/accounts/{}".format(session['bot']), 303) else: error = "Couldn't access ActivityPub outbox. {} may require authenticated fetches, which FediBooks doesn't support yet.".format(instance) diff --git a/app/setup.py b/app/setup.py new file mode 100755 index 0000000..c212a44 --- /dev/null +++ b/app/setup.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python3 + +from mastodon import Mastodon +import json + +cfg = json.load(open("config.json")) + +scopes = ["write:statuses"] + +print("FediBooks needs access to an account to notify users when they've been added to bots.") +print("What instance would you like FediBooks' account to be on?") +instance = input("https://") +client_id, client_secret = Mastodon.create_app( + "FediBooks", + api_base_url="https://{}".format(instance), + scopes=scopes, + website=cfg['base_uri'] +) + +client = Mastodon( + client_id=client_id, + client_secret=client_secret, + api_base_url="https://{}".format(instance) +) + +url = client.auth_request_url( + client_id=client_id, + scopes=scopes +) +print("Create an account on {}, then click this link to give FediBooks access to the account: {}".format(instance, url)) +print("Authorise FediBooks to access the account, then paste the code below.") +code = input("Code: ") + +print("Authenticating...") + +secret = client.log_in( + code = code, + scopes=scopes +) +client.status_post("FediBooks has successfully been set up to use this account.") + +cfg['account'] = { + 'client_id': client_id, + 'client_secret': client_secret, + 'secret': secret, + 'instance': instance +} + +json.dump(cfg, open('config.json', 'w')) + +print("Done! Thanks for using FediBooks!") diff --git a/app/webui.py b/app/webui.py index 3a22a79..ebe4d37 100644 --- a/app/webui.py +++ b/app/webui.py @@ -127,7 +127,7 @@ def bot_accounts(id): @app.route("/bot/accounts/add", methods = ['GET', 'POST']) def render_bot_accounts_add(): - return bot_accounts_add(mysql) + return bot_accounts_add(mysql, cfg) @app.route("/bot/accounts/toggle/") def bot_accounts_toggle(id):