changed toot logic

This commit is contained in:
b3yond 2018-09-24 20:54:57 +02:00
parent b94ead7041
commit 732ac1c5d3
3 changed files with 10 additions and 27 deletions

View file

@ -29,12 +29,10 @@ 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() is 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()): not user.toot_is_seen(status['status']['uri'])):
# save state # save state
user.save_seen_toot(status['status']['id']) user.toot_witness(status['status']['uri'])
# add mention to mentions # add mention to mentions
text = re.sub(r'<[^>]*>', '', status['status']['content']) text = re.sub(r'<[^>]*>', '', status['status']['content'])
text = re.sub( text = re.sub(

5
db.py
View file

@ -69,11 +69,8 @@ class DB(object):
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,
user_id INTEGER, user_id INTEGER,
mastodon_accounts_id INTEGER, toot_uri TEXT,
toot_id INTEGER,
FOREIGN KEY(user_id) REFERENCES user(id), FOREIGN KEY(user_id) REFERENCES user(id),
FOREIGN KEY(mastodon_accounts_id)
REFERENCES mastodon_accounts(id)
); );
CREATE TABLE IF NOT EXISTS seen_telegrams ( CREATE TABLE IF NOT EXISTS seen_telegrams (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,

26
user.py
View file

@ -154,26 +154,14 @@ schlitz
(date, self.uid)) (date, self.uid))
db.commit() db.commit()
def init_seen_toot(self, instance_url): def toot_is_seen(self, toot_uri):
db.execute("SELECT id FROM mastodon_instances WHERE instance = ?;", db.execute("SELECT COUNT(*) FROM seen_toots WHERE user_id = ? AND toot_uri = ?;",
(instance_url,)) (self.uid, toot_uri))
masto_instance = db.cur.fetchone()[0] return db.cur.fetchone()[0] > 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 get_seen_toot(self): def toot_witness(self, toot_uri):
db.execute("SELECT toot_id FROM seen_toots WHERE user_id = ?;", db.execute("INSERT INTO seen_toots SET (toot_uri, user_id) VALUES (?,?);",
(self.uid,)) (toot_uri, 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))
db.commit() db.commit()
def get_seen_tweet(self): def get_seen_tweet(self):