mirror of
https://github.com/Lynnesbian/FediBooks/
synced 2024-11-26 00:58:59 +00:00
Compare commits
No commits in common. "dd3679d8db4051d2570bf506b5346ede123030ea" and "8f476ca9818f45711fc707afa9a7fe474472e650" have entirely different histories.
dd3679d8db
...
8f476ca981
11 changed files with 20 additions and 153 deletions
|
@ -6,5 +6,3 @@ Flask==1.1.1
|
||||||
flask-mysqldb==0.2.0
|
flask-mysqldb==0.2.0
|
||||||
bcrypt == 3.1.7
|
bcrypt == 3.1.7
|
||||||
requests==2.22.0
|
requests==2.22.0
|
||||||
http-ece==1.1.0
|
|
||||||
cryptography==2.7
|
|
||||||
|
|
33
service.py
33
service.py
|
@ -1,33 +0,0 @@
|
||||||
#!/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")
|
|
|
@ -18,10 +18,7 @@ CREATE TABLE IF NOT EXISTS `bots` (
|
||||||
`handle` VARCHAR(128) PRIMARY KEY,
|
`handle` VARCHAR(128) PRIMARY KEY,
|
||||||
`user_id` INT NOT NULL,
|
`user_id` INT NOT NULL,
|
||||||
`credentials_id` INT NOT NULL,
|
`credentials_id` INT NOT NULL,
|
||||||
`push_private_key` BINARY(128) NOT NULL,
|
`enabled` BOOLEAN DEFAULT 1,
|
||||||
`push_public_key` BINARY(128) NOT NULL,
|
|
||||||
`push_secret` BINARY(16)),
|
|
||||||
`enabled` BOOLEAN DEFAULT 0,
|
|
||||||
`replies_enabled` BOOLEAN DEFAULT 1,
|
`replies_enabled` BOOLEAN DEFAULT 1,
|
||||||
`post_frequency` SMALLINT UNSIGNED DEFAULT 30,
|
`post_frequency` SMALLINT UNSIGNED DEFAULT 30,
|
||||||
`content_warning` VARCHAR(128),
|
`content_warning` VARCHAR(128),
|
||||||
|
|
|
@ -178,11 +178,3 @@ form .row {
|
||||||
background: center/contain url("https://lynnesbian.space/img/bune.png");
|
background: center/contain url("https://lynnesbian.space/img/bune.png");
|
||||||
display: inline-block;
|
display: inline-block;
|
||||||
}
|
}
|
||||||
|
|
||||||
.error {
|
|
||||||
background-color: #e66;
|
|
||||||
color: white;
|
|
||||||
text-align: center;
|
|
||||||
font-size: 1.6em;
|
|
||||||
padding: 10px;
|
|
||||||
}
|
|
||||||
|
|
|
@ -11,8 +11,6 @@
|
||||||
<h1 class="thin centred">Add account</h1>
|
<h1 class="thin centred">Add account</h1>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{%include 'error.html' %}
|
|
||||||
|
|
||||||
<div class="container centred">
|
<div class="container centred">
|
||||||
<form action="/bot/accounts/add" method="POST">
|
<form action="/bot/accounts/add" method="POST">
|
||||||
{% if session['step'] == 1 %}
|
{% if session['step'] == 1 %}
|
||||||
|
|
|
@ -1,27 +0,0 @@
|
||||||
<!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>
|
|
|
@ -11,8 +11,6 @@
|
||||||
<h1 class="thin centred">Create bot</h1>
|
<h1 class="thin centred">Create bot</h1>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{% include 'error.html' %}
|
|
||||||
|
|
||||||
<div class="container centred">
|
<div class="container centred">
|
||||||
<form action="/bot/create" method="POST">
|
<form action="/bot/create" method="POST">
|
||||||
{% if session['step'] == 1 %}
|
{% if session['step'] == 1 %}
|
||||||
|
|
|
@ -1,3 +0,0 @@
|
||||||
{% if error != None %}
|
|
||||||
<div class="error"><i class="fas fa-exclamation-triangle"></i> {{ error }}</div>
|
|
||||||
{% endif %}
|
|
|
@ -1,5 +1,5 @@
|
||||||
<footer>
|
<footer>
|
||||||
<div class='subtle'>
|
<div class='subtle'>
|
||||||
<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>
|
<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>
|
||||||
</div>
|
</div>
|
||||||
</footer>
|
</footer>
|
||||||
|
|
|
@ -11,8 +11,6 @@
|
||||||
<h1 class="thin centred">{% if signup %}Sign up{% else %}Log in{% endif %}</h1>
|
<h1 class="thin centred">{% if signup %}Sign up{% else %}Log in{% endif %}</h1>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{% include 'error.html' %}
|
|
||||||
|
|
||||||
<div class="container centred">
|
<div class="container centred">
|
||||||
<form action="/do/{% if signup %}signup{% else %}login{% endif %}" method="POST">
|
<form action="/do/{% if signup %}signup{% else %}login{% endif %}" method="POST">
|
||||||
<p>
|
<p>
|
||||||
|
|
87
webui.py
87
webui.py
|
@ -18,7 +18,7 @@ app.config['MYSQL_PASSWORD'] = cfg['db_pass']
|
||||||
|
|
||||||
mysql = MySQL(app)
|
mysql = MySQL(app)
|
||||||
|
|
||||||
scopes = ['write:statuses', 'write:accounts', 'read:accounts', 'read:notifications', 'read:statuses', 'push']
|
scopes = ['write:statuses', 'write:accounts', 'read:accounts', 'read:notifications', 'read:statuses']
|
||||||
|
|
||||||
@app.route("/")
|
@app.route("/")
|
||||||
def home():
|
def home():
|
||||||
|
@ -60,17 +60,12 @@ def about():
|
||||||
|
|
||||||
@app.route("/login")
|
@app.route("/login")
|
||||||
def show_login_page():
|
def show_login_page():
|
||||||
error = None
|
return render_template("login.html", signup = False)
|
||||||
if 'error' in session:
|
|
||||||
error = session.pop('error')
|
|
||||||
return render_template("login.html", signup = False, error = error)
|
|
||||||
|
|
||||||
@app.route("/signup")
|
@app.route("/signup")
|
||||||
def show_signup_page():
|
def show_signup_page(error = None):
|
||||||
error = None
|
#TODO: display error if any
|
||||||
if 'error' in session:
|
return render_template("login.html", signup = True)
|
||||||
error = session.pop('error')
|
|
||||||
return render_template("login.html", signup = True, error = error)
|
|
||||||
|
|
||||||
@app.route("/settings")
|
@app.route("/settings")
|
||||||
def settings():
|
def settings():
|
||||||
|
@ -141,13 +136,8 @@ def bot_accounts(id):
|
||||||
|
|
||||||
@app.route("/bot/accounts/add", methods = ['GET', 'POST'])
|
@app.route("/bot/accounts/add", methods = ['GET', 'POST'])
|
||||||
def bot_accounts_add():
|
def bot_accounts_add():
|
||||||
error = None
|
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
if session['step'] == 1:
|
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
|
# look up user
|
||||||
handle_list = request.form['account'].split('@')
|
handle_list = request.form['account'].split('@')
|
||||||
username = handle_list[1]
|
username = handle_list[1]
|
||||||
|
@ -169,8 +159,7 @@ def bot_accounts_add():
|
||||||
found = True
|
found = True
|
||||||
break
|
break
|
||||||
if not found:
|
if not found:
|
||||||
error = "Couldn't find a valid ActivityPub outbox URL."
|
return "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
|
# 3. format as outbox URL and check to make sure it works
|
||||||
outbox = "{}/outbox?page=true".format(uri)
|
outbox = "{}/outbox?page=true".format(uri)
|
||||||
|
@ -182,12 +171,10 @@ def bot_accounts_add():
|
||||||
c.execute("INSERT INTO `bot_learned_accounts` (`bot_id`, `fedi_id`) VALUES (%s, %s)", (session['bot'], request.form['account']))
|
c.execute("INSERT INTO `bot_learned_accounts` (`bot_id`, `fedi_id`) VALUES (%s, %s)", (session['bot'], request.form['account']))
|
||||||
c.close()
|
c.close()
|
||||||
mysql.connection.commit()
|
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", error = error)
|
return redirect("/bot/accounts/{}".format(session['bot']), 303)
|
||||||
|
|
||||||
|
return render_template("bot_accounts_add.html")
|
||||||
|
|
||||||
@app.route("/bot/accounts/toggle/<id>")
|
@app.route("/bot/accounts/toggle/<id>")
|
||||||
def bot_accounts_toggle(id):
|
def bot_accounts_toggle(id):
|
||||||
|
@ -197,28 +184,10 @@ def bot_accounts_toggle(id):
|
||||||
c.close()
|
c.close()
|
||||||
return redirect("/bot/accounts/{}".format(session['bot']), 303)
|
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'])
|
@app.route("/bot/create/", methods=['GET', 'POST'])
|
||||||
def bot_create():
|
def bot_create():
|
||||||
error = None
|
#TODO: error handling
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
if session['step'] == 1:
|
if session['step'] == 1:
|
||||||
# strip leading https://, if provided
|
# strip leading https://, if provided
|
||||||
|
@ -245,7 +214,7 @@ def bot_create():
|
||||||
# all other instance types are also unsupported
|
# all other instance types are also unsupported
|
||||||
# return an error message
|
# return an error message
|
||||||
#TODO: misskey
|
#TODO: misskey
|
||||||
session['error'] = "Unsupported instance type. Misskey support is planned."
|
session['error'] = "Unsupported instance type."
|
||||||
|
|
||||||
elif session['step'] == 2:
|
elif session['step'] == 2:
|
||||||
# nothing needs to be done here, this step just informs the user that their instance type is supported
|
# nothing needs to be done here, this step just informs the user that their instance type is supported
|
||||||
|
@ -283,7 +252,7 @@ def bot_create():
|
||||||
del session['instance']
|
del session['instance']
|
||||||
del session['instance_type']
|
del session['instance_type']
|
||||||
session['step'] = 1
|
session['step'] = 1
|
||||||
return redirect(url_for("bot_create"), 303)
|
return bot_create()
|
||||||
|
|
||||||
else:
|
else:
|
||||||
if session['step'] == 4:
|
if session['step'] == 4:
|
||||||
|
@ -295,9 +264,7 @@ def bot_create():
|
||||||
handle = "@{}@{}".format(username, session['instance'])
|
handle = "@{}@{}".format(username, session['instance'])
|
||||||
except:
|
except:
|
||||||
# authentication error occurred
|
# authentication error occurred
|
||||||
error = "Authentication failed."
|
return render_template("bot_oauth_error.html")
|
||||||
session['step'] = 3
|
|
||||||
return render_template("bot_create.html", error = error)
|
|
||||||
|
|
||||||
# authentication success!!
|
# authentication success!!
|
||||||
c = mysql.connection.cursor()
|
c = mysql.connection.cursor()
|
||||||
|
@ -305,15 +272,7 @@ def bot_create():
|
||||||
credentials_id = c.lastrowid
|
credentials_id = c.lastrowid
|
||||||
mysql.connection.commit()
|
mysql.connection.commit()
|
||||||
|
|
||||||
# get webpush url
|
c.execute("INSERT INTO `bots` (handle, user_id, credentials_id) VALUES (%s, %s, %s)", (handle, session['user_id'], credentials_id))
|
||||||
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()
|
mysql.connection.commit()
|
||||||
c.close()
|
c.close()
|
||||||
|
|
||||||
|
@ -324,9 +283,7 @@ def bot_create():
|
||||||
del session['client_id']
|
del session['client_id']
|
||||||
del session['client_secret']
|
del session['client_secret']
|
||||||
|
|
||||||
if 'error' in session:
|
return render_template("bot_create.html")
|
||||||
error = session.pop('error')
|
|
||||||
return render_template("bot_create.html", error = error)
|
|
||||||
|
|
||||||
@app.route("/bot/create/back")
|
@app.route("/bot/create/back")
|
||||||
def bot_create_back():
|
def bot_create_back():
|
||||||
|
@ -344,12 +301,10 @@ def do_signup():
|
||||||
# email validation is basically impossible without actually sending an email to the address
|
# 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 @ ;)
|
# because fedibooks can't send email yet, we'll just check if the string contains an @ ;)
|
||||||
if "@" not in request.form['email']:
|
if "@" not in request.form['email']:
|
||||||
session['error'] = "Invalid email address."
|
return show_signup_page("Invalid email address.")
|
||||||
return redirect(url_for("show_signup_page"), 303)
|
|
||||||
|
|
||||||
if len(request.form['password']) < 8:
|
if len(request.form['password']) < 8:
|
||||||
session['error'] = "Password too short."
|
return show_signup_page("Password too short.")
|
||||||
return redirect(url_for("show_signup_page"), 303)
|
|
||||||
|
|
||||||
pw_hashed = hashlib.sha256(request.form['password'].encode('utf-8')).digest()
|
pw_hashed = hashlib.sha256(request.form['password'].encode('utf-8')).digest()
|
||||||
pw = bcrypt.hashpw(pw_hashed, bcrypt.gensalt(12))
|
pw = bcrypt.hashpw(pw_hashed, bcrypt.gensalt(12))
|
||||||
|
@ -377,24 +332,18 @@ def do_login():
|
||||||
c.execute("SELECT * FROM users WHERE email = %s", (request.form['email'],))
|
c.execute("SELECT * FROM users WHERE email = %s", (request.form['email'],))
|
||||||
data = c.fetchone()
|
data = c.fetchone()
|
||||||
c.close()
|
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']):
|
if bcrypt.checkpw(pw_hashed, data['password']):
|
||||||
session['user_id'] = data['id']
|
session['user_id'] = data['id']
|
||||||
return redirect(url_for("home"))
|
return redirect(url_for("home"))
|
||||||
|
|
||||||
else:
|
else:
|
||||||
session['error'] = "Incorrect login information."
|
return "invalid login"
|
||||||
return redirect(url_for("show_login_page"), 303)
|
|
||||||
|
|
||||||
@app.route("/img/bot_generic.png")
|
@app.route("/img/bot_generic.png")
|
||||||
def img_bot_generic():
|
def img_bot_generic():
|
||||||
return send_file("static/bot_generic.png", mimetype="image/png")
|
return send_file("static/bot_generic.png", mimetype="image/png")
|
||||||
|
|
||||||
def bot_check(bot):
|
def bot_check(bot):
|
||||||
# check to ensure bot is owned by user
|
|
||||||
c = mysql.connection.cursor()
|
c = mysql.connection.cursor()
|
||||||
c.execute("SELECT COUNT(*) FROM `bots` WHERE `handle` = %s AND `user_id` = %s", (bot, session['user_id']))
|
c.execute("SELECT COUNT(*) FROM `bots` WHERE `handle` = %s AND `user_id` = %s", (bot, session['user_id']))
|
||||||
return c.fetchone()[0] == 1
|
return c.fetchone()[0] == 1
|
||||||
|
|
Loading…
Reference in a new issue