2022-11-10 06:48:31 +00:00
|
|
|
from asgiref.sync import sync_to_async
|
|
|
|
from django.db import models
|
|
|
|
|
|
|
|
from stator.models import State, StateField, StateGraph, StatorModel
|
|
|
|
|
|
|
|
|
|
|
|
class InboxMessageStates(StateGraph):
|
|
|
|
received = State(try_interval=300)
|
|
|
|
processed = State()
|
|
|
|
|
|
|
|
received.transitions_to(processed)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
async def handle_received(cls, instance: "InboxMessage"):
|
2022-11-14 01:42:47 +00:00
|
|
|
from activities.models import Post, PostInteraction
|
2022-11-17 05:23:32 +00:00
|
|
|
from users.models import Follow, Identity
|
2022-11-12 05:02:43 +00:00
|
|
|
|
|
|
|
match instance.message_type:
|
|
|
|
case "follow":
|
|
|
|
await sync_to_async(Follow.handle_request_ap)(instance.message)
|
2022-11-14 01:42:47 +00:00
|
|
|
case "announce":
|
|
|
|
await sync_to_async(PostInteraction.handle_ap)(instance.message)
|
|
|
|
case "like":
|
|
|
|
await sync_to_async(PostInteraction.handle_ap)(instance.message)
|
2022-11-12 05:02:43 +00:00
|
|
|
case "create":
|
|
|
|
match instance.message_object_type:
|
|
|
|
case "note":
|
|
|
|
await sync_to_async(Post.handle_create_ap)(instance.message)
|
|
|
|
case unknown:
|
|
|
|
raise ValueError(
|
|
|
|
f"Cannot handle activity of type create.{unknown}"
|
|
|
|
)
|
2022-11-17 05:23:32 +00:00
|
|
|
case "update":
|
|
|
|
match instance.message_object_type:
|
|
|
|
case "note":
|
|
|
|
await sync_to_async(Post.handle_update_ap)(instance.message)
|
|
|
|
case "person":
|
|
|
|
await sync_to_async(Identity.handle_update_ap)(instance.message)
|
|
|
|
case unknown:
|
|
|
|
raise ValueError(
|
|
|
|
f"Cannot handle activity of type update.{unknown}"
|
|
|
|
)
|
2022-11-12 05:02:43 +00:00
|
|
|
case "accept":
|
|
|
|
match instance.message_object_type:
|
|
|
|
case "follow":
|
|
|
|
await sync_to_async(Follow.handle_accept_ap)(instance.message)
|
|
|
|
case unknown:
|
|
|
|
raise ValueError(
|
|
|
|
f"Cannot handle activity of type accept.{unknown}"
|
|
|
|
)
|
|
|
|
case "undo":
|
|
|
|
match instance.message_object_type:
|
|
|
|
case "follow":
|
|
|
|
await sync_to_async(Follow.handle_undo_ap)(instance.message)
|
2022-11-16 01:36:39 +00:00
|
|
|
case "like":
|
|
|
|
await sync_to_async(PostInteraction.handle_undo_ap)(
|
|
|
|
instance.message
|
|
|
|
)
|
|
|
|
case "announce":
|
|
|
|
await sync_to_async(PostInteraction.handle_undo_ap)(
|
|
|
|
instance.message
|
|
|
|
)
|
2022-11-12 05:02:43 +00:00
|
|
|
case unknown:
|
|
|
|
raise ValueError(
|
|
|
|
f"Cannot handle activity of type undo.{unknown}"
|
|
|
|
)
|
2022-11-16 01:30:30 +00:00
|
|
|
case "delete":
|
|
|
|
match instance.message_object_type:
|
|
|
|
case "tombstone":
|
|
|
|
await sync_to_async(Post.handle_delete_ap)(instance.message)
|
|
|
|
case unknown:
|
|
|
|
raise ValueError(
|
|
|
|
f"Cannot handle activity of type delete.{unknown}"
|
|
|
|
)
|
2022-11-12 05:02:43 +00:00
|
|
|
case unknown:
|
|
|
|
raise ValueError(f"Cannot handle activity of type {unknown}")
|
2022-11-11 06:42:43 +00:00
|
|
|
return cls.processed
|
2022-11-10 06:48:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
class InboxMessage(StatorModel):
|
|
|
|
"""
|
|
|
|
an incoming inbox message that needs processing.
|
|
|
|
|
|
|
|
Yes, this is kind of its own message queue built on the state graph system.
|
|
|
|
It's fine. It'll scale up to a decent point.
|
|
|
|
"""
|
|
|
|
|
|
|
|
message = models.JSONField()
|
|
|
|
|
|
|
|
state = StateField(InboxMessageStates)
|
|
|
|
|
2022-11-11 06:42:43 +00:00
|
|
|
@property
|
|
|
|
def message_type(self):
|
|
|
|
return self.message["type"].lower()
|
2022-11-10 06:48:31 +00:00
|
|
|
|
2022-11-12 05:02:43 +00:00
|
|
|
@property
|
|
|
|
def message_object_type(self):
|
|
|
|
return self.message["object"]["type"].lower()
|
2022-11-13 04:14:21 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def message_actor(self):
|
|
|
|
return self.message.get("actor")
|