1
0
Fork 0
mirror of https://github.com/Lynnesbian/FediBooks/ synced 2024-11-25 16:48:58 +00:00

Compare commits

...

11 commits

11 changed files with 152 additions and 19 deletions

View file

@ -6,3 +6,5 @@ Flask==1.1.1
flask-mysqldb==0.2.0
bcrypt == 3.1.7
requests==2.22.0
http-ece==1.1.0
cryptography==2.7

View file

@ -0,0 +1,33 @@
#!/usr/bin/env python3
from mastodon import Mastodon
import MySQLdb
import requests
from multiprocessing import Pool
import json
cfg = json.load(open('config.json'))
def scrape_posts(handle, outbox):
# check for min_id
last_post = 0
r = requests.get(outbox)
j = r.json()
pleroma = 'next' not in j
if pleroma:
j = j['first']
else:
uri = "{}&min_id={}".format(outbox, last_post)
r = requests.get(uri)
j = r.json()
print("Establishing DB connection")
db = MySQLdb.connect(
host = cfg['db_host'],
user=cfg['db_user'],
passwd=cfg['db_pass'],
db=cfg['db_name']
)
c = db.cursor()
print("Downloading posts")

View file

@ -18,7 +18,10 @@ CREATE TABLE IF NOT EXISTS `bots` (
`handle` VARCHAR(128) PRIMARY KEY,
`user_id` INT NOT NULL,
`credentials_id` INT NOT NULL,
`enabled` BOOLEAN DEFAULT 1,
`push_private_key` BINARY(128) NOT NULL,
`push_public_key` BINARY(128) NOT NULL,
`push_secret` BINARY(16)),
`enabled` BOOLEAN DEFAULT 0,
`replies_enabled` BOOLEAN DEFAULT 1,
`post_frequency` SMALLINT UNSIGNED DEFAULT 30,
`content_warning` VARCHAR(128),

View file

@ -178,3 +178,11 @@ form .row {
background: center/contain url("https://lynnesbian.space/img/bune.png");
display: inline-block;
}
.error {
background-color: #e66;
color: white;
text-align: center;
font-size: 1.6em;
padding: 10px;
}

View file

@ -11,6 +11,8 @@
<h1 class="thin centred">Add account</h1>
</div>
{%include 'error.html' %}
<div class="container centred">
<form action="/bot/accounts/add" method="POST">
{% if session['step'] == 1 %}

View file

@ -0,0 +1,27 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>FediBooks</title>
{% include 'imports.html' %}
</head>
<body>
<div class="container centred">
<h1 class="thin centred">Stop learning from account</h1>
</div>
<div class="container centred">
<form method='POST'>
<div class="panel-icon large"></div>
<div class="container centred">
<p>Are you sure you want {{ bot }} to stop learning from {{ user }}?</p>
<a class="button btn-secondary" href="/"><i class="fas fa-times"></i> Cancel</a>
<button class="button btn-dangerous"><i class="fas fa-trash"></i> Stop learning</button>
</div>
</form>
</div>
{% include 'footer.html' %}
</body>
</html>

View file

@ -11,6 +11,8 @@
<h1 class="thin centred">Create bot</h1>
</div>
{% include 'error.html' %}
<div class="container centred">
<form action="/bot/create" method="POST">
{% if session['step'] == 1 %}

3
templates/error.html Normal file
View file

@ -0,0 +1,3 @@
{% if error != None %}
<div class="error"><i class="fas fa-exclamation-triangle"></i> {{ error }}</div>
{% endif %}

View file

@ -1,5 +1,5 @@
<footer>
<div class='subtle'>
<p>Website design and FediBooks software by <a href='https://fedi.lynnesbian.space/@LynnearSoftware'>Lynne</a>. This site uses <a href="https://fontawesome.com">Font Awesome</a>. Source code available <a href="https://github.com/Lynnesbian/Fedibooks">here</a>.</p>
<p>FediBooks is beta software. It might behave unexpectedly. Website design and FediBooks software by <a href='https://fedi.lynnesbian.space/@LynnearSoftware'>Lynne</a>. This site uses <a href="https://fontawesome.com">Font Awesome</a>. Source code available <a href="https://github.com/Lynnesbian/Fedibooks">here</a>.</p>
</div>
</footer>

