Optimize getGroupPastChats (#4976)

* Optimize getGroupPastChats

* Add return type to getGroupPastChats
This commit is contained in:
Cohee
2026-01-09 02:17:18 +02:00
committed by GitHub
parent 8372e7bf9d
commit 68d4da1c83
2 changed files with 29 additions and 22 deletions
+9 -19
View File
@@ -18,7 +18,6 @@ import {
paginationDropdownChangeHandler,
waitUntilCondition,
uuidv4,
humanFileSize,
} from './utils.js';
import { RA_CountCharTokens, humanizedDateTime, dragElement, favsToHotswap, getMessageTimeStamp } from './RossAscends-mods.js';
import { power_user, loadMovingUIState, sortEntitiesList } from './power-user.js';
@@ -2124,7 +2123,7 @@ export async function createNewGroupChat(groupId) {
/**
* Retrieves past chats for a specified group.
* @param {string} groupId Group ID
* @returns {Promise<Array>} Array of past chats
* @returns {Promise<Array<import('../../src/endpoints/chats.js').ChatInfo>>} Array of past chats
*/
export async function getGroupPastChats(groupId) {
const group = groups.find(x => x.id === groupId);
@@ -2137,24 +2136,15 @@ export async function getGroupPastChats(groupId) {
try {
for (const chatId of group.chats) {
const messages = await loadGroupChat(chatId);
if (!Array.isArray(messages)) {
continue;
}
const fileSize = humanFileSize(JSON.stringify(messages).length);
if (messages.length > 0 && Object.hasOwn(messages[0], 'chat_metadata')) {
messages.shift();
}
const chatItems = messages.length;
const lastMessage = messages.length ? messages[messages.length - 1].mes : '[The chat is empty]';
const lastMessageDate = messages.length ? (messages[messages.length - 1].send_date || Date.now()) : Date.now();
chats.push({
'file_name': chatId,
'mes': lastMessage,
'last_mes': lastMessageDate,
'file_size': fileSize,
'chat_items': chatItems,
const response = await fetch('/api/chats/group/info', {
method: 'POST',
headers: getRequestHeaders(),
body: JSON.stringify({ id: chatId }),
});
if (response.ok) {
const data = await response.json();
chats.push(data);
}
}
} catch (err) {
console.error(err);
+20 -3
View File
@@ -760,11 +760,28 @@ router.post('/group/get', (request, response) => {
}
const id = request.body.id;
const chatFilePath = path.join(request.user.directories.groupChats, `${id}.jsonl`);
const chatFilePath = path.join(request.user.directories.groupChats, sanitize(`${id}.jsonl`));
return response.send(getChatData(chatFilePath));
});
router.post('/group/info', async (request, response) => {
try {
if (!request.body || !request.body.id) {
return response.sendStatus(400);
}
const id = request.body.id;
const chatFilePath = path.join(request.user.directories.groupChats, sanitize(`${id}.jsonl`));
const chatInfo = await getChatInfo(chatFilePath);
return response.send(chatInfo);
} catch (error) {
console.error(error);
return response.sendStatus(500);
}
});
router.post('/group/delete', (request, response) => {
try {
if (!request.body || !request.body.id) {
@@ -772,13 +789,13 @@ router.post('/group/delete', (request, response) => {
}
const id = request.body.id;
const chatFilePath = path.join(request.user.directories.groupChats, `${id}.jsonl`);
const chatFilePath = path.join(request.user.directories.groupChats, sanitize(`${id}.jsonl`));
//Return success if the file was deleted.
if (tryDeleteFile(chatFilePath)) {
return response.send({ ok: true });
} else {
console.error('The group chat file was not deleted.\'');
console.error('The group chat file was not deleted.');
return response.sendStatus(400);
}
} catch (error) {