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
) 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,

View File

@ -38,7 +38,7 @@
<div class="container centred">
<a href="/" class="button btn-secondary"><i class="fas fa-times"></i> Cancel</a>
{% 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 %}
<button class="button btn-primary"><i class="fas fa-arrow-right"></i> Next</button>
</div>

View File

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

View File

@ -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()