Skip to content
Snippets Groups Projects
Verified Commit 2a4ce0a4 authored by Eliot Berriot's avatar Eliot Berriot
Browse files

ActorSerializer is not a modelserializer anymore

parent dda1cd25
No related branches found
No related tags found
No related merge requests found
...@@ -22,7 +22,6 @@ def scan_from_account_name(account_name): ...@@ -22,7 +22,6 @@ def scan_from_account_name(account_name):
and return corresponding data in a dictionary. and return corresponding data in a dictionary.
""" """
data = {} data = {}
try: try:
username, domain = webfinger.clean_acct( username, domain = webfinger.clean_acct(
......
...@@ -22,40 +22,38 @@ AP_CONTEXT = [ ...@@ -22,40 +22,38 @@ AP_CONTEXT = [
] ]
class ActorSerializer(serializers.ModelSerializer): class ActorSerializer(serializers.Serializer):
# left maps to activitypub fields, right to our internal models id = serializers.URLField()
id = serializers.URLField(source='url') outbox = serializers.URLField()
outbox = serializers.URLField(source='outbox_url') inbox = serializers.URLField()
inbox = serializers.URLField(source='inbox_url') type = serializers.ChoiceField(choices=models.TYPE_CHOICES)
following = serializers.URLField( preferredUsername = serializers.CharField()
source='following_url', required=False, allow_null=True) manuallyApprovesFollowers = serializers.NullBooleanField(required=False)
followers = serializers.URLField( name = serializers.CharField(required=False, max_length=200)
source='followers_url', required=False, allow_null=True)
preferredUsername = serializers.CharField(
source='preferred_username', required=False)
publicKey = serializers.JSONField(source='public_key', required=False)
manuallyApprovesFollowers = serializers.NullBooleanField(
source='manually_approves_followers', required=False)
summary = serializers.CharField(max_length=None, required=False) summary = serializers.CharField(max_length=None, required=False)
followers = serializers.URLField(required=False, allow_null=True)
class Meta: following = serializers.URLField(required=False, allow_null=True)
model = models.Actor publicKey = serializers.JSONField(required=False)
fields = [
'id',
'type',
'name',
'summary',
'preferredUsername',
'publicKey',
'inbox',
'outbox',
'following',
'followers',
'manuallyApprovesFollowers',
]
def to_representation(self, instance): def to_representation(self, instance):
ret = super().to_representation(instance) ret = {
'id': instance.url,
'outbox': instance.outbox_url,
'inbox': instance.inbox_url,
'preferredUsername': instance.preferred_username,
'type': instance.type,
}
if instance.name:
ret['name'] = instance.name
if instance.followers_url:
ret['followers'] = instance.followers_url
if instance.following_url:
ret['following'] = instance.following_url
if instance.summary:
ret['summary'] = instance.summary
if instance.manually_approves_followers is not None:
ret['manuallyApprovesFollowers'] = instance.manually_approves_followers
ret['@context'] = AP_CONTEXT ret['@context'] = AP_CONTEXT
if instance.public_key: if instance.public_key:
ret['publicKey'] = { ret['publicKey'] = {
...@@ -69,8 +67,21 @@ class ActorSerializer(serializers.ModelSerializer): ...@@ -69,8 +67,21 @@ class ActorSerializer(serializers.ModelSerializer):
return ret return ret
def prepare_missing_fields(self): def prepare_missing_fields(self):
kwargs = {} kwargs = {
domain = urllib.parse.urlparse(self.validated_data['url']).netloc 'url': self.validated_data['id'],
'outbox_url': self.validated_data['outbox'],
'inbox_url': self.validated_data['inbox'],
'following_url': self.validated_data.get('following'),
'followers_url': self.validated_data.get('followers'),
'summary': self.validated_data.get('summary'),
'type': self.validated_data['type'],
'name': self.validated_data.get('name'),
'preferred_username': self.validated_data['preferredUsername'],
}
maf = self.validated_data.get('manuallyApprovesFollowers')
if maf is not None:
kwargs['manually_approves_followers'] = maf
domain = urllib.parse.urlparse(kwargs['url']).netloc
kwargs['domain'] = domain kwargs['domain'] = domain
for endpoint, url in self.initial_data.get('endpoints', {}).items(): for endpoint, url in self.initial_data.get('endpoints', {}).items():
if endpoint == 'sharedInbox': if endpoint == 'sharedInbox':
...@@ -83,13 +94,15 @@ class ActorSerializer(serializers.ModelSerializer): ...@@ -83,13 +94,15 @@ class ActorSerializer(serializers.ModelSerializer):
return kwargs return kwargs
def build(self): def build(self):
d = self.validated_data.copy() d = self.prepare_missing_fields()
d.update(self.prepare_missing_fields()) return models.Actor(**d)
return self.Meta.model(**d)
def save(self, **kwargs): def save(self, **kwargs):
kwargs.update(self.prepare_missing_fields()) d = self.prepare_missing_fields()
return super().save(**kwargs) d.update(kwargs)
return models.Actor.objects.create(
**d
)
def validate_summary(self, value): def validate_summary(self, value):
if value: if value:
...@@ -118,9 +131,6 @@ class LibraryActorSerializer(ActorSerializer): ...@@ -118,9 +131,6 @@ class LibraryActorSerializer(ActorSerializer):
url = serializers.ListField( url = serializers.ListField(
child=serializers.JSONField()) child=serializers.JSONField())
class Meta(ActorSerializer.Meta):
fields = ActorSerializer.Meta.fields + ['url']
def validate(self, validated_data): def validate(self, validated_data):
try: try:
urls = validated_data['url'] urls = validated_data['url']
......
...@@ -10,8 +10,10 @@ def test_authenticate(factories, mocker, api_request): ...@@ -10,8 +10,10 @@ def test_authenticate(factories, mocker, api_request):
'funkwhale_api.federation.actors.get_actor_data', 'funkwhale_api.federation.actors.get_actor_data',
return_value={ return_value={
'id': actor_url, 'id': actor_url,
'type': 'Person',
'outbox': 'https://test.com', 'outbox': 'https://test.com',
'inbox': 'https://test.com', 'inbox': 'https://test.com',
'preferredUsername': 'test',
'publicKey': { 'publicKey': {
'publicKeyPem': public.decode('utf-8'), 'publicKeyPem': public.decode('utf-8'),
'owner': actor_url, 'owner': actor_url,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment