Add metadata flag to api/characters/chats

6c43ab64b52ed904a246768383a770536558a3d6

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

2 files changed, +15 -5Showing whitespace changes
src/endpoints/characters.js+2 -1
@@ -1382,8 +1382,9 @@ router.post('/chats', validateAvatarUrlMiddleware, async function (request, resp
13821382 }
13831383
13841384 const jsonFilesPromise = jsonFiles.map((file) => {
1385+ const withMetadata = !!request.body.metadata;
13851386 const pathToFile = path.join(request.user.directories.chats, characterDirectory, file);
13861387 return getChatInfo(pathToFile, {}, false, withMetadata);
13871388 });
13881389
13891390 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) {
359359 * @property {number} [chat_items] - The number of chat items in the file
360360 * @property {string} [mes] - The last message in the chat
361361 * @property {number} [last_mes] - The timestamp of the last message
362+ * @property {object} [chat_metadata] - Additional chat metadata
362363 */
363364
364365/**
365366 * Reads the information from a chat file.
366367 * @param {string} pathToFile - Path to the chat file
367368 * @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
368371 * @returns {Promise<ChatInfo>}
369372 */
370373export async function getChatInfo(pathToFile, additionalData = {}, isGroup = false, withMetadata = false) {
371374 return new Promise(async (res) => {
372375 const stats = await fs.promises.stat(pathToFile);
373376 const fileSizeInKB = `${(stats.size / 1024).toFixed(2)}kb`;
@@ -401,6 +404,12 @@ export async function getChatInfo(pathToFile, additionalData = {}, isGroup = fal
401404 let lastLine;
402405 let itemCounter = 0;
403406 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+ }
404413 itemCounter++;
405414 lastLine = line;
406415 });
@@ -409,7 +418,7 @@ export async function getChatInfo(pathToFile, additionalData = {}, isGroup = fal
409418
410419 if (lastLine) {
411420 const jsonData = tryParse(lastLine);
412421 if (jsonData && (jsonData.name || jsonData.character_name || jsonData.chat_metadata)) {
413422 chatData.chat_items = isGroup ? itemCounter : (itemCounter - 1);
414423 chatData.mes = jsonData['mes'] || '[The message is empty]';
415424 chatData.last_mes = jsonData['send_date'] || stats.mtimeMs;