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

View file

@ -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.")
@ -33,9 +34,8 @@ class TelegramBot(Bot):
else:
reports.append(Report(update.message.sender.username, self,
update.message.text, None, update.message.date))
user.save_seen_tg(update.update_id)
except AttributeError:
print(updates[0], updates[1]) # Telegram API returns an Error
logger.error('Some Attribute Error. ', exc_info=True)
return reports
return reports

View file

@ -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-_]+)",

18
db.py
View file

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

View file

@ -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.')

12
user.py
View file

@ -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,))
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 = ?;",