diff options
author | Andrew Godwin | 2022-11-07 00:19:00 -0700 |
---|---|---|
committer | Andrew Godwin | 2022-11-07 00:19:00 -0700 |
commit | c391e7bc4151ae148d42acd8bbe303338cdde31c (patch) | |
tree | d2b79043a63b52ec4692469eab596d831d396b9a /users/tasks | |
parent | fb6c409a9af5b8a686e977ee2251c359071e0ec3 (diff) | |
download | takahe-c391e7bc4151ae148d42acd8bbe303338cdde31c.tar.gz takahe-c391e7bc4151ae148d42acd8bbe303338cdde31c.tar.bz2 takahe-c391e7bc4151ae148d42acd8bbe303338cdde31c.zip |
THE FOLLOWS, THEY WORK
Well, in one direction anyway
Diffstat (limited to 'users/tasks')
-rw-r--r-- | users/tasks/follow.py | 5 | ||||
-rw-r--r-- | users/tasks/inbox.py | 24 |
2 files changed, 25 insertions, 4 deletions
diff --git a/users/tasks/follow.py b/users/tasks/follow.py index 3260124..872b35f 100644 --- a/users/tasks/follow.py +++ b/users/tasks/follow.py @@ -24,5 +24,6 @@ async def handle_follow_request(task_handler): response = await HttpSignature.signed_request( follow.target.inbox_uri, request, follow.source ) - print(response) - print(response.content) + if response.status_code >= 400: + raise ValueError(f"Request error: {response.status_code} {response.content}") + await Follow.objects.filter(pk=follow.pk).aupdate(requested=True) diff --git a/users/tasks/inbox.py b/users/tasks/inbox.py index ab80648..27c602d 100644 --- a/users/tasks/inbox.py +++ b/users/tasks/inbox.py @@ -1,3 +1,5 @@ +from asgiref.sync import sync_to_async + from users.models import Follow, Identity @@ -5,14 +7,20 @@ async def handle_inbox_item(task_handler): type = task_handler.payload["type"].lower() if type == "follow": await inbox_follow(task_handler.payload) + elif type == "accept": + inner_type = task_handler.payload["object"]["type"].lower() + if inner_type == "follow": + await sync_to_async(accept_follow)(task_handler.payload["object"]) + else: + raise ValueError(f"Cannot handle activity of type accept.{inner_type}") elif type == "undo": inner_type = task_handler.payload["object"]["type"].lower() if inner_type == "follow": await inbox_unfollow(task_handler.payload["object"]) else: - raise ValueError("Cannot undo activity of type {inner_type}") + raise ValueError(f"Cannot handle activity of type undo.{inner_type}") else: - raise ValueError("Cannot handle activity of type {inner_type}") + raise ValueError(f"Cannot handle activity of type {inner_type}") async def inbox_follow(payload): @@ -34,3 +42,15 @@ async def inbox_follow(payload): async def inbox_unfollow(payload): pass + + +def accept_follow(payload): + """ + Another server has acknowledged our follow request + """ + source = Identity.by_actor_uri_with_create(payload["actor"]) + target = Identity.by_actor_uri(payload["object"]) + follow = Follow.maybe_get(source, target) + if follow: + follow.accepted = True + follow.save() |