From 852ffc00c6a1cc461617b09287770d4bceac8e18 Mon Sep 17 00:00:00 2001 From: Thomas Sileo Date: Fri, 16 Aug 2019 21:16:25 +0200 Subject: [PATCH] Fix reply counter --- blueprints/tasks.py | 8 +++++++- core/activitypub.py | 19 +++++++++++-------- core/meta.py | 1 + 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/blueprints/tasks.py b/blueprints/tasks.py index 0070e09..1457f29 100644 --- a/blueprints/tasks.py +++ b/blueprints/tasks.py @@ -548,6 +548,12 @@ def task_process_reply() -> _Response: if not find_one_activity(by_object_id(root_reply)): return "" + # In case the activity was from the inbox + update_one_activity( + {**by_object_id(activity.id), **by_type(ap.ActivityType.CREATE)}, + upsert({MetaKey.THREAD_ROOT_PARENT: root_reply}), + ) + for new_reply in new_replies: if find_one_activity(by_object_id(new_reply.id)) or DB.replies.find_one( {"remote_id": root_reply} @@ -558,7 +564,7 @@ def task_process_reply() -> _Response: save_reply( new_reply, { - "meta.thread_root_parent": root_reply, + **flag(MetaKey.THREAD_ROOT_PARENT, root_reply), **flag(MetaKey.ACTOR, actor.to_dict(embed=True)), **flag(MetaKey.ACTOR_HASH, _actor_hash(actor)), }, diff --git a/core/activitypub.py b/core/activitypub.py index d68185c..11eeecf 100644 --- a/core/activitypub.py +++ b/core/activitypub.py @@ -511,11 +511,12 @@ class MicroblogPubBackend(Backend): ) if not creply: # Maybe it's the reply of a reply? - if not DB.replies.find_one_and_update( + DB.replies.find_one_and_update( by_remote_id(in_reply_to), inc(MetaKey.COUNT_REPLY, 1) - ): - # We don't have the reply stored, spawn a task to process it (and determine if it needs to be saved) - Tasks.process_reply(create.get_object().id) + ) + + # Spawn a task to process it (and determine if it needs to be saved) + Tasks.process_reply(create.get_object().id) def embed_collection(total_items, first_page_id): @@ -643,20 +644,22 @@ def _add_answers_to_question(raw_doc: Dict[str, Any]) -> None: def add_extra_collection(raw_doc: Dict[str, Any]) -> Dict[str, Any]: - if raw_doc["activity"]["type"] != ap.ActivityType.CREATE.value: + if not ap._has_type(raw_doc["activity"]["type"], ap.ActivityType.CREATE.value): return raw_doc raw_doc["activity"]["object"]["replies"] = embed_collection( - raw_doc.get("meta", {}).get("count_direct_reply", 0), + raw_doc.get("meta", {}).get(MetaKey.COUNT_REPLY.value, 0), f'{raw_doc["remote_id"]}/replies', ) raw_doc["activity"]["object"]["likes"] = embed_collection( - raw_doc.get("meta", {}).get("count_like", 0), f'{raw_doc["remote_id"]}/likes' + raw_doc.get("meta", {}).get(MetaKey.COUNT_LIKE.value, 0), + f'{raw_doc["remote_id"]}/likes', ) raw_doc["activity"]["object"]["shares"] = embed_collection( - raw_doc.get("meta", {}).get("count_boost", 0), f'{raw_doc["remote_id"]}/shares' + raw_doc.get("meta", {}).get(MetaKey.COUNT_BOOST.value, 0), + f'{raw_doc["remote_id"]}/shares', ) return raw_doc diff --git a/core/meta.py b/core/meta.py index 0c99127..6067b8b 100644 --- a/core/meta.py +++ b/core/meta.py @@ -35,6 +35,7 @@ class MetaKey(Enum): OBJECT_ACTOR_ID = "object_actor_id" OBJECT_ACTOR_HASH = "object_actor_hash" PUBLIC = "public" + THREAD_ROOT_PARENT = "thread_root_parent" DELETED = "deleted" BOOSTED = "boosted"