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

Can now fetch links from ap username

parent 76e08567
No related branches found
No related tags found
No related merge requests found
......@@ -9,6 +9,7 @@ class Registry(object):
if not obj.id:
raise ValueError("{} has no id".format(obj.id))
self._data[obj.id] = obj()
return obj
def __iter__(self):
yield from self._data.items()
......
......@@ -37,5 +37,7 @@ class AttachmentSerializer(serializers.Serializer):
class ActorSerializer(serializers.Serializer):
id = serializers.URLField()
url = serializers.URLField(required=False)
attachment = serializers.ListField(child=AttachmentSerializer(), min_length=1)
tag = serializers.ListField(child=TagSerializer(), min_length=0)
from .. import providers
from . import activitypub
from . import webfinger
class Registry(object):
def __init__(self):
self._data = {}
def register(self, obj):
if not obj.id:
raise ValueError("{} has no id".format(obj.id))
self._data[obj.id] = obj()
return obj
registry = Registry()
class Source(object):
id = None
async def get(self, lookup):
raise NotImplementedError()
@registry.register
class Webfinger(Source):
id = "webfinger"
async def get(self, lookup, session):
webfinger_data = await webfinger.lookup(lookup, session)
links = webfinger.get_links(webfinger_data)
found = None
if "activitypub" in links:
found = await self.get_activitypub(links["activitypub"], session)
return found
async def get_activitypub(self, url, session):
response = await session.get(
url, headers={"Accept": "application/activity+json"}
)
response.raise_for_status()
actor_data = await response.json()
serializer = activitypub.ActorSerializer(data=actor_data)
serializer.is_valid(raise_exception=True)
for tag in serializer.validated_data["tag"]:
if tag["name"] in ["#nobot"]:
return
data = {
"links": activitypub.extract_urls_from_attachments(
serializer.validated_data["attachment"]
)
}
return data
......@@ -18,7 +18,18 @@ class AccountLinkSerializer(serializers.Serializer):
class WebfingerSerializer(serializers.Serializer):
links = serializers.ListField(child=AccountLinkSerializer(), min_length=1)
links = serializers.ListField(child=serializers.DictField(), min_length=1)
def validate_links(self, value):
final = []
for link in value:
s = AccountLinkSerializer(data=link)
if s.is_valid():
final.append(s.validated_data)
if not final:
raise serializers.ValidationError("No valid webfinger link found")
return final
def get_links(payload):
......
......@@ -7,7 +7,7 @@ import asynctest
pytest_plugins = "aiohttp.pytest_plugin"
@pytest.fixture
@pytest.fixture(autouse=True)
def responses():
with aioresponses() as m:
yield m
......
......@@ -3,6 +3,8 @@ from retribute_api.search import activitypub
def test_profile_serializer():
payload = {
"id": "https://test.domain",
"url": "https://test.domain",
"tag": [{"type": "Hashtag", "name": "#nobot"}],
"attachment": [
{
......
from retribute_api.search import sources
async def test_webfinger_source(mocker, session, responses):
name = "username@domain.test"
webfinger_response = {
"subject": "acct:user@domain.test",
"aliases": ["https://domain.test/@user", "https://domain.test/users/user"],
"links": [
{
"rel": "self",
"type": "application/activity+json",
"href": "https://domain.test/users/user",
}
],
}
responses.get(
"https://domain.test/.well-known/webfinger?resource=acct:{}".format(name),
payload=webfinger_response,
)
actor_response = {
"id": "https://test.domain",
"tag": [{"type": "Hashtag", "name": "#helo"}],
"attachment": [
{
"type": "PropertyValue",
"name": "Support me on Patreon",
"value": '<a href="https://patreon.com/username" rel="me nofollow noopener">Test</a>',
},
{
"type": "PropertyValue",
"name": "Tip me on Ko-Fi",
"value": '<a href="https://ko-fi.com/username" rel="me nofollow noopener">Test</a>',
},
],
}
responses.get("https://domain.test/users/user", payload=actor_response)
expected = {
"links": [
{"summary": "Support me on Patreon", "url": "https://patreon.com/username"},
{"summary": "Tip me on Ko-Fi", "url": "https://ko-fi.com/username"},
]
}
source = sources.Webfinger()
result = await source.get(name, session)
assert result == expected
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment