From 84745034e7bfa9339bad32392c17fc69c0bcbf8c Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Wed, 21 May 2025 22:50:15 +0300 Subject: [PATCH] Fix handling of recent group chats if empty Fixes #4022 --- src/endpoints/chats.js | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/endpoints/chats.js b/src/endpoints/chats.js index 63e3ebbcd..6fe6ab13f 100644 --- a/src/endpoints/chats.js +++ b/src/endpoints/chats.js @@ -372,12 +372,26 @@ export async function getChatInfo(pathToFile, additionalData = {}, isGroup = fal const stats = await fs.promises.stat(pathToFile); const fileSizeInKB = `${(stats.size / 1024).toFixed(2)}kb`; - if (stats.size === 0) { + const chatData = { + file_name: path.parse(pathToFile).base, + file_size: fileSizeInKB, + chat_items: 0, + mes: '[The chat is empty]', + last_mes: stats.mtimeMs, + ...additionalData, + }; + + if (stats.size === 0 && !isGroup) { console.warn(`Found an empty chat file: ${pathToFile}`); res({}); return; } + if (stats.size === 0 && isGroup) { + res(chatData); + return; + } + const rl = readline.createInterface({ input: fileStream, crlfDelay: Infinity, @@ -395,14 +409,9 @@ export async function getChatInfo(pathToFile, additionalData = {}, isGroup = fal if (lastLine) { const jsonData = tryParse(lastLine); if (jsonData && (jsonData.name || jsonData.character_name)) { - const chatData = {}; - - chatData['file_name'] = path.parse(pathToFile).base; - chatData['file_size'] = fileSizeInKB; - chatData['chat_items'] = isGroup ? itemCounter : (itemCounter - 1); - chatData['mes'] = jsonData['mes'] || '[The chat is empty]'; - chatData['last_mes'] = jsonData['send_date'] || stats.mtimeMs; - Object.assign(chatData, additionalData); + chatData.chat_items = isGroup ? itemCounter : (itemCounter - 1); + chatData.mes = jsonData['mes'] || '[The chat is empty]'; + chatData.last_mes = jsonData['send_date'] || stats.mtimeMs; res(chatData); } else {