View file

@ -11,6 +11,8 @@
<h1 class="thin centred">{% if signup %}Sign up{% else %}Log in{% endif %}</h1>
</div>
{% include 'error.html' %}
<div class="container centred">
<form action="/do/{% if signup %}signup{% else %}login{% endif %}" method="POST">
<p>

View file

@ -18,7 +18,7 @@ app.config['MYSQL_PASSWORD'] = cfg['db_pass']
mysql = MySQL(app)
scopes = ['write:statuses', 'write:accounts', 'read:accounts', 'read:notifications', 'read:statuses']
scopes = ['write:statuses', 'write:accounts', 'read:accounts', 'read:notifications', 'read:statuses', 'push']
@app.route("/")
def home():
@ -60,12 +60,17 @@ def about():
@app.route("/login")
def show_login_page():
return render_template("login.html", signup = False)
error = None
if 'error' in session:
error = session.pop('error')
return render_template("login.html", signup = False, error = error)
@app.route("/signup")
def show_signup_page(error = None):
#TODO: display error if any
return render_template("login.html", signup = True)
def show_signup_page():
error = None
if 'error' in session:
error = session.pop('error')
return render_template("login.html", signup = True, error = error)
@app.route("/settings")
def settings():
@ -136,8 +141,13 @@ def bot_accounts(id):
@app.route("/bot/accounts/add", methods = ['GET', 'POST'])
def bot_accounts_add():
error = None
if request.method == 'POST':
if session['step'] == 1:
if request.form['account'] == session['bot']:
error = "Bots cannot learn from themselves."
return render_template("bot_accounts_add.html", error = error)
# look up user
handle_list = request.form['account'].split('@')
username = handle_list[1]
@ -159,7 +169,8 @@ def bot_accounts_add():
found = True
break
if not found:
return "Couldn't find a valid ActivityPub outbox URL."
error = "Couldn't find a valid ActivityPub outbox URL."
return render_template("bot_accounts_add.html", error = error)
# 3. format as outbox URL and check to make sure it works
outbox = "{}/outbox?page=true".format(uri)
@ -171,10 +182,12 @@ def bot_accounts_add():
c.execute("INSERT INTO `bot_learned_accounts` (`bot_id`, `fedi_id`) VALUES (%s, %s)", (session['bot'], request.form['account']))
c.close()
mysql.connection.commit()
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."
return render_template("bot_accounts_add.html", error = error)
return render_template("bot_accounts_add.html")
return render_template("bot_accounts_add.html", error = error)
@app.route("/bot/accounts/toggle/<id>")
def bot_accounts_toggle(id):
@ -184,10 +197,28 @@ def bot_accounts_toggle(id):
c.close()
return redirect("/bot/accounts/{}".format(session['bot']), 303)
@app.route("/bot/accounts/delete/<id>", methods=['GET', 'POST'])
def bot_accounts_delete(id):
if request.method == 'GET':
instance = id.split("@")[2]
return render_template("bot_accounts_delete.html", user = id, instance = instance)
else:
#NOTE: when user credential support is added, we'll need to delete the creds too
c = mysql.connection.cursor()
c.execute("DELETE FROM `bot_learned_accounts` WHERE `fedi_id` = %s AND bot_id = %s", (id, session['bot']))
# check to see if anyone else is learning from this account
c.execute("SELECT COUNT(*) FROM `bot_learned_accounts` WHERE `fedi_id` = %s", (id,))
if c.fetchone()[0] == 0:
# nobody else learns from this account, remove it from the db
c.execute("DELETE FROM `fedi_accounts` WHERE `handle` = %s", (id,))
c.close()
mysql.connection.commit()
return redirect(url_for("/bot/accounts/{}".format(session['bot'])), 303)
@app.route("/bot/create/", methods=['GET', 'POST'])
def bot_create():
#TODO: error handling
error = None
if request.method == 'POST':
if session['step'] == 1:
# strip leading https://, if provided
@ -214,7 +245,7 @@ def bot_create():
# all other instance types are also unsupported
# return an error message
#TODO: misskey
session['error'] = "Unsupported instance type."
session['error'] = "Unsupported instance type. Misskey support is planned."
elif session['step'] == 2:
# nothing needs to be done here, this step just informs the user that their instance type is supported
@ -252,7 +283,7 @@ def bot_create():
del session['instance']
del session['instance_type']
session['step'] = 1
return bot_create()
return redirect(url_for("bot_create"), 303)
else:
if session['step'] == 4:
@ -264,7 +295,9 @@ def bot_create():
handle = "@{}@{}".format(username, session['instance'])
except:
# authentication error occurred
return render_template("bot_oauth_error.html")
error = "Authentication failed."
session['step'] = 3
return render_template("bot_create.html", error = error)
# authentication success!!
c = mysql.connection.cursor()
@ -272,7 +305,15 @@ def bot_create():
credentials_id = c.lastrowid
mysql.connection.commit()
c.execute("INSERT INTO `bots` (handle, user_id, credentials_id) VALUES (%s, %s, %s)", (handle, session['user_id'], credentials_id))
# get webpush url
privated, publicd = client.push_subscription_generate_keys()
private = privated['privkey']
public = publicd['pubkey']
secret = privated['auth']
# replace fedibooks.com with cfg['base_uri'] on release
client.push_subscription_set("https://fedibooks.com/push/{}".format(handle), publicd, mention_events = True)
c.execute("INSERT INTO `bots` (handle, user_id, credentials_id, push_public_key, push_private_key, push_secret) VALUES (%s, %s, %s, %s, %s, %s)", (handle, session['user_id'], credentials_id, public, private, secret))
mysql.connection.commit()
c.close()
@ -283,7 +324,9 @@ def bot_create():
del session['client_id']
del session['client_secret']
return render_template("bot_create.html")
if 'error' in session:
error = session.pop('error')
return render_template("bot_create.html", error = error)
@app.route("/bot/create/back")
def bot_create_back():
@ -301,10 +344,12 @@ def do_signup():
# email validation is basically impossible without actually sending an email to the address
# because fedibooks can't send email yet, we'll just check if the string contains an @ ;)
if "@" not in request.form['email']:
return show_signup_page("Invalid email address.")
session['error'] = "Invalid email address."
return redirect(url_for("show_signup_page"), 303)
if len(request.form['password']) < 8:
return show_signup_page("Password too short.")
session['error'] = "Password too short."
return redirect(url_for("show_signup_page"), 303)
pw_hashed = hashlib.sha256(request.form['password'].encode('utf-8')).digest()
pw = bcrypt.hashpw(pw_hashed, bcrypt.gensalt(12))
@ -332,18 +377,24 @@ def do_login():
c.execute("SELECT * FROM users WHERE email = %s", (request.form['email'],))
data = c.fetchone()
c.close()
if data == None:
session['error'] = "Incorrect login information."
return redirect(url_for("show_login_page"), 303)
if bcrypt.checkpw(pw_hashed, data['password']):
session['user_id'] = data['id']
return redirect(url_for("home"))
else:
return "invalid login"
session['error'] = "Incorrect login information."
return redirect(url_for("show_login_page"), 303)
@app.route("/img/bot_generic.png")
def img_bot_generic():
return send_file("static/bot_generic.png", mimetype="image/png")
def bot_check(bot):
# check to ensure bot is owned by user
c = mysql.connection.cursor()
c.execute("SELECT COUNT(*) FROM `bots` WHERE `handle` = %s AND `user_id` = %s", (bot, session['user_id']))
return c.fetchone()[0] == 1