diff --git a/bot/notification.py b/bot/notification.py index 0570cd8..71ec814 100644 --- a/bot/notification.py +++ b/bot/notification.py @@ -12,8 +12,6 @@ WHITELISTED_INSTANCES: list[str] = [] def process_notification(client, notification): '''Processes an individual notification''' - notif_id = notification.get('id') - user = notification.get('user', {}) username = user.get('username', 'unknown') host = user.get('host') # None if local user @@ -29,7 +27,8 @@ def process_notification(client, notification): visibility = 'home' 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 parsed_command = parse_notification(notification, client) @@ -57,7 +56,6 @@ def process_notification(client, notification): file_ids=response[1] #visible_user_ids=[] #todo: write actual visible users ids so pleromers can use the bot privately ) - return notif_id def process_notifications(client): '''Processes a batch of unread notifications. Returns False if there are @@ -69,7 +67,10 @@ def process_notifications(client): try: 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, # Let misskey handle the filtering include_types=['mention', 'reply'], @@ -84,8 +85,15 @@ def process_notifications(client): # Iterate oldest to newest for notification in notifications: try: - # Process notification and update new last seen id - new_last_seen_id = process_notification(client, notification) + # Skip if we've processed already + 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: print(f'An exception has occured while processing a notification: {e}') print(traceback.format_exc()) @@ -100,7 +108,7 @@ def process_notifications(client): finally: # Quality jank right here, but finally lets us update the last_seen_id # 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) return False