diff --git a/activitypub.py b/activitypub.py index 8aaf85c..1ea69a7 100644 --- a/activitypub.py +++ b/activitypub.py @@ -287,6 +287,9 @@ class MicroblogPubBackend(Backend): @ensure_it_is_me def outbox_like(self, as_actor: ap.Person, like: ap.Like) -> None: obj = like.get_object() + if obj.has_type(ap.ActivityType.QUESTION): + Tasks.fetch_remote_question(obj) + DB.activities.update_one( {"activity.object.id": obj.id}, {"$inc": {"meta.count_like": 1}, "$set": {"meta.liked": like.id}}, @@ -313,6 +316,9 @@ class MicroblogPubBackend(Backend): ) return + if obj.has_type(ap.ActivityType.QUESTION): + Tasks.fetch_remote_question(obj) + DB.activities.update_one( {"remote_id": announce.id}, { @@ -340,6 +346,9 @@ class MicroblogPubBackend(Backend): @ensure_it_is_me def outbox_announce(self, as_actor: ap.Person, announce: ap.Announce) -> None: obj = announce.get_object() + if obj.has_type(ap.ActivityType.QUESTION): + Tasks.fetch_remote_question(obj) + DB.activities.update_one( {"remote_id": announce.id}, { @@ -478,12 +487,7 @@ class MicroblogPubBackend(Backend): # local copy) question = create.get_object() if question.has_type(ap.ActivityType.QUESTION): - now = datetime.now(timezone.utc) - dt = parser.parse(question.closed or question.endTime).astimezone( - timezone.utc - ) - minutes = int((dt - now).total_seconds() / 60) - Tasks.fetch_remote_question(create.id, minutes) + Tasks.fetch_remote_question(question) self._handle_replies(as_actor, create) diff --git a/app.py b/app.py index 3927f98..0bcb2b9 100644 --- a/app.py +++ b/app.py @@ -3018,12 +3018,20 @@ def task_fetch_remote_question(): try: app.logger.info(f"Fetching remote question {iri}") local_question = DB.activities.find_one( - {"box": Box.INBOX.value, "remote_id": iri} + { + "box": Box.INBOX.value, + "type": ActivityType.CREATE.value, + "activity.object.id": iri, + } ) remote_question = get_backend().fetch_iri(iri, no_cache=True) if ( - local_question["meta"].get("voted_for") - or local_question["meta"]["subscribed"] + local_question + and ( + local_question["meta"].get("voted_for") + or local_question["meta"]["subscribed"] + ) + and not DB.notifications.find_one({"activity.id": remote_question["id"]}) ): DB.notifications.insert_one( { @@ -3033,9 +3041,17 @@ def task_fetch_remote_question(): } ) - DB.activities.update_one( - {"remote_id": iri, "box": Box.INBOX.value}, - {"$set": {"activity": remote_question}}, + # Update the Create if we received it in the inbox + if local_question: + DB.activities.update_one( + {"remote_id": local_question["remote_id"], "box": Box.INBOX.value}, + {"$set": {"activity.object": remote_question}}, + ) + + # Also update all the cached copies (Like, Announce...) + DB.activities.update_many( + {"meta.object.id": remote_question["id"]}, + {"$set": {"activity.object": remote_question}}, ) except HTTPError as err: diff --git a/tasks.py b/tasks.py index e3169f1..34e4bb2 100644 --- a/tasks.py +++ b/tasks.py @@ -1,4 +1,8 @@ import os +from datetime import datetime +from datetime import timezone + +from dateutil import parser from poussetaches import PousseTaches @@ -55,7 +59,11 @@ class Tasks: ) # XXX: delay expects minutes @staticmethod - def fetch_remote_question(iri: str, delay: int) -> None: + def fetch_remote_question(question) -> None: + now = datetime.now(timezone.utc) + dt = parser.parse(question.closed or question.endTime).astimezone(timezone.utc) + minutes = int((dt - now).total_seconds() / 60) + p.push( - iri, "/task/fetch_remote_question", delay=delay + question.id, "/task/fetch_remote_question", delay=minutes ) # XXX: delay expects minutes diff --git a/templates/stream.html b/templates/stream.html index d6d06e8..bc8d91f 100644 --- a/templates/stream.html +++ b/templates/stream.html @@ -58,7 +58,7 @@ {% if item | has_type('question_ended') %}

poll ended

- {{ utils.display_note(item.activity.object) }} + {{ utils.display_note(item.activity) }} {% endif %} {% endif %}