Skip to content

Commit

Permalink
Added SSEChat().pinned_message for event handling
Browse files Browse the repository at this point in the history
  • Loading branch information
thelabcat committed May 5, 2024
1 parent 3bb72ef commit 6e9f4a7
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/cocorum/ssechat.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ def __init__(self, stream_id):

self.__mailbox = [] #A mailbox if you will
self.deleted_message_ids = [] #IDs of messages that were deleted, as reported by the client
self.pinned_message = None #If a message is pinned, it is assigned to this
self.users = {} #Dictionary of users by user ID
self.channels = {} #Dictionary of channels by channel ID
self.badges = {}
Expand All @@ -268,12 +269,16 @@ def next_jsondata(self):
if not self.chat_running: #Do not try to query a new message if chat is closed
return

message = next(self.client, None)
try:
message = next(self.client, None)
except requests.exceptions.ReadTimeout:
message = None

if not message:
self.chat_running = False #Chat has been closed
return
if not message.data: #Blank SSE event
print("Blank SSE event:", message)
print("Blank SSE event:>", message, "<:")
#Self recursion should work so long as we don't get dozens of blank events in a row
return self.next_jsondata()

Expand Down Expand Up @@ -356,6 +361,10 @@ def get_message(self):
elif jsondata["type"] == "init":
self.parse_init_data(jsondata)

#Pinned message
elif jsondata["type"] == "pin_message":
self.pinned_message = SSEChatMessage(jsondata["data"]["message"])

#New messages
elif jsondata["type"] == "messages":
#Parse messages, users, and channels
Expand Down

0 comments on commit 6e9f4a7

Please sign in to comment.