Add "Link" header to actor views.

Partial fix for issue 28.
trilby
Marnanel Thurman 2019-09-30 20:58:08 +01:00
rodzic 52dbd81059
commit 5e4e5462c9
2 zmienionych plików z 91 dodań i 0 usunięć

Wyświetl plik

@ -314,6 +314,9 @@ class ThingView(KepiView):
class ActorView(ThingView): class ActorView(ThingView):
def activity_get(self, request, *args, **kwargs): def activity_get(self, request, *args, **kwargs):
self._username = kwargs['username']
logger.debug('Looking up Actor by username==%s', logger.debug('Looking up Actor by username==%s',
kwargs['username']) kwargs['username'])
@ -353,6 +356,57 @@ class ActorView(ThingView):
inbox.append(request.activity) 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 <https://www.w3.org/wiki/LinkHeader>.
"""
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): class FollowingView(KepiView):
def activity_get(self, request, *args, **kwargs): def activity_get(self, request, *args, **kwargs):

Wyświetl plik

@ -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,
[
'<https://testserver/.well-known/webfinger?resource='+\
'acct%3Aalice%40testserver>; rel="lrdd"; '+\
'type="application/xrd+xml"',
'<https://testserver/users/alice>; '+\
'rel="alternate"; type="application/activity+json"',
])