From ae8ed147c74691159b22c9c94484b04e888e49df Mon Sep 17 00:00:00 2001 From: Thomas Sileo Date: Sun, 11 Aug 2019 11:41:16 +0200 Subject: [PATCH] Support AP Update actor --- blueprints/tasks.py | 45 +++------------------------------------------ core/activitypub.py | 39 +++++++++++++++++++++++++++++++++++++++ core/inbox.py | 7 ++++++- 3 files changed, 48 insertions(+), 43 deletions(-) diff --git a/blueprints/tasks.py b/blueprints/tasks.py index 08ca54e..6368355 100644 --- a/blueprints/tasks.py +++ b/blueprints/tasks.py @@ -21,7 +21,7 @@ from core.activitypub import _actor_hash from core.activitypub import _add_answers_to_question from core.activitypub import no_cache from core.activitypub import post_to_outbox -from core.db import update_many_activities +from core.activitypub import update_cached_actor from core.db import update_one_activity from core.inbox import process_inbox from core.meta import MetaKey @@ -155,11 +155,8 @@ def task_cache_object() -> _Response: cache[MetaKey.OBJECT_ACTOR_ID] = obj_actor.id cache[MetaKey.OBJECT_ACTOR_HASH] = obj_actor_hash - # Cache the actor icon if any - _cache_actor_icon(obj_actor) - # Update the actor cache for the other activities - _update_cached_actor(obj_actor) + update_cached_actor(obj_actor) update_one_activity(by_remote_id(activity.id), upsert(cache)) @@ -252,39 +249,6 @@ def task_cache_attachments() -> _Response: return "" -def _update_cached_actor(actor: ap.BaseActivity) -> None: - actor_hash = _actor_hash(actor) - update_many_activities( - { - **flag(MetaKey.ACTOR_ID, actor.id), - **flag(MetaKey.ACTOR_HASH, {"$ne": actor_hash}), - }, - upsert( - {MetaKey.ACTOR: actor.to_dict(embed=True), MetaKey.ACTOR_HASH: actor_hash} - ), - ) - update_many_activities( - { - **flag(MetaKey.OBJECT_ACTOR_ID, actor.id), - **flag(MetaKey.OBJECT_ACTOR_HASH, {"$ne": actor_hash}), - }, - upsert( - { - MetaKey.OBJECT_ACTOR: actor.to_dict(embed=True), - MetaKey.OBJECT_ACTOR_HASH: actor_hash, - } - ), - ) - - -def _cache_actor_icon(actor: ap.BaseActivity) -> None: - if actor.icon: - if isinstance(actor.icon, dict) and "url" in actor.icon: - config.MEDIA_CACHE.cache_actor_icon(actor.icon["url"]) - else: - app.logger.warning(f"failed to parse icon {actor.icon} for {actor!r}") - - @blueprint.route("/task/cache_actor", methods=["POST"]) def task_cache_actor() -> _Response: task = p.parse(flask.request) @@ -302,9 +266,6 @@ def task_cache_actor() -> _Response: if activity.has_type(ap.ActivityType.CREATE): Tasks.fetch_og_meta(iri) - # Cache the actor icon if any - _cache_actor_icon(actor) - if activity.has_type(ap.ActivityType.FOLLOW): if actor.id == config.ID: # It's a new following, cache the "object" (which is the actor we follow) @@ -314,7 +275,7 @@ def task_cache_actor() -> _Response: ) # Cache the actor info - _update_cached_actor(actor) + update_cached_actor(actor) # TODO(tsileo): Also update following (it's in the object) # DB.activities.update_many( diff --git a/core/activitypub.py b/core/activitypub.py index 52826c0..784df75 100644 --- a/core/activitypub.py +++ b/core/activitypub.py @@ -29,8 +29,13 @@ from config import DB from config import EXTRA_INBOXES from config import ID from config import ME +from config import MEDIA_CACHE from config import USER_AGENT +from core.db import update_many_activities from core.meta import Box +from core.meta import MetaKey +from core.meta import flag +from core.meta import upsert from core.tasks import Tasks logger = logging.getLogger(__name__) @@ -648,3 +653,37 @@ def activity_from_doc(raw_doc: Dict[str, Any], embed: bool = False) -> Dict[str, if embed: return remove_context(activity) return activity + + +def _cache_actor_icon(actor: ap.BaseActivity) -> None: + if actor.icon: + if isinstance(actor.icon, dict) and "url" in actor.icon: + MEDIA_CACHE.cache_actor_icon(actor.icon["url"]) + else: + logger.warning(f"failed to parse icon {actor.icon} for {actor!r}") + + +def update_cached_actor(actor: ap.BaseActivity) -> None: + _cache_actor_icon(actor) + actor_hash = _actor_hash(actor) + update_many_activities( + { + **flag(MetaKey.ACTOR_ID, actor.id), + **flag(MetaKey.ACTOR_HASH, {"$ne": actor_hash}), + }, + upsert( + {MetaKey.ACTOR: actor.to_dict(embed=True), MetaKey.ACTOR_HASH: actor_hash} + ), + ) + update_many_activities( + { + **flag(MetaKey.OBJECT_ACTOR_ID, actor.id), + **flag(MetaKey.OBJECT_ACTOR_HASH, {"$ne": actor_hash}), + }, + upsert( + { + MetaKey.OBJECT_ACTOR: actor.to_dict(embed=True), + MetaKey.OBJECT_ACTOR_HASH: actor_hash, + } + ), + ) diff --git a/core/inbox.py b/core/inbox.py index 6bb3516..0d1b369 100644 --- a/core/inbox.py +++ b/core/inbox.py @@ -8,7 +8,9 @@ from little_boxes.errors import NotAnActivityError import config from core.activitypub import _answer_key +from core.activitypub import no_cache from core.activitypub import post_to_outbox +from core.activitypub import update_cached_actor from core.db import DB from core.db import update_one_activity from core.meta import MetaKey @@ -90,7 +92,10 @@ def _update_process_inbox(update: ap.Update, new_meta: _NewMeta) -> None: by_object_id(obj.id), upsert({MetaKey.OBJECT: obj.to_dict()}) ) - # FIXME(tsileo): handle update actor amd inbox_update_note/inbox_update_actor + elif obj.has_type(ap.ACTOR_TYPES): + with no_cache(): + actor = ap.fetch_remote_activity(obj.get_actor().id) + update_cached_actor(actor) @process_inbox.register