Added direct messages and followers to streaming API implementation

This commit is contained in:
Manuel Cortez 2022-11-19 19:36:21 -06:00
parent 149ce51f49
commit ce458a8d4d
No known key found for this signature in database
GPG Key ID: 9E0735CA15EFE790
3 changed files with 60 additions and 16 deletions

View File

@ -128,6 +128,7 @@ class Controller(object):
# Mastodon specific events.
pub.subscribe(self.mastodon_new_item, "mastodon.new_item")
pub.subscribe(self.mastodon_new_conversation, "mastodon.conversation_received")
# connect application events to GUI
widgetUtils.connect_event(self.view, widgetUtils.CLOSE_EVENT, self.exit_)
@ -1233,7 +1234,24 @@ class Controller(object):
elif buff == "mentions": sound_to_play = "mention_received.ogg"
elif buff == "direct_messages": sound_to_play = "dm_received.ogg"
elif buff == "sent": sound_to_play = "tweet_send.ogg"
elif buff == "followers" or buff == "following": sound_to_play = "update_followers.ogg"
elif "timeline" in buff: sound_to_play = "tweet_timeline.ogg"
else: sound_to_play = None
if sound_to_play != None and buff not in buffer.session.settings["other_buffers"]["muted_buffers"]:
self.notify(buffer.session, sound_to_play)
# Normally, we'd define this function on mastodon's session, but we need to access conversationListBuffer and here is the best place to do so.
def mastodon_new_conversation(self, conversation, session_name):
buffer = self.search_buffer("direct_messages", session_name)
if buffer == None:
print("Buffer not found.")
return # Direct messages buffer is hidden
new_position, number_of_items = buffer.order_buffer([conversation])
print(new_position, number_of_items)
buffer.put_items_on_list(number_of_items)
if new_position > -1:
buffer.buffer.list.select_item(new_position)
if number_of_items > 0:
sound_to_play = "dm_received.ogg"
if "direct_messages" not in buffer.session.settings["other_buffers"]["muted_buffers"]:
self.notify(buffer.session, sound_to_play)

View File

@ -30,7 +30,8 @@ class Session(base.baseSession):
self.type = "mastodon"
self.db["pagination_info"] = dict()
self.char_limit = 500
pub.subscribe(self.on_status, "mastodon.new_status")
pub.subscribe(self.on_status, "mastodon.status_received")
pub.subscribe(self.on_notification, "mastodon.notification_received")
def login(self, verify_credentials=True):
if self.settings["mastodon"]["access_token"] != None and self.settings["mastodon"]["instance"] != None:
@ -125,9 +126,6 @@ class Session(base.baseSession):
return user_alias
return user.name
def check_streams(self):
pass
def order_buffer(self, name, data, ignore_older=False):
num = 0
last_id = None
@ -209,22 +207,25 @@ class Session(base.baseSession):
if config.app["app-settings"]["no_streaming"]:
return
listener = streaming.StreamListener(session_name=self.get_name(), user_id=self.db["user_id"])
self.stream = self.api.stream_user(listener, run_async=True, reconnect_async=True)
self.user_stream = self.api.stream_user(listener, run_async=True)
self.direct_stream = self.api.stream_direct(listener, run_async=True)
def stop_streaming(self):
if config.app["app-settings"]["no_streaming"]:
return
if hasattr(self, "stream"):
self.stream.close()
log.debug("Stream stopped for accounr {}".format(self.db["user_name"]))
# if hasattr(self, "user_stream"):
# self.user_stream.close()
# log.debug("Stream stopped for accounr {}".format(self.db["user_name"]))
# if hasattr(self, "direct_stream"):
# self.direct_stream.close()
# log.debug("Stream stopped for accounr {}".format(self.db["user_name"]))
def check_streams(self):
if config.app["app-settings"]["no_streaming"]:
return
if not hasattr(self, "stream"):
if not hasattr(self, "user_stream"):
return
log.debug("Status of running stream for user {}: {}".format(self.db["user_name"], self.stream.running))
if self.stream.is_alive() == False or self.stream.is_receiving() == False:
if self.user_stream.is_alive() == False or self.user_stream.is_receiving() == False or self.direct_stream.is_alive() == False or self.direct_stream.is_receiving() == False:
self.start_streaming()
def check_buffers(self, status):
@ -232,9 +233,6 @@ class Session(base.baseSession):
buffers.append("home_timeline")
if status.account.id == self.db["user_id"]:
buffers.append("sent")
mentions = [user.id for user in status.mentions]
if self.db["user_id"] in mentions:
buffers.append("mentions")
return buffers
def on_status(self, status, session_name):
@ -247,3 +245,21 @@ class Session(base.baseSession):
if num == 0:
buffers.remove(b)
pub.sendMessage("mastodon.new_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:
return
buffers = []
obj = None
if notification.type == "mention":
buffers = ["mentions"]
obj = notification.status
elif notification.type == "follow":
buffers = ["followers"]
obj = notification.account
for b in buffers[::]:
num = self.order_buffer(b, [obj])
if num == 0:
buffers.remove(b)
pub.sendMessage("mastodon.new_item", session_name=self.get_name(), item=obj, _buffers=buffers)

View File

@ -10,4 +10,14 @@ class StreamListener(mastodon.StreamListener):
super(StreamListener, self).__init__()
def on_update(self, status):
pub.sendMessage("mastodon.new_status", status=status, session_name=self.session_name)
pub.sendMessage("mastodon.status_received", status=status, session_name=self.session_name)
def on_conversation(self, conversation):
print("New conversation: {}".format(conversation))
pub.sendMessage("mastodon.conversation_received", conversation=conversation, session_name=self.session_name)
def on_notification(self, notification):
pub.sendMessage("mastodon.notification_received", notification=notification, session_name=self.session_name)
def on_unknown_event(self, event):
log.error("Unknown event: {}".format(event))