kopia lustrzana https://gitlab.com/jaywink/federation
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
rodzic
40c652c5f0
commit
f5db2ab835
|
@ -36,6 +36,9 @@ class ActivitypubFollow(ActivitypubObjectMixin, Follow):
|
||||||
"""
|
"""
|
||||||
Post receive hook - send back follow ack.
|
Post receive hook - send back follow ack.
|
||||||
"""
|
"""
|
||||||
|
if not self.following:
|
||||||
|
return
|
||||||
|
|
||||||
from federation.utils.activitypub import retrieve_and_parse_profile # Circulars
|
from federation.utils.activitypub import retrieve_and_parse_profile # Circulars
|
||||||
try:
|
try:
|
||||||
from federation.utils.django import get_function_from_config
|
from federation.utils.django import get_function_from_config
|
||||||
|
@ -76,13 +79,27 @@ class ActivitypubFollow(ActivitypubObjectMixin, Follow):
|
||||||
logger.exception("ActivitypubFollow.post_receive - Failed to send Accept back")
|
logger.exception("ActivitypubFollow.post_receive - Failed to send Accept back")
|
||||||
|
|
||||||
def to_as2(self) -> Dict:
|
def to_as2(self) -> Dict:
|
||||||
as2 = {
|
if self.following:
|
||||||
"@context": CONTEXTS_DEFAULT,
|
as2 = {
|
||||||
"id": self.activity_id,
|
"@context": CONTEXTS_DEFAULT,
|
||||||
"type": self._type,
|
"id": self.activity_id,
|
||||||
"actor": self.actor_id,
|
"type": self._type,
|
||||||
"object": self.target_id,
|
"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
|
return as2
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -20,6 +20,32 @@ class TestEntitiesConvertToAS2:
|
||||||
"object": "https://example.com/follow/1234",
|
"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):
|
def test_post_to_as2(self, activitypubpost):
|
||||||
result = activitypubpost.to_as2()
|
result = activitypubpost.to_as2()
|
||||||
assert result == {
|
assert result == {
|
||||||
|
|
|
@ -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
|
@pytest.fixture
|
||||||
def profile():
|
def profile():
|
||||||
return Profile(
|
return Profile(
|
||||||
|
|
Ładowanie…
Reference in New Issue