From b13c239213147b7acae4060aff35640d625b5169 Mon Sep 17 00:00:00 2001 From: Andrew Godwin Date: Wed, 16 Nov 2022 22:23:32 -0700 Subject: Handle post edits, follow undos --- users/models/follow.py | 8 ++++---- users/models/identity.py | 22 +++++++++++++++++++--- users/models/inbox_message.py | 12 +++++++++++- 3 files changed, 34 insertions(+), 8 deletions(-) (limited to 'users/models') diff --git a/users/models/follow.py b/users/models/follow.py index 0236d19..defe399 100644 --- a/users/models/follow.py +++ b/users/models/follow.py @@ -13,15 +13,15 @@ class FollowStates(StateGraph): local_requested = State(try_interval=24 * 60 * 60) remote_requested = State(try_interval=24 * 60 * 60) accepted = State(externally_progressed=True) - undone_locally = State(try_interval=60 * 60) + undone = State(try_interval=60 * 60) undone_remotely = State() unrequested.transitions_to(local_requested) unrequested.transitions_to(remote_requested) local_requested.transitions_to(accepted) remote_requested.transitions_to(accepted) - accepted.transitions_to(undone_locally) - undone_locally.transitions_to(undone_remotely) + accepted.transitions_to(undone) + undone.transitions_to(undone_remotely) @classmethod async def handle_unrequested(cls, instance: "Follow"): @@ -63,7 +63,7 @@ class FollowStates(StateGraph): return cls.accepted @classmethod - async def handle_undone_locally(cls, instance: "Follow"): + async def handle_undone(cls, instance: "Follow"): """ Delivers the Undo object to the target server """ diff --git a/users/models/identity.py b/users/models/identity.py index d97f5f0..ba8559b 100644 --- a/users/models/identity.py +++ b/users/models/identity.py @@ -162,7 +162,7 @@ class Identity(StatorModel): if create: return cls.objects.create(actor_uri=uri, local=False) else: - raise KeyError(f"No identity found matching {uri}") + raise cls.DoesNotExist(f"No identity found with actor_uri {uri}") ### Dynamic properties ### @@ -192,7 +192,7 @@ class Identity(StatorModel): # TODO: Setting return self.data_age > 60 * 24 * 24 - ### ActivityPub (boutbound) ### + ### ActivityPub (outbound) ### def to_ap(self): response = { @@ -206,7 +206,7 @@ class Identity(StatorModel): "publicKeyPem": self.public_key, }, "published": self.created.strftime("%Y-%m-%dT%H:%M:%SZ"), - "url": self.urls.view_nice, + "url": str(self.urls.view_nice), } if self.name: response["name"] = self.name @@ -214,6 +214,21 @@ class Identity(StatorModel): response["summary"] = self.summary return response + ### ActivityPub (inbound) ### + + @classmethod + def handle_update_ap(cls, data): + """ + Takes an incoming update.person message and just forces us to add it + to our fetch queue (don't want to bother with two load paths right now) + """ + # Find by actor + try: + actor = cls.by_actor_uri(data["actor"]) + actor.transition_perform(IdentityStates.outdated) + except cls.DoesNotExist: + pass + ### Actor/Webfinger fetching ### @classmethod @@ -314,4 +329,5 @@ class Identity(StatorModel): ) .decode("ascii") ) + self.public_key_id = self.actor_uri + "#main-key" self.save() diff --git a/users/models/inbox_message.py b/users/models/inbox_message.py index 55eb3cb..ee23ae6 100644 --- a/users/models/inbox_message.py +++ b/users/models/inbox_message.py @@ -13,7 +13,7 @@ class InboxMessageStates(StateGraph): @classmethod async def handle_received(cls, instance: "InboxMessage"): from activities.models import Post, PostInteraction - from users.models import Follow + from users.models import Follow, Identity match instance.message_type: case "follow": @@ -30,6 +30,16 @@ class InboxMessageStates(StateGraph): raise ValueError( f"Cannot handle activity of type create.{unknown}" ) + 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}" + ) case "accept": match instance.message_object_type: case "follow": -- cgit v1.2.3