From f92951e91faaa249994074bbacb23307d082acdb Mon Sep 17 00:00:00 2001 From: Lynne Date: Wed, 11 Sep 2019 14:37:13 +1000 Subject: [PATCH] implement replies --- functions.py | 15 ++++++++++----- webui.py | 35 +++++++++++++++++++++++++++++++---- 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/functions.py b/functions.py index 6502fe2..4754140 100644 --- a/functions.py +++ b/functions.py @@ -34,8 +34,11 @@ def extract_post(post): text = text.rstrip("\n") # remove trailing newline(s) return text -def make_post(handle): - handle = handle[0] +def make_post(args): + id = None + if len(args) == 2: + id = args[1] + handle = args[0] db = MySQLdb.connect( host = cfg['db_host'], user=cfg['db_user'], @@ -122,7 +125,9 @@ def make_post(handle): post = re.sub(r"@(\w+)@([\w.]+)", r"@{}\1".format(zws), post) print(post) - client.status_post(post, visibility = bot['post_privacy'], spoiler_text = bot['content_warning']) + client.status_post(post, id, visibility = bot['post_privacy'], spoiler_text = bot['content_warning']) - c.execute("UPDATE bots SET last_post = CURRENT_TIMESTAMP() WHERE handle = %s", (handle,)) - db.commit() + if id == None: + # this wasn't a reply, it was a regular post, so update the last post date + c.execute("UPDATE bots SET last_post = CURRENT_TIMESTAMP() WHERE handle = %s", (handle,)) + db.commit() diff --git a/webui.py b/webui.py index 2f78fe0..cbdd2be 100644 --- a/webui.py +++ b/webui.py @@ -5,6 +5,7 @@ import requests import MySQLdb import bcrypt import json, hashlib, re +import functions cfg = json.load(open("config.json")) @@ -501,13 +502,39 @@ def push(id): api_base_url = "https://{}".format(id.split("@")[2]) ) - c.execute("SELECT push_private_key, push_secret FROM bots WHERE handle = %s", (id,)) - p = c.fetchone() + c.execute("SELECT push_private_key, push_secret, replies_enabled FROM bots WHERE handle = %s", (id,)) + bot = c.fetchone() + if not bot[2]: + return "Replies disabled." + params = { - 'privkey': int(p[0].rstrip("\0")), - 'auth': p[1] + 'privkey': int(bot[0].rstrip("\0")), + 'auth': bot[1] } push_object = client.push_subscription_decrypt_push(request.data, params, request.headers['Encryption'], request.headers['Crypto-Key']) + notification = client.notifications(id = push_object['notification_id']) + me = client.account_verify_credentials()['id'] + + # first, check how many times the bot has posted in this thread. + # if it's over 15, don't reply. + # this is to stop endless reply chains between two bots. + try: + context = client.status_context(notification['status']['id']) + my_posts = 0 + for post in context['ancestors']: + if post['account']['id'] == me: + my_posts += 1 + if my_posts >= 15: + # don't reply + return "Didn't reply." + except: + # failed to fetch context + # assume we haven't been participating in this thread + pass + + functions.make_post([id, notification['status']['id']]) + + return "Success!" @app.route("/do/signup", methods=['POST']) def do_signup():