This repository has been archived on 2023-09-24. You can view files and clone it, but cannot push or open issues or pull requests.
takahe/users/models/inbox_message.py

69 lines
2.3 KiB
Python
Raw Normal View History

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-12 06:02:43 +01:00
from activities.models import Post
from users.models import Follow
match instance.message_type:
case "follow":
await sync_to_async(Follow.handle_request_ap)(instance.message)
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}"
)
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)
case unknown:
raise ValueError(
f"Cannot handle activity of type undo.{unknown}"
)
case unknown:
raise ValueError(f"Cannot handle activity of type {unknown}")
return cls.processed
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)
@property
def message_type(self):
return self.message["type"].lower()
2022-11-12 06:02:43 +01:00
@property
def message_object_type(self):
return self.message["object"]["type"].lower()