Add metadata flag to api/characters/chats

6c43ab64b52ed904a246768383a770536558a3d6

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

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