From d177ef5be25bf63499a28d2bf1416799911a25fe Mon Sep 17 00:00:00 2001 From: Manuel Cortez Date: Fri, 25 Nov 2022 17:30:57 -0600 Subject: [PATCH] Initial support for edited statuses in Streaming API. Only Works in invisible interface for now --- src/sessions/mastodon/session.py | 25 +++++++++++++++++++++++++ src/sessions/mastodon/streaming.py | 3 +++ 2 files changed, 28 insertions(+) diff --git a/src/sessions/mastodon/session.py b/src/sessions/mastodon/session.py index e021abeb..31a8d0a5 100644 --- a/src/sessions/mastodon/session.py +++ b/src/sessions/mastodon/session.py @@ -31,6 +31,7 @@ class Session(base.baseSession): self.db["pagination_info"] = dict() self.char_limit = 500 pub.subscribe(self.on_status, "mastodon.status_received") + pub.subscribe(self.on_status_updated, "mastodon.status_updated") pub.subscribe(self.on_notification, "mastodon.notification_received") def login(self, verify_credentials=True): @@ -149,6 +150,19 @@ class Session(base.baseSession): self.db[name] = objects return num + def update_item(self, name, item): + if name not in self.db: + return False + items = self.db[name] + if type(items) != list: + return False + # determine item position in buffer. + item_position = next((x for x in range(len(items)) if items[x].id == item.id), None) + if item_position != None: + self.db[name][item_position] = item + return True + return False + def api_call(self, call_name, action="", _sound=None, report_success=False, report_failure=True, preexec_message="", *args, **kwargs): finished = False tries = 0 @@ -246,6 +260,17 @@ class Session(base.baseSession): buffers.remove(b) pub.sendMessage("mastodon.new_item", session_name=self.get_name(), item=status, _buffers=buffers) + def on_status_updated(self, status, session_name): + # Discard processing the status if the streaming sends a tweet for another account. + if self.get_name() != session_name: + return + buffers = [] + for b in list(self.db.keys()): + updated = self.update_item(b, status) + if updated: + buffers.append(b) + pub.sendMessage("mastodon.updated_item", session_name=self.get_name(), item=status, _buffers=buffers) + def on_notification(self, notification, session_name): # Discard processing the notification if the streaming sends a tweet for another account. if self.get_name() != session_name: diff --git a/src/sessions/mastodon/streaming.py b/src/sessions/mastodon/streaming.py index e74fc2d5..b9e56135 100644 --- a/src/sessions/mastodon/streaming.py +++ b/src/sessions/mastodon/streaming.py @@ -12,6 +12,9 @@ class StreamListener(mastodon.StreamListener): def on_update(self, status): pub.sendMessage("mastodon.status_received", status=status, session_name=self.session_name) + def on_status_update(self, status): + pub.sendMessage("mastodon.status_updated", status=status, session_name=self.session_name) + def on_conversation(self, conversation): pub.sendMessage("mastodon.conversation_received", conversation=conversation, session_name=self.session_name)