-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuninstall.js
More file actions
199 lines (163 loc) · 5.39 KB
/
Copy pathuninstall.js
File metadata and controls
199 lines (163 loc) · 5.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import { getContext, extension_settings } from '../../../extensions.js';
import {
getRequestHeaders,
saveSettings,
} from '../../../../script.js';
const MODULE_NAME = 'threadkeeper';
const EXTENSION_FOLDER_NAME = 'ST---ThreadKeeper';
const FETCH_HOOK_FLAG = '__threadkeeperUninstallHookInstalled';
const CLEANUP_FLAG = '__threadkeeperCleanupRunning';
function hasThreadKeeperMetadata(chatData) {
return Array.isArray(chatData)
&& chatData.length > 0
&& chatData[0]?.chat_metadata
&& Object.prototype.hasOwnProperty.call(chatData[0].chat_metadata, MODULE_NAME);
}
function stripThreadKeeperMetadata(chatData) {
if (!hasThreadKeeperMetadata(chatData)) {
return null;
}
const nextHeader = {
...chatData[0],
chat_metadata: { ...chatData[0].chat_metadata },
};
delete nextHeader.chat_metadata[MODULE_NAME];
return [nextHeader, ...chatData.slice(1)];
}
async function postJson(url, body) {
const response = await fetch(url, {
method: 'POST',
headers: getRequestHeaders(),
body: JSON.stringify(body),
});
if (!response.ok) {
throw new Error(`${url} failed with status ${response.status}`);
}
return await response.json();
}
async function cleanupCurrentChat() {
const context = getContext();
if (!context.chatMetadata || !Object.prototype.hasOwnProperty.call(context.chatMetadata, MODULE_NAME)) {
return false;
}
delete context.chatMetadata[MODULE_NAME];
await context.saveMetadata();
return true;
}
async function cleanupCharacterChat(character, fileName) {
const chatData = await postJson('/api/chats/get', {
ch_name: character.name,
file_name: fileName,
avatar_url: character.avatar,
});
const cleanedChat = stripThreadKeeperMetadata(chatData);
if (!cleanedChat) {
return false;
}
await postJson('/api/chats/save', {
ch_name: character.name,
file_name: fileName,
avatar_url: character.avatar,
chat: cleanedChat,
force: true,
});
return true;
}
async function cleanupGroupChat(chatId) {
const chatData = await postJson('/api/chats/group/get', { id: chatId });
const cleanedChat = stripThreadKeeperMetadata(chatData);
if (!cleanedChat) {
return false;
}
await postJson('/api/chats/group/save', {
id: chatId,
chat: cleanedChat,
force: true,
});
return true;
}
async function cleanupAllStoredChats() {
const context = getContext();
let cleanedChats = 0;
const seenCharacters = new Set();
for (const character of context.characters || []) {
if (!character?.avatar || seenCharacters.has(character.avatar)) {
continue;
}
seenCharacters.add(character.avatar);
const results = await postJson('/api/chats/search', {
query: '',
avatar_url: character.avatar,
group_id: null,
});
for (const result of results) {
if (await cleanupCharacterChat(character, result.file_name)) {
cleanedChats++;
}
}
}
for (const group of context.groups || []) {
if (!group?.id) {
continue;
}
const results = await postJson('/api/chats/search', {
query: '',
avatar_url: null,
group_id: group.id,
});
for (const result of results) {
if (await cleanupGroupChat(result.file_name)) {
cleanedChats++;
}
}
}
return cleanedChats;
}
async function cleanupSettings() {
if (!Object.prototype.hasOwnProperty.call(extension_settings, MODULE_NAME)) {
return false;
}
delete extension_settings[MODULE_NAME];
await saveSettings();
return true;
}
export async function wipeThreadKeeperData() {
if (window[CLEANUP_FLAG]) {
return { cleanedSettings: false, cleanedChats: 0 };
}
window[CLEANUP_FLAG] = true;
try {
const cleanedCurrent = await cleanupCurrentChat();
const cleanedChats = await cleanupAllStoredChats();
const cleanedSettings = await cleanupSettings();
return {
cleanedSettings,
cleanedChats: cleanedChats + (cleanedCurrent ? 1 : 0),
};
} finally {
window[CLEANUP_FLAG] = false;
}
}
export function installUninstallHook() {
if (window[FETCH_HOOK_FLAG]) {
return;
}
const originalFetch = window.fetch.bind(window);
window.fetch = async function (input, init) {
const response = await originalFetch(input, init);
try {
const url = typeof input === 'string' ? input : input?.url;
if (!response.ok || !url || !url.includes('/api/extensions/delete')) {
return response;
}
const payload = init?.body ? JSON.parse(init.body) : null;
if (payload?.extensionName === EXTENSION_FOLDER_NAME) {
await wipeThreadKeeperData();
}
} catch (error) {
console.warn('ThreadKeeper uninstall cleanup did not complete.', error);
}
return response;
};
window[FETCH_HOOK_FLAG] = true;
}