Cache actor icon in its own task

This commit is contained in:
Thomas Sileo 2019-08-11 12:07:30 +02:00
parent ae8ed147c7
commit 6a459d0f87
4 changed files with 24 additions and 4 deletions

View file

@ -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)

View file

@ -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)

View file

@ -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

View file

@ -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")