From cbd9d4e6daae246c2dac5b410399d450ddec87a3 Mon Sep 17 00:00:00 2001 From: Thomas Sileo Date: Mon, 30 Jul 2018 18:12:27 +0200 Subject: [PATCH] Show likes in the notifications --- activitypub.py | 10 ---------- app.py | 2 ++ tasks.py | 27 +++++++++++++++++++++++++++ templates/stream.html | 9 +++++++++ 4 files changed, 38 insertions(+), 10 deletions(-) diff --git a/activitypub.py b/activitypub.py index 470dd84..ff75354 100644 --- a/activitypub.py +++ b/activitypub.py @@ -280,16 +280,6 @@ class MicroblogPubBackend(Backend): {"$inc": {"meta.count_like": 1}, "$set": {"meta.liked": like.id}}, ) - DB.activities.update_one( - {"remote_id": like.id}, - { - "$set": { - "meta.object": obj.to_dict(embed=True), - "meta.object_actor": _actor_to_meta(obj.get_actor()), - } - }, - ) - @ensure_it_is_me def outbox_undo_like(self, as_actor: ap.Person, like: ap.Like) -> None: obj = like.get_object() diff --git a/app.py b/app.py index dce07d3..c2f32c3 100644 --- a/app.py +++ b/app.py @@ -1397,6 +1397,7 @@ def admin_notifications(): "type": ActivityType.UNDO.value, "activity.object.type": ActivityType.FOLLOW.value, } + likes_query = {"type": ActivityType.LIKE.value} followed_query = {"type": ActivityType.ACCEPT.value} q = { "box": Box.INBOX.value, @@ -1407,6 +1408,7 @@ def admin_notifications(): new_followers_query, followed_query, unfollow_query, + likes_query, ], } inbox_data, older_than, newer_than = paginated_query(DB.activities, q) diff --git a/tasks.py b/tasks.py index a62a1ae..944b464 100644 --- a/tasks.py +++ b/tasks.py @@ -176,6 +176,30 @@ def fetch_og_metadata(self, iri: str) -> None: self.retry(exc=err, countdown=int(random.uniform(2, 4) ** self.request.retries)) +@app.task(bind=True, max_retries=12) +def cache_object(self, iri: str) -> None: + try: + activity = ap.fetch_remote_activity(iri) + log.info(f"activity={activity!r}") + + obj = activity.get_object() + DB.activities.update_one( + {"remote_id": activity.id}, + { + "$set": { + "meta.object": obj.to_dict(embed=True), + "meta.object_actor": activitypub._actor_to_meta(obj.get_actor()), + } + }, + ) + except (ActivityGoneError, ActivityNotFoundError, NotAnActivityError): + DB.activities.update_one({"remote_id": iri}, {"$set": {"meta.deleted": True}}) + log.exception(f"flagging activity {iri} as deleted, no object caching") + except Exception as err: + log.exception(f"failed to cache object for {iri}") + self.retry(exc=err, countdown=int(random.uniform(2, 4) ** self.request.retries)) + + @app.task(bind=True, max_retries=12) def cache_actor(self, iri: str, also_cache_attachments: bool = True) -> None: try: @@ -185,6 +209,9 @@ def cache_actor(self, iri: str, also_cache_attachments: bool = True) -> None: if activity.has_type(ap.ActivityType.CREATE): fetch_og_metadata.delay(iri) + if activity.has_type([ap.ActivityType.LIKE, ap.ActivityType.ANNOUNCE]): + cache_object.delay(iri) + actor = activity.get_actor() cache_actor_with_inbox = False diff --git a/templates/stream.html b/templates/stream.html index f12c0c7..d9af1c2 100644 --- a/templates/stream.html +++ b/templates/stream.html @@ -19,6 +19,15 @@ {{ utils.display_note(item.meta.object, ui=True) }} {% endif %} {% endif %} + + {% if item | has_type('Like') %} + {% set boost_actor = item.meta.actor %} +

{{ boost_actor.name or boost_actor.preferredUsername }} liked

+ {{ item }} + {% if item.meta.object %} + {{ utils.display_note(item.meta.object, ui=False, meta={'actor': item.meta.object_actor}) }} + {% endif %} + {% endif %} {% if item | has_type('Follow') %}

new follower