From d44c8a58aab8d79ecd159c8997b0b9e74b015214 Mon Sep 17 00:00:00 2001 From: Thomas Sileo Date: Mon, 19 Sep 2022 20:46:05 +0200 Subject: [PATCH] More improvements for the replies counter --- app/boxes.py | 43 ++++++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/app/boxes.py b/app/boxes.py index 34f36ee..9af4631 100644 --- a/app/boxes.py +++ b/app/boxes.py @@ -461,6 +461,7 @@ async def send_create( content, tags, mentioned_actors = await markdownify(db_session, source) attachments = [] + in_reply_to_object: AnyboxObject | None = None if in_reply_to: in_reply_to_object = await get_anybox_object_by_ap_id(db_session, in_reply_to) if not in_reply_to_object: @@ -478,23 +479,6 @@ async def send_create( context = in_reply_to_object.ap_context conversation = in_reply_to_object.ap_context - if in_reply_to_object.is_from_outbox: - await db_session.execute( - update(models.OutboxObject) - .where( - models.OutboxObject.ap_id == in_reply_to, - ) - .values(replies_count=models.OutboxObject.replies_count + 1) - ) - elif in_reply_to_object.is_from_inbox: - await db_session.execute( - update(models.InboxObject) - .where( - models.InboxObject.ap_id == in_reply_to, - ) - .values(replies_count=models.InboxObject.replies_count + 1) - ) - for (upload, filename, alt_text) in uploads: attachments.append(upload_to_attachment(upload, filename, alt_text)) @@ -613,6 +597,31 @@ async def send_create( ) await db_session.commit() + + # Refresh the replies counter if needed + if in_reply_to_object: + new_replies_count = await _get_replies_count( + db_session, in_reply_to_object.ap_id + ) + if in_reply_to_object.is_from_outbox: + await db_session.execute( + update(models.OutboxObject) + .where( + models.OutboxObject.ap_id == in_reply_to_object.ap_id, + ) + .values(replies_count=new_replies_count) + ) + elif in_reply_to_object.is_from_inbox: + await db_session.execute( + update(models.InboxObject) + .where( + models.InboxObject.ap_id == in_reply_to_object.ap_id, + ) + .values(replies_count=new_replies_count) + ) + + await db_session.commit() + return note_id