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

use bot handle as PK

This commit is contained in:
Lynne Megido 2019-09-02 16:36:42 +10:00
parent 4230986c2b
commit 879b53889d
4 changed files with 12 additions and 14 deletions

View file

@ -15,10 +15,9 @@ CREATE TABLE IF NOT EXISTS `credentials` (
`secret` VARCHAR(128) NOT NULL `secret` VARCHAR(128) NOT NULL
) ENGINE=INNODB; ) ENGINE=INNODB;
CREATE TABLE IF NOT EXISTS `bots` ( CREATE TABLE IF NOT EXISTS `bots` (
`id` BINARY(32) 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,
`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,
@ -43,9 +42,9 @@ CREATE TABLE IF NOT EXISTS `fedi_accounts` (
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` ( CREATE TABLE IF NOT EXISTS `bot_learned_accounts` (
`bot_id` BINARY(32) NOT NULL, `bot_id` VARCHAR(128) NOT NULL,
`fedi_id` VARCHAR(128) NOT NULL, `fedi_id` VARCHAR(128) NOT NULL,
FOREIGN KEY (`bot_id`) REFERENCES bots(id) ON DELETE CASCADE, FOREIGN KEY (`bot_id`) REFERENCES bots(handle) ON DELETE CASCADE,
FOREIGN KEY (`fedi_id`) REFERENCES fedi_accounts(handle) ON DELETE CASCADE FOREIGN KEY (`fedi_id`) REFERENCES fedi_accounts(handle) ON DELETE CASCADE
) ENGINE=INNODB; ) ENGINE=INNODB;
CREATE TABLE IF NOT EXISTS `posts` ( CREATE TABLE IF NOT EXISTS `posts` (
@ -58,10 +57,10 @@ CREATE TABLE IF NOT EXISTS `posts` (
) ENGINE=INNODB; ) ENGINE=INNODB;
CREATE TABLE IF NOT EXISTS `word_blacklist` ( CREATE TABLE IF NOT EXISTS `word_blacklist` (
`id` INT AUTO_INCREMENT PRIMARY KEY, `id` INT AUTO_INCREMENT PRIMARY KEY,
`bot_id` BINARY(32) NOT NULL, `bot_id` VARCHAR(128) NOT NULL,
`phrase` VARCHAR(128) NOT NULL, `phrase` VARCHAR(128) NOT NULL,
`whole_word` BOOLEAN NOT NULL, `whole_word` BOOLEAN NOT NULL,
FOREIGN KEY (`bot_id`) REFERENCES bots(id) ON DELETE CASCADE FOREIGN KEY (`bot_id`) REFERENCES bots(handle) ON DELETE CASCADE
) ENGINE=INNODB; ) ENGINE=INNODB;
CREATE TABLE IF NOT EXISTS `contact_history` ( CREATE TABLE IF NOT EXISTS `contact_history` (
`user_id` INT NOT NULL, `user_id` INT NOT NULL,

View file

@ -38,7 +38,7 @@
<div class="container centred"> <div class="container centred">
<a href="/" class="button btn-secondary"><i class="fas fa-times"></i> Cancel</a> <a href="/" class="button btn-secondary"><i class="fas fa-times"></i> Cancel</a>
{% if session['step'] != 1 %} {% if session['step'] != 1 %}
<button class="button btn-secondary"><i class="fas fa-arrow-left"></i> Back</button> <a href="/bot/accounts/add/back" class="button btn-secondary"><i class="fas fa-arrow-left"></i> Back</a>
{% endif %} {% endif %}
<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>
</div> </div>

View file

@ -42,7 +42,7 @@
{% if session['step'] != 1 %} {% if session['step'] != 1 %}
<a href="/bot/create/back" class="button btn-secondary"><i class="fas fa-arrow-left"></i> Back</a> <a href="/bot/create/back" class="button btn-secondary"><i class="fas fa-arrow-left"></i> Back</a>
{% endif %} {% endif %}
{% if session['step'] < 5 %} {% if session['step'] < 4 %}
<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 %} {% else %}
<a href="/" class="button btn-primary"><i class="fas fa-check"></i> Finish</a> <a href="/" class="button btn-primary"><i class="fas fa-check"></i> Finish</a>

View file

@ -28,22 +28,22 @@ def home():
c.execute("SELECT COUNT(*) FROM `bots` WHERE user_id = %s", (session['user_id'],)) 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 bots = {}
bot_users = 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['user_id'],)) 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 = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
dc.execute("SELECT * FROM `bots` WHERE user_id = %s", (session['user_id'],)) dc.execute("SELECT handle` FROM `bots` WHERE user_id = %s", (session['user_id'],))
bots = dc.fetchall() bots = dc.fetchall()
dc.close() dc.close()
bot_users = {} bot_users = {}
for bot in bots: for bot in bots:
# multiple SELECTS is slow, maybe SELECT all at once and filter with python? # multiple SELECTS is slow, maybe SELECT all at once and filter with python?
c.execute("SELECT COUNT(*) FROM `bot_learned_accounts` WHERE bot_id = %s", (bot['id'],)) c.execute("SELECT COUNT(*) FROM `bot_learned_accounts` WHERE bot_id = %s", (bot['handle'],))
bot_users[bot['id']] = c.fetchone()[0] bot_users[bot['handle']] = c.fetchone()[0]
c.close() c.close()
return render_template("home.html", bot_count = bot_count, active_count = active_count, bots = bots, bot_users = bot_users) return render_template("home.html", bot_count = bot_count, active_count = active_count, bots = bots, bot_users = bot_users)
@ -173,8 +173,7 @@ def bot_create():
credentials_id = c.lastrowid credentials_id = c.lastrowid
mysql.connection.commit() mysql.connection.commit()
bot_id = hashlib.sha256(handle.encode('utf-8')).digest() c.execute("INSERT INTO `bots` (handle, user_id, credentials_id) VALUES (%s, %s, %s)", (handle, session['user_id'], credentials_id))
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() mysql.connection.commit()
c.close() c.close()