diff --git a/django_kepi/views/activitypub.py b/django_kepi/views/activitypub.py index a006372..33ef538 100644 --- a/django_kepi/views/activitypub.py +++ b/django_kepi/views/activitypub.py @@ -314,6 +314,9 @@ class ThingView(KepiView): class ActorView(ThingView): def activity_get(self, request, *args, **kwargs): + + self._username = kwargs['username'] + logger.debug('Looking up Actor by username==%s', kwargs['username']) @@ -353,6 +356,57 @@ class ActorView(ThingView): inbox.append(request.activity) + def _to_json(self, data): + """ + Adds the Link header to an Actor's record. + + The Link header is described in RFC 5988, + and . + """ + + result = super()._to_json(data) + + user_url = settings.KEPI['USER_URL_FORMAT'] % { + 'username': self._username, + 'hostname': settings.KEPI['LOCAL_OBJECT_HOSTNAME'], + } + + webfinger_url = 'https://%s/.well-known/webfinger?resource=%s' % ( + settings.KEPI['LOCAL_OBJECT_HOSTNAME'], + urllib.parse.quote( + 'acct:%(name)s@%(host)s' % { + 'name': self._username, + 'host': settings.KEPI['LOCAL_OBJECT_HOSTNAME'], + })) + links = [ + { + 'url': webfinger_url, + 'rel': 'lrdd', + 'type': 'application/xrd+xml', + }, + + # TODO: rel: alternate, type: application/atom+xml, + # but that will be set by tophat and not kepi + + { + 'url': user_url, + 'rel': 'alternate', + 'type': 'application/activity+json', + }, + ] + + link_value = ', '.join([ + '<%(url)s>; rel="%(rel)s"; type="%(type)s"' % x + for x in links + ]) + + logger.info('Setting Link header to: %s', + link_value) + + result['Link'] = link_value + + return result + class FollowingView(KepiView): def activity_get(self, request, *args, **kwargs): diff --git a/tests/test_headers.py b/tests/test_headers.py new file mode 100644 index 0000000..fcaa365 --- /dev/null +++ b/tests/test_headers.py @@ -0,0 +1,37 @@ +from django.test import TestCase, Client +from . import create_local_person +from django.conf import settings +import logging + +MIME_TYPE = 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"' + +class TestHeaders(TestCase): + + def setUp(self): + settings.KEPI['LOCAL_OBJECT_HOSTNAME'] = 'testserver' + + def test_link(self): + + alice = create_local_person('alice') + client = Client() + alice_url = settings.KEPI['USER_URL_FORMAT'] % { + 'username': 'alice', + 'hostname': settings.KEPI['LOCAL_OBJECT_HOSTNAME'], + } + + response = client.get(alice_url, + HTTP_ACCEPT = MIME_TYPE, + ) + + links = response['Link'].split(', ') + + self.assertEqual( + links, + [ + '; rel="lrdd"; '+\ + 'type="application/xrd+xml"', + + '; '+\ + 'rel="alternate"; type="application/activity+json"', + ])