From 732ac1c5d3ce6f0b2481fdf3321014052d36f708 Mon Sep 17 00:00:00 2001 From: b3yond Date: Mon, 24 Sep 2018 20:54:57 +0200 Subject: [PATCH] changed toot logic --- active_bots/mastodonbot.py | 6 ++---- db.py | 5 +---- user.py | 26 +++++++------------------- 3 files changed, 10 insertions(+), 27 deletions(-) diff --git a/active_bots/mastodonbot.py b/active_bots/mastodonbot.py index 6e74f3e..0189dca 100755 --- a/active_bots/mastodonbot.py +++ b/active_bots/mastodonbot.py @@ -29,12 +29,10 @@ class MastodonBot(Bot): logger.error("Unknown Mastodon API Error.", exc_info=True) return mentions for status in notifications: - if user.get_seen_toot() is None: - user.init_seen_toot(m.instance()['uri']) if (status['type'] == 'mention' and - status['status']['id'] > user.get_seen_toot()): + not user.toot_is_seen(status['status']['uri'])): # save state - user.save_seen_toot(status['status']['id']) + user.toot_witness(status['status']['uri']) # add mention to mentions text = re.sub(r'<[^>]*>', '', status['status']['content']) text = re.sub( diff --git a/db.py b/db.py index b8d7318..2aff08a 100644 --- a/db.py +++ b/db.py @@ -69,11 +69,8 @@ class DB(object): CREATE TABLE IF NOT EXISTS seen_toots ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, user_id INTEGER, - mastodon_accounts_id INTEGER, - toot_id INTEGER, + toot_uri TEXT, FOREIGN KEY(user_id) REFERENCES user(id), - FOREIGN KEY(mastodon_accounts_id) - REFERENCES mastodon_accounts(id) ); CREATE TABLE IF NOT EXISTS seen_telegrams ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, diff --git a/user.py b/user.py index 3c62e22..ccd9656 100644 --- a/user.py +++ b/user.py @@ -154,26 +154,14 @@ schlitz (date, self.uid)) db.commit() - def init_seen_toot(self, instance_url): - db.execute("SELECT id FROM mastodon_instances WHERE instance = ?;", - (instance_url,)) - masto_instance = db.cur.fetchone()[0] - db.execute("INSERT INTO seen_toots (user_id, mastodon_accounts_id, toot_id) VALUES (?,?,?);", - (self.uid, masto_instance, 0)) - db.conn.commit() - return + def toot_is_seen(self, toot_uri): + db.execute("SELECT COUNT(*) FROM seen_toots WHERE user_id = ? AND toot_uri = ?;", + (self.uid, toot_uri)) + return db.cur.fetchone()[0] > 0 - def get_seen_toot(self): - db.execute("SELECT toot_id FROM seen_toots WHERE user_id = ?;", - (self.uid,)) - try: - return db.cur.fetchone()[0] - except TypeError: - return None - - def save_seen_toot(self, toot_id): - db.execute("UPDATE seen_toots SET toot_id = ? WHERE user_id = ?;", - (toot_id, self.uid)) + def toot_witness(self, toot_uri): + db.execute("INSERT INTO seen_toots SET (toot_uri, user_id) VALUES (?,?);", + (toot_uri, self.uid)) db.commit() def get_seen_tweet(self):