Add metadata flag to api/characters/chats
| @@ -1382,8 +1382,9 @@ router.post('/chats', validateAvatarUrlMiddleware, async function (request, resp | ||
| 1382 | 1382 | } |
| 1383 | 1383 | |
| 1384 | 1384 | const jsonFilesPromise = jsonFiles.map((file) => { |
| 1385 | + const withMetadata = !!request.body.metadata; | |
| 1385 | 1386 | const pathToFile = path.join(request.user.directories.chats, characterDirectory, file); |
| 1386 | 1387 | return getChatInfo(pathToFile, {}, false, withMetadata); |
| 1387 | 1388 | }); |
| 1388 | 1389 | |
| 1389 | 1390 | const chatData = (await Promise.allSettled(jsonFilesPromise)).filter(x => x.status === 'fulfilled').map(x => x.value); |
| @@ -359,15 +359,18 @@ async function checkChatIntegrity(filePath, integritySlug) { | ||
| 359 | 359 | * @property {number} [chat_items] - The number of chat items in the file |
| 360 | 360 | * @property {string} [mes] - The last message in the chat |
| 361 | 361 | * @property {number} [last_mes] - The timestamp of the last message |
| 362 | + * @property {object} [chat_metadata] - Additional chat metadata | |
| 362 | 363 | */ |
| 363 | 364 | |
| 364 | 365 | /** |
| 365 | 366 | * Reads the information from a chat file. |
| 366 | 367 | * @param {string} pathToFile - Path to the chat file |
| 367 | 368 | * @param {object} additionalData - Additional data to include in the result |
| 369 | + * @param {boolean} isGroup - Whether the chat is a group chat | |
| 370 | + * @param {boolean} withMetadata - Whether to read chat metadata | |
| 368 | 371 | * @returns {Promise<ChatInfo>} |
| 369 | 372 | */ |
| 370 | 373 | export async function getChatInfo(pathToFile, additionalData = {}, isGroup = false, withMetadata = false) { |
| 371 | 374 | return new Promise(async (res) => { |
| 372 | 375 | const stats = await fs.promises.stat(pathToFile); |
| 373 | 376 | const fileSizeInKB = `${(stats.size / 1024).toFixed(2)}kb`; |
| @@ -401,6 +404,12 @@ export async function getChatInfo(pathToFile, additionalData = {}, isGroup = fal | ||
| 401 | 404 | let lastLine; |
| 402 | 405 | let itemCounter = 0; |
| 403 | 406 | rl.on('line', (line) => { |
| 407 | + if (withMetadata && itemCounter === 0) { | |
| 408 | + const jsonData = tryParse(line); | |
| 409 | + if (jsonData && _.isObject(jsonData.chat_metadata)) { | |
| 410 | + chatData.chat_metadata = jsonData.chat_metadata; | |
| 411 | + } | |
| 412 | + } | |
| 404 | 413 | itemCounter++; |
| 405 | 414 | lastLine = line; |
| 406 | 415 | }); |
| @@ -409,7 +418,7 @@ export async function getChatInfo(pathToFile, additionalData = {}, isGroup = fal | ||
| 409 | 418 | |
| 410 | 419 | if (lastLine) { |
| 411 | 420 | const jsonData = tryParse(lastLine); |
| 412 | 421 | if (jsonData && (jsonData.name || jsonData.character_name || jsonData.chat_metadata)) { |
| 413 | 422 | chatData.chat_items = isGroup ? itemCounter : (itemCounter - 1); |
| 414 | 423 | chatData.mes = jsonData['mes'] || '[The message is empty]'; |
| 415 | 424 | chatData.last_mes = jsonData['send_date'] || stats.mtimeMs; |