forked from waifu/kemoverse
Fix notification loop
This commit is contained in:
parent
d2a7e523e8
commit
9be92afce3
1 changed files with 16 additions and 8 deletions
|
@ -12,8 +12,6 @@ WHITELISTED_INSTANCES: list[str] = []
|
||||||
|
|
||||||
def process_notification(client, notification):
|
def process_notification(client, notification):
|
||||||
'''Processes an individual notification'''
|
'''Processes an individual notification'''
|
||||||
notif_id = notification.get('id')
|
|
||||||
|
|
||||||
user = notification.get('user', {})
|
user = notification.get('user', {})
|
||||||
username = user.get('username', 'unknown')
|
username = user.get('username', 'unknown')
|
||||||
host = user.get('host') # None if local user
|
host = user.get('host') # None if local user
|
||||||
|
@ -29,7 +27,8 @@ def process_notification(client, notification):
|
||||||
visibility = 'home'
|
visibility = 'home'
|
||||||
|
|
||||||
notif_type = notification.get('type', 'unknown')
|
notif_type = notification.get('type', 'unknown')
|
||||||
print(f'📨 [{notif_type}] from @{username}@{instance}')
|
notif_id = notification.get('id')
|
||||||
|
print(f'📨 <{notif_id}> [{notif_type}] from @{username}@{instance}')
|
||||||
|
|
||||||
# 🧠 Send to the parser
|
# 🧠 Send to the parser
|
||||||
parsed_command = parse_notification(notification, client)
|
parsed_command = parse_notification(notification, client)
|
||||||
|
@ -57,7 +56,6 @@ def process_notification(client, notification):
|
||||||
file_ids=response[1]
|
file_ids=response[1]
|
||||||
#visible_user_ids=[] #todo: write actual visible users ids so pleromers can use the bot privately
|
#visible_user_ids=[] #todo: write actual visible users ids so pleromers can use the bot privately
|
||||||
)
|
)
|
||||||
return notif_id
|
|
||||||
|
|
||||||
def process_notifications(client):
|
def process_notifications(client):
|
||||||
'''Processes a batch of unread notifications. Returns False if there are
|
'''Processes a batch of unread notifications. Returns False if there are
|
||||||
|
@ -69,7 +67,10 @@ def process_notifications(client):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
notifications = client.i_notifications(
|
notifications = client.i_notifications(
|
||||||
# Fetch notifications we haven't seen yet
|
# Fetch notifications we haven't seen yet. This option is a bit
|
||||||
|
# tempermental, sometimes it'll include since_id, sometimes it
|
||||||
|
# won't. We need to keep track of what notifications we've
|
||||||
|
# already processed.
|
||||||
since_id=last_seen_id,
|
since_id=last_seen_id,
|
||||||
# Let misskey handle the filtering
|
# Let misskey handle the filtering
|
||||||
include_types=['mention', 'reply'],
|
include_types=['mention', 'reply'],
|
||||||
|
@ -84,8 +85,15 @@ def process_notifications(client):
|
||||||
# Iterate oldest to newest
|
# Iterate oldest to newest
|
||||||
for notification in notifications:
|
for notification in notifications:
|
||||||
try:
|
try:
|
||||||
# Process notification and update new last seen id
|
# Skip if we've processed already
|
||||||
new_last_seen_id = process_notification(client, notification)
|
notif_id = notification.get('id')
|
||||||
|
if notif_id <= last_seen_id:
|
||||||
|
continue
|
||||||
|
|
||||||
|
# Update new_last_seen_id and process
|
||||||
|
new_last_seen_id = notif_id
|
||||||
|
process_notification(client, notification)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f'An exception has occured while processing a notification: {e}')
|
print(f'An exception has occured while processing a notification: {e}')
|
||||||
print(traceback.format_exc())
|
print(traceback.format_exc())
|
||||||
|
@ -100,7 +108,7 @@ def process_notifications(client):
|
||||||
finally:
|
finally:
|
||||||
# Quality jank right here, but finally lets us update the last_seen_id
|
# Quality jank right here, but finally lets us update the last_seen_id
|
||||||
# even if we hit an exception or return early
|
# even if we hit an exception or return early
|
||||||
if new_last_seen_id != last_seen_id:
|
if new_last_seen_id > last_seen_id:
|
||||||
set_config('last_seen_notif_id', new_last_seen_id)
|
set_config('last_seen_notif_id', new_last_seen_id)
|
||||||
|
|
||||||
return False
|
return False
|
||||||
|
|
Loading…
Add table
Reference in a new issue