Add support for outgoing unfollow in ActivityPub

Sent as Unfo{Follow}.

Also don't send an accept to an Undo{Follow}..
merge-requests/146/head
Jason Robinson 2019-06-29 01:21:33 +03:00
rodzic 40c652c5f0
commit f5db2ab835
3 zmienionych plików z 60 dodań i 7 usunięć

Wyświetl plik

@ -36,6 +36,9 @@ class ActivitypubFollow(ActivitypubObjectMixin, Follow):
"""
Post receive hook - send back follow ack.
"""
if not self.following:
return
from federation.utils.activitypub import retrieve_and_parse_profile # Circulars
try:
from federation.utils.django import get_function_from_config
@ -76,6 +79,7 @@ class ActivitypubFollow(ActivitypubObjectMixin, Follow):
logger.exception("ActivitypubFollow.post_receive - Failed to send Accept back")
def to_as2(self) -> Dict:
if self.following:
as2 = {
"@context": CONTEXTS_DEFAULT,
"id": self.activity_id,
@ -83,6 +87,19 @@ class ActivitypubFollow(ActivitypubObjectMixin, Follow):
"actor": self.actor_id,
"object": self.target_id,
}
else:
as2 = {
"@context": CONTEXTS_DEFAULT,
"id": self.activity_id,
"type": ActivityType.UNDO.value,
"actor": self.actor_id,
"object": {
"id": f"{self.actor_id}#follow-{uuid.uuid4()}",
"type": self._type,
"actor": self.actor_id,
"object": self.target_id,
},
}
return as2

Wyświetl plik

@ -20,6 +20,32 @@ class TestEntitiesConvertToAS2:
"object": "https://example.com/follow/1234",
}
def test_follow_to_as2(self, activitypubfollow):
result = activitypubfollow.to_as2()
assert result == {
"@context": CONTEXTS_DEFAULT,
"id": "https://localhost/follow",
"type": "Follow",
"actor": "https://localhost/profile",
"object": "https://example.com/profile"
}
def test_follow_to_as2__undo(self, activitypubundofollow):
result = activitypubundofollow.to_as2()
result["object"]["id"] = "https://localhost/follow" # Real object will have a random UUID postfix here
assert result == {
"@context": CONTEXTS_DEFAULT,
"id": "https://localhost/undo",
"type": "Undo",
"actor": "https://localhost/profile",
"object": {
"id": "https://localhost/follow",
"type": "Follow",
"actor": "https://localhost/profile",
"object": "https://example.com/profile",
}
}
def test_post_to_as2(self, activitypubpost):
result = activitypubpost.to_as2()
assert result == {

Wyświetl plik

@ -59,6 +59,16 @@ def activitypubprofile():
)
@pytest.fixture
def activitypubundofollow():
return ActivitypubFollow(
activity_id="https://localhost/undo",
actor_id="https://localhost/profile",
target_id="https://example.com/profile",
following=False,
)
@pytest.fixture
def profile():
return Profile(