Rework sessions to be handled with unique names

This commit is contained in:
Manuel Cortez 2022-11-15 11:54:59 -06:00
parent 862dbd0b8a
commit 2e8c8c6db4
No known key found for this signature in database
GPG Key ID: 9E0735CA15EFE790
5 changed files with 61 additions and 55 deletions

View File

@ -899,17 +899,19 @@ class Controller(object):
if message != None: if message != None:
output.speak(message, speech=session.settings["reporting"]["speech_reporting"], braille=session.settings["reporting"]["braille_reporting"]) output.speak(message, speech=session.settings["reporting"]["speech_reporting"], braille=session.settings["reporting"]["braille_reporting"])
def manage_sent_dm(self, data, user): def manage_sent_dm(self, data, session_name):
buffer = self.search_buffer("sent_direct_messages", user) buffer = self.search_buffer("sent_direct_messages", session_name)
if buffer == None: return if buffer == None:
return
play_sound = "dm_sent.ogg" play_sound = "dm_sent.ogg"
if "sent_direct_messages" not in buffer.session.settings["other_buffers"]["muted_buffers"]: if "sent_direct_messages" not in buffer.session.settings["other_buffers"]["muted_buffers"]:
self.notify(buffer.session, play_sound=play_sound) self.notify(buffer.session, play_sound=play_sound)
buffer.add_new_item(data) buffer.add_new_item(data)
def manage_sent_tweets(self, data, user): def manage_sent_tweets(self, data, session_name):
buffer = self.search_buffer("sent_tweets", user) buffer = self.search_buffer("sent_tweets", session_name)
if buffer == None: return if buffer == None:
return
data = buffer.session.check_quoted_status(data) data = buffer.session.check_quoted_status(data)
data = buffer.session.check_long_tweet(data) data = buffer.session.check_long_tweet(data)
if data == False: # Long tweet deleted from twishort. if data == False: # Long tweet deleted from twishort.
@ -922,37 +924,43 @@ class Controller(object):
buffer.session.db[buffer.name] = items buffer.session.db[buffer.name] = items
buffer.add_new_item(data) buffer.add_new_item(data)
def manage_friend(self, data, user): def manage_friend(self, data, session_name):
buffer = self.search_buffer("friends", user) buffer = self.search_buffer("friends", session_name)
if buffer == None: return if buffer == None:
return
buffer.add_new_item(data) buffer.add_new_item(data)
def manage_unfollowing(self, item, user): def manage_unfollowing(self, item, session_name):
buffer = self.search_buffer("friends", user) buffer = self.search_buffer("friends", session_name)
if buffer == None: return if buffer == None:
return
buffer.remove_item(item) buffer.remove_item(item)
def manage_favourite(self, data, user): def manage_favourite(self, data, session_name):
buffer = self.search_buffer("favourites", user) buffer = self.search_buffer("favourites", session_name)
if buffer == None: return if buffer == None:
return
play_sound = "favourite.ogg" play_sound = "favourite.ogg"
if "favourites" not in buffer.session.settings["other_buffers"]["muted_buffers"]: if "favourites" not in buffer.session.settings["other_buffers"]["muted_buffers"]:
self.notify(buffer.session, play_sound=play_sound) self.notify(buffer.session, play_sound=play_sound)
buffer.add_new_item(data) buffer.add_new_item(data)
def manage_unfavourite(self, item, user): def manage_unfavourite(self, item, session_name):
buffer = self.search_buffer("favourites", user) buffer = self.search_buffer("favourites", session_name)
if buffer == None: return if buffer == None:
return
buffer.remove_item(item) buffer.remove_item(item)
def manage_blocked_user(self, data, user): def manage_blocked_user(self, data, session_name):
buffer = self.search_buffer("blocked", user) buffer = self.search_buffer("blocked", session_name)
if buffer == None: return if buffer == None:
return
buffer.add_new_item(data) buffer.add_new_item(data)
def manage_unblocked_user(self, item, user): def manage_unblocked_user(self, item, session_name):
buffer = self.search_buffer("blocked", user) buffer = self.search_buffer("blocked", session_name)
if buffer == None: return if buffer == None:
return
buffer.remove_item(item) buffer.remove_item(item)
def start_buffers(self, session): def start_buffers(self, session):
@ -1151,8 +1159,8 @@ class Controller(object):
if hasattr(buffer, "ocr_image"): if hasattr(buffer, "ocr_image"):
return buffer.ocr_image() return buffer.ocr_image()
def update_sent_dms(self, total, account): def update_sent_dms(self, total, session_name):
sent_dms = self.search_buffer("sent_direct_messages", account) sent_dms = self.search_buffer("sent_direct_messages", session_name)
if sent_dms != None: if sent_dms != None:
sent_dms.put_items_on_list(total) sent_dms.put_items_on_list(total)
@ -1165,11 +1173,11 @@ class Controller(object):
for i in sessions.sessions: for i in sessions.sessions:
sessions.sessions[i].save_persistent_data() sessions.sessions[i].save_persistent_data()
def manage_new_tweet(self, data, name, _buffers): def manage_new_tweet(self, data, session_name, _buffers):
sound_to_play = None sound_to_play = None
for buff in _buffers: for buff in _buffers:
buffer = self.search_buffer(buff, name) buffer = self.search_buffer(buff, session_name)
if buffer == None or buffer.session.get_name() != name: if buffer == None or buffer.session.get_name() != session_name:
return return
buffer.add_new_item(data) buffer.add_new_item(data)
if buff == "home_timeline": sound_to_play = "tweet_received.ogg" if buff == "home_timeline": sound_to_play = "tweet_received.ogg"

