2022-11-09 22:48:31 -08:00
|
|
|
from django.db import models
|
2023-07-07 15:32:57 -07:00
|
|
|
from pyld.jsonld import JsonLdError
|
2022-11-09 22:48:31 -08:00
|
|
|
|
2023-07-07 15:29:12 -07:00
|
|
|
from core.exceptions import ActivityPubError
|
2022-11-09 22:48:31 -08:00
|
|
|
from stator.models import State, StateField, StateGraph, StatorModel
|
|
|
|
|
|
|
|
|
|
|
|
class InboxMessageStates(StateGraph):
|
2023-01-08 11:43:32 -08:00
|
|
|
received = State(try_interval=300, delete_after=86400 * 3)
|
|
|
|
processed = State(externally_progressed=True, delete_after=86400)
|
2023-07-07 15:29:12 -07:00
|
|
|
errored = State(externally_progressed=True, delete_after=86400)
|
2022-11-09 22:48:31 -08:00
|
|
|
|
|
|
|
received.transitions_to(processed)
|
2023-07-07 15:29:12 -07:00
|
|
|
received.transitions_to(errored)
|
2022-11-09 22:48:31 -08:00
|
|
|
|
|
|
|
@classmethod
|
2023-07-07 14:14:06 -07:00
|
|
|
def handle_received(cls, instance: "InboxMessage"):
|
2023-01-16 10:53:40 -08:00
|
|
|
from activities.models import Post, PostInteraction, TimelineEvent
|
2023-01-15 12:35:45 -08:00
|
|
|
from users.models import Block, Follow, Identity, Report
|
2023-02-13 19:50:43 -08:00
|
|
|
from users.services import IdentityService
|
2022-11-11 21:02:43 -08:00
|
|
|
|
2023-07-07 15:29:12 -07:00
|
|
|
try:
|
|
|
|
match instance.message_type:
|
|
|
|
case "follow":
|
|
|
|
Follow.handle_request_ap(instance.message)
|
|
|
|
case "block":
|
|
|
|
Block.handle_ap(instance.message)
|
|
|
|
case "announce":
|
|
|
|
PostInteraction.handle_ap(instance.message)
|
|
|
|
case "like":
|
|
|
|
PostInteraction.handle_ap(instance.message)
|
|
|
|
case "create":
|
|
|
|
match instance.message_object_type:
|
|
|
|
case "note":
|
|
|
|
if instance.message_object_has_content:
|
|
|
|
Post.handle_create_ap(instance.message)
|
|
|
|
else:
|
|
|
|
# Notes without content are Interaction candidates
|
|
|
|
PostInteraction.handle_ap(instance.message)
|
|
|
|
case "question":
|
2023-07-07 14:14:06 -07:00
|
|
|
Post.handle_create_ap(instance.message)
|
2023-07-07 15:29:12 -07:00
|
|
|
case unknown:
|
|
|
|
if unknown in Post.Types.names:
|
|
|
|
Post.handle_create_ap(instance.message)
|
|
|
|
case "update":
|
2022-11-20 11:36:40 -08:00
|
|
|
match instance.message_object_type:
|
2022-12-17 11:18:04 -08:00
|
|
|
case "note":
|
2023-07-07 15:29:12 -07:00
|
|
|
Post.handle_update_ap(instance.message)
|
|
|
|
case "person":
|
|
|
|
Identity.handle_update_ap(instance.message)
|
|
|
|
case "service":
|
|
|
|
Identity.handle_update_ap(instance.message)
|
|
|
|
case "group":
|
|
|
|
Identity.handle_update_ap(instance.message)
|
|
|
|
case "organization":
|
|
|
|
Identity.handle_update_ap(instance.message)
|
|
|
|
case "application":
|
|
|
|
Identity.handle_update_ap(instance.message)
|
|
|
|
case "question":
|
|
|
|
Post.handle_update_ap(instance.message)
|
|
|
|
case unknown:
|
|
|
|
if unknown in Post.Types.names:
|
|
|
|
Post.handle_update_ap(instance.message)
|
|
|
|
case "accept":
|
|
|
|
match instance.message_object_type:
|
|
|
|
case "follow":
|
|
|
|
Follow.handle_accept_ap(instance.message)
|
|
|
|
case None:
|
|
|
|
# It's a string object, but these will only be for Follows
|
|
|
|
Follow.handle_accept_ap(instance.message)
|
|
|
|
case unknown:
|
2023-07-07 15:39:02 -07:00
|
|
|
return cls.errored
|
2023-07-07 15:29:12 -07:00
|
|
|
case "reject":
|
|
|
|
match instance.message_object_type:
|
|
|
|
case "follow":
|
|
|
|
Follow.handle_reject_ap(instance.message)
|
|
|
|
case None:
|
|
|
|
# It's a string object, but these will only be for Follows
|
|
|
|
Follow.handle_reject_ap(instance.message)
|
|
|
|
case unknown:
|
2023-07-07 15:39:02 -07:00
|
|
|
return cls.errored
|
2023-07-07 15:29:12 -07:00
|
|
|
case "undo":
|
|
|
|
match instance.message_object_type:
|
|
|
|
case "follow":
|
|
|
|
Follow.handle_undo_ap(instance.message)
|
|
|
|
case "block":
|
|
|
|
Block.handle_undo_ap(instance.message)
|
|
|
|
case "like":
|
|
|
|
PostInteraction.handle_undo_ap(instance.message)
|
|
|
|
case "announce":
|
|
|
|
PostInteraction.handle_undo_ap(instance.message)
|
|
|
|
case "http://litepub.social/ns#emojireact":
|
|
|
|
# We're ignoring emoji reactions for now
|
|
|
|
pass
|
|
|
|
case unknown:
|
2023-07-07 15:39:02 -07:00
|
|
|
return cls.errored
|
2023-07-07 15:29:12 -07:00
|
|
|
case "delete":
|
|
|
|
# If there is no object type, we need to see if it's a profile or a post
|
|
|
|
if not isinstance(instance.message["object"], dict):
|
|
|
|
if Identity.objects.filter(
|
|
|
|
actor_uri=instance.message["object"]
|
|
|
|
).exists():
|
|
|
|
Identity.handle_delete_ap(instance.message)
|
|
|
|
elif Post.objects.filter(
|
|
|
|
object_uri=instance.message["object"]
|
|
|
|
).exists():
|
2023-07-07 14:14:06 -07:00
|
|
|
Post.handle_delete_ap(instance.message)
|
2023-07-07 15:29:12 -07:00
|
|
|
else:
|
|
|
|
# It is presumably already deleted
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
match instance.message_object_type:
|
|
|
|
case "tombstone":
|
|
|
|
Post.handle_delete_ap(instance.message)
|
|
|
|
case "note":
|
|
|
|
Post.handle_delete_ap(instance.message)
|
|
|
|
case unknown:
|
2023-07-07 15:39:02 -07:00
|
|
|
return cls.errored
|
2023-07-07 15:29:12 -07:00
|
|
|
case "add":
|
|
|
|
PostInteraction.handle_add_ap(instance.message)
|
|
|
|
case "remove":
|
|
|
|
PostInteraction.handle_remove_ap(instance.message)
|
|
|
|
case "move":
|
|
|
|
# We're ignoring moves for now
|
|
|
|
pass
|
|
|
|
case "http://litepub.social/ns#emojireact":
|
|
|
|
# We're ignoring emoji reactions for now
|
|
|
|
pass
|
|
|
|
case "flag":
|
|
|
|
# Received reports
|
|
|
|
Report.handle_ap(instance.message)
|
|
|
|
case "__internal__":
|
|
|
|
match instance.message_object_type:
|
|
|
|
case "fetchpost":
|
|
|
|
Post.handle_fetch_internal(instance.message["object"])
|
|
|
|
case "cleartimeline":
|
|
|
|
TimelineEvent.handle_clear_timeline(
|
|
|
|
instance.message["object"]
|
|
|
|
)
|
|
|
|
case "addfollow":
|
|
|
|
IdentityService.handle_internal_add_follow(
|
|
|
|
instance.message["object"]
|
|
|
|
)
|
2022-11-20 11:36:40 -08:00
|
|
|
case unknown:
|
2023-07-07 15:39:02 -07:00
|
|
|
return cls.errored
|
2023-07-07 15:29:12 -07:00
|
|
|
case unknown:
|
2023-07-07 15:33:55 -07:00
|
|
|
return cls.errored
|
2023-07-07 15:29:12 -07:00
|
|
|
return cls.processed
|
2023-07-07 15:32:57 -07:00
|
|
|
except (ActivityPubError, JsonLdError):
|
2023-07-07 15:29:12 -07:00
|
|
|
return cls.errored
|
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)
|
|
|
|
|
2023-01-16 10:53:40 -08:00
|
|
|
@classmethod
|
|
|
|
def create_internal(cls, payload):
|
|
|
|
"""
|
|
|
|
Creates an internal action message
|
|
|
|
"""
|
|
|
|
cls.objects.create(
|
|
|
|
message={
|
|
|
|
"type": "__internal__",
|
|
|
|
"object": payload,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
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")
|
2023-02-15 09:42:56 -08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def message_object_has_content(self):
|
2023-03-23 11:27:32 -07:00
|
|
|
object = self.message.get("object", {})
|
|
|
|
return "content" in object or "contentMap" in object
|