2022-11-09 22:48:31 -08:00
|
|
|
from asgiref.sync import sync_to_async
|
|
|
|
from django.db import models
|
|
|
|
|
2022-11-27 16:05:31 -08:00
|
|
|
from core.models import Config
|
2022-11-09 22:48:31 -08:00
|
|
|
from stator.models import State, StateField, StateGraph, StatorModel
|
|
|
|
|
|
|
|
|
|
|
|
class InboxMessageStates(StateGraph):
|
|
|
|
received = State(try_interval=300)
|
2022-11-27 16:05:31 -08:00
|
|
|
processed = State(try_interval=86400, attempt_immediately=False)
|
|
|
|
purged = State() # This is actually deletion, it will never get here
|
2022-11-09 22:48:31 -08:00
|
|
|
|
|
|
|
received.transitions_to(processed)
|
2022-11-27 16:05:31 -08:00
|
|
|
processed.transitions_to(purged)
|
2022-11-09 22:48:31 -08:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
async def handle_received(cls, instance: "InboxMessage"):
|
2022-11-13 17:42:47 -08:00
|
|
|
from activities.models import Post, PostInteraction
|
2022-12-17 13:58:04 -08:00
|
|
|
from users.models import Follow, Identity, Report
|
2022-11-11 21:02:43 -08:00
|
|
|
|
|
|
|
match instance.message_type:
|
|
|
|
case "follow":
|
|
|
|
await sync_to_async(Follow.handle_request_ap)(instance.message)
|
2022-11-13 17:42:47 -08: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-11 21:02:43 -08:00
|
|
|
case "create":
|
|
|
|
match instance.message_object_type:
|
|
|
|
case "note":
|
|
|
|
await sync_to_async(Post.handle_create_ap)(instance.message)
|
2022-12-17 13:58:04 -08:00
|
|
|
case "question":
|
|
|
|
pass # Drop for now
|
2022-11-11 21:02:43 -08:00
|
|
|
case unknown:
|
2022-12-18 08:09:25 -08:00
|
|
|
if unknown in Post.Types.names:
|
|
|
|
await sync_to_async(Post.handle_create_ap)(instance.message)
|
|
|
|
else:
|
|
|
|
raise ValueError(
|
|
|
|
f"Cannot handle activity of type create.{unknown}"
|
|
|
|
)
|
2022-11-16 21:23:32 -08: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)
|
2022-12-19 22:52:33 -08:00
|
|
|
case "service":
|
|
|
|
await sync_to_async(Identity.handle_update_ap)(instance.message)
|
|
|
|
case "group":
|
|
|
|
await sync_to_async(Identity.handle_update_ap)(instance.message)
|
|
|
|
case "organization":
|
|
|
|
await sync_to_async(Identity.handle_update_ap)(instance.message)
|
|
|
|
case "application":
|
|
|
|
await sync_to_async(Identity.handle_update_ap)(instance.message)
|
2022-12-17 13:58:04 -08:00
|
|
|
case "question":
|
|
|
|
pass # Drop for now
|
2022-11-16 21:23:32 -08:00
|
|
|
case unknown:
|
2022-12-18 08:09:25 -08:00
|
|
|
if unknown in Post.Types.names:
|
|
|
|
await sync_to_async(Post.handle_update_ap)(instance.message)
|
|
|
|
else:
|
|
|
|
raise ValueError(
|
|
|
|
f"Cannot handle activity of type update.{unknown}"
|
|
|
|
)
|
2022-11-11 21:02:43 -08:00
|
|
|
case "accept":
|
|
|
|
match instance.message_object_type:
|
|
|
|
case "follow":
|
|
|
|
await sync_to_async(Follow.handle_accept_ap)(instance.message)
|
2022-12-28 21:25:07 -08:00
|
|
|
case None:
|
|
|
|
await sync_to_async(Follow.handle_accept_ref_ap)(
|
|
|
|
instance.message
|
|
|
|
)
|
2022-11-11 21:02:43 -08:00
|
|
|
case unknown:
|
|
|
|
raise ValueError(
|
|
|
|
f"Cannot handle activity of type accept.{unknown}"
|
|
|
|
)
|
2022-12-21 08:11:58 -08:00
|
|
|
case "reject":
|
|
|
|
match instance.message_object_type:
|
|
|
|
case "follow":
|
|
|
|
await sync_to_async(Follow.handle_reject_ap)(instance.message)
|
|
|
|
case unknown:
|
|
|
|
raise ValueError(
|
|
|
|
f"Cannot handle activity of type reject.{unknown}"
|
|
|
|
)
|
2022-11-11 21:02:43 -08:00
|
|
|
case "undo":
|
|
|
|
match instance.message_object_type:
|
|
|
|
case "follow":
|
|
|
|
await sync_to_async(Follow.handle_undo_ap)(instance.message)
|
2022-11-15 17:36:39 -08: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-11 21:02:43 -08:00
|
|
|
case unknown:
|
|
|
|
raise ValueError(
|
|
|
|
f"Cannot handle activity of type undo.{unknown}"
|
|
|
|
)
|
2022-11-15 17:30:30 -08:00
|
|
|
case "delete":
|
2022-12-28 21:47:28 -08:00
|
|
|
# If there is no object type, we need to see if it's a profile or a post
|
2022-11-20 11:24:03 -08:00
|
|
|
if not isinstance(instance.message["object"], dict):
|
2022-12-28 21:47:28 -08:00
|
|
|
if await Identity.objects.filter(
|
|
|
|
actor_uri=instance.message["object"]
|
|
|
|
).aexists():
|
|
|
|
await sync_to_async(Identity.handle_delete_ap)(instance.message)
|
|
|
|
elif await Post.objects.filter(
|
|
|
|
object_uri=instance.message["object"]
|
|
|
|
).aexists():
|
|
|
|
await sync_to_async(Post.handle_delete_ap)(instance.message)
|
|
|
|
else:
|
|
|
|
raise ValueError(
|
|
|
|
f"Cannot handle activity of type delete on URI {instance.message['object']}"
|
|
|
|
)
|
2022-11-20 11:36:40 -08:00
|
|
|
else:
|
|
|
|
match instance.message_object_type:
|
|
|
|
case "tombstone":
|
|
|
|
await sync_to_async(Post.handle_delete_ap)(instance.message)
|
2022-12-17 11:18:04 -08:00
|
|
|
case "note":
|
|
|
|
await sync_to_async(Post.handle_delete_ap)(instance.message)
|
2022-11-20 11:36:40 -08:00
|
|
|
case unknown:
|
|
|
|
raise ValueError(
|
|
|
|
f"Cannot handle activity of type delete.{unknown}"
|
|
|
|
)
|
2022-12-04 06:36:38 -08:00
|
|
|
case "add":
|
|
|
|
# We are ignoring these right now (probably pinned items)
|
|
|
|
pass
|
|
|
|
case "remove":
|
|
|
|
# We are ignoring these right now (probably pinned items)
|
|
|
|
pass
|
2022-12-28 21:25:07 -08:00
|
|
|
case "move":
|
|
|
|
# We're ignoring moves for now
|
|
|
|
pass
|
2022-12-28 10:01:30 -08:00
|
|
|
case "http://litepub.social/ns#emojireact":
|
|
|
|
# We're ignoring emoji reactions for now
|
|
|
|
pass
|
2022-12-17 13:58:04 -08:00
|
|
|
case "flag":
|
|
|
|
# Received reports
|
|
|
|
await sync_to_async(Report.handle_ap)(instance.message)
|
2022-12-30 10:06:38 -08:00
|
|
|
case "__internal__":
|
|
|
|
match instance.message_object_type:
|
|
|
|
case "fetchpost":
|
|
|
|
await sync_to_async(Post.handle_fetch_internal)(
|
|
|
|
instance.message
|
|
|
|
)
|
|
|
|
case unknown:
|
|
|
|
raise ValueError(
|
|
|
|
f"Cannot handle activity of type __internal__.{unknown}"
|
|
|
|
)
|
2022-11-11 21:02:43 -08:00
|
|
|
case unknown:
|
|
|
|
raise ValueError(f"Cannot handle activity of type {unknown}")
|
2022-11-10 22:42:43 -08:00
|
|
|
return cls.processed
|
2022-11-09 22:48:31 -08:00
|
|
|
|
2022-11-27 16:05:31 -08:00
|
|
|
@classmethod
|
|
|
|
async def handle_processed(cls, instance: "InboxMessage"):
|
|
|
|
if instance.state_age > Config.system.inbox_message_purge_after:
|
|
|
|
await InboxMessage.objects.filter(pk=instance.pk).adelete()
|
|
|
|
|
2022-11-09 22:48:31 -08: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-10 22:42:43 -08:00
|
|
|
@property
|
|
|
|
def message_type(self):
|
|
|
|
return self.message["type"].lower()
|
2022-11-09 22:48:31 -08:00
|
|
|
|
2022-11-11 21:02:43 -08:00
|
|
|
@property
|
2022-12-19 23:01:30 -08:00
|
|
|
def message_object_type(self) -> str | None:
|
|
|
|
if isinstance(self.message["object"], dict):
|
|
|
|
return self.message["object"]["type"].lower()
|
|
|
|
else:
|
|
|
|
return None
|
2022-11-12 20:14:21 -08:00
|
|
|
|
2022-12-16 15:38:52 -08:00
|
|
|
@property
|
|
|
|
def message_type_full(self):
|
|
|
|
if isinstance(self.message.get("object"), dict):
|
|
|
|
return f"{self.message_type}.{self.message_object_type}"
|
|
|
|
else:
|
|
|
|
return f"{self.message_type}"
|
|
|
|
|
2022-11-12 20:14:21 -08:00
|
|
|
@property
|
|
|
|
def message_actor(self):
|
|
|
|
return self.message.get("actor")
|