View File

@ -44,27 +44,27 @@ class Handler(object):
pub.sendMessage("createBuffer", buffer_type="PeopleBuffer", session_type=session.type, buffer_title=_("Blocked users"), parent_tab=root_position, start=False, kwargs=dict(parent=controller.view.nb, function="get_blocks", name="blocked", sessionObject=session, account=session.get_name())) pub.sendMessage("createBuffer", buffer_type="PeopleBuffer", session_type=session.type, buffer_title=_("Blocked users"), parent_tab=root_position, start=False, kwargs=dict(parent=controller.view.nb, function="get_blocks", name="blocked", sessionObject=session, account=session.get_name()))
elif i == 'muted': elif i == 'muted':
pub.sendMessage("createBuffer", buffer_type="PeopleBuffer", session_type=session.type, buffer_title=_("Muted users"), parent_tab=root_position, start=False, kwargs=dict(parent=controller.view.nb, function="get_mutes", name="muted", sessionObject=session, account=session.get_name())) pub.sendMessage("createBuffer", buffer_type="PeopleBuffer", session_type=session.type, buffer_title=_("Muted users"), parent_tab=root_position, start=False, kwargs=dict(parent=controller.view.nb, function="get_mutes", name="muted", sessionObject=session, account=session.get_name()))
pub.sendMessage("createBuffer", buffer_type="EmptyBuffer", session_type="base", buffer_title=_("Timelines"), parent_tab=root_position, start=False, kwargs=dict(parent=controller.view.nb, name="timelines", account=session.db["user_name"])) pub.sendMessage("createBuffer", buffer_type="EmptyBuffer", session_type="base", buffer_title=_("Timelines"), parent_tab=root_position, start=False, kwargs=dict(parent=controller.view.nb, name="timelines", account=name))
timelines_position =controller.view.search("timelines", name) timelines_position =controller.view.search("timelines", name)
for i in session.settings["other_buffers"]["timelines"]: for i in session.settings["other_buffers"]["timelines"]:
pub.sendMessage("createBuffer", buffer_type="BaseBuffer", session_type=session.type, buffer_title=_(u"Timeline for {}").format(i,), parent_tab=timelines_position, start=False, kwargs=dict(parent=controller.view.nb, function="user_timeline", name="%s-timeline" % (i,), sessionObject=session, account=session.get_name(), sound="tweet_timeline.ogg", bufferType=None, user_id=i, include_ext_alt_text=True, tweet_mode="extended")) pub.sendMessage("createBuffer", buffer_type="BaseBuffer", session_type=session.type, buffer_title=_(u"Timeline for {}").format(i,), parent_tab=timelines_position, start=False, kwargs=dict(parent=controller.view.nb, function="user_timeline", name="%s-timeline" % (i,), sessionObject=session, account=session.get_name(), sound="tweet_timeline.ogg", bufferType=None, user_id=i, include_ext_alt_text=True, tweet_mode="extended"))
pub.sendMessage("createBuffer", buffer_type="EmptyBuffer", session_type="base", buffer_title=_("Likes timelines"), parent_tab=root_position, start=False, kwargs=dict(parent=controller.view.nb, name="favs_timelines", account=session.db["user_name"])) pub.sendMessage("createBuffer", buffer_type="EmptyBuffer", session_type="base", buffer_title=_("Likes timelines"), parent_tab=root_position, start=False, kwargs=dict(parent=controller.view.nb, name="favs_timelines", account=name))
favs_timelines_position =controller.view.search("favs_timelines", name) favs_timelines_position =controller.view.search("favs_timelines", name)
for i in session.settings["other_buffers"]["favourites_timelines"]: for i in session.settings["other_buffers"]["favourites_timelines"]:
pub.sendMessage("createBuffer", buffer_type="BaseBuffer", session_type=session.type, buffer_title=_("Likes for {}").format(i,), parent_tab=favs_timelines_position, start=False, kwargs=dict(parent=controller.view.nb, function="get_favorites", name="%s-favorite" % (i,), sessionObject=session, account=session.db["user_name"], bufferType=None, sound="favourites_timeline_updated.ogg", user_id=i, include_ext_alt_text=True, tweet_mode="extended")) pub.sendMessage("createBuffer", buffer_type="BaseBuffer", session_type=session.type, buffer_title=_("Likes for {}").format(i,), parent_tab=favs_timelines_position, start=False, kwargs=dict(parent=controller.view.nb, function="get_favorites", name="%s-favorite" % (i,), sessionObject=session, account=name, bufferType=None, sound="favourites_timeline_updated.ogg", user_id=i, include_ext_alt_text=True, tweet_mode="extended"))
pub.sendMessage("createBuffer", buffer_type="EmptyBuffer", session_type="base", buffer_title=_("Followers timelines"), parent_tab=root_position, start=False, kwargs=dict(parent=controller.view.nb, name="followers_timelines", account=session.get_name())) pub.sendMessage("createBuffer", buffer_type="EmptyBuffer", session_type="base", buffer_title=_("Followers timelines"), parent_tab=root_position, start=False, kwargs=dict(parent=controller.view.nb, name="followers_timelines", account=session.get_name()))
followers_timelines_position =controller.view.search("followers_timelines", name) followers_timelines_position =controller.view.search("followers_timelines", name)
for i in session.settings["other_buffers"]["followers_timelines"]: for i in session.settings["other_buffers"]["followers_timelines"]:
pub.sendMessage("createBuffer", buffer_type="PeopleBuffer", session_type=session.type, buffer_title=_("Followers for {}").format(i,), parent_tab=followers_timelines_position, start=False, kwargs=dict(parent=controller.view.nb, function="get_followers", name="%s-followers" % (i,), sessionObject=session, account=session.get_name(), sound="new_event.ogg", user_id=i)) pub.sendMessage("createBuffer", buffer_type="PeopleBuffer", session_type=session.type, buffer_title=_("Followers for {}").format(i,), parent_tab=followers_timelines_position, start=False, kwargs=dict(parent=controller.view.nb, function="get_followers", name="%s-followers" % (i,), sessionObject=session, account=session.get_name(), sound="new_event.ogg", user_id=i))
pub.sendMessage("createBuffer", buffer_type="EmptyBuffer", session_type="base", buffer_title=_("Following timelines"), parent_tab=root_position, start=False, kwargs=dict(parent=controller.view.nb, name="friends_timelines", account=session.db["user_name"])) pub.sendMessage("createBuffer", buffer_type="EmptyBuffer", session_type="base", buffer_title=_("Following timelines"), parent_tab=root_position, start=False, kwargs=dict(parent=controller.view.nb, name="friends_timelines", account=name))
friends_timelines_position =controller.view.search("friends_timelines", name) friends_timelines_position =controller.view.search("friends_timelines", name)
for i in session.settings["other_buffers"]["friends_timelines"]: for i in session.settings["other_buffers"]["friends_timelines"]:
pub.sendMessage("createBuffer", buffer_type="PeopleBuffer", session_type=session.type, buffer_title=_(u"Friends for {}").format(i,), parent_tab=friends_timelines_position, start=False, kwargs=dict(parent=controller.view.nb, function="get_friends", name="%s-friends" % (i,), sessionObject=session, account=session.get_name(), sound="new_event.ogg", user_id=i)) pub.sendMessage("createBuffer", buffer_type="PeopleBuffer", session_type=session.type, buffer_title=_(u"Friends for {}").format(i,), parent_tab=friends_timelines_position, start=False, kwargs=dict(parent=controller.view.nb, function="get_friends", name="%s-friends" % (i,), sessionObject=session, account=session.get_name(), sound="new_event.ogg", user_id=i))
pub.sendMessage("createBuffer", buffer_type="EmptyBuffer", session_type="base", buffer_title=_("Lists"), parent_tab=root_position, start=False, kwargs=dict(parent=controller.view.nb, name="lists", account=session.db["user_name"])) pub.sendMessage("createBuffer", buffer_type="EmptyBuffer", session_type="base", buffer_title=_("Lists"), parent_tab=root_position, start=False, kwargs=dict(parent=controller.view.nb, name="lists", account=name))
lists_position =controller.view.search("lists", name) lists_position =controller.view.search("lists", name)
for i in session.settings["other_buffers"]["lists"]: for i in session.settings["other_buffers"]["lists"]:
pub.sendMessage("createBuffer", buffer_type="ListBuffer", session_type=session.type, buffer_title=_(u"List for {}").format(i), parent_tab=lists_position, start=False, kwargs=dict(parent=controller.view.nb, function="list_timeline", name="%s-list" % (i,), sessionObject=session, account=session.get_name(), bufferType=None, sound="list_tweet.ogg", list_id=utils.find_list(i, session.db["lists"]), include_ext_alt_text=True, tweet_mode="extended")) pub.sendMessage("createBuffer", buffer_type="ListBuffer", session_type=session.type, buffer_title=_(u"List for {}").format(i), parent_tab=lists_position, start=False, kwargs=dict(parent=controller.view.nb, function="list_timeline", name="%s-list" % (i,), sessionObject=session, account=session.get_name(), bufferType=None, sound="list_tweet.ogg", list_id=utils.find_list(i, session.db["lists"]), include_ext_alt_text=True, tweet_mode="extended"))
pub.sendMessage("createBuffer", buffer_type="EmptyBuffer", session_type="base", buffer_title=_("Searches"), parent_tab=root_position, start=False, kwargs=dict(parent=controller.view.nb, name="searches", account=session.db["user_name"])) pub.sendMessage("createBuffer", buffer_type="EmptyBuffer", session_type="base", buffer_title=_("Searches"), parent_tab=root_position, start=False, kwargs=dict(parent=controller.view.nb, name="searches", account=name))
searches_position =controller.view.search("searches", name) searches_position =controller.view.search("searches", name)
for i in session.settings["other_buffers"]["tweet_searches"]: for i in session.settings["other_buffers"]["tweet_searches"]:
pub.sendMessage("createBuffer", buffer_type="SearchBuffer", session_type=session.type, buffer_title=_(u"Search for {}").format(i), parent_tab=searches_position, start=False, kwargs=dict(parent=controller.view.nb, function="search_tweets", name="%s-searchterm" % (i,), sessionObject=session, account=session.get_name(), bufferType="searchPanel", sound="search_updated.ogg", q=i, include_ext_alt_text=True, tweet_mode="extended")) pub.sendMessage("createBuffer", buffer_type="SearchBuffer", session_type=session.type, buffer_title=_(u"Search for {}").format(i), parent_tab=searches_position, start=False, kwargs=dict(parent=controller.view.nb, function="search_tweets", name="%s-searchterm" % (i,), sessionObject=session, account=session.get_name(), bufferType="searchPanel", sound="search_updated.ogg", q=i, include_ext_alt_text=True, tweet_mode="extended"))

