implement replies

This commit is contained in:
Lynne Megido 2019-09-11 14:37:13 +10:00
parent 7e1393e7b9
commit f92951e91f
2 changed files with 41 additions and 9 deletions

View File

@ -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()

View File

@ -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():