Fix handling of recent group chats if empty Fixes #4022

84745034e7bfa9339bad32392c17fc69c0bcbf8c

Cohee <18619528+Cohee1207@users.noreply.github.com>

1 files changed, +18 -9Showing whitespace changes
src/endpoints/chats.js+18 -9
@@ -372,12 +372,26 @@ export async function getChatInfo(pathToFile, additionalData = {}, isGroup = fal
372 const stats = await fs.promises.stat(pathToFile);372 const stats = await fs.promises.stat(pathToFile);
373 const fileSizeInKB = `${(stats.size / 1024).toFixed(2)}kb`;373 const fileSizeInKB = `${(stats.size / 1024).toFixed(2)}kb`;
374374
375 if (stats.size === 0) {375 const chatData = {
376 file_name: path.parse(pathToFile).base,
377 file_size: fileSizeInKB,
378 chat_items: 0,
379 mes: '[The chat is empty]',
380 last_mes: stats.mtimeMs,
381 ...additionalData,
382 };
383
384 if (stats.size === 0 && !isGroup) {
376 console.warn(`Found an empty chat file: ${pathToFile}`);385 console.warn(`Found an empty chat file: ${pathToFile}`);
377 res({});386 res({});
378 return;387 return;
379 }388 }
380389
390 if (stats.size === 0 && isGroup) {
391 res(chatData);
392 return;
393 }
394
381 const rl = readline.createInterface({395 const rl = readline.createInterface({
382 input: fileStream,396 input: fileStream,
383 crlfDelay: Infinity,397 crlfDelay: Infinity,
@@ -395,14 +409,9 @@ export async function getChatInfo(pathToFile, additionalData = {}, isGroup = fal
395 if (lastLine) {409 if (lastLine) {
396 const jsonData = tryParse(lastLine);410 const jsonData = tryParse(lastLine);
397 if (jsonData && (jsonData.name || jsonData.character_name)) {411 if (jsonData && (jsonData.name || jsonData.character_name)) {
398 const chatData = {};412 chatData.chat_items = isGroup ? itemCounter : (itemCounter - 1);
399413 chatData.mes = jsonData['mes'] || '[The chat is empty]';
400 chatData['file_name'] = path.parse(pathToFile).base;414 chatData.last_mes = jsonData['send_date'] || stats.mtimeMs;
401 chatData['file_size'] = fileSizeInKB;
402 chatData['chat_items'] = isGroup ? itemCounter : (itemCounter - 1);
403 chatData['mes'] = jsonData['mes'] || '[The chat is empty]';
404 chatData['last_mes'] = jsonData['send_date'] || stats.mtimeMs;
405 Object.assign(chatData, additionalData);
406415
407 res(chatData);416 res(chatData);
408 } else {417 } else {