View File

@ -169,4 +169,4 @@ class Session(base.baseSession):
instance = self.settings["mastodon"]["instance"] instance = self.settings["mastodon"]["instance"]
instance = instance.replace("https://", "") instance = instance.replace("https://", "")
user = self.db["user_name"] user = self.db["user_name"]
return "{}@{} (mastodon)".format(user, instance) return "Mastodon: {}@{}".format(user, instance)

View File

@ -106,10 +106,8 @@ class Session(base.baseSession):
else: objects.insert(0, i) else: objects.insert(0, i)
incoming = incoming+1 incoming = incoming+1
self.db["direct_messages"] = objects self.db["direct_messages"] = objects
self.db["sent_direct_messages"] = sent_objects self.db["sent_direct_messages"] = sent_objects
pub.sendMessage("sent-dms-updated", total=sent, account=self.db["user_name"]) pub.sendMessage("sent-dms-updated", total=sent, session_name=self.get_name())
return incoming return incoming
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
@ -555,7 +553,7 @@ class Session(base.baseSession):
def start_streaming(self): def start_streaming(self):
if config.app["app-settings"]["no_streaming"]: if config.app["app-settings"]["no_streaming"]:
return return
self.stream = streaming.Stream(twitter_api=self.twitter, user=self.get_name(), user_id=self.db["user_id"], muted_users=self.db["muted_users"], consumer_key=appkeys.twitter_api_key, consumer_secret=appkeys.twitter_api_secret, access_token=self.settings["twitter"]["user_key"], access_token_secret=self.settings["twitter"]["user_secret"], chunk_size=1025) self.stream = streaming.Stream(twitter_api=self.twitter, session_name=self.get_name(), user_id=self.db["user_id"], muted_users=self.db["muted_users"], consumer_key=appkeys.twitter_api_key, consumer_secret=appkeys.twitter_api_secret, access_token=self.settings["twitter"]["user_key"], access_token_secret=self.settings["twitter"]["user_secret"], chunk_size=1025)
self.stream_thread = call_threaded(self.stream.filter, follow=self.stream.users, stall_warnings=True) self.stream_thread = call_threaded(self.stream.filter, follow=self.stream.users, stall_warnings=True)
def stop_streaming(self): def stop_streaming(self):
@ -565,12 +563,12 @@ class Session(base.baseSession):
self.stream.running = False self.stream.running = False
log.debug("Stream stopped for accounr {}".format(self.db["user_name"])) log.debug("Stream stopped for accounr {}".format(self.db["user_name"]))
def handle_new_status(self, status, user): def handle_new_status(self, status, session_name):
""" Handles a new status present in the Streaming API. """ """ Handles a new status present in the Streaming API. """
if self.logged == False: if self.logged == False:
return return
# Discard processing the status if the streaming sends a tweet for another account. # Discard processing the status if the streaming sends a tweet for another account.
if self.get_name() != user: if self.get_name() != session_name:
return return
# the Streaming API sends non-extended tweets with an optional parameter "extended_tweets" which contains full_text and other data. # the Streaming API sends non-extended tweets with an optional parameter "extended_tweets" which contains full_text and other data.
# so we have to make sure we check it before processing the normal status. # so we have to make sure we check it before processing the normal status.
@ -607,7 +605,7 @@ class Session(base.baseSession):
status = self.check_quoted_status(status) status = self.check_quoted_status(status)
status = self.check_long_tweet(status) status = self.check_long_tweet(status)
# Send it to the main controller object. # Send it to the main controller object.
pub.sendMessage("newTweet", data=status, user=self.get_name(), _buffers=buffers_to_send) pub.sendMessage("newTweet", data=status, session_name=self.get_name(), _buffers=buffers_to_send)
def check_streams(self): def check_streams(self):
if config.app["app-settings"]["no_streaming"]: if config.app["app-settings"]["no_streaming"]:
@ -618,11 +616,11 @@ class Session(base.baseSession):
if self.stream.running == False: if self.stream.running == False:
self.start_streaming() self.start_streaming()
def handle_connected(self, user): def handle_connected(self, session_name):
if self.logged == False: if self.logged == False:
return return
if user != self.get_name(): if session_name != self.get_name():
log.debug("Connected streaming endpoint on account {}".format(user)) log.debug("Connected streaming endpoint on session {}".format(session_name))
def send_tweet(self, *tweets): def send_tweet(self, *tweets):
""" Convenience function to send a thread. """ """ Convenience function to send a thread. """
@ -673,7 +671,7 @@ class Session(base.baseSession):
else: else:
sent_dms.insert(0, item) sent_dms.insert(0, item)
self.db["sent_direct_messages"] = sent_dms self.db["sent_direct_messages"] = sent_dms
pub.sendMessage("sent-dm", data=item, user=self.db["user_name"]) pub.sendMessage("sent-dm", data=item, session_name=self.get_name())
def get_name(self): def get_name(self):
return "{} ({})".format(self.db["user_name"], "Twitter") return "Twitter: {}".format(self.db["user_name"])

