From 6a459d0f87f4f0e2231d3cf8f6bfbb7fa7fea547 Mon Sep 17 00:00:00 2001 From: Thomas Sileo Date: Sun, 11 Aug 2019 12:07:30 +0200 Subject: [PATCH] Cache actor icon in its own task --- blueprints/tasks.py | 17 +++++++++++++++++ core/activitypub.py | 5 ++--- core/inbox.py | 2 +- core/tasks.py | 4 ++++ 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/blueprints/tasks.py b/blueprints/tasks.py index 6368355..116b3df 100644 --- a/blueprints/tasks.py +++ b/blueprints/tasks.py @@ -15,6 +15,7 @@ from requests.exceptions import HTTPError import config from config import DB +from config import MEDIA_CACHE from core import gc from core.activitypub import Box from core.activitypub import _actor_hash @@ -296,6 +297,22 @@ def task_cache_actor() -> _Response: return "" +@blueprint.route("/task/cache_actor_icon", methods=["POST"]) +def task_cache_actor_icon() -> _Response: + task = p.parse(flask.request) + app.logger.info(f"task={task!r}") + actor_iri = task.payload["actor_iri"] + icon_url = task.payload["icon_url"] + try: + MEDIA_CACHE.cache_actor_icon(icon_url) + except Exception as exc: + err = f"failed to cache actor icon {icon_url} for {actor_iri}" + app.logger.exception(err) + raise TaskError() from exc + + return "" + + @blueprint.route("/task/forward_activity", methods=["POST"]) def task_forward_activity() -> _Response: task = p.parse(flask.request) diff --git a/core/activitypub.py b/core/activitypub.py index 784df75..43f2f5e 100644 --- a/core/activitypub.py +++ b/core/activitypub.py @@ -29,7 +29,6 @@ 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 @@ -658,13 +657,12 @@ def activity_from_doc(raw_doc: Dict[str, Any], embed: bool = False) -> Dict[str, 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"]) + Tasks.cache_actor_icon(actor.icon["url"], actor.id) 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( { @@ -687,3 +685,4 @@ def update_cached_actor(actor: ap.BaseActivity) -> None: } ), ) + _cache_actor_icon(actor) diff --git a/core/inbox.py b/core/inbox.py index 0d1b369..fc4b1f1 100644 --- a/core/inbox.py +++ b/core/inbox.py @@ -95,7 +95,7 @@ def _update_process_inbox(update: ap.Update, new_meta: _NewMeta) -> None: elif obj.has_type(ap.ACTOR_TYPES): with no_cache(): actor = ap.fetch_remote_activity(obj.get_actor().id) - update_cached_actor(actor) + update_cached_actor(actor) @process_inbox.register diff --git a/core/tasks.py b/core/tasks.py index 3f90f10..13a9d04 100644 --- a/core/tasks.py +++ b/core/tasks.py @@ -24,6 +24,10 @@ class Tasks: "/task/cache_actor", ) + @staticmethod + def cache_actor_icon(icon_url: str, actor_iri: str): + p.push({"icon_url": icon_url, "actor_iri": actor_iri}, "/task/cache_actor_icon") + @staticmethod def post_to_remote_inbox(payload: str, recp: str) -> None: p.push({"payload": payload, "to": recp}, "/task/post_to_remote_inbox")