Optimize getGroupPastChats (#4976) * Optimize getGroupPastChats * Add return type to getGroupPastChats
Signed| @@ -18,7 +18,6 @@ import { | ||
| 18 | 18 | paginationDropdownChangeHandler, |
| 19 | 19 | waitUntilCondition, |
| 20 | 20 | uuidv4, |
| 21 | - humanFileSize, | |
| 22 | 21 | } from './utils.js'; |
| 23 | 22 | import { RA_CountCharTokens, humanizedDateTime, dragElement, favsToHotswap, getMessageTimeStamp } from './RossAscends-mods.js'; |
| 24 | 23 | import { power_user, loadMovingUIState, sortEntitiesList } from './power-user.js'; |
| @@ -2124,7 +2123,7 @@ export async function createNewGroupChat(groupId) { | ||
| 2124 | 2123 | /** |
| 2125 | 2124 | * Retrieves past chats for a specified group. |
| 2126 | 2125 | * @param {string} groupId Group ID |
| 2127 | 2126 | * @returns {Promise<Array<import('../../src/endpoints/chats.js').ChatInfo>>} Array of past chats |
| 2128 | 2127 | */ |
| 2129 | 2128 | export async function getGroupPastChats(groupId) { |
| 2130 | 2129 | const group = groups.find(x => x.id === groupId); |
| @@ -2137,24 +2136,15 @@ export async function getGroupPastChats(groupId) { | ||
| 2137 | 2136 | |
| 2138 | 2137 | try { |
| 2139 | 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 | 2149 | } catch (err) { |
| 2160 | 2150 | console.error(err); |
| @@ -760,11 +760,28 @@ router.post('/group/get', (request, response) => { | ||
| 760 | 760 | } |
| 761 | 761 | |
| 762 | 762 | const id = request.body.id; |
| 763 | 763 | const chatFilePath = path.join(request.user.directories.groupChats, sanitize(`${id}.jsonl`)); |
| 764 | 764 | |
| 765 | 765 | return response.send(getChatData(chatFilePath)); |
| 766 | 766 | }); |
| 767 | 767 | |
| 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 | + | |
| 768 | 785 | router.post('/group/delete', (request, response) => { |
| 769 | 786 | try { |
| 770 | 787 | if (!request.body || !request.body.id) { |
| @@ -772,13 +789,13 @@ router.post('/group/delete', (request, response) => { | ||
| 772 | 789 | } |
| 773 | 790 | |
| 774 | 791 | const id = request.body.id; |
| 775 | 792 | const chatFilePath = path.join(request.user.directories.groupChats, sanitize(`${id}.jsonl`)); |
| 776 | 793 | |
| 777 | 794 | //Return success if the file was deleted. |
| 778 | 795 | if (tryDeleteFile(chatFilePath)) { |
| 779 | 796 | return response.send({ ok: true }); |
| 780 | 797 | } else { |
| 781 | 798 | console.error('The group chat file was not deleted.\''); |
| 782 | 799 | return response.sendStatus(400); |
| 783 | 800 | } |
| 784 | 801 | } catch (error) { |