From 2f58bade3aa2871898af9bfe4bad0eb444560c1c Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Mon, 14 Mar 2022 15:51:12 +0000 Subject: [PATCH] Add alternate domains to alsoKnownAs --- daemon.py | 4 +++- person.py | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/daemon.py b/daemon.py index 110ced53f..f3a09c420 100644 --- a/daemon.py +++ b/daemon.py @@ -60,6 +60,7 @@ from donate import get_donation_url from donate import set_donation_url from donate import get_website from donate import set_website +from person import add_alternate_domains from person import add_actor_update_timestamp from person import set_person_notes from person import get_default_person_context @@ -428,7 +429,7 @@ class PubServer(BaseHTTPRequestHandler): def _convert_domains(self, calling_domain, referer_domain, msg_str: str) -> str: - """Convert domains to onion or i2p + """Convert domains to onion or i2p, depending upon who is asking """ curr_http_prefix = self.server.http_prefix + '://' if is_onion_request(calling_domain, referer_domain, @@ -13428,6 +13429,7 @@ class PubServer(BaseHTTPRequestHandler): actor_json = person_lookup(domain, path, base_dir) if not actor_json: return False + add_alternate_domains(actor_json, domain, onion_domain, i2p_domain) if self._request_http(): curr_session = \ self._establish_session("showPersonProfile", diff --git a/person.py b/person.py index 69026c9fc..c6ddc773c 100644 --- a/person.py +++ b/person.py @@ -38,6 +38,7 @@ from roles import set_role from roles import set_rolesFromList from roles import get_actor_roles_list from media import process_meta_data +from utils import get_nickname_from_actor from utils import remove_html from utils import contains_invalid_chars from utils import replace_users_with_at @@ -861,6 +862,31 @@ def person_upgrade_actor(base_dir: str, person_json: {}, save_json(person_json, actor_cache_filename) +def add_alternate_domains(actor_json: {}, domain: str, + onion_domain: str, i2p_domain: str) -> None: + """Adds alternate onion and/or i2p domains to alsoKnownAs + """ + if not onion_domain and not i2p_domain: + return + if not actor_json.get('id'): + return + if domain not in actor_json['id']: + return + nickname = get_nickname_from_actor(actor_json['id']) + if not nickname: + return + if 'alsoKnownAs' not in actor_json: + actor_json['alsoKnownAs'] = [] + if onion_domain: + onion_actor = 'http://' + onion_domain + '/users/' + nickname + if onion_actor not in actor_json['alsoKnownAs']: + actor_json['alsoKnownAs'].append(onion_actor) + if i2p_domain: + i2p_actor = 'http://' + i2p_domain + '/users/' + nickname + if i2p_actor not in actor_json['alsoKnownAs']: + actor_json['alsoKnownAs'].append(i2p_actor) + + def person_lookup(domain: str, path: str, base_dir: str) -> {}: """Lookup the person for an given nickname """