From 33884716851b9192e4ec4a3bcb4d6a424b48df61 Mon Sep 17 00:00:00 2001 From: Colin Powell Date: Thu, 31 Oct 2024 15:28:06 -0400 Subject: [PATCH] [tasks] Don't stop tasks when modified while in progress --- vrobbler/apps/scrobbles/scrobblers.py | 11 ++++------ vrobbler/apps/tasks/webhooks.py | 30 ++++++++++++++++++++++----- 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/vrobbler/apps/scrobbles/scrobblers.py b/vrobbler/apps/scrobbles/scrobblers.py index abc9f6e..fe5abf8 100644 --- a/vrobbler/apps/scrobbles/scrobblers.py +++ b/vrobbler/apps/scrobbles/scrobblers.py @@ -301,7 +301,10 @@ def todoist_scrobble_task_finish( todoist_task: dict, user_id: int ) -> Optional[Scrobble]: scrobble = Scrobble.objects.filter( - user_id=user_id, log__todoist_id=todoist_task.get("todoist_id") + user_id=user_id, + log__todoist_id=todoist_task.get("todoist_id"), + in_progress=True, + played_to_completion=False, ).first() if not scrobble: @@ -310,12 +313,6 @@ def todoist_scrobble_task_finish( ) return - if not scrobble.in_progress or scrobble.played_to_completion: - logger.info( - "[todoist_scrobble_task_finish] todoist webhook finish called on finished task" - ) - return - scrobble.stop(force_finish=True) return scrobble diff --git a/vrobbler/apps/tasks/webhooks.py b/vrobbler/apps/tasks/webhooks.py index 246394a..b72df68 100644 --- a/vrobbler/apps/tasks/webhooks.py +++ b/vrobbler/apps/tasks/webhooks.py @@ -31,9 +31,19 @@ def todoist_webhook(request): event_data = post_data.get("event_data", {}) is_item_type = todoist_type == "item" is_note_type = todoist_type == "note" - - is_updated = todoist_event == ["updated"] - is_added = todoist_event == ["added"] + new_labels = event_data.get("labels") + old_labels = ( + post_data.get("event_data_extra", {}) + .get("old_item", {}) + .get("labels", []) + ) + # TODO Don't hard code status strings in here + is_updated = ( + todoist_event in ["updated"] + and "inprogress" in new_labels + and "inprogress" not in old_labels + ) + is_added = todoist_event in ["added"] if is_item_type: todoist_task = { @@ -70,16 +80,26 @@ def todoist_webhook(request): ) return Response({}, status=status.HTTP_304_NOT_MODIFIED) + if is_item_type and new_labels == old_labels: + logger.info( + "[todoist_webhook] ignoring item, labels unchanged", + extra={ + "todoist_type": todoist_task["todoist_type"], + "todoist_event": todoist_task["todoist_event"], + }, + ) + return Response({}, status=status.HTTP_304_NOT_MODIFIED) + user_id = ( UserProfile.objects.filter(todoist_user_id=post_data.get("user_id")) .first() .user_id ) - if todoist_task and todoist_event == "updated": + if todoist_task and is_updated: scrobble = todoist_scrobble_task(todoist_task, user_id) - if todoist_note and todoist_event == "added": + if todoist_note and is_added: scrobble = todoist_scrobble_update_task(todoist_note, user_id) if not scrobble: