fixed seen_toot problem

This commit is contained in:
b3yond 2018-09-09 20:22:41 +02:00
parent 6f3c953736
commit a54538bcea
6 changed files with 32 additions and 18 deletions

View file

@ -29,6 +29,8 @@ class MastodonBot(Bot):
logger.error("Unknown Mastodon API Error.", exc_info=True) logger.error("Unknown Mastodon API Error.", exc_info=True)
return mentions return mentions
for status in notifications: for status in notifications:
if user.get_seen_toot() == None:
user.init_seen_toot(m.instance()['uri'])
if (status['type'] == 'mention' and if (status['type'] == 'mention' and
status['status']['id'] > user.get_seen_toot()): status['status']['id'] > user.get_seen_toot()):
# save state # save state

View file

@ -19,6 +19,7 @@ class TelegramBot(Bot):
reports = [] reports = []
for update in updates: for update in updates:
try: try:
user.save_seen_tg(update.update_id)
if update.message.text.lower() == "/start": if update.message.text.lower() == "/start":
user.add_telegram_subscribers(update.message.sender.id) user.add_telegram_subscribers(update.message.sender.id)
tb.send_message(update.message.sender.id, "You are now subscribed to report notifications.") tb.send_message(update.message.sender.id, "You are now subscribed to report notifications.")
@ -33,9 +34,8 @@ class TelegramBot(Bot):
else: else:
reports.append(Report(update.message.sender.username, self, reports.append(Report(update.message.sender.username, self,
update.message.text, None, update.message.date)) update.message.text, None, update.message.date))
user.save_seen_tg(update.update_id)
except AttributeError: except AttributeError:
print(updates[0], updates[1]) # Telegram API returns an Error logger.error('Some Attribute Error. ', exc_info=True)
return reports return reports
return reports return reports

View file

@ -46,7 +46,6 @@ class TwitterBot(Bot):
else: else:
mentions = api.mentions_timeline(since_id=last_mention) mentions = api.mentions_timeline(since_id=last_mention)
user.set_last_twitter_request(time()) user.set_last_twitter_request(time())
print(time())
for status in mentions: for status in mentions:
text = re.sub( text = re.sub(
"(?<=^|(?<=[^a-zA-Z0-9-_\.]))@([A-Za-z]+[A-Za-z0-9-_]+)", "(?<=^|(?<=[^a-zA-Z0-9-_\.]))@([A-Za-z]+[A-Za-z0-9-_]+)",

18
db.py
View file

@ -67,10 +67,10 @@ class DB(object):
FOREIGN KEY(instance_id) REFERENCES mastodon_instances(id) FOREIGN KEY(instance_id) REFERENCES mastodon_instances(id)
); );
CREATE TABLE IF NOT EXISTS seen_toots ( CREATE TABLE IF NOT EXISTS seen_toots (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE DEFAULT 1,
user_id INTEGER, user_id INTEGER DEFAULT 1,
mastodon_accounts_id INTEGER, mastodon_accounts_id INTEGER DEFAULT 1,
toot_id TEXT, toot_id INTEGER DEFAULT 0,
FOREIGN KEY(user_id) REFERENCES user(id), FOREIGN KEY(user_id) REFERENCES user(id),
FOREIGN KEY(mastodon_accounts_id) FOREIGN KEY(mastodon_accounts_id)
REFERENCES mastodon_accounts(id) REFERENCES mastodon_accounts(id)
@ -235,8 +235,7 @@ class DB(object):
self.execute("INSERT INTO user (passhash) VALUES(?);", self.execute("INSERT INTO user (passhash) VALUES(?);",
(json['passhash'], )) (json['passhash'], ))
uid = self.cur.lastrowid uid = self.cur.lastrowid
default_triggerpatterns = """ default_triggerpatterns = """kontroll?e
kontroll?e
konti konti
db db
vgn vgn
@ -252,8 +251,7 @@ linie
nuernberg nuernberg
nürnberg nürnberg
s\d s\d
u\d\d? u\d\d?"""
"""
self.execute("""INSERT INTO triggerpatterns (user_id, patterns) self.execute("""INSERT INTO triggerpatterns (user_id, patterns)
VALUES(?, ?); """, (uid, default_triggerpatterns)) VALUES(?, ?); """, (uid, default_triggerpatterns))
self.execute("INSERT INTO badwords (user_id, words) VALUES(?, ?);", self.execute("INSERT INTO badwords (user_id, words) VALUES(?, ?);",
@ -266,10 +264,10 @@ u\d\d?
active) VALUES(?, ?, ?);""", (uid, "", 1)) active) VALUES(?, ?, ?);""", (uid, "", 1))
self.execute("INSERT INTO seen_telegrams (user_id, tg_id) VALUES (?,?);", self.execute("INSERT INTO seen_telegrams (user_id, tg_id) VALUES (?,?);",
(uid, 0)) (uid, 0))
self.execute("INSERT INTO seen_mail (user_id, mail_date) VALUES (?,?);",
(uid, 0))
self.commit() self.commit()
user = User(uid) user = User(uid)
self.execute("INSERT INTO seen_mail (user_id, mail_date) VALUES (?,?)",
(uid, 0))
user.set_city(city) user.set_city(city)
return user return user

View file

@ -236,16 +236,19 @@ def login_mastodon(user):
# get app tokens # get app tokens
instance_url = request.forms.get('instance_url') instance_url = request.forms.get('instance_url')
masto_email = request.forms.get('email') masto_email = request.forms.get('email')
print(masto_email)
masto_pass = request.forms.get('pass') masto_pass = request.forms.get('pass')
print(masto_pass)
client_id, client_secret = user.get_mastodon_app_keys(instance_url) client_id, client_secret = user.get_mastodon_app_keys(instance_url)
m = Mastodon(client_id=client_id, client_secret=client_secret, m = Mastodon(client_id=client_id, client_secret=client_secret,
api_base_url=instance_url) api_base_url=instance_url)
try: try:
access_token = m.log_in(masto_email, masto_pass) access_token = m.log_in(masto_email, masto_pass)
user.save_masto_token(access_token, instance_url) 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: except Exception:
logger.error('Login to Mastodon failed.', exc_info=True) logger.error('Login to Mastodon failed.', exc_info=True)
return dict(error='Login to Mastodon failed.') return dict(error='Login to Mastodon failed.')

12
user.py
View file

@ -154,10 +154,22 @@ schlitz
(date, self.uid)) (date, self.uid))
db.commit() 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): def get_seen_toot(self):
db.execute("SELECT toot_id FROM seen_toots WHERE user_id = ?;", db.execute("SELECT toot_id FROM seen_toots WHERE user_id = ?;",
(self.uid,)) (self.uid,))
try:
return db.cur.fetchone()[0] return db.cur.fetchone()[0]
except TypeError:
return None
def save_seen_toot(self, toot_id): def save_seen_toot(self, toot_id):
db.execute("UPDATE seen_toots SET toot_id = ? WHERE user_id = ?;", db.execute("UPDATE seen_toots SET toot_id = ? WHERE user_id = ?;",