Verified Commit 8203fa01 authored by Eliot Berriot's avatar Eliot Berriot
Browse files

Added logic to build retribute profile from extracted links

parent 16d1f9ea
class SearchError(ValueError):
pass
class MeansNotFound(SearchError):
pass
class SkippedProfile(SearchError):
pass
from django.conf import settings
from django.utils import timezone
from .. import providers
from . import activitypub
from . import exceptions
from . import means
from . import webfinger
......@@ -25,21 +30,12 @@ class Source(object):
@registry.register
class Webfinger(Source):
id = "webfinger"
class Activitypub(Source):
id = "activitypub"
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"}
lookup, headers={"Accept": "application/activity+json"}
)
response.raise_for_status()
actor_data = await response.json()
......@@ -47,7 +43,7 @@ class Webfinger(Source):
serializer.is_valid(raise_exception=True)
for tag in serializer.validated_data["tag"]:
if tag["name"] in ["#nobot"]:
return
raise exceptions.SkippedProfile()
data = {
"links": activitypub.extract_urls_from_attachments(
......@@ -56,3 +52,47 @@ class Webfinger(Source):
}
return data
@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 Activitypub().get(links["activitypub"], session)
return found
def result_to_retribute_profile(lookup_type, lookup, data):
path = settings.BASE_URL + "/compat/"
now = timezone.now()
valid_means = [
(link, means.extract_from_url(link["url"])) for link in data["links"]
]
valid_means = list(filter(lambda v: v[1] is not None, valid_means))
if not valid_means:
raise exceptions.MeansNotFound()
final_means = []
for i, mean in enumerate(reversed(valid_means)):
link_data = mean[0]
mean_data = mean[1]
mean_data.setdefault("weight", i)
mean_data.setdefault("url", link_data["url"])
if link_data.get("summary"):
mean_data.setdefault("summary", link_data["summary"])
final_means.append(mean_data)
final = {
"version": "0.1",
"id": "https://retribute.me.test/compat/{}:{}".format(lookup_type, lookup),
"title": "Compat profile for {}:{}".format(lookup_type, lookup),
"updated": now.isoformat(),
"identities": [],
"means": list(sorted(final_means, key=lambda v: v["weight"], reverse=True)),
}
return final
......@@ -3,6 +3,8 @@ import aiohttp
from aioresponses import aioresponses
import asynctest
from django.utils import timezone
pytest_plugins = "aiohttp.pytest_plugin"
......@@ -22,3 +24,10 @@ async def session(loop):
@pytest.fixture
async def coroutine_mock():
return asynctest.CoroutineMock
@pytest.fixture
def now(mocker):
now = timezone.now()
mocker.patch.object(timezone, "now", return_value=now)
return now
......@@ -49,3 +49,44 @@ async def test_webfinger_source(mocker, session, responses):
result = await source.get(name, session)
assert result == expected
def test_source_result_to_retribute_profile(settings, now):
settings.BASE_URL = "https://retribute.me.test"
lookup_type = "webfinger"
lookup = "test@domain.test"
data = {
"links": [
{"summary": "Support me on Patreon", "url": "https://patreon.com/username"},
{"summary": "Tip me on Ko-Fi", "url": "https://ko-fi.com/username"},
]
}
expected = {
"version": "0.1",
"id": "https://retribute.me.test/compat/{}:{}".format(lookup_type, lookup),
"title": "Compat profile for {}:{}".format(lookup_type, lookup),
"updated": now.isoformat(),
"identities": [],
"means": [
{
"provider": "patreon",
"id": "username",
"summary": "Support me on Patreon",
"weight": 1,
"url": "https://patreon.com/username",
},
{
"provider": "ko-fi",
"summary": "Tip me on Ko-Fi",
"id": "username",
"weight": 0,
"url": "https://ko-fi.com/username",
},
],
}
result = sources.result_to_retribute_profile(lookup_type, lookup, data)
assert result == expected
Markdown is supported
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