1
0
Fork 0
mirror of https://github.com/Lynnesbian/FediBooks/ synced 2024-11-25 08:38:59 +00:00

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) text = text.rstrip("\n") # remove trailing newline(s)
return text return text
def make_post(handle): def make_post(args):
handle = handle[0] id = None
if len(args) == 2:
id = args[1]
handle = args[0]
db = MySQLdb.connect( db = MySQLdb.connect(
host = cfg['db_host'], host = cfg['db_host'],
user=cfg['db_user'], user=cfg['db_user'],
@ -122,7 +125,9 @@ def make_post(handle):
post = re.sub(r"@(\w+)@([\w.]+)", r"@{}\1".format(zws), post) post = re.sub(r"@(\w+)@([\w.]+)", r"@{}\1".format(zws), post)
print(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,)) if id == None:
db.commit() # 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 MySQLdb
import bcrypt import bcrypt
import json, hashlib, re import json, hashlib, re
import functions
cfg = json.load(open("config.json")) cfg = json.load(open("config.json"))
@ -501,13 +502,39 @@ def push(id):
api_base_url = "https://{}".format(id.split("@")[2]) api_base_url = "https://{}".format(id.split("@")[2])
) )
c.execute("SELECT push_private_key, push_secret FROM bots WHERE handle = %s", (id,)) c.execute("SELECT push_private_key, push_secret, replies_enabled FROM bots WHERE handle = %s", (id,))
p = c.fetchone() bot = c.fetchone()
if not bot[2]:
return "Replies disabled."
params = { params = {
'privkey': int(p[0].rstrip("\0")), 'privkey': int(bot[0].rstrip("\0")),
'auth': p[1] 'auth': bot[1]
} }
push_object = client.push_subscription_decrypt_push(request.data, params, request.headers['Encryption'], request.headers['Crypto-Key']) 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']) @app.route("/do/signup", methods=['POST'])
def do_signup(): def do_signup():