From d2a118ea7e64b004ffcbce80e693dadda43be9f5 Mon Sep 17 00:00:00 2001 From: Jurgis Date: Thu, 26 Feb 2026 09:44:54 +0200 Subject: [PATCH] assign thread if parent is with thread --- worker/assign-thread.ts | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/worker/assign-thread.ts b/worker/assign-thread.ts index ab19432..fb67f17 100644 --- a/worker/assign-thread.ts +++ b/worker/assign-thread.ts @@ -47,4 +47,41 @@ export async function assignThread(repository: Repository) { }) console.log(`Updating reply thread for ${replyWithoutThreadWithRoot.totalItems} articles. Total pages: ${replyWithoutThreadWithRoot.totalPages}`); } + + + // replies with parent with threads + let replyWithoutThreadWithParent = await repository.getArticlesCollection().getList(1, 100, { + filter: 'thread=null&&parent.thread!=null', + expand: 'parent', + }); + + let totalItems = replyWithoutThreadWithParent.totalItems > replyWithoutThreadWithParent.perPage ? replyWithoutThreadWithParent.perPage : replyWithoutThreadWithParent.totalItems; + let passNum = 1 + console.log(`Updating reply thread (parent based) for ${totalItems} articles. Pass #: ${passNum}`); + + while (replyWithoutThreadWithParent.items.length > 0) { + for (const item of replyWithoutThreadWithParent.items) { + // @ts-ignore + const rootId = item?.expand?.parent?.thread; + + if (rootId) { + await repository.updateArticle(item.id, { thread: rootId }); + } + } + + replyWithoutThreadWithParent = await repository.getArticlesCollection().getList(1, 100, { + filter: 'thread=null&&parent.thread!=null', + expand: 'parent', + }) + + totalItems = replyWithoutThreadWithParent.totalItems > replyWithoutThreadWithParent.perPage ? replyWithoutThreadWithParent.perPage : replyWithoutThreadWithParent.totalItems; + passNum += 1 + console.log(`Updating reply thread (parent based) for ${totalItems} articles. Pass #: ${passNum}`); + + // prevent infinite loop + if (passNum > 20) { + console.log('replyWithoutThreadWithParent::break preventing infinite loop'); + break; + } + } }