Fix wrong URI for the relay's followers collection
[relay.git/.git] / relay / actor.py
index b27f6bb..ad09ecb 100644 (file)
@@ -22,8 +22,8 @@ if "actorKeys" not in DATABASE:
     pubkey = privkey.publickey()
 
     DATABASE["actorKeys"] = {
-        "publicKey": pubkey.exportKey('PEM'),
-        "privateKey": privkey.exportKey('PEM')
+        "publicKey": pubkey.exportKey('PEM').decode('utf-8'),
+        "privateKey": privkey.exportKey('PEM').decode('utf-8')
     }
 
 
@@ -105,9 +105,12 @@ async def push_message_to_actor(actor, message, our_key_id):
 
 
 async def follow_remote_actor(actor_uri):
-    logging.info('following: %r', actor_uri)
-
     actor = await fetch_actor(actor_uri)
+    if not actor:
+        logging.info('failed to fetch actor at: %r', actor_uri)
+        return
+
+    logging.info('following: %r', actor_uri)
 
     message = {
         "@context": "https://www.w3.org/ns/activitystreams",
@@ -121,9 +124,12 @@ async def follow_remote_actor(actor_uri):
 
 
 async def unfollow_remote_actor(actor_uri):
-    logging.info('unfollowing: %r', actor_uri)
-
     actor = await fetch_actor(actor_uri)
+    if not actor:
+        logging.info('failed to fetch actor at: %r', actor_uri)
+        return
+
+    logging.info('unfollowing: %r', actor_uri)
 
     message = {
         "@context": "https://www.w3.org/ns/activitystreams",
@@ -147,13 +153,18 @@ def strip_html(data):
     return cgi.escape(no_tags)
 
 
-def distill_inboxes(actor):
+def distill_inboxes(actor, object_id):
     global DATABASE
 
+    origin_hostname = urlsplit(object_id).hostname
+
     inbox = get_actor_inbox(actor)
     targets = [target for target in DATABASE.get('relay-list', []) if target != inbox]
+    targets = [target for target in targets if urlsplit(target).hostname != origin_hostname]
+    hostnames = [urlsplit(target).hostname for target in targets]
 
     assert inbox not in targets
+    assert origin_hostname not in hostnames
 
     return targets
 
@@ -177,16 +188,12 @@ async def handle_relay(actor, data, request):
         logging.debug('>> already relayed %r as %r', object_id, CACHE[object_id])
         return
 
-    # don't relay mastodon announces -- causes LRP fake direction issues
-    if data['type'] == 'Announce' and len(data.get('cc', [])) > 0:
-        return
-
     activity_id = "https://{}/activities/{}".format(request.host, uuid.uuid4())
 
     message = {
         "@context": "https://www.w3.org/ns/activitystreams",
         "type": "Announce",
-        "to": ["https://{}/actor/followers".format(request.host)],
+        "to": ["https://{}/followers".format(request.host)],
         "actor": "https://{}/actor".format(request.host),
         "object": object_id,
         "id": activity_id
@@ -194,7 +201,7 @@ async def handle_relay(actor, data, request):
 
     logging.debug('>> relay: %r', message)
 
-    inboxes = distill_inboxes(actor)
+    inboxes = distill_inboxes(actor, object_id)
 
     futures = [push_message_to_actor({'inbox': inbox}, message, 'https://{}/actor#main-key'.format(request.host)) for inbox in inboxes]
     asyncio.ensure_future(asyncio.gather(*futures))