[mastodon] Working now: toot reports from mastodon, but only when the next report arrives

This commit is contained in:
missytake 2022-03-04 18:13:20 +01:00
parent f2db6d4409
commit f66375cd77

View file

@ -8,8 +8,8 @@ from kibicara.platformapi import Censor, Spawner, Message
from kibicara.platforms.mastodon.model import MastodonAccount from kibicara.platforms.mastodon.model import MastodonAccount
from mastodon import Mastodon, MastodonError from mastodon import Mastodon, MastodonError
from asyncio import gather, get_event_loop from asyncio import gather
import sys import re
from logging import getLogger from logging import getLogger
@ -27,6 +27,7 @@ class MastodonBot(Censor):
self.account = Mastodon( self.account = Mastodon(
client_id=self.model.instance.client_id, client_id=self.model.instance.client_id,
client_secret=self.model.instance.client_secret, client_secret=self.model.instance.client_secret,
api_base_url=self.model.instance.name,
access_token=self.model.access_token, access_token=self.model.access_token,
) )
await gather(self.poll(), self.push()) await gather(self.poll(), self.push())
@ -36,22 +37,26 @@ class MastodonBot(Censor):
while True: while True:
try: try:
notifications = self.account.notifications() notifications = self.account.notifications()
except MastodonError: except MastodonError as e:
logger.warning( logger.warning("%s in hood %s" % (e, self.model.hood.name))
"%s in hood %s" % (sys.exc_info()[0], self.model.hood.name)
)
continue continue
last_seen = int(self.model.last_seen) last_seen = int(self.model.last_seen)
for status in notifications: for status in notifications:
try:
status_id = int(status['status']['id']) status_id = int(status['status']['id'])
except KeyError:
continue # ignore notifications which don't have a status
if status_id <= last_seen: if status_id <= last_seen:
continue # toot was already processed in the past continue # toot was already processed in the past
if status_id > self.model.last_seen: if status_id > int(self.model.last_seen):
self.model.last_seen = status_id # save last_seen in database await self.model.update(last_seen=str(status_id))
text = status['status']['content'] text = re.sub(r'<[^>]*>', '', status['status']['content'])
# :TODO sanitize toot content; see ticketfrei2 for regex magic text = re.sub(
"(?<=^|(?<=[^a-zA-Z0-9-_.]))@([A-Za-z]+[A-Za-z0-9-_]+)", "", text
)
logger.debug( logger.debug(
"Mastodon in %s received message: " % (self.model.hood.name,) "Mastodon in %s received toot #%s: %s"
% (self.model.hood.name, status_id, text)
) )
if status['status']['visibility'] == 'public': if status['status']['visibility'] == 'public':
await self.publish(Message(text, toot_id=status_id)) await self.publish(Message(text, toot_id=status_id))
@ -63,9 +68,11 @@ class MastodonBot(Censor):
while True: while True:
message = await self.receive() message = await self.receive()
if hasattr(message, "tood_id"): if hasattr(message, "tood_id"):
await self.account.status_reblog(message.tood_id) logger.debug("Boosting post %s: %s" % (message.tood_id, message.text))
self.account.status_reblog(message.tood_id)
else: else:
await self.account.status_post(message.text) logger.debug("Posting message: %s" % (message.text,))
self.account.status_post(message.text)
spawner = Spawner(MastodonAccount, MastodonBot) spawner = Spawner(MastodonAccount, MastodonBot)