sombrero gains the ability to look up "user@hostname"-style addresses,

via webfinger. Tests updated.
trilby-heavy
Marnanel Thurman 2020-05-16 21:44:50 +01:00
rodzic 461083c8ed
commit b69cc119fe
4 zmienionych plików z 134 dodań i 10 usunięć

Wyświetl plik

@ -12,6 +12,23 @@ from kepi.trilby_api.models import RemotePerson
logger = logging.Logger("kepi")
def _get_url_from_atstyle(user):
url = f'https://{user.hostname}/.well-known/'+\
f'webfinger?acct={user.acct}'
response = requests.get(
url,
headers = {
'Accept': 'application/activity+json',
},
)
self_link = [x for x in response.json()['links']
if x['rel']=='self']
user.url = self_link[0]['href']
@shared_task()
def _async_fetch_user(user):
@ -26,6 +43,12 @@ def _async_fetch_user(user):
raise ValueError(complaint)
if user.url is None and user.acct is not None:
_get_url_from_atstyle(user)
if user.url is None:
complain("No URL given.")
response = requests.get(
user.url,
headers = {
@ -108,11 +131,17 @@ def _async_fetch_user(user):
return user
def fetch_user(url):
def fetch_user(username):
if '@' in username:
result = RemotePerson(
acct = username,
)
else:
result = RemotePerson(
url = username,
)
result = RemotePerson(
url = url,
)
result.save()
_async_fetch_user(result)

Wyświetl plik

@ -13,9 +13,10 @@ import logging
logger = logging.Logger("kepi")
EXAMPLE_URL = "https://example.org/users/wombat"
EXAMPLE_ACTIVITY_URL = "https://example.org/users/wombat"
EXAMPLE_ATSTYLE = "wombat@example.org"
EXAMPLE_USER = """{"@context":["https://www.w3.org/ns/activitystreams",
EXAMPLE_ACTIVITY_RESULT = """{"@context":["https://www.w3.org/ns/activitystreams",
"https://w3id.org/security/v1",
{"manuallyApprovesFollowers":"as:manuallyApprovesFollowers",
"sensitive":"as:sensitive",
@ -63,21 +64,45 @@ EXAMPLE_USER = """{"@context":["https://www.w3.org/ns/activitystreams",
"url":"https://example.org/header.png"}}
"""
EXAMPLE_WEBFINGER_URL = 'https://example.org/.well-known/webfinger?acct='+EXAMPLE_ATSTYLE
EXAMPLE_WEBFINGER_RESULT = """{"subject":"acct:wombat@example.org",
"aliases":["https://example.org/@wombat",
"https://example.org/users/wombat"],
"links":[{"rel":"http://webfinger.net/rel/profile-page",
"type":"text/html",
"href":"https://example.org/@wombat"},
{"rel":"http://schemas.google.com/g/2010#updates-from",
"type":"application/atom+xml",
"href":"https://example.org/users/wombat.atom"},
{"rel":"self",
"type":"application/activity+json",
"href":"https://example.org/users/wombat"},
{"rel":"salmon",
"href":"https://example.org/api/salmon/15322"},
{"rel":"magic-public-key",
"href":"data:application/magic-public-key,RSA.qjX5AgZxDY0udxuZlBRo6K-mA6XNfmEoscra_YUOZ0c8tnl122vPV5DOdOrm0jpah-GUn7CK43UOCXMLJe3DIO7Q3w4TgNTGFjNERO1Dlh3Jgw_CbFBNbIb1QyFS0QjKBUcLKgPezGxklyk8U2-ErSiP1xOlZZMlSTcMlR5c0LRdQQ0TJ9Lx8MbH66B9qM6HnYP-Z2nkm6SwBw9QOAloIiz1H6JkHX9CUa8ZzVQwp82LRWI25I_Szc-MDvTqdLu3lljyxcHlpxhs9_5hfxu99_CUdbPU6TqAkpXMtzcfaSKb7bzbYTtxzlzTnQX6EtLdpGjBp-kGLAt-XozlBeSybQ==.AQAB"},
{"rel":"http://ostatus.org/schema/1.0/subscribe",
"template":"https://example.org/authorize_interaction?uri={uri}"}]}"""
class TestFetch(TestCase):
@httpretty.activate
def test_fetch_user(self):
httpretty.register_uri(
'GET',
EXAMPLE_URL,
EXAMPLE_ACTIVITY_URL,
status=200,
headers = {
'Content-Type': 'application/activity+json',
},
body = EXAMPLE_USER,
body = EXAMPLE_ACTIVITY_RESULT,
)
user = fetch_user(EXAMPLE_URL)
user = fetch_user(EXAMPLE_ACTIVITY_URL)
self._asserts_for_example_user(user)
def _asserts_for_example_user(self, user):
self.assertEqual(
user.display_name,
@ -101,7 +126,7 @@ class TestFetch(TestCase):
self.assertEqual(
user.url,
EXAMPLE_URL,
EXAMPLE_ACTIVITY_URL,
)
self.assertEqual(
@ -128,3 +153,30 @@ class TestFetch(TestCase):
user.is_local,
False,
)
@httpretty.activate
def test_atstyle(self):
httpretty.register_uri(
'GET',
EXAMPLE_ACTIVITY_URL,
status=200,
headers = {
'Content-Type': 'application/activity+json',
},
body = EXAMPLE_ACTIVITY_RESULT,
)
httpretty.register_uri(
'GET',
EXAMPLE_WEBFINGER_URL,
status=200,
headers = {
'Content-Type': 'application/jrd+json',
},
body = EXAMPLE_WEBFINGER_RESULT,
)
user = fetch_user(EXAMPLE_ATSTYLE)
self._asserts_for_example_user(user)

Wyświetl plik

@ -0,0 +1,23 @@
# Generated by Django 3.0.4 on 2020-05-16 19:46
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('trilby_api', '0013_auto_20200516_0059'),
]
operations = [
migrations.AlterField(
model_name='remoteperson',
name='acct',
field=models.CharField(blank=True, default=None, max_length=255, null=True, unique=True),
),
migrations.AlterField(
model_name='remoteperson',
name='url',
field=models.URLField(blank=True, max_length=255, null=True, unique=True),
),
]

Wyświetl plik

@ -14,6 +14,7 @@ from kepi.bowler_pub.utils import uri_to_url
import kepi.trilby_api.utils as trilby_utils
from django.utils.timezone import now
from django.core.exceptions import ValidationError
from urllib.parse import urlparse
import logging
logger = logging.Logger('kepi')
@ -117,6 +118,8 @@ class RemotePerson(Person):
url = models.URLField(
max_length = 255,
unique = True,
null = True,
blank = True,
)
status = models.IntegerField(
@ -174,6 +177,7 @@ class RemotePerson(Person):
null = True,
blank = True,
default = None,
unique = True,
)
@property
@ -183,6 +187,22 @@ class RemotePerson(Person):
def __str__(self):
return self.url
@property
def hostname(self):
if self.url is not None:
return urlparse(self.url).netloc
if self.acct is not None:
parts = self.acct.split('@')
if parts[0]=='':
# the format was @user@hostname
parts.pop(0)
return parts[1]
return None
########################################
class TrilbyUser(AbstractUser):