Optimize getGroupPastChats (#4976) * Optimize getGroupPastChats * Add return type to getGroupPastChats

68d4da1c83d74a53f77a28ca0e0aae5c26aa9d7b

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

Signed
2 files changed, +29 -22Showing whitespace changes
public/scripts/group-chats.js+9 -19
@@ -18,7 +18,6 @@ import {
18 paginationDropdownChangeHandler,18 paginationDropdownChangeHandler,
19 waitUntilCondition,19 waitUntilCondition,
20 uuidv4,20 uuidv4,
21 humanFileSize,
22} from './utils.js';21} from './utils.js';
23import { RA_CountCharTokens, humanizedDateTime, dragElement, favsToHotswap, getMessageTimeStamp } from './RossAscends-mods.js';22import { RA_CountCharTokens, humanizedDateTime, dragElement, favsToHotswap, getMessageTimeStamp } from './RossAscends-mods.js';
24import { power_user, loadMovingUIState, sortEntitiesList } from './power-user.js';23import { power_user, loadMovingUIState, sortEntitiesList } from './power-user.js';
@@ -2124,7 +2123,7 @@ export async function createNewGroupChat(groupId) {
2124/**2123/**
2125 * Retrieves past chats for a specified group.2124 * Retrieves past chats for a specified group.
2126 * @param {string} groupId Group ID2125 * @param {string} groupId Group ID
2127 * @returns {Promise<Array>} Array of past chats2126 * @returns {Promise<Array<import('../../src/endpoints/chats.js').ChatInfo>>} Array of past chats
2128 */2127 */
2129export async function getGroupPastChats(groupId) {2128export async function getGroupPastChats(groupId) {
2130 const group = groups.find(x => x.id === groupId);2129 const group = groups.find(x => x.id === groupId);
@@ -2137,24 +2136,15 @@ export async function getGroupPastChats(groupId) {
21372136
2138 try {2137 try {
2139 for (const chatId of group.chats) {2138 for (const chatId of group.chats) {
2140 const messages = await loadGroupChat(chatId);2139 const response = await fetch('/api/chats/group/info', {
2141 if (!Array.isArray(messages)) {2140 method: 'POST',
2142 continue;2141 headers: getRequestHeaders(),
2143 }2142 body: JSON.stringify({ id: chatId }),
2144 const fileSize = humanFileSize(JSON.stringify(messages).length);
2145 if (messages.length > 0 && Object.hasOwn(messages[0], 'chat_metadata')) {
2146 messages.shift();
2147 }
2148 const chatItems = messages.length;
2149 const lastMessage = messages.length ? messages[messages.length - 1].mes : '[The chat is empty]';
2150 const lastMessageDate = messages.length ? (messages[messages.length - 1].send_date || Date.now()) : Date.now();
2151 chats.push({
2152 'file_name': chatId,
2153 'mes': lastMessage,
2154 'last_mes': lastMessageDate,
2155 'file_size': fileSize,
2156 'chat_items': chatItems,
2157 });2143 });
2144 if (response.ok) {
2145 const data = await response.json();
2146 chats.push(data);
2147 }
2158 }2148 }
2159 } catch (err) {2149 } catch (err) {
2160 console.error(err);2150 console.error(err);
src/endpoints/chats.js+20 -3
@@ -760,11 +760,28 @@ router.post('/group/get', (request, response) => {
760 }760 }
761761
762 const id = request.body.id;762 const id = request.body.id;
763 const chatFilePath = path.join(request.user.directories.groupChats, `${id}.jsonl`);763 const chatFilePath = path.join(request.user.directories.groupChats, sanitize(`${id}.jsonl`));
764764
765 return response.send(getChatData(chatFilePath));765 return response.send(getChatData(chatFilePath));
766});766});
767767
768router.post('/group/info', async (request, response) => {
769 try {
770 if (!request.body || !request.body.id) {
771 return response.sendStatus(400);
772 }
773
774 const id = request.body.id;
775 const chatFilePath = path.join(request.user.directories.groupChats, sanitize(`${id}.jsonl`));
776
777 const chatInfo = await getChatInfo(chatFilePath);
778 return response.send(chatInfo);
779 } catch (error) {
780 console.error(error);
781 return response.sendStatus(500);
782 }
783});
784
768router.post('/group/delete', (request, response) => {785router.post('/group/delete', (request, response) => {
769 try {786 try {
770 if (!request.body || !request.body.id) {787 if (!request.body || !request.body.id) {
@@ -772,13 +789,13 @@ router.post('/group/delete', (request, response) => {
772 }789 }
773790
774 const id = request.body.id;791 const id = request.body.id;
775 const chatFilePath = path.join(request.user.directories.groupChats, `${id}.jsonl`);792 const chatFilePath = path.join(request.user.directories.groupChats, sanitize(`${id}.jsonl`));
776793
777 //Return success if the file was deleted.794 //Return success if the file was deleted.
778 if (tryDeleteFile(chatFilePath)) {795 if (tryDeleteFile(chatFilePath)) {
779 return response.send({ ok: true });796 return response.send({ ok: true });
780 } else {797 } else {
781 console.error('The group chat file was not deleted.\'');798 console.error('The group chat file was not deleted.');
782 return response.sendStatus(400);799 return response.sendStatus(400);
783 }800 }
784 } catch (error) {801 } catch (error) {