From 879b53889dee0987fc1596e4269a74d13c3165e7 Mon Sep 17 00:00:00 2001 From: Lynne Date: Mon, 2 Sep 2019 16:36:42 +1000 Subject: [PATCH] use bot handle as PK --- setup.sql | 11 +++++------ templates/bot_accounts_add.html | 2 +- templates/bot_create.html | 2 +- webui.py | 11 +++++------ 4 files changed, 12 insertions(+), 14 deletions(-) diff --git a/setup.sql b/setup.sql index ed0eae6..513fcdf 100644 --- a/setup.sql +++ b/setup.sql @@ -15,10 +15,9 @@ CREATE TABLE IF NOT EXISTS `credentials` ( `secret` VARCHAR(128) NOT NULL ) ENGINE=INNODB; CREATE TABLE IF NOT EXISTS `bots` ( - `id` BINARY(32) PRIMARY KEY, + `handle` VARCHAR(128) PRIMARY KEY, `user_id` INT NOT NULL, `credentials_id` INT NOT NULL, - `handle` VARCHAR(128) NOT NULL, `enabled` BOOLEAN DEFAULT 1, `replies_enabled` BOOLEAN DEFAULT 1, `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 ) ENGINE=INNODB; 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, - 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 ) ENGINE=INNODB; CREATE TABLE IF NOT EXISTS `posts` ( @@ -58,10 +57,10 @@ CREATE TABLE IF NOT EXISTS `posts` ( ) ENGINE=INNODB; CREATE TABLE IF NOT EXISTS `word_blacklist` ( `id` INT AUTO_INCREMENT PRIMARY KEY, - `bot_id` BINARY(32) NOT NULL, + `bot_id` VARCHAR(128) NOT NULL, `phrase` VARCHAR(128) 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; CREATE TABLE IF NOT EXISTS `contact_history` ( `user_id` INT NOT NULL, diff --git a/templates/bot_accounts_add.html b/templates/bot_accounts_add.html index aff0f70..69c26ce 100644 --- a/templates/bot_accounts_add.html +++ b/templates/bot_accounts_add.html @@ -38,7 +38,7 @@
Cancel {% if session['step'] != 1 %} - + Back {% endif %}
diff --git a/templates/bot_create.html b/templates/bot_create.html index 3f84fc7..1298b13 100644 --- a/templates/bot_create.html +++ b/templates/bot_create.html @@ -42,7 +42,7 @@ {% if session['step'] != 1 %} Back {% endif %} - {% if session['step'] < 5 %} + {% if session['step'] < 4 %} {% else %} Finish diff --git a/webui.py b/webui.py index a492f68..60f2bc1 100644 --- a/webui.py +++ b/webui.py @@ -28,22 +28,22 @@ def home(): c.execute("SELECT COUNT(*) FROM `bots` WHERE user_id = %s", (session['user_id'],)) bot_count = c.fetchone()[0] active_count = None - bots = None + bots = {} bot_users = None if bot_count > 0: c.execute("SELECT COUNT(*) FROM `bots` WHERE user_id = %s AND enabled = TRUE", (session['user_id'],)) active_count = c.fetchone()[0] 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() dc.close() bot_users = {} for bot in bots: # 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'],)) - bot_users[bot['id']] = c.fetchone()[0] + c.execute("SELECT COUNT(*) FROM `bot_learned_accounts` WHERE bot_id = %s", (bot['handle'],)) + bot_users[bot['handle']] = c.fetchone()[0] c.close() 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 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)) + c.execute("INSERT INTO `bots` (handle, user_id, credentials_id) VALUES (%s, %s, %s)", (handle, session['user_id'], credentials_id)) mysql.connection.commit() c.close()