Verified Commit c97db31c authored by Eliot Berriot's avatar Eliot Berriot
Browse files

Include following state in scan payload

parent f0ef9ea5
import json
import requests import requests
from django.conf import settings from django.conf import settings
...@@ -5,6 +6,7 @@ from django.conf import settings ...@@ -5,6 +6,7 @@ from django.conf import settings
from funkwhale_api.common import session from funkwhale_api.common import session
from . import actors from . import actors
from . import models
from . import serializers from . import serializers
from . import signing from . import signing
from . import webfinger from . import webfinger
...@@ -22,6 +24,39 @@ def scan_from_account_name(account_name): ...@@ -22,6 +24,39 @@ def scan_from_account_name(account_name):
""" """
data = {} data = {}
try:
username, domain = webfinger.clean_acct(
account_name, ensure_local=False)
except serializers.ValidationError:
return {
'webfinger': {
'errors': ['Invalid account string']
}
}
system_library = actors.SYSTEM_ACTORS['library'].get_actor_instance()
library = models.Library.objects.filter(
actor__domain=domain,
actor__preferred_username=username
).select_related('actor').first()
follow_request = None
if library:
data['local']['following'] = True
data['local']['awaiting_approval'] = True
else:
follow_request = models.FollowRequest.objects.filter(
target__preferred_username=username,
target__domain=username,
actor=system_library,
).first()
data['local'] = {
'following': False,
'awaiting_approval': False,
}
if follow_request:
data['awaiting_approval'] = follow_request.approved is None
follow_request = models.Follow
try: try:
data['webfinger'] = webfinger.get_resource( data['webfinger'] = webfinger.get_resource(
'acct:{}'.format(account_name)) 'acct:{}'.format(account_name))
...@@ -39,6 +74,12 @@ def scan_from_account_name(account_name): ...@@ -39,6 +74,12 @@ def scan_from_account_name(account_name):
e.response.status_code)] e.response.status_code)]
} }
} }
except json.JSONDecodeError as e:
return {
'webfinger': {
'errors': ['Could not process webfinger response']
}
}
try: try:
data['actor'] = actors.get_actor_data(data['webfinger']['actor_url']) data['actor'] = actors.get_actor_data(data['webfinger']['actor_url'])
...@@ -56,7 +97,11 @@ def scan_from_account_name(account_name): ...@@ -56,7 +97,11 @@ def scan_from_account_name(account_name):
return data return data
serializer = serializers.LibraryActorSerializer(data=data['actor']) serializer = serializers.LibraryActorSerializer(data=data['actor'])
serializer.is_valid(raise_exception=True) if not serializer.is_valid():
data['actor'] = {
'errors': ['Invalid ActivityPub actor']
}
return data
data['library'] = get_library_data( data['library'] = get_library_data(
serializer.validated_data['library_url']) serializer.validated_data['library_url'])
......
...@@ -39,6 +39,10 @@ def test_library_scan_from_account_name(mocker, factories): ...@@ -39,6 +39,10 @@ def test_library_scan_from_account_name(mocker, factories):
'webfinger': get_resource_result, 'webfinger': get_resource_result,
'actor': actor_data, 'actor': actor_data,
'library': get_library_data_result, 'library': get_library_data_result,
'local': {
'following': False,
'awaiting_approval': False,
},
} }
...@@ -63,4 +67,4 @@ def test_get_library_data_requires_authentication(r_mock, factories): ...@@ -63,4 +67,4 @@ def test_get_library_data_requires_authentication(r_mock, factories):
url = 'https://test.library' url = 'https://test.library'
r_mock.get(url, status_code=403) r_mock.get(url, status_code=403)
result = library.get_library_data(url) result = library.get_library_data(url)
assert result['errors'] == ['This library requires authentication'] assert result['errors'] == ['Permission denied while scanning library']
...@@ -218,7 +218,6 @@ def test_paginated_collection_serializer_validation(): ...@@ -218,7 +218,6 @@ def test_paginated_collection_serializer_validation():
assert serializer.validated_data['totalItems'] == 5 assert serializer.validated_data['totalItems'] == 5
assert serializer.validated_data['id'] == data['id'] assert serializer.validated_data['id'] == data['id']
assert serializer.validated_data['actor'] == data['actor'] assert serializer.validated_data['actor'] == data['actor']
assert serializer.validated_data['items'] == []
def test_collection_page_serializer_validation(): def test_collection_page_serializer_validation():
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment