mirror of
https://github.com/Lynnesbian/FediBooks/
synced 2024-11-25 16:48:58 +00:00
Compare commits
11 commits
8f476ca981
...
dd3679d8db
Author | SHA1 | Date | |
---|---|---|---|
dd3679d8db | |||
0294f8e004 | |||
4680bc19c1 | |||
802120540a | |||
be0a843c1a | |||
1f7bcf58db | |||
252a8dd6cf | |||
fb54d65d74 | |||
4f2741c384 | |||
4e79872eae | |||
c25e0764fe |
11 changed files with 152 additions and 19 deletions
|
@ -6,3 +6,5 @@ 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
|
@ -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")
|
|
@ -18,7 +18,10 @@ 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,
|
||||||
`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,
|
`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,3 +178,11 @@ 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,6 +11,8 @@
|
||||||
<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 %}
|
||||||
|
|
27
templates/bot_accounts_delete.html
Normal file
27
templates/bot_accounts_delete.html
Normal 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>
|
|
@ -11,6 +11,8 @@
|
||||||
<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 %}
|
||||||
|
|
3
templates/error.html
Normal file
3
templates/error.html
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
{% 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>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>
|
</div>
|
||||||
</footer>
|
</footer>
|
||||||
|
|
|
@ -11,6 +11,8 @@
|
||||||
<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>
|
||||||
|
|
85
webui.py
85
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']
|
scopes = ['write:statuses', 'write:accounts', 'read:accounts', 'read:notifications', 'read:statuses', 'push']
|
||||||
|
|
||||||
@app.route("/")
|
@app.route("/")
|
||||||
def home():
|
def home():
|
||||||
|
@ -60,12 +60,17 @@ def about():
|
||||||
|
|
||||||
@app.route("/login")
|
@app.route("/login")
|
||||||
def show_login_page():
|
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")
|
@app.route("/signup")
|
||||||
def show_signup_page(error = None):
|
def show_signup_page():
|
||||||
#TODO: display error if any
|
error = None
|
||||||
return render_template("login.html", signup = True)
|
if 'error' in session:
|
||||||
|
error = session.pop('error')
|
||||||
|
return render_template("login.html", signup = True, error = error)
|
||||||
|
|
||||||
@app.route("/settings")
|
@app.route("/settings")
|
||||||
def settings():
|
def settings():
|
||||||
|
@ -136,8 +141,13 @@ 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]
|
||||||
|
@ -159,7 +169,8 @@ def bot_accounts_add():
|
||||||
found = True
|
found = True
|
||||||
break
|
break
|
||||||
if not found:
|
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
|
# 3. format as outbox URL and check to make sure it works
|
||||||
outbox = "{}/outbox?page=true".format(uri)
|
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.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)
|
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>")
|
@app.route("/bot/accounts/toggle/<id>")
|
||||||
def bot_accounts_toggle(id):
|
def bot_accounts_toggle(id):
|
||||||
|
@ -184,10 +197,28 @@ 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():
|
||||||
#TODO: error handling
|
error = None
|
||||||
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
|
||||||
|
@ -214,7 +245,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."
|
session['error'] = "Unsupported instance type. Misskey support is planned."
|
||||||
|
|
||||||
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
|
||||||
|
@ -252,7 +283,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 bot_create()
|
return redirect(url_for("bot_create"), 303)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
if session['step'] == 4:
|
if session['step'] == 4:
|
||||||
|
@ -264,7 +295,9 @@ def bot_create():
|
||||||
handle = "@{}@{}".format(username, session['instance'])
|
handle = "@{}@{}".format(username, session['instance'])
|
||||||
except:
|
except:
|
||||||
# authentication error occurred
|
# 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!!
|
# authentication success!!
|
||||||
c = mysql.connection.cursor()
|
c = mysql.connection.cursor()
|
||||||
|
@ -272,7 +305,15 @@ def bot_create():
|
||||||
credentials_id = c.lastrowid
|
credentials_id = c.lastrowid
|
||||||
mysql.connection.commit()
|
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()
|
mysql.connection.commit()
|
||||||
c.close()
|
c.close()
|
||||||
|
|
||||||
|
@ -283,7 +324,9 @@ def bot_create():
|
||||||
del session['client_id']
|
del session['client_id']
|
||||||
del session['client_secret']
|
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")
|
@app.route("/bot/create/back")
|
||||||
def 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
|
# 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']:
|
||||||
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:
|
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_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))
|
||||||
|
@ -332,18 +377,24 @@ 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:
|
||||||
return "invalid login"
|
session['error'] = "Incorrect login information."
|
||||||
|
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