View File

@ -14,13 +14,13 @@ log = logging.getLogger("sessions.twitter.streaming")
class Stream(tweepy.Stream): class Stream(tweepy.Stream):
def __init__(self, twitter_api, user, user_id, muted_users=[], *args, **kwargs): def __init__(self, twitter_api, session_name, user_id, muted_users=[], *args, **kwargs):
super(Stream, self).__init__(*args, **kwargs) super(Stream, self).__init__(*args, **kwargs)
log.debug("Starting streaming listener for account {}".format(user)) log.debug("Starting streaming listener for session {}".format(session_name))
self.started = False self.started = False
self.users = [] self.users = []
self.api = twitter_api self.api = twitter_api
self.user = user self.session_name = session_name
self.user_id = user_id self.user_id = user_id
friends = self.api.get_friend_ids() friends = self.api.get_friend_ids()
log.debug("Retrieved {} friends to add to the streaming listener.".format(len(friends))) log.debug("Retrieved {} friends to add to the streaming listener.".format(len(friends)))
@ -33,15 +33,15 @@ class Stream(tweepy.Stream):
log.debug("Streaming listener started with {} users to follow.".format(len(self.users))) log.debug("Streaming listener started with {} users to follow.".format(len(self.users)))
def on_connect(self): def on_connect(self):
pub.sendMessage("streamConnected", user=self.user) pub.sendMessage("streamConnected", session_name=self.session_name)
def on_exception(self, ex): def on_exception(self, ex):
log.exception("Exception received on streaming endpoint for user {}".format(self.user)) log.exception("Exception received on streaming endpoint for session {}".format(self.session_name))
def on_status(self, status): def on_status(self, status):
""" Checks data arriving as a tweet. """ """ Checks data arriving as a tweet. """
# Hide replies to users not followed by current account. # Hide replies to users not followed by current account.
if status.in_reply_to_user_id_str != None and status.in_reply_to_user_id_str not in self.users and status.user.screen_name != self.user: if status.in_reply_to_user_id_str != None and status.in_reply_to_user_id_str not in self.users and status.user.id != self.user_id:
return return
if status.user.id_str in self.users: if status.user.id_str in self.users:
pub.sendMessage("newStatus", status=status, user=self.user) pub.sendMessage("newStatus", status=status, session_name=self.session_name)