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 {
1818 paginationDropdownChangeHandler,
1919 waitUntilCondition,
2020 uuidv4,
21- humanFileSize,
2221} from './utils.js';
2322import { RA_CountCharTokens, humanizedDateTime, dragElement, favsToHotswap, getMessageTimeStamp } from './RossAscends-mods.js';
2423import { power_user, loadMovingUIState, sortEntitiesList } from './power-user.js';
@@ -2124,7 +2123,7 @@ export async function createNewGroupChat(groupId) {
21242123/**
21252124 * Retrieves past chats for a specified group.
21262125 * @param {string} groupId Group ID
21272126 * @returns {Promise<Array<import('../../src/endpoints/chats.js').ChatInfo>>} Array of past chats
21282127 */
21292128export async function getGroupPastChats(groupId) {
21302129 const group = groups.find(x => x.id === groupId);
@@ -2137,24 +2136,15 @@ export async function getGroupPastChats(groupId) {
21372136
21382137 try {
21392138 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,
21572143 });
2144+ if (response.ok) {
2145+ const data = await response.json();
2146+ chats.push(data);
2147+ }
21582148 }
21592149 } catch (err) {
21602150 console.error(err);
src/endpoints/chats.js+20 -3
@@ -760,11 +760,28 @@ router.post('/group/get', (request, response) => {
760760 }
761761
762762 const id = request.body.id;
763763 const chatFilePath = path.join(request.user.directories.groupChats, sanitize(`${id}.jsonl`));
764764
765765 return response.send(getChatData(chatFilePath));
766766});
767767
768+router.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+
768785router.post('/group/delete', (request, response) => {
769786 try {
770787 if (!request.body || !request.body.id) {
@@ -772,13 +789,13 @@ router.post('/group/delete', (request, response) => {
772789 }
773790
774791 const id = request.body.id;
775792 const chatFilePath = path.join(request.user.directories.groupChats, sanitize(`${id}.jsonl`));
776793
777794 //Return success if the file was deleted.
778795 if (tryDeleteFile(chatFilePath)) {
779796 return response.send({ ok: true });
780797 } else {
781798 console.error('The group chat file was not deleted.\'');
782799 return response.sendStatus(400);
783800 }
784801 } catch (error) {