Fix not jumping twice to group chat when creating a new group chat with deleting the old one (#4602) * fix: do not jumo to chat twice on new group chat with delete * fix: prevent metadata reset when deleting non-active group chat * Bring back group chat splice call * Always delete past group metadata on chat delete --------- Co-authored-by: Cohee <18619528+Cohee1207@users.noreply.github.com>
Signed| @@ -9011,7 +9011,7 @@ export async function doNewChat({ deleteCurrentChat = false } = {}) { | |||
| 9011 | 9011 | ||
| 9012 | if (selected_group) { | 9012 | if (selected_group) { |
| 9013 | await createNewGroupChat(selected_group); | 9013 | await createNewGroupChat(selected_group); |
| 9014 | if (deleteCurrentChat) await deleteGroupChat(selected_group, chat_file_for_del); | 9014 | if (deleteCurrentChat) await deleteGroupChat(selected_group, chat_file_for_del, { jumpToNewChat: false }); // don't jump, new chat was already created and jumped to above |
| 9015 | } | 9015 | } |
| 9016 | else { | 9016 | else { |
| 9017 | //RossAscends: added character name to new chat filenames and replaced Date.now() with humanizedDateTime; | 9017 | //RossAscends: added character name to new chat filenames and replaced Date.now() with humanizedDateTime; |
| @@ -2018,7 +2018,14 @@ export async function deleteGroupChatByName(groupId, chatName) { | |||
| 2018 | await eventSource.emit(event_types.GROUP_CHAT_DELETED, chatName); | 2018 | await eventSource.emit(event_types.GROUP_CHAT_DELETED, chatName); |
| 2019 | } | 2019 | } |
| 2020 | 2020 | ||
| 2021 | export async function deleteGroupChat(groupId, chatId) { | 2021 | /** |
| 2022 | * Deletes a group chat by name. | ||
| 2023 | * @param {string} groupId The ID of the group containing the chat to delete. | ||
| 2024 | * @param {string} chatId The id/name of the chat to delete. | ||
| 2025 | * @param {object} [options={}] Options for the deletion. | ||
| 2026 | * @param {boolean} [options.jumpToNewChat=true] Whether to jump to a new chat after deletion (existing one, or create a new one if none exists) | ||
| 2027 | */ | ||
| 2028 | export async function deleteGroupChat(groupId, chatId, { jumpToNewChat = true } = {}) { | ||
| 2022 | const group = groups.find(x => x.id === groupId); | 2029 | const group = groups.find(x => x.id === groupId); |
| 2023 | 2030 | ||
| 2024 | if (!group || !group.chats.includes(chatId)) { | 2031 | if (!group || !group.chats.includes(chatId)) { |
| @@ -2026,10 +2033,13 @@ export async function deleteGroupChat(groupId, chatId) { | |||
| 2026 | } | 2033 | } |
| 2027 | 2034 | ||
| 2028 | group.chats.splice(group.chats.indexOf(chatId), 1); | 2035 | group.chats.splice(group.chats.indexOf(chatId), 1); |
| 2029 | group.chat_metadata = {}; | ||
| 2030 | group.chat_id = ''; | ||
| 2031 | delete group.past_metadata[chatId]; | 2036 | delete group.past_metadata[chatId]; |
| 2032 | updateChatMetadata(group.chat_metadata, true); | 2037 | |
| 2038 | if (group.chat_id === chatId) { | ||
| 2039 | group.chat_id = ''; | ||
| 2040 | group.chat_metadata = {}; | ||
| 2041 | updateChatMetadata(group.chat_metadata, true); | ||
| 2042 | } | ||
| 2033 | 2043 | ||
| 2034 | const response = await fetch('/api/chats/group/delete', { | 2044 | const response = await fetch('/api/chats/group/delete', { |
| 2035 | method: 'POST', | 2045 | method: 'POST', |
| @@ -2038,10 +2048,12 @@ export async function deleteGroupChat(groupId, chatId) { | |||
| 2038 | }); | 2048 | }); |
| 2039 | 2049 | ||
| 2040 | if (response.ok) { | 2050 | if (response.ok) { |
| 2041 | if (group.chats.length) { | 2051 | if (jumpToNewChat) { |
| 2042 | await openGroupChat(groupId, group.chats[group.chats.length - 1]); | 2052 | if (group.chats.length) { |
| 2043 | } else { | 2053 | await openGroupChat(groupId, group.chats[group.chats.length - 1]); |
| 2044 | await createNewGroupChat(groupId); | 2054 | } else { |
| 2055 | await createNewGroupChat(groupId); | ||
| 2056 | } | ||
| 2045 | } | 2057 | } |
| 2046 | 2058 | ||
| 2047 | await eventSource.emit(event_types.GROUP_CHAT_DELETED, chatId); | 2059 | await eventSource.emit(event_types.GROUP_CHAT_DELETED, chatId); |