Fix reply counter

This commit is contained in:
Thomas Sileo 2019-08-16 21:16:25 +02:00
parent 7bd2aa4592
commit 852ffc00c6
3 changed files with 19 additions and 9 deletions

View file

@ -548,6 +548,12 @@ def task_process_reply() -> _Response:
if not find_one_activity(by_object_id(root_reply)): if not find_one_activity(by_object_id(root_reply)):
return "" 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: for new_reply in new_replies:
if find_one_activity(by_object_id(new_reply.id)) or DB.replies.find_one( if find_one_activity(by_object_id(new_reply.id)) or DB.replies.find_one(
{"remote_id": root_reply} {"remote_id": root_reply}
@ -558,7 +564,7 @@ def task_process_reply() -> _Response:
save_reply( save_reply(
new_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, actor.to_dict(embed=True)),
**flag(MetaKey.ACTOR_HASH, _actor_hash(actor)), **flag(MetaKey.ACTOR_HASH, _actor_hash(actor)),
}, },

View file

@ -511,10 +511,11 @@ class MicroblogPubBackend(Backend):
) )
if not creply: if not creply:
# Maybe it's the reply of a reply? # 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) 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)
# Spawn a task to process it (and determine if it needs to be saved)
Tasks.process_reply(create.get_object().id) Tasks.process_reply(create.get_object().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]: 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 return raw_doc
raw_doc["activity"]["object"]["replies"] = embed_collection( 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', f'{raw_doc["remote_id"]}/replies',
) )
raw_doc["activity"]["object"]["likes"] = embed_collection( 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["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 return raw_doc

View file

@ -35,6 +35,7 @@ class MetaKey(Enum):
OBJECT_ACTOR_ID = "object_actor_id" OBJECT_ACTOR_ID = "object_actor_id"
OBJECT_ACTOR_HASH = "object_actor_hash" OBJECT_ACTOR_HASH = "object_actor_hash"
PUBLIC = "public" PUBLIC = "public"
THREAD_ROOT_PARENT = "thread_root_parent"
DELETED = "deleted" DELETED = "deleted"
BOOSTED = "boosted" BOOSTED = "boosted"