瀏覽代碼

[tasks] Don't stop tasks when modified while in progress

Colin Powell 8 月之前
父節點
當前提交
3388471685
共有 2 個文件被更改,包括 29 次插入12 次删除
  1. 4 7
      vrobbler/apps/scrobbles/scrobblers.py
  2. 25 5
      vrobbler/apps/tasks/webhooks.py

+ 4 - 7
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

+ 25 - 5
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: