mirror of
https://github.com/Lynnesbian/FediBooks/
synced 2024-11-25 16:48:58 +00:00
Compare commits
4 commits
7572536361
...
c060f8d7ca
Author | SHA1 | Date | |
---|---|---|---|
c060f8d7ca | |||
025b56e1b6 | |||
556dea9fae | |||
461d5fa1be |
5 changed files with 114 additions and 16 deletions
|
@ -18,6 +18,7 @@ CREATE TABLE IF NOT EXISTS `bots` (
|
||||||
`id` BINARY(64) PRIMARY KEY,
|
`id` BINARY(64) PRIMARY KEY,
|
||||||
`user_id` INT NOT NULL,
|
`user_id` INT NOT NULL,
|
||||||
`credentials_id` INT NOT NULL,
|
`credentials_id` INT NOT NULL,
|
||||||
|
`handle` VARCHAR(128) NOT NULL,
|
||||||
`enabled` BOOLEAN DEFAULT 1,
|
`enabled` BOOLEAN DEFAULT 1,
|
||||||
`replies_enabled` BOOLEAN DEFAULT 1,
|
`replies_enabled` BOOLEAN DEFAULT 1,
|
||||||
`post_frequency` SMALLINT UNSIGNED DEFAULT 30,
|
`post_frequency` SMALLINT UNSIGNED DEFAULT 30,
|
||||||
|
@ -33,7 +34,7 @@ CREATE TABLE IF NOT EXISTS `bots` (
|
||||||
FOREIGN KEY (`user_id`) REFERENCES users(id) ON DELETE CASCADE,
|
FOREIGN KEY (`user_id`) REFERENCES users(id) ON DELETE CASCADE,
|
||||||
FOREIGN KEY (`credentials_id`) REFERENCES credentials(id) ON DELETE CASCADE
|
FOREIGN KEY (`credentials_id`) REFERENCES credentials(id) ON DELETE CASCADE
|
||||||
) ENGINE=INNODB;
|
) ENGINE=INNODB;
|
||||||
CREATE TABLE IF NOT EXISTS `fedi_account` (
|
CREATE TABLE IF NOT EXISTS `fedi_accounts` (
|
||||||
`handle` VARCHAR(128) PRIMARY KEY,
|
`handle` VARCHAR(128) PRIMARY KEY,
|
||||||
`outbox` VARCHAR(256),
|
`outbox` VARCHAR(256),
|
||||||
`credentials_id` INT NOT NULL,
|
`credentials_id` INT NOT NULL,
|
||||||
|
@ -41,6 +42,12 @@ CREATE TABLE IF NOT EXISTS `fedi_account` (
|
||||||
`icon_update_time` DATETIME DEFAULT 0,
|
`icon_update_time` DATETIME DEFAULT 0,
|
||||||
FOREIGN KEY (`credentials_id`) REFERENCES credentials(id) ON DELETE CASCADE
|
FOREIGN KEY (`credentials_id`) REFERENCES credentials(id) ON DELETE CASCADE
|
||||||
) ENGINE=INNODB;
|
) ENGINE=INNODB;
|
||||||
|
CREATE TABLE IF NOT EXISTS `bot_learned_accounts` (
|
||||||
|
`bot_id` BINARY(64) NOT NULL,
|
||||||
|
`fedi_id` VARCHAR(128) NOT NULL,
|
||||||
|
FOREIGN KEY (`bot_id`) REFERENCES bots(id) ON DELETE CASCADE,
|
||||||
|
FOREIGN KEY (`fedi_id`) REFERENCES fedi_accounts(handle) ON DELETE CASCADE
|
||||||
|
) ENGINE=INNODB;
|
||||||
CREATE TABLE IF NOT EXISTS `posts` (
|
CREATE TABLE IF NOT EXISTS `posts` (
|
||||||
`id` BIGINT AUTO_INCREMENT PRIMARY KEY,
|
`id` BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||||
`fedi_id` VARCHAR(128),
|
`fedi_id` VARCHAR(128),
|
||||||
|
|
|
@ -135,7 +135,6 @@ h1, h2, h3, h4, h5, h6 {
|
||||||
}
|
}
|
||||||
|
|
||||||
form {
|
form {
|
||||||
text-align: justify;
|
|
||||||
display:inline-block;
|
display:inline-block;
|
||||||
}
|
}
|
||||||
label.important {
|
label.important {
|
||||||
|
|
|
@ -9,7 +9,6 @@
|
||||||
<body>
|
<body>
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<h1 class="thin centred">Create bot</h1>
|
<h1 class="thin centred">Create bot</h1>
|
||||||
<p class="large thin centred">Step {{ session['step'] }}</p>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="container centred">
|
<div class="container centred">
|
||||||
|
@ -43,7 +42,11 @@
|
||||||
{% if session['step'] != 1 %}
|
{% if session['step'] != 1 %}
|
||||||
<button class="button btn-secondary"><i class="fas fa-arrow-left"></i> Back</button>
|
<button class="button btn-secondary"><i class="fas fa-arrow-left"></i> Back</button>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
{% if session['step'] < 5 %}
|
||||||
<button class="button btn-primary"><i class="fas fa-arrow-right"></i> Next</button>
|
<button class="button btn-primary"><i class="fas fa-arrow-right"></i> Next</button>
|
||||||
|
{% else %}
|
||||||
|
<a href="/" class="button btn-primary"><i class="fas fa-check"></i> Finish</a>
|
||||||
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -17,16 +17,18 @@
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="container" style="min-height: 300px;">
|
<div class="container" style="min-height: 300px;">
|
||||||
|
{% for bot in bots %}
|
||||||
<div class="row light">
|
<div class="row light">
|
||||||
<div class="panel-icon online"></div>
|
<div class="panel-icon {% if bot['enabled'] %}online{% else %}offline{% endif %}"></div>
|
||||||
<div class="panel-text">
|
<div class="panel-text">
|
||||||
<div class="panel-name">My bot!!</div>
|
<div class="panel-name">{{ bot['handle'] }}</div>
|
||||||
<div class="panel-status">Online, learning from 3 accounts, 12345 posts in database</div>
|
<div class="panel-status">{% if bot['enabled'] %}Online{% else %}Offline{% endif %}, learning from {{ bot_users[bot['id']] }} accounts</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="panel-actions">
|
<div class="panel-actions">
|
||||||
<a class="button btn-secondary" href="/bot/toggle/insert id here" title="Turn on/off"><i class="fas fa-power-off"></i></a><a class="button btn-secondary" href="/bot/edit/insert id here" title="Configure"><i class="fas fa-cog"></i></a><a class="button btn-secondary" href="/bot/accounts/insert id here" title="Accounts learned from"><i class="fas fa-users"></i></a><a class="button btn-secondary" href="/bot/blacklist/insert id here" title="Banned words"><i class="fas fa-strikethrough"></i></a><a class="button btn-secondary" href="/bot/chat/insert id here" title="Chat"><i class="fas fa-comment"></i></a><a class="button btn-dangerous" href="/bot/delete/insert id here" title="Delete"><i class="fas fa-trash"></i></a>
|
<a class="button btn-secondary" href="/bot/toggle/{{ bot['id'] }}" title="Turn on/off"><i class="fas fa-power-off"></i></a><a class="button btn-secondary" href="/bot/edit/{{ bot['id'] }}" title="Configure"><i class="fas fa-cog"></i></a><a class="button btn-secondary" href="/bot/accounts/{{ bot['id'] }}" title="Accounts learned from"><i class="fas fa-users"></i></a><a class="button btn-secondary" href="/bot/blacklist/{{ bot['id'] }}" title="Banned words"><i class="fas fa-strikethrough"></i></a><a class="button btn-secondary" href="/bot/chat/{{ bot['id'] }}" title="Chat"><i class="fas fa-comment"></i></a><a class="button btn-dangerous" href="/bot/delete/{{ bot['id'] }}" title="Delete"><i class="fas fa-trash"></i></a>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
{% endfor %}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="container">
|
<div class="container">
|
||||||
|
|
105
webui.py
105
webui.py
|
@ -1,5 +1,6 @@
|
||||||
from flask import Flask, render_template, session, request, redirect, url_for
|
from flask import Flask, render_template, session, request, redirect, url_for
|
||||||
from flask_mysqldb import MySQL
|
from flask_mysqldb import MySQL
|
||||||
|
from mastodon import Mastodon
|
||||||
import requests
|
import requests
|
||||||
import MySQLdb
|
import MySQLdb
|
||||||
import bcrypt
|
import bcrypt
|
||||||
|
@ -17,19 +18,34 @@ app.config['MYSQL_PASSWORD'] = cfg['db_pass']
|
||||||
|
|
||||||
mysql = MySQL(app)
|
mysql = MySQL(app)
|
||||||
|
|
||||||
|
scopes = ['write:statuses', 'write:accounts', 'read:accounts', 'read:notifications', 'read:statuses']
|
||||||
|
|
||||||
@app.route("/")
|
@app.route("/")
|
||||||
def home():
|
def home():
|
||||||
if 'userid' in session:
|
if 'user_id' in session:
|
||||||
session['step'] = 1
|
session['step'] = 1
|
||||||
c = mysql.connection.cursor()
|
c = mysql.connection.cursor()
|
||||||
c.execute("SELECT COUNT(*) FROM `bots` WHERE user_id = %s", (session['userid'],))
|
c.execute("SELECT COUNT(*) FROM `bots` WHERE user_id = %s", (session['user_id'],))
|
||||||
bot_count = c.fetchone()[0]
|
bot_count = c.fetchone()[0]
|
||||||
active_count = None
|
active_count = None
|
||||||
|
bots = None
|
||||||
|
bot_users = None
|
||||||
|
|
||||||
if bot_count > 0:
|
if bot_count > 0:
|
||||||
c.execute("SELECT COUNT(*) FROM `bots` WHERE user_id = %s AND enabled = TRUE", (session['userid'],))
|
c.execute("SELECT COUNT(*) FROM `bots` WHERE user_id = %s AND enabled = TRUE", (session['user_id'],))
|
||||||
active_count = c.fetchone()[0]
|
active_count = c.fetchone()[0]
|
||||||
|
dc = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
|
||||||
|
dc.execute("SELECT * FROM `bots` WHERE user_id = %s", (session['user_id'],))
|
||||||
|
bots = dc.fetchall()
|
||||||
|
dc.close()
|
||||||
|
bot_users = {}
|
||||||
|
|
||||||
|
for bot in bots:
|
||||||
|
c.execute("SELECT COUNT(*) FROM `bot_learned_accounts` WHERE bot_id = %s", (bot['id'],))
|
||||||
|
bot_users[bot['id']] = c.fetchone()[0]
|
||||||
|
|
||||||
c.close()
|
c.close()
|
||||||
return render_template("home.html", bot_count = bot_count, active_count = active_count)
|
return render_template("home.html", bot_count = bot_count, active_count = active_count, bots = bots, bot_users = bot_users)
|
||||||
else:
|
else:
|
||||||
return render_template("front_page.html")
|
return render_template("front_page.html")
|
||||||
|
|
||||||
|
@ -72,6 +88,7 @@ def bot_accounts_add():
|
||||||
|
|
||||||
@app.route("/bot/create/", methods=['GET', 'POST'])
|
@app.route("/bot/create/", methods=['GET', 'POST'])
|
||||||
def bot_create():
|
def bot_create():
|
||||||
|
#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
|
||||||
|
@ -101,10 +118,81 @@ def bot_create():
|
||||||
session['error'] = "Unsupported instance type."
|
session['error'] = "Unsupported instance type."
|
||||||
|
|
||||||
elif session['step'] == 2:
|
elif session['step'] == 2:
|
||||||
pass
|
# nothing needs to be done here, this step just informs the user that their instance type is supported
|
||||||
|
session['step'] += 1
|
||||||
|
|
||||||
|
elif session['step'] == 3:
|
||||||
|
# authenticate with the given instance and obtain credentials
|
||||||
|
if session['instance_type'] in ['Mastodon', 'Pleroma']:
|
||||||
|
redirect_uri = '{}/do/authenticate_bot'.format(cfg['base_uri'])
|
||||||
|
|
||||||
|
session['client_id'], session['client_secret'] = Mastodon.create_app(
|
||||||
|
"FediBooks",
|
||||||
|
api_base_url="https://{}".format(session['instance']),
|
||||||
|
scopes=scopes,
|
||||||
|
redirect_uris=[redirect_uri],
|
||||||
|
website=cfg['base_uri']
|
||||||
|
)
|
||||||
|
|
||||||
|
client = Mastodon(
|
||||||
|
client_id=session['client_id'],
|
||||||
|
client_secret=session['client_secret'],
|
||||||
|
api_base_url="https://{}".format(session['instance'])
|
||||||
|
)
|
||||||
|
|
||||||
|
url = client.auth_request_url(client_id=session['client_id'], redirect_uris=redirect_uri, scopes=scopes)
|
||||||
|
return redirect(url, code=303)
|
||||||
|
|
||||||
|
elif session['instance_type'] == 'Misskey':
|
||||||
|
# todo
|
||||||
|
pass
|
||||||
|
|
||||||
|
else:
|
||||||
|
# the user clicked next on step 2 while having an unsupported instance type
|
||||||
|
# take them back to step 1
|
||||||
|
del session['instance']
|
||||||
|
del session['instance_type']
|
||||||
|
session['step'] = 1
|
||||||
|
return bot_create()
|
||||||
|
|
||||||
|
elif session['step'] == 4:
|
||||||
|
try:
|
||||||
|
# test authentication
|
||||||
|
client = Mastodon(client_id=session['client_id'], client_secret=session['client_secret'], api_base_url=session['instance'])
|
||||||
|
session['secret'] = client.log_in(code = session['code'], scopes=scopes, redirect_uri='{}/do/authenticate_bot'.format(cfg['base_uri']))
|
||||||
|
username = client.account_verify_credentials()['username']
|
||||||
|
handle = "@{}@{}".format(username, session['instance'])
|
||||||
|
except:
|
||||||
|
# authentication error occurred
|
||||||
|
return render_template("bot_oauth_error.html")
|
||||||
|
|
||||||
|
# authentication success!!
|
||||||
|
c = mysql.connection.cursor()
|
||||||
|
c.execute("INSERT INTO `credentials` (client_id, client_secret, secret) VALUES (%s, %s, %s)", (session['client_id'], session['client_secret'], session['code']))
|
||||||
|
credentials_id = c.lastrowid
|
||||||
|
mysql.connection.commit()
|
||||||
|
|
||||||
|
bot_id = hashlib.sha256(handle.encode('utf-8')).digest()
|
||||||
|
c.execute("INSERT INTO `bots` (id, user_id, credentials_id, handle) VALUES (%s, %s, %s, %s)", (bot_id, session['user_id'], credentials_id, handle))
|
||||||
|
mysql.connection.commit()
|
||||||
|
|
||||||
|
c.close()
|
||||||
|
|
||||||
|
# clean up unneeded variables
|
||||||
|
del session['code']
|
||||||
|
del session['instance']
|
||||||
|
del session['instance_type']
|
||||||
|
del session['client_id']
|
||||||
|
del session['client_secret']
|
||||||
|
|
||||||
return render_template("bot_create.html")
|
return render_template("bot_create.html")
|
||||||
|
|
||||||
|
@app.route("/do/authenticate_bot")
|
||||||
|
def do_authenticate_bot():
|
||||||
|
session['code'] = request.args.get('code')
|
||||||
|
session['step'] = 4
|
||||||
|
return redirect(url_for("bot_create"), 303)
|
||||||
|
|
||||||
@app.route("/do/signup", methods=['POST'])
|
@app.route("/do/signup", methods=['POST'])
|
||||||
def do_signup():
|
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
|
||||||
|
@ -115,19 +203,18 @@ def do_signup():
|
||||||
if len(request.form['password']) < 8:
|
if len(request.form['password']) < 8:
|
||||||
return show_signup_page("Password too short.")
|
return show_signup_page("Password too short.")
|
||||||
|
|
||||||
user_id = hashlib.sha256(request.form['email'].encode('utf-8')).digest()
|
|
||||||
|
|
||||||
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))
|
||||||
|
|
||||||
# try to sign up
|
# try to sign up
|
||||||
c = mysql.connection.cursor()
|
c = mysql.connection.cursor()
|
||||||
c.execute("INSERT INTO `users` (email, password) VALUES (%s, %s)", (request.form['email'], pw))
|
c.execute("INSERT INTO `users` (email, password) VALUES (%s, %s)", (request.form['email'], pw))
|
||||||
|
user_id = c.lastrowid
|
||||||
mysql.connection.commit()
|
mysql.connection.commit()
|
||||||
c.close()
|
c.close()
|
||||||
|
|
||||||
# success!
|
# success!
|
||||||
session['userid'] = user_id
|
session['user_id'] = user_id
|
||||||
return redirect(url_for('home'))
|
return redirect(url_for('home'))
|
||||||
|
|
||||||
@app.route("/do/signout")
|
@app.route("/do/signout")
|
||||||
|
@ -143,7 +230,7 @@ def do_login():
|
||||||
data = c.fetchone()
|
data = c.fetchone()
|
||||||
c.close()
|
c.close()
|
||||||
if bcrypt.checkpw(pw_hashed, data['password']):
|
if bcrypt.checkpw(pw_hashed, data['password']):
|
||||||
session['userid'] = data['id']
|
session['user_id'] = data['id']
|
||||||
return redirect(url_for("home"))
|
return redirect(url_for("home"))
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
|
Loading…
Reference in a new issue