From 4705a8ebee510512d812a82dd860a270f6993f13 Mon Sep 17 00:00:00 2001
From: Eliot Berriot <contact@eliotberriot.com>
Date: Wed, 29 May 2019 14:31:44 +0200
Subject: [PATCH] First working PoC with API

---
 config/settings/base.py           |  1 +
 retribute_api/search/consumers.py | 22 +++++++++++++---------
 retribute_api/search/sources.py   |  6 +++++-
 tests/search/test_activitypub.py  |  2 +-
 tests/search/test_consumers.py    |  2 +-
 5 files changed, 21 insertions(+), 12 deletions(-)

diff --git a/config/settings/base.py b/config/settings/base.py
index 3c6f357..fa5d814 100644
--- a/config/settings/base.py
+++ b/config/settings/base.py
@@ -283,3 +283,4 @@ ADMIN_URL = env("DJANGO_ADMIN_URL", default="admin/")
 # ------------------------------------------------------------------------------
 # http://whitenoise.evans.io/en/latest/django.html#enable-whitenoise
 MIDDLEWARE.insert(1, "whitenoise.middleware.WhiteNoiseMiddleware")  # noqa F405
+BASE_URL = env("BASE_URL")
diff --git a/retribute_api/search/consumers.py b/retribute_api/search/consumers.py
index 01a53ef..c3a8a79 100644
--- a/retribute_api/search/consumers.py
+++ b/retribute_api/search/consumers.py
@@ -11,11 +11,22 @@ async def json_response(self, status, content):
     await self.send_response(
         status,
         json.dumps(content, indent=2, sort_keys=True).encode("utf-8"),
-        headers=[{"Content-Type": "application/json"}],
+        headers=[(b"Content-Type", b"application/json")],
     )
 
 
+def wrapper_500(callback):
+    async def inner(self, body):
+        try:
+            await callback(self, body)
+        except Exception as e:
+            await json_response(self, 400, {"detail": str(e)})
+
+    return callback
+
+
 class SearchSingleConsumer(AsyncHttpConsumer):
+    @wrapper_500
     async def handle(self, body):
         lookup_type = self.scope["url_route"]["kwargs"]["lookup_type"]
         lookup = self.scope["url_route"]["kwargs"]["lookup"]
@@ -24,17 +35,10 @@ class SearchSingleConsumer(AsyncHttpConsumer):
         except KeyError:
             await json_response(self, 400, {"detail": "Invalid lookup"})
         try:
-            import ipdb
-
-            ipdb.set_trace()
             async with aiohttp.client.ClientSession() as session:
                 data = await source.get(lookup, session)
+            profile = sources.result_to_retribute_profile(lookup_type, lookup, data)
         except exceptions.SearchError as e:
             await json_response(self, 400, {"detail": e.message})
-            raise
-        try:
-            profile = sources.result_to_retribute_profile(lookup_type, lookup, data)
-        except Exception:
-            raise
 
         await json_response(self, 200, profile)
diff --git a/retribute_api/search/sources.py b/retribute_api/search/sources.py
index e5a4b23..ea74c24 100644
--- a/retribute_api/search/sources.py
+++ b/retribute_api/search/sources.py
@@ -32,6 +32,10 @@ class Source(object):
 @registry.register
 class Activitypub(Source):
     id = "activitypub"
+    excluded_tags = [
+        "#noretribute",
+        # '#nobot',
+    ]
 
     async def get(self, lookup, session):
         async with session.get(
@@ -42,7 +46,7 @@ class Activitypub(Source):
         serializer = activitypub.ActorSerializer(data=actor_data)
         serializer.is_valid(raise_exception=True)
         for tag in serializer.validated_data["tag"]:
-            if tag["name"] in ["#nobot"]:
+            if tag["name"].lower() in self.excluded_tags:
                 raise exceptions.SkippedProfile()
 
         data = {
diff --git a/tests/search/test_activitypub.py b/tests/search/test_activitypub.py
index 86735c3..6c27c6c 100644
--- a/tests/search/test_activitypub.py
+++ b/tests/search/test_activitypub.py
@@ -5,7 +5,7 @@ def test_profile_serializer():
     payload = {
         "id": "https://test.domain",
         "url": "https://test.domain",
-        "tag": [{"type": "Hashtag", "name": "#nobot"}],
+        "tag": [{"type": "Hashtag", "name": "#NoRetribute"}],
         "attachment": [
             {
                 "type": "PropertyValue",
diff --git a/tests/search/test_consumers.py b/tests/search/test_consumers.py
index dfecbca..de59c04 100644
--- a/tests/search/test_consumers.py
+++ b/tests/search/test_consumers.py
@@ -20,5 +20,5 @@ async def test_search_consumer_success(loop, application, mocker, coroutine_mock
         "webfinger", "test@user.domain", get.return_value
     )
     assert response["status"] == 200
-    assert response["headers"] == [{"Content-Type": "application/json"}]
+    assert response["headers"] == [(b"Content-Type", b"application/json")]
     assert response["body"] == json.dumps(expected, indent=2, sort_keys=True).encode()
-- 
GitLab