From 5183fb40a245d3e4ef8362d304acf9c05fd03f7b Mon Sep 17 00:00:00 2001 From: Juha Jeronen Date: Wed, 7 Feb 2024 23:09:51 +0200 Subject: [PATCH] refactor to improve proposed implementation of /getchatname --- public/script.js | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/public/script.js b/public/script.js index d732f3c25..297e9ac0a 100644 --- a/public/script.js +++ b/public/script.js @@ -6089,6 +6089,14 @@ export async function getPastCharacterChats(characterId = null) { return data; } +function getCurrentChatDetails() { // helper for `displayPastChats`, to make the same info consistently available for other functions + const group = selected_group ? groups.find(x => x.id === selected_group) : null; + const currentChat = selected_group ? group?.chat_id : characters[this_chid]['chat']; + const displayName = selected_group ? group?.name : characters[this_chid].name; + const avatarImg = selected_group ? group?.avatar_url : getThumbnailUrl('avatar', characters[this_chid]['avatar']); + return { sessionName: currentChat, group: group, characterName: displayName, avatarImgURL: avatarImg }; +} + /** * Displays the past chats for a character or a group based on the selected context. * The function first fetches the chats, processes them, and then displays them in @@ -6099,7 +6107,6 @@ export async function displayPastChats() { $('#select_chat_div').empty(); $('#select_chat_search').val('').off('input'); - const group = selected_group ? groups.find(x => x.id === selected_group) : null; const data = await (selected_group ? getGroupPastChats(selected_group) : getPastCharacterChats()); if (!data) { @@ -6107,10 +6114,14 @@ export async function displayPastChats() { return; } - const currentChat = selected_group ? group?.chat_id : characters[this_chid]['chat']; - const displayName = selected_group ? group?.name : characters[this_chid].name; - const avatarImg = selected_group ? group?.avatar_url : getThumbnailUrl('avatar', characters[this_chid]['avatar']); + const chatDetails = getCurrentChatDetails(); + const group = chatDetails.group; + const currentChat = chatDetails.sessionName; + const displayName = chatDetails.characterName; + const avatarImg = chatDetails.avatarImgURL; + const rawChats = await getChatsFromFiles(data, selected_group); + // Sort by last message date descending data.sort((a, b) => sortMoments(timestampToMoment(a.last_mes), timestampToMoment(b.last_mes))); console.log(data); @@ -7825,9 +7836,7 @@ async function doDeleteChat() { } async function doGetChatName() { // `/getchatname` slash command - const group = selected_group ? groups.find(x => x.id === selected_group) : null; - const currentChat = selected_group ? group?.chat_id : characters[this_chid]['chat']; - return currentChat; + return getCurrentChatDetails().sessionName; } const isPwaMode = window.navigator.standalone;