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

This commit is contained in:
2024-10-31 15:28:06 -04:00
parent 58a957c98a
commit 3388471685
2 changed files with 29 additions and 12 deletions

View File

@ -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

View File

@ -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: