diff --git a/active_bots/mastodonbot.py b/active_bots/mastodonbot.py index a7f735b..5127eab 100755 --- a/active_bots/mastodonbot.py +++ b/active_bots/mastodonbot.py @@ -29,6 +29,8 @@ class MastodonBot(Bot): logger.error("Unknown Mastodon API Error.", exc_info=True) return mentions for status in notifications: + if user.get_seen_toot() == None: + user.init_seen_toot(m.instance()['uri']) if (status['type'] == 'mention' and status['status']['id'] > user.get_seen_toot()): # save state diff --git a/active_bots/telegrambot.py b/active_bots/telegrambot.py index 722ce17..12a85a6 100644 --- a/active_bots/telegrambot.py +++ b/active_bots/telegrambot.py @@ -19,6 +19,7 @@ class TelegramBot(Bot): reports = [] for update in updates: try: + user.save_seen_tg(update.update_id) if update.message.text.lower() == "/start": user.add_telegram_subscribers(update.message.sender.id) tb.send_message(update.message.sender.id, "You are now subscribed to report notifications.") @@ -32,10 +33,9 @@ class TelegramBot(Bot): # TODO: /help message should be set in frontend else: reports.append(Report(update.message.sender.username, self, - update.message.text, None, update.message.date)) - user.save_seen_tg(update.update_id) + update.message.text, None, update.message.date)) except AttributeError: - print(updates[0], updates[1]) # Telegram API returns an Error + logger.error('Some Attribute Error. ', exc_info=True) return reports return reports diff --git a/active_bots/twitterbot.py b/active_bots/twitterbot.py index eb4e4bd..bf127a2 100755 --- a/active_bots/twitterbot.py +++ b/active_bots/twitterbot.py @@ -46,7 +46,6 @@ class TwitterBot(Bot): else: mentions = api.mentions_timeline(since_id=last_mention) user.set_last_twitter_request(time()) - print(time()) for status in mentions: text = re.sub( "(?<=^|(?<=[^a-zA-Z0-9-_\.]))@([A-Za-z]+[A-Za-z0-9-_]+)", diff --git a/db.py b/db.py index a224a0f..dd3a695 100644 --- a/db.py +++ b/db.py @@ -67,10 +67,10 @@ class DB(object): FOREIGN KEY(instance_id) REFERENCES mastodon_instances(id) ); CREATE TABLE IF NOT EXISTS seen_toots ( - id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, - user_id INTEGER, - mastodon_accounts_id INTEGER, - toot_id TEXT, + id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE DEFAULT 1, + user_id INTEGER DEFAULT 1, + mastodon_accounts_id INTEGER DEFAULT 1, + toot_id INTEGER DEFAULT 0, FOREIGN KEY(user_id) REFERENCES user(id), FOREIGN KEY(mastodon_accounts_id) REFERENCES mastodon_accounts(id) @@ -235,8 +235,7 @@ class DB(object): self.execute("INSERT INTO user (passhash) VALUES(?);", (json['passhash'], )) uid = self.cur.lastrowid - default_triggerpatterns = """ -kontroll?e + default_triggerpatterns = """kontroll?e konti db vgn @@ -252,8 +251,7 @@ linie nuernberg nürnberg s\d -u\d\d? - """ +u\d\d?""" self.execute("""INSERT INTO triggerpatterns (user_id, patterns) VALUES(?, ?); """, (uid, default_triggerpatterns)) self.execute("INSERT INTO badwords (user_id, words) VALUES(?, ?);", @@ -266,10 +264,10 @@ u\d\d? active) VALUES(?, ?, ?);""", (uid, "", 1)) self.execute("INSERT INTO seen_telegrams (user_id, tg_id) VALUES (?,?);", (uid, 0)) + self.execute("INSERT INTO seen_mail (user_id, mail_date) VALUES (?,?);", + (uid, 0)) self.commit() user = User(uid) - self.execute("INSERT INTO seen_mail (user_id, mail_date) VALUES (?,?)", - (uid, 0)) user.set_city(city) return user diff --git a/frontend.py b/frontend.py index adae5fc..7b71ee6 100755 --- a/frontend.py +++ b/frontend.py @@ -236,16 +236,19 @@ def login_mastodon(user): # get app tokens instance_url = request.forms.get('instance_url') masto_email = request.forms.get('email') - print(masto_email) masto_pass = request.forms.get('pass') - print(masto_pass) client_id, client_secret = user.get_mastodon_app_keys(instance_url) m = Mastodon(client_id=client_id, client_secret=client_secret, api_base_url=instance_url) try: access_token = m.log_in(masto_email, masto_pass) user.save_masto_token(access_token, instance_url) - city_page(user.get_city(), info='Thanks for supporting decentralized social networks!') + + # Trying to set the seen_toot to 0, thereby initializing it. + # It should work now, but has default values. Not sure if I need them. + user.init_seen_toot(instance_url) + + return city_page(user.get_city(), info='Thanks for supporting decentralized social networks!') except Exception: logger.error('Login to Mastodon failed.', exc_info=True) return dict(error='Login to Mastodon failed.') diff --git a/user.py b/user.py index da7ffe5..49b4aef 100644 --- a/user.py +++ b/user.py @@ -154,10 +154,22 @@ 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) VALUES (?,?);", + (self.uid, masto_instance)) + db.conn.commit() + return + def get_seen_toot(self): db.execute("SELECT toot_id FROM seen_toots WHERE user_id = ?;", (self.uid,)) - return db.cur.fetchone()[0] + 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 = ?;",