diff --git a/bsky.py b/bsky.py --- a/bsky.py +++ b/bsky.py @@ -1054,6 +1054,22 @@ logger.error(f"Error detaching user blocks: {e}") + +def queue_file_sort_key(filepath): + """ + Sort key for queue files: priority first (0 before 1), then newest first within each priority. + Filename format: {priority}_{YYYYMMDD}_{HHMMSS}_{reason}_{hash}.json + """ + parts = filepath.name.split('_') + if len(parts) >= 3: + priority = int(parts[0]) # 0 or 1 + date_part = parts[1] # YYYYMMDD + time_part = parts[2] # HHMMSS + timestamp = int(date_part + time_part) # YYYYMMDDHHMMSS as integer + # Return (priority ascending, timestamp descending) + return (priority, -timestamp) + return (1, 0) # Fallback: treat as normal priority, oldest + def notification_to_dict(notification): """Convert a notification object to a dictionary for JSON serialization.""" return { @@ -1167,11 +1183,13 @@ def load_and_process_queued_notifications(void_agent, atproto_client, testing_mode=False): - """Load and process all notifications from the queue in priority order.""" + """Load and process all notifications from the queue in priority order (newest first).""" try: # Get all JSON files in queue directory (excluding processed_notifications.json) - # Files are sorted by name, which puts priority files first (0_ prefix before 1_ prefix) - all_queue_files = sorted([f for f in QUEUE_DIR.glob("*.json") if f.name != "processed_notifications.json"]) + all_queue_files = [f for f in QUEUE_DIR.glob("*.json") if f.name != "processed_notifications.json"] + + # Sort by priority first (0_ before 1_), then by timestamp (newest first within each priority) + all_queue_files = sorted(all_queue_files, key=queue_file_sort_key) # Filter out and delete like notifications immediately queue_files = [] @@ -1228,8 +1246,9 @@ if new_count > 0: logger.info(f"Added {new_count} new notifications to queue") - # Reload the queue files to include the new items - updated_queue_files = sorted([f for f in QUEUE_DIR.glob("*.json") if f.name != "processed_notifications.json"]) + # Reload the queue files to include the new items with same sorting + updated_queue_files = [f for f in QUEUE_DIR.glob("*.json") if f.name != "processed_notifications.json"] + updated_queue_files = sorted(updated_queue_files, key=queue_file_sort_key) queue_files = updated_queue_files logger.info(f"Queue updated: now {len(queue_files)} total items") except Exception as e: