nanos / FediFetcher

FediFetcher is a tool for Mastodon that automatically fetches missing replies and posts from other fediverse instances, and adds them to your own Mastodon instance.
https://blog.thms.uk/fedifetcher?utm_source=github
MIT License
297 stars 215 forks source link

fix: handle zero notifications #61

Closed Teqed closed 1 year ago

Teqed commented 1 year ago

Expected Behavior

When from-notifications > 0: When a user has notifications, fetch context. When a user does not have notifications, continue to the next step.

Current Behavior

When a user has no notifications, /api/v1/notifications returns [].

2023-07-02 23:52:37.204976 Eastern Daylight Time: Getting notifications for last 10 hours
2023-07-02 23:52:37.277612 Eastern Daylight Time: Job failed after 0:00:00.480230.
Traceback (most recent call last):
  File "{...}}\FediFetcher\find_posts.py", line 1053, in <module>
    notification_users = get_notification_users(arguments.server, token, all_known_users, arguments.from_notifications)
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "{...}\FediFetcher\find_posts.py", line 43, in get_notification_users
    notifications = get_paginated_mastodon(f"https://{server}/api/v1/notifications", since, headers={
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "{...}\FediFetcher\find_posts.py", line 800, in get_paginated_mastodon
    while parser.parse(result[-1]['created_at']) >= max and 'next' in response.links:
                       ~~~~~~^^^^
IndexError: list index out of range

We encounter IndexError: list index out of range when trying to access result[-1].

Possible Solution

Use isInstance() to check if result is of type list before assigning it, use while result and... instead of while ..., and provide some additional error handling surrounding the request.

Steps to Reproduce

  1. Clear all notifications in Mastodon.
  2. Set from-notifications to any value greater than 0.
  3. Run FediFetcher and reach the get_notification_users function.
  4. See failed run, check logs.

Context (Environment)

This will cause the job to fail, causing a notification if using GitHub Actions, and it will not save seen_urls into ./artifacts if the program exits ungracefully, causing a growing backlog of reprocessed-posts every time it attempts to run on schedule again. Ideally, exceptions from these functions should at the very least lead to a graceful exit where artifacts are still attempted to be written, and is worth its own PR.

nanos commented 1 year ago

Good catch! Thank you!