|
@@ -1,16 +1,16 @@
|
|
import logging
|
|
import logging
|
|
|
|
|
|
-import pendulum
|
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
|
|
+from profiles.models import UserProfile
|
|
from rest_framework import status
|
|
from rest_framework import status
|
|
from rest_framework.decorators import api_view, permission_classes
|
|
from rest_framework.decorators import api_view, permission_classes
|
|
from rest_framework.permissions import IsAuthenticated
|
|
from rest_framework.permissions import IsAuthenticated
|
|
from rest_framework.response import Response
|
|
from rest_framework.response import Response
|
|
from scrobbles.scrobblers import (
|
|
from scrobbles.scrobblers import (
|
|
|
|
+ emacs_scrobble_task,
|
|
todoist_scrobble_task,
|
|
todoist_scrobble_task,
|
|
todoist_scrobble_update_task,
|
|
todoist_scrobble_update_task,
|
|
)
|
|
)
|
|
-from profiles.models import UserProfile
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
@@ -29,7 +29,7 @@ def todoist_webhook(request):
|
|
todoist_type, todoist_event = post_data.get("event_name").split(":")
|
|
todoist_type, todoist_event = post_data.get("event_name").split(":")
|
|
event_data = post_data.get("event_data", {})
|
|
event_data = post_data.get("event_data", {})
|
|
is_item_type = todoist_type == "item"
|
|
is_item_type = todoist_type == "item"
|
|
- is_note_type = todoist_type == "note"
|
|
|
|
|
|
+ is_note_type = todoist_tyllll = "note"
|
|
new_labels = event_data.get("labels", [])
|
|
new_labels = event_data.get("labels", [])
|
|
old_labels = (
|
|
old_labels = (
|
|
post_data.get("event_data_extra", {})
|
|
post_data.get("event_data_extra", {})
|
|
@@ -118,3 +118,53 @@ def todoist_webhook(request):
|
|
extra={"scrobble_id": scrobble.id},
|
|
extra={"scrobble_id": scrobble.id},
|
|
)
|
|
)
|
|
return Response({"scrobble_id": scrobble.id}, status=status.HTTP_200_OK)
|
|
return Response({"scrobble_id": scrobble.id}, status=status.HTTP_200_OK)
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+@csrf_exempt
|
|
|
|
+@permission_classes([IsAuthenticated])
|
|
|
|
+@api_view(["POST"])
|
|
|
|
+def emacs_webhook(request):
|
|
|
|
+ post_data = request.data
|
|
|
|
+ logger.info(
|
|
|
|
+ "[emacs_webhook] called",
|
|
|
|
+ extra={"post_data": post_data},
|
|
|
|
+ )
|
|
|
|
+ print(post_data)
|
|
|
|
+ emacs_task = post_data
|
|
|
|
+
|
|
|
|
+ task_started = emacs_task.get("state") == "STRT"
|
|
|
|
+ task_stopped = emacs_task.get("state") == "DONE"
|
|
|
|
+
|
|
|
|
+ user_id = (
|
|
|
|
+ UserProfile.objects.filter(todoist_user_id=post_data.get("user_id"))
|
|
|
|
+ .first()
|
|
|
|
+ .user_id
|
|
|
|
+ )
|
|
|
|
+
|
|
|
|
+ scrobble = None
|
|
|
|
+ if emacs_task:
|
|
|
|
+ scrobble = emacs_scrobble_task(
|
|
|
|
+ emacs_task, user_id, stopped=task_stopped
|
|
|
|
+ )
|
|
|
|
+
|
|
|
|
+ if scrobble and task_started:
|
|
|
|
+ scrobble = emacs_scrobble_update_task(
|
|
|
|
+ emacs_task.get("notes"),
|
|
|
|
+ user_id,
|
|
|
|
+ )
|
|
|
|
+
|
|
|
|
+ if not scrobble:
|
|
|
|
+ logger.info(
|
|
|
|
+ "[todoist_webhook] finished with no note or task found",
|
|
|
|
+ extra={"scrobble_id": None},
|
|
|
|
+ )
|
|
|
|
+ return Response(
|
|
|
|
+ {"error": "No scrobble found to be updated"},
|
|
|
|
+ status=status.HTTP_304_NOT_MODIFIED,
|
|
|
|
+ )
|
|
|
|
+
|
|
|
|
+ logger.info(
|
|
|
|
+ "[todoist_webhook] finished",
|
|
|
|
+ extra={"scrobble_id": scrobble.id},
|
|
|
|
+ )
|
|
|
|
+ return Response({"scrobble_id": scrobble.id}, status=status.HTTP_200_OK)
|