Follow serializer

merge-requests/154/head
Eliot Berriot 2018-04-03 23:24:51 +02:00
rodzic f19418d2c2
commit 657bd4b01a
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: DD6965E2476E5C27
2 zmienionych plików z 48 dodań i 5 usunięć

Wyświetl plik

@ -11,6 +11,12 @@ from . import models
from . import utils
AP_CONTEXT = [
'https://www.w3.org/ns/activitystreams',
'https://w3id.org/security/v1',
{},
]
class ActorSerializer(serializers.ModelSerializer):
# left maps to activitypub fields, right to our internal models
id = serializers.URLField(source='url')
@ -43,11 +49,7 @@ class ActorSerializer(serializers.ModelSerializer):
def to_representation(self, instance):
ret = super().to_representation(instance)
ret['@context'] = [
'https://www.w3.org/ns/activitystreams',
'https://w3id.org/security/v1',
{},
]
ret['@context'] = AP_CONTEXT
if instance.public_key:
ret['publicKey'] = {
'owner': instance.url,
@ -87,6 +89,28 @@ class ActorSerializer(serializers.ModelSerializer):
return value[:500]
class FollowSerializer(serializers.ModelSerializer):
# left maps to activitypub fields, right to our internal models
id = serializers.URLField(source='get_federation_url')
object = serializers.URLField(source='target.url')
actor = serializers.URLField(source='actor.url')
type = serializers.CharField(source='ap_type')
class Meta:
model = models.Actor
fields = [
'id',
'object',
'actor',
'type'
]
def to_representation(self, instance):
ret = super().to_representation(instance)
ret['@context'] = AP_CONTEXT
return ret
class ActorWebfingerSerializer(serializers.ModelSerializer):
class Meta:
model = models.Actor

Wyświetl plik

@ -144,3 +144,22 @@ def test_webfinger_serializer():
serializer = serializers.ActorWebfingerSerializer(actor)
assert serializer.data == expected
def test_follow_serializer_to_ap(factories):
follow = factories['federation.Follow'](local=True)
serializer = serializers.FollowSerializer(follow)
expected = {
'@context': [
'https://www.w3.org/ns/activitystreams',
'https://w3id.org/security/v1',
{},
],
'id': follow.get_federation_url(),
'type': 'Follow',
'actor': follow.actor.url,
'object': follow.target.url,
}
assert serializer.data == expected