From 99bf6a4a01373c516ec247c3933c66d7fe4b4147 Mon Sep 17 00:00:00 2001 From: Thomas Sileo Date: Fri, 1 Nov 2019 12:15:42 +0100 Subject: [PATCH] Fix for HubZilla actor caching --- core/activitypub.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/core/activitypub.py b/core/activitypub.py index bd5ce4d..b09134c 100644 --- a/core/activitypub.py +++ b/core/activitypub.py @@ -78,6 +78,24 @@ def _answer_key(choice: str) -> str: return h.hexdigest() +def _actor_url(actor: ap.ActivityType) -> str: + if isinstance(actor.url, dict): + if actor.url.get("type") == ap.ActivityType.LINK.value: + return actor.url["href"] + + raise ValueError(f"unkown actor url object type: {actor.url!r}") + + elif isinstance(actor.url, str): + return actor.url + + # Return the actor ID if we cannot get the URL + elif isinstance(actor.id, str): + return actor.id + + else: + raise ValueError(f"invalid actor URL: {actor.url!r}") + + def _actor_hash(actor: ap.ActivityType, local: bool = False) -> str: """Used to know when to update the meta actor cache, like an "actor version".""" h = hashlib.new("sha1") @@ -85,7 +103,7 @@ def _actor_hash(actor: ap.ActivityType, local: bool = False) -> str: h.update((actor.name or "").encode()) h.update((actor.preferredUsername or "").encode()) h.update((actor.summary or "").encode()) - h.update((actor.url or "").encode()) + h.update(_actor_url(actor).encode()) key = actor.get_key() h.update(key.pubkey_pem.encode()) h.update(key.key_id().encode())