From 23afd31bff27fb306c708c6aedc4987460bdf55c Mon Sep 17 00:00:00 2001 From: Thomas Sileo Date: Thu, 11 Aug 2022 23:10:24 +0200 Subject: [PATCH] Improve outgoing worker --- app/main.py | 4 +++- app/outgoing_activities.py | 11 ++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index 678c299..37a3adc 100644 --- a/app/main.py +++ b/app/main.py @@ -76,7 +76,9 @@ _RESIZED_CACHE: MutableMapping[tuple[str, int], tuple[bytes, str, Any]] = LFUCac # TODO(ts): # # Next: -# - allow to manually approve follow requests +# - Add a TTL cache for the LD sig (no need to re-compute) +# - only show 10 most recent threads in DMs +# - custom CSS for disabled button (e.g. sharing on a direct post) # - prevent double accept/double follow # - UI support for updating posts # - indieauth tweaks diff --git a/app/outgoing_activities.py b/app/outgoing_activities.py index 31bbb96..c5327d3 100644 --- a/app/outgoing_activities.py +++ b/app/outgoing_activities.py @@ -4,8 +4,10 @@ import time import traceback from datetime import datetime from datetime import timedelta +from typing import MutableMapping import httpx +from cachetools import TTLCache from loguru import logger from sqlalchemy import func from sqlalchemy import select @@ -26,6 +28,9 @@ from app.utils.workers import Worker _MAX_RETRIES = 16 +_LD_SIG_CACHE: MutableMapping[str, ap.RawObject] = TTLCache(maxsize=5, ttl=60 * 5) + + k = Key(config.ID, f"{config.ID}#main-key") k.load(KEY_PATH.read_text()) @@ -237,7 +242,11 @@ async def process_next_outgoing_activity( ]: # But only if the object is public (to help with deniability/privacy) if next_activity.outbox_object.visibility == ap.VisibilityEnum.PUBLIC: # type: ignore # noqa: E501 - ldsig.generate_signature(payload, k) + if p := _LD_SIG_CACHE.get(payload["id"]): + payload = p + else: + ldsig.generate_signature(payload, k) + _LD_SIG_CACHE[payload["id"]] = payload logger.info(f"{payload=}")