@vite-hub/comments 0.3.1 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +20 -10
- package/dist/anchor.d.ts +2 -2
- package/dist/{client-diagnostics-CcFpkbAk.js → client-diagnostics-Bubm7Shd.js} +20 -5
- package/dist/client-files-CJZl5Kc4.js +53 -0
- package/dist/client.d.ts +151 -10
- package/dist/client.js +807 -32
- package/dist/database.js +1 -1
- package/dist/{define-B3TF9zzn.d.ts → define-CHTWfHPo.d.ts} +1 -1
- package/dist/{define-43mMR39K.js → define-CX_Yr_hz.js} +1 -1
- package/dist/{errors-CyQyKAvy.d.ts → errors-BEChnUo7.d.ts} +14 -2
- package/dist/{errors-C6qptPoG.js → errors-BHsVXlpd.js} +7 -3
- package/dist/{index-DNhy69Wt.d.ts → index-iPGlSAgj.d.ts} +13 -4
- package/dist/index.d.ts +7 -7
- package/dist/index.js +41 -7
- package/dist/{install-W9ghhGoh.d.ts → install-96NXWnrw.d.ts} +3 -3
- package/dist/{install-BcMPgPk_.js → install-jRKmuLHK.js} +72 -26
- package/dist/{installed-B-PfERBU.js → installed-g6nDr5ac.js} +1 -1
- package/dist/{migrate-BDQ8xqUs.js → migrate-Bpw3xL5e.js} +1 -1
- package/dist/{operations-D4A1Fcgf.d.ts → operations-7ycAhDCw.d.ts} +5 -0
- package/dist/{operations-DMMIfYPW.js → operations-9tskoLsk.js} +22 -11
- package/dist/{operations-GLeOqgMt.d.ts → operations-D93EV10O.d.ts} +5 -4
- package/dist/operations.d.ts +2 -2
- package/dist/operations.js +2 -2
- package/dist/overlay/client-lZesR40x.js +4 -0
- package/dist/overlay/overlay.js +20 -40
- package/dist/{page-DtbMOEMd.d.ts → page-CWwnmF3K.d.ts} +1 -1
- package/dist/{page-B41ozN63.js → page-DRz_sDY4.js} +1 -1
- package/dist/{plugin-PS8PMYus.js → plugin-BNFd1zGz.js} +27 -6
- package/dist/realtime.d.ts +2 -2
- package/dist/realtime.js +2 -2
- package/dist/reply-id-P_bSuEng.js +12 -0
- package/dist/{rpc-RKPAusHp.js → rpc-BQqYx8kb.js} +23 -7
- package/dist/{rpc-HBcWtg0T.d.ts → rpc-BZ1QNkCC.d.ts} +1 -1
- package/dist/rpc.d.ts +1 -1
- package/dist/rpc.js +1 -1
- package/dist/runtime/auth.d.ts +2 -2
- package/dist/runtime/auth.js +2 -2
- package/dist/runtime/console.js +3 -3
- package/dist/runtime/index.d.ts +38 -11
- package/dist/runtime/index.js +83 -15
- package/dist/{server-D8k-0GXe.js → server-CqyzrqTI.js} +1 -1
- package/dist/server.d.ts +5 -5
- package/dist/server.js +6 -6
- package/dist/{service-GHhhyQj4.js → service-C8ZgFWvy.js} +34 -5
- package/dist/{service-CFVm23k7.d.ts → service-wC9kx9EX.d.ts} +11 -4
- package/dist/{session-EkudzUmm.js → session-ByjMyTak.js} +1 -1
- package/dist/{session-DVx-1Tym.d.ts → session-CKfW6DGE.d.ts} +3 -3
- package/dist/table-names-DfiKUlOV.js +14 -0
- package/dist/testing.d.ts +1 -1
- package/dist/testing.js +37 -4
- package/dist/{types-BIS0hCEl.d.ts → types-C1qzObs2.d.ts} +12 -3
- package/dist/{types-CZujrMFd.d.ts → types-C7LzhgRb.d.ts} +47 -3
- package/dist/vite.d.ts +6 -6
- package/dist/vite.js +187 -113
- package/package.json +9 -9
- package/dist/overlay/client-DmsSMOzi.js +0 -4
package/dist/client.js
CHANGED
|
@@ -1,8 +1,733 @@
|
|
|
1
|
-
import { i as commentsUnansweredError, r as commentsClientDiagnostics, t as commentsAnswerError } from "./client-diagnostics-
|
|
2
|
-
import { t as
|
|
1
|
+
import { i as commentsUnansweredError, r as commentsClientDiagnostics, t as commentsAnswerError } from "./client-diagnostics-Bubm7Shd.js";
|
|
2
|
+
import { i as withCommentFiles, n as outboxAccountHeader, r as retryAfterOf, t as failedCommentPreparation } from "./client-files-CJZl5Kc4.js";
|
|
3
|
+
import { t as replyMessageId } from "./reply-id-P_bSuEng.js";
|
|
4
|
+
import { t as commentsErrorDiagnostics } from "./errors-BHsVXlpd.js";
|
|
3
5
|
import { t as foldName } from "./fold-name-B7Zb3-MF.js";
|
|
4
|
-
import { i as commentsRoute } from "./page-
|
|
6
|
+
import { i as commentsRoute } from "./page-DRz_sDY4.js";
|
|
5
7
|
import { n as isRecordOrFunction } from "./object-Da1zyPCR.js";
|
|
8
|
+
//#region ../internal/src/browser-environment.ts
|
|
9
|
+
/** The real browser: window events, Web Locks, and a BroadcastChannel, each only when the browser has it. */
|
|
10
|
+
function createBrowserEnvironment(options) {
|
|
11
|
+
let channel;
|
|
12
|
+
return {
|
|
13
|
+
listen(listener) {
|
|
14
|
+
const online = () => listener("online");
|
|
15
|
+
const offline = () => listener("offline");
|
|
16
|
+
const visibility = () => listener(document.visibilityState === "hidden" ? "hidden" : "visible");
|
|
17
|
+
window.addEventListener("online", online);
|
|
18
|
+
window.addEventListener("offline", offline);
|
|
19
|
+
document.addEventListener("visibilitychange", visibility);
|
|
20
|
+
return () => {
|
|
21
|
+
window.removeEventListener("online", online);
|
|
22
|
+
window.removeEventListener("offline", offline);
|
|
23
|
+
document.removeEventListener("visibilitychange", visibility);
|
|
24
|
+
};
|
|
25
|
+
},
|
|
26
|
+
isVisible: () => document.visibilityState !== "hidden",
|
|
27
|
+
isOnline: () => navigator.onLine !== false,
|
|
28
|
+
lock(name, task) {
|
|
29
|
+
const locks = navigator.locks;
|
|
30
|
+
return locks ? locks.request(name, task) : task();
|
|
31
|
+
},
|
|
32
|
+
lead(name, task) {
|
|
33
|
+
const controller = new AbortController();
|
|
34
|
+
const run = () => controller.signal.aborted ? Promise.resolve() : task(controller.signal);
|
|
35
|
+
const locks = navigator.locks;
|
|
36
|
+
if (locks) locks.request(name, { signal: controller.signal }, run).catch(() => void 0);
|
|
37
|
+
else run();
|
|
38
|
+
return () => controller.abort();
|
|
39
|
+
},
|
|
40
|
+
post(message) {
|
|
41
|
+
if (channel) channel.postMessage(message);
|
|
42
|
+
else postOnce(options.channel, message);
|
|
43
|
+
},
|
|
44
|
+
receive(listener) {
|
|
45
|
+
if (typeof BroadcastChannel !== "function") return () => void 0;
|
|
46
|
+
const own = new BroadcastChannel(options.channel);
|
|
47
|
+
channel = own;
|
|
48
|
+
own.onmessage = (event) => {
|
|
49
|
+
if (options.isMessage(event.data)) listener(event.data);
|
|
50
|
+
};
|
|
51
|
+
return () => {
|
|
52
|
+
own.close();
|
|
53
|
+
if (channel === own) channel = void 0;
|
|
54
|
+
};
|
|
55
|
+
},
|
|
56
|
+
schedule(task, ms) {
|
|
57
|
+
const timer = setTimeout(task, ms);
|
|
58
|
+
return () => clearTimeout(timer);
|
|
59
|
+
},
|
|
60
|
+
random: Math.random
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
/** Posts one message on a channel of its own, so every open channel of that name gets it. */
|
|
64
|
+
function postOnce(channel, message) {
|
|
65
|
+
if (typeof BroadcastChannel !== "function") return;
|
|
66
|
+
const own = new BroadcastChannel(channel);
|
|
67
|
+
own.postMessage(message);
|
|
68
|
+
own.close();
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* A version 4 UUID. `crypto.randomUUID()` exists only on HTTPS and localhost, and an app on a LAN address in development
|
|
72
|
+
* has neither, so this falls back to `crypto.getRandomValues()`.
|
|
73
|
+
*/
|
|
74
|
+
function randomUUID() {
|
|
75
|
+
if (typeof crypto.randomUUID === "function") return crypto.randomUUID();
|
|
76
|
+
const bytes = crypto.getRandomValues(/* @__PURE__ */ new Uint8Array(16));
|
|
77
|
+
bytes[6] = bytes[6] & 15 | 64;
|
|
78
|
+
bytes[8] = bytes[8] & 63 | 128;
|
|
79
|
+
const hex = Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
80
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
81
|
+
}
|
|
82
|
+
/** The wait before try `attempt + 1`: 1 s doubling to 30 s, with jitter over its upper half. `random` is in [0, 1). */
|
|
83
|
+
function backoffDelay(attempt, random) {
|
|
84
|
+
const ceiling = Math.min(3e4, 1e3 * 2 ** attempt);
|
|
85
|
+
return ceiling / 2 + random * ceiling / 2;
|
|
86
|
+
}
|
|
87
|
+
//#endregion
|
|
88
|
+
//#region src/client-outbox.ts
|
|
89
|
+
function changeable(entry) {
|
|
90
|
+
return entry.status !== "sending" && !entry.uncertain;
|
|
91
|
+
}
|
|
92
|
+
const channel = "vitehub-comments";
|
|
93
|
+
function isOutboxMessage(value) {
|
|
94
|
+
if (!value || typeof value !== "object") return false;
|
|
95
|
+
const type = Reflect.get(value, "type");
|
|
96
|
+
if (typeof Reflect.get(value, "account") !== "string") return false;
|
|
97
|
+
if (type === "change") return true;
|
|
98
|
+
const kind = Reflect.get(value, "kind");
|
|
99
|
+
return type === "sent" && (kind === "create" || kind === "reply") && typeof Reflect.get(value, "id") === "string" && typeof Reflect.get(value, "key") === "string" && Boolean(Reflect.get(value, "thread"));
|
|
100
|
+
}
|
|
101
|
+
function copyEntry(entry) {
|
|
102
|
+
const input = JSON.parse(JSON.stringify(entry.input));
|
|
103
|
+
const subject = entry.kind === "reply" && entry.subject ? {
|
|
104
|
+
kind: entry.subject.kind,
|
|
105
|
+
id: entry.subject.id
|
|
106
|
+
} : void 0;
|
|
107
|
+
return {
|
|
108
|
+
...entry,
|
|
109
|
+
input,
|
|
110
|
+
files: [...entry.files],
|
|
111
|
+
...subject ? { subject } : {},
|
|
112
|
+
...entry.uploaded ? { uploaded: [...entry.uploaded] } : {},
|
|
113
|
+
...entry.error ? { error: { ...entry.error } } : {}
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
function sortEntries(entries) {
|
|
117
|
+
return entries.sort((a, b) => a.order - b.order);
|
|
118
|
+
}
|
|
119
|
+
/** An outbox store in memory. Entries live as long as the store object. Pass one store to two clients to test a reload. */
|
|
120
|
+
function createMemoryOutboxStore() {
|
|
121
|
+
const accounts = /* @__PURE__ */ new Map();
|
|
122
|
+
return {
|
|
123
|
+
persisted: false,
|
|
124
|
+
async list(account) {
|
|
125
|
+
return sortEntries([...accounts.get(account)?.values() ?? []].map(copyEntry));
|
|
126
|
+
},
|
|
127
|
+
async update(account, change) {
|
|
128
|
+
const next = new Map([...accounts.get(account) ?? []].map(([id, entry]) => [id, copyEntry(entry)]));
|
|
129
|
+
const result = change(next);
|
|
130
|
+
accounts.set(account, new Map([...next].map(([id, entry]) => [id, copyEntry(entry)])));
|
|
131
|
+
return result;
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
const databaseName = "vitehub-comments";
|
|
136
|
+
const format = 2;
|
|
137
|
+
function done(transaction) {
|
|
138
|
+
return new Promise((resolve, reject) => {
|
|
139
|
+
transaction.oncomplete = () => resolve();
|
|
140
|
+
transaction.onabort = () => reject(transaction.error ?? /* @__PURE__ */ new Error("The IndexedDB transaction was aborted."));
|
|
141
|
+
transaction.onerror = () => reject(transaction.error ?? /* @__PURE__ */ new Error("The IndexedDB transaction failed."));
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
function requested(request) {
|
|
145
|
+
return new Promise((resolve, reject) => {
|
|
146
|
+
request.onsuccess = () => resolve(request.result);
|
|
147
|
+
request.onerror = () => reject(request.error ?? /* @__PURE__ */ new Error("The IndexedDB request failed."));
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
function openDatabase() {
|
|
151
|
+
return new Promise((resolve, reject) => {
|
|
152
|
+
const request = indexedDB.open(databaseName, format);
|
|
153
|
+
request.onupgradeneeded = (event) => {
|
|
154
|
+
const drafts = request.result.createObjectStore("drafts", { keyPath: ["account", "id"] });
|
|
155
|
+
drafts.createIndex("account", "account");
|
|
156
|
+
if (event.oldVersion === 1 && request.transaction) {
|
|
157
|
+
const previous = request.transaction.objectStore("outbox").openCursor();
|
|
158
|
+
previous.onsuccess = () => {
|
|
159
|
+
const cursor = previous.result;
|
|
160
|
+
if (!cursor) {
|
|
161
|
+
request.result.deleteObjectStore("outbox");
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
const entry = cursor.value;
|
|
165
|
+
drafts.put({
|
|
166
|
+
...entry,
|
|
167
|
+
id: entry.requestId,
|
|
168
|
+
uncertain: true
|
|
169
|
+
});
|
|
170
|
+
cursor.continue();
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
request.onsuccess = () => {
|
|
175
|
+
const database = request.result;
|
|
176
|
+
database.onversionchange = () => database.close();
|
|
177
|
+
resolve(database);
|
|
178
|
+
};
|
|
179
|
+
request.onerror = () => reject(request.error ?? /* @__PURE__ */ new Error("IndexedDB did not open."));
|
|
180
|
+
request.onblocked = () => reject(/* @__PURE__ */ new Error("IndexedDB is blocked by another tab."));
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* The default store: the `drafts` object store of the `vitehub-comments` IndexedDB database. When IndexedDB is missing or
|
|
185
|
+
* does not open, as in some private windows, it keeps the entries in memory and `persisted` is `false`.
|
|
186
|
+
*/
|
|
187
|
+
function createIndexedDBOutboxStore() {
|
|
188
|
+
let backend;
|
|
189
|
+
let persisted = typeof indexedDB !== "undefined";
|
|
190
|
+
const open = () => backend ??= (persisted ? openDatabase() : Promise.reject(/* @__PURE__ */ new Error("No IndexedDB."))).then((database) => ({ database }), () => {
|
|
191
|
+
persisted = false;
|
|
192
|
+
return { memory: createMemoryOutboxStore() };
|
|
193
|
+
});
|
|
194
|
+
const run = async (memory, database) => {
|
|
195
|
+
const opened = await open();
|
|
196
|
+
return "database" in opened ? database(opened.database) : memory(opened.memory);
|
|
197
|
+
};
|
|
198
|
+
return {
|
|
199
|
+
get persisted() {
|
|
200
|
+
return persisted;
|
|
201
|
+
},
|
|
202
|
+
list: (account) => run((store) => store.list(account), async (database) => {
|
|
203
|
+
return sortEntries(await requested(database.transaction("drafts", "readonly").objectStore("drafts").index("account").getAll(account)));
|
|
204
|
+
}),
|
|
205
|
+
update: (account, change) => run((store) => store.update(account, change), async (database) => {
|
|
206
|
+
const transaction = database.transaction("drafts", "readwrite");
|
|
207
|
+
const drafts = transaction.objectStore("drafts");
|
|
208
|
+
const read = drafts.index("account").getAll(account);
|
|
209
|
+
const complete = done(transaction);
|
|
210
|
+
const result = new Promise((resolve, reject) => {
|
|
211
|
+
read.onsuccess = () => {
|
|
212
|
+
const previous = new Map(read.result.map((entry) => [entry.id, entry]));
|
|
213
|
+
const next = new Map(previous);
|
|
214
|
+
try {
|
|
215
|
+
const value = change(next);
|
|
216
|
+
for (const id of previous.keys()) if (!next.has(id)) drafts.delete([account, id]);
|
|
217
|
+
for (const [id, entry] of next) if (previous.get(id) !== entry) drafts.put(copyEntry(entry));
|
|
218
|
+
resolve(value);
|
|
219
|
+
} catch (error) {
|
|
220
|
+
reject(error);
|
|
221
|
+
transaction.abort();
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
read.onerror = () => reject(read.error);
|
|
225
|
+
});
|
|
226
|
+
return (await Promise.all([result, complete]))[0];
|
|
227
|
+
})
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
/** The error of a try, as plain data that the store keeps. */
|
|
231
|
+
function deliveryError(error) {
|
|
232
|
+
const field = (name) => error && typeof error === "object" ? Reflect.get(error, name) : void 0;
|
|
233
|
+
const status = field("status");
|
|
234
|
+
const code = field("code");
|
|
235
|
+
const why = field("why") ?? field("message");
|
|
236
|
+
const fix = field("fix");
|
|
237
|
+
const retryAfter = field("retryAfter");
|
|
238
|
+
return {
|
|
239
|
+
status: typeof status === "number" ? status : 0,
|
|
240
|
+
why: typeof why === "string" && why ? why : String(error),
|
|
241
|
+
...typeof code === "string" ? { code } : {},
|
|
242
|
+
...typeof fix === "string" ? { fix } : {},
|
|
243
|
+
...typeof retryAfter === "number" && retryAfter >= 0 ? { retryAfter } : {}
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
/** `wait`: try again later. `hold`: wait for `retry()`, such as after a sign-in or a plan upgrade. `fail`: the server refused this entry. */
|
|
247
|
+
function outcomeOf(status) {
|
|
248
|
+
if (status === 401 || status === 402) return "hold";
|
|
249
|
+
if (status === 408 || status === 429) return "wait";
|
|
250
|
+
return status >= 400 && status < 500 ? "fail" : "wait";
|
|
251
|
+
}
|
|
252
|
+
/** The outbox of one client. */
|
|
253
|
+
function createOutbox(options, host) {
|
|
254
|
+
const { account } = options;
|
|
255
|
+
const store = options.store ?? createIndexedDBOutboxStore();
|
|
256
|
+
const environment = options.environment ?? createBrowserEnvironment({
|
|
257
|
+
channel,
|
|
258
|
+
isMessage: isOutboxMessage
|
|
259
|
+
});
|
|
260
|
+
const lockName = `vitehub-comments-outbox:${account}`;
|
|
261
|
+
let entries = [];
|
|
262
|
+
const sent = /* @__PURE__ */ new Map();
|
|
263
|
+
const receipts = /* @__PURE__ */ new Map();
|
|
264
|
+
const keyOf = (entry) => entry.kind === "create" ? JSON.stringify([
|
|
265
|
+
entry.kind,
|
|
266
|
+
entry.input.subject.kind,
|
|
267
|
+
entry.input.subject.id,
|
|
268
|
+
entry.requestId
|
|
269
|
+
]) : JSON.stringify([
|
|
270
|
+
entry.kind,
|
|
271
|
+
entry.input.threadId,
|
|
272
|
+
entry.requestId
|
|
273
|
+
]);
|
|
274
|
+
const replyKey = (threadId, requestId) => JSON.stringify([threadId, requestId]);
|
|
275
|
+
let replyIds = /* @__PURE__ */ new Map();
|
|
276
|
+
let loading = Promise.resolve();
|
|
277
|
+
let leading;
|
|
278
|
+
let again = false;
|
|
279
|
+
let cancelWait;
|
|
280
|
+
const post = (message) => environment.post(message);
|
|
281
|
+
const reload = () => loading = loading.catch(() => void 0).then(async () => {
|
|
282
|
+
const pending = await store.list(account);
|
|
283
|
+
const ids = /* @__PURE__ */ new Map();
|
|
284
|
+
if (globalThis.crypto?.subtle) await Promise.all(pending.map(async (entry) => {
|
|
285
|
+
if (entry.kind !== "reply") return;
|
|
286
|
+
const threadId = realId(entry.input.threadId);
|
|
287
|
+
const key = replyKey(threadId, entry.requestId);
|
|
288
|
+
ids.set(key, replyIds.get(key) ?? await replyMessageId(threadId, entry.requestId));
|
|
289
|
+
}));
|
|
290
|
+
replyIds = ids;
|
|
291
|
+
entries = pending;
|
|
292
|
+
host.changed();
|
|
293
|
+
});
|
|
294
|
+
const changed = async () => {
|
|
295
|
+
await reload();
|
|
296
|
+
post({
|
|
297
|
+
type: "change",
|
|
298
|
+
account
|
|
299
|
+
});
|
|
300
|
+
};
|
|
301
|
+
const serverThread = (id) => {
|
|
302
|
+
for (const thread of host.threads()) if (thread.id === id) return thread;
|
|
303
|
+
};
|
|
304
|
+
/** The server id of a thread that the client may still name by its local id. */
|
|
305
|
+
const realId = (threadId) => {
|
|
306
|
+
return sent.get(threadId)?.id ?? threadId;
|
|
307
|
+
};
|
|
308
|
+
const delivery = (entry) => ({
|
|
309
|
+
status: entry.status === "failed" ? "failed" : "pending",
|
|
310
|
+
requestId: entry.requestId,
|
|
311
|
+
files: entry.files,
|
|
312
|
+
...entry.error ? { error: entry.error } : {}
|
|
313
|
+
});
|
|
314
|
+
const author = () => host.user?.id === account ? host.user : { id: account };
|
|
315
|
+
const localMessage = (entry, threadId) => ({
|
|
316
|
+
id: entry.id,
|
|
317
|
+
threadId,
|
|
318
|
+
body: entry.input.body,
|
|
319
|
+
author: author(),
|
|
320
|
+
mentions: entry.input.mentions ?? [],
|
|
321
|
+
attachments: entry.input.attachments ?? [],
|
|
322
|
+
createdAt: entry.createdAt,
|
|
323
|
+
delivery: delivery(entry)
|
|
324
|
+
});
|
|
325
|
+
const overlay = (threads) => {
|
|
326
|
+
const replies = /* @__PURE__ */ new Map();
|
|
327
|
+
for (const entry of entries) {
|
|
328
|
+
if (entry.kind !== "reply") continue;
|
|
329
|
+
const id = realId(entry.input.threadId);
|
|
330
|
+
replies.set(id, [...replies.get(id) ?? [], entry]);
|
|
331
|
+
}
|
|
332
|
+
const withReplies = (thread) => {
|
|
333
|
+
const waiting = replies.get(thread.id);
|
|
334
|
+
if (!waiting) return thread;
|
|
335
|
+
const have = new Set(thread.messages.filter((message) => message.author.id === account).map((message) => message.id));
|
|
336
|
+
const added = waiting.filter((entry) => {
|
|
337
|
+
const id = thread.delivery ? void 0 : replyIds.get(replyKey(thread.id, entry.requestId));
|
|
338
|
+
return !(id && have.has(id));
|
|
339
|
+
}).map((entry) => localMessage(entry, thread.id));
|
|
340
|
+
return added.length ? {
|
|
341
|
+
...thread,
|
|
342
|
+
messages: [...thread.messages, ...added]
|
|
343
|
+
} : thread;
|
|
344
|
+
};
|
|
345
|
+
const server = threads.map(withReplies);
|
|
346
|
+
const local = [];
|
|
347
|
+
for (const entry of entries) {
|
|
348
|
+
if (entry.kind !== "create" || sent.has(entry.id) || threads.some((thread) => matchesCreate(entry, thread))) continue;
|
|
349
|
+
local.push(withReplies(localThread(entry)));
|
|
350
|
+
}
|
|
351
|
+
return {
|
|
352
|
+
server,
|
|
353
|
+
local
|
|
354
|
+
};
|
|
355
|
+
};
|
|
356
|
+
const matchesCreate = (entry, thread) => thread.requestId === entry.requestId && thread.author.id === account && thread.subject.kind === entry.input.subject.kind && thread.subject.id === entry.input.subject.id;
|
|
357
|
+
const localThread = (entry) => {
|
|
358
|
+
const { input } = entry;
|
|
359
|
+
return {
|
|
360
|
+
id: entry.id,
|
|
361
|
+
scope: "",
|
|
362
|
+
n: 0,
|
|
363
|
+
subject: input.subject,
|
|
364
|
+
anchor: input.anchor,
|
|
365
|
+
status: "open",
|
|
366
|
+
author: author(),
|
|
367
|
+
messages: [localMessage(entry, entry.id)],
|
|
368
|
+
...input.metadata === void 0 ? {} : { metadata: input.metadata },
|
|
369
|
+
...input.revision === void 0 ? {} : { revision: input.revision },
|
|
370
|
+
createdAt: entry.createdAt,
|
|
371
|
+
updatedAt: entry.createdAt,
|
|
372
|
+
requestId: entry.requestId,
|
|
373
|
+
delivery: delivery(entry)
|
|
374
|
+
};
|
|
375
|
+
};
|
|
376
|
+
/** The first entry that may go out now. A reply waits while its comment has not gone out. */
|
|
377
|
+
const next = (list) => {
|
|
378
|
+
const waiting = new Set(list.flatMap((entry) => entry.kind === "create" ? [entry.id] : []));
|
|
379
|
+
return list.find((entry) => entry.status !== "failed" && !(entry.kind === "reply" && waiting.has(entry.input.threadId)));
|
|
380
|
+
};
|
|
381
|
+
const wait = (ms) => {
|
|
382
|
+
cancelWait?.();
|
|
383
|
+
cancelWait = environment.schedule(() => {
|
|
384
|
+
cancelWait = void 0;
|
|
385
|
+
kick();
|
|
386
|
+
}, ms);
|
|
387
|
+
};
|
|
388
|
+
const update = (change) => store.update(account, change);
|
|
389
|
+
const send = async (entry) => {
|
|
390
|
+
const withClaim = (change) => update((drafts) => {
|
|
391
|
+
const current = drafts.get(entry.id);
|
|
392
|
+
if (current && current.claim === entry.claim) return change(current, drafts);
|
|
393
|
+
});
|
|
394
|
+
let thread;
|
|
395
|
+
let writing = false;
|
|
396
|
+
const outbox = {
|
|
397
|
+
account,
|
|
398
|
+
async uploaded(key) {
|
|
399
|
+
if (!await withClaim((current, drafts) => drafts.set(entry.id, {
|
|
400
|
+
...current,
|
|
401
|
+
uploaded: [...current.uploaded ?? [], key]
|
|
402
|
+
}))) throw new DOMException("This draft is no longer in the outbox.", "AbortError");
|
|
403
|
+
}
|
|
404
|
+
};
|
|
405
|
+
try {
|
|
406
|
+
const files = {
|
|
407
|
+
files: entry.files.slice(entry.uploaded?.length ?? 0),
|
|
408
|
+
outbox
|
|
409
|
+
};
|
|
410
|
+
const attachments = [...entry.input.attachments ?? [], ...entry.uploaded ?? []];
|
|
411
|
+
const extra = {
|
|
412
|
+
...files,
|
|
413
|
+
requestId: entry.requestId,
|
|
414
|
+
...attachments.length ? { attachments } : {}
|
|
415
|
+
};
|
|
416
|
+
if (entry.kind === "create") {
|
|
417
|
+
if (!await withClaim(() => true)) return;
|
|
418
|
+
writing = true;
|
|
419
|
+
thread = await host.transport.create({
|
|
420
|
+
...entry.input,
|
|
421
|
+
...extra
|
|
422
|
+
});
|
|
423
|
+
} else {
|
|
424
|
+
const threadId = realId(entry.input.threadId);
|
|
425
|
+
const subject = entry.subject ?? serverThread(threadId)?.subject ?? (files.files?.length ? (await host.transport.get({ id: threadId }))?.subject : void 0);
|
|
426
|
+
if (files.files?.length && !subject) throw Object.assign(/* @__PURE__ */ new Error("The thread for this reply does not exist."), { status: 404 });
|
|
427
|
+
if (!await withClaim(() => true)) return;
|
|
428
|
+
writing = true;
|
|
429
|
+
thread = await host.transport.reply({
|
|
430
|
+
...entry.input,
|
|
431
|
+
...extra,
|
|
432
|
+
threadId,
|
|
433
|
+
...subject ? { subject } : {}
|
|
434
|
+
});
|
|
435
|
+
}
|
|
436
|
+
} catch (cause) {
|
|
437
|
+
const preparation = failedCommentPreparation.get(outbox);
|
|
438
|
+
const beforeWrite = preparation !== void 0 && Object.is(preparation.cause, cause);
|
|
439
|
+
const { retryAfter, ...error } = deliveryError(cause);
|
|
440
|
+
const outcome = outcomeOf(error.status);
|
|
441
|
+
const attempts = entry.attempts ?? 0;
|
|
442
|
+
if (await withClaim((current, drafts) => drafts.set(entry.id, {
|
|
443
|
+
...current,
|
|
444
|
+
status: outcome === "fail" ? "failed" : "pending",
|
|
445
|
+
uncertain: entry.uncertain || writing && !beforeWrite && outcome === "wait" && error.status !== 429,
|
|
446
|
+
held: outcome === "hold",
|
|
447
|
+
retryAt: outcome === "wait" ? Date.now() + Math.max(1, retryAfter ?? backoffDelay(attempts, environment.random())) : void 0,
|
|
448
|
+
attempts: attempts + 1,
|
|
449
|
+
error
|
|
450
|
+
}))) await changed();
|
|
451
|
+
return;
|
|
452
|
+
}
|
|
453
|
+
if (!await withClaim((current, drafts) => {
|
|
454
|
+
if (current.kind === "create") {
|
|
455
|
+
for (const reply of drafts.values()) if (reply.kind === "reply" && reply.input.threadId === entry.id) drafts.set(reply.id, {
|
|
456
|
+
...reply,
|
|
457
|
+
subject: thread.subject,
|
|
458
|
+
input: {
|
|
459
|
+
...reply.input,
|
|
460
|
+
threadId: thread.id
|
|
461
|
+
}
|
|
462
|
+
});
|
|
463
|
+
}
|
|
464
|
+
return drafts.delete(entry.id);
|
|
465
|
+
})) return;
|
|
466
|
+
const key = keyOf(entry);
|
|
467
|
+
if (entry.kind === "create") sent.set(entry.id, thread);
|
|
468
|
+
receipts.set(key, thread);
|
|
469
|
+
host.upsert(thread);
|
|
470
|
+
await reload();
|
|
471
|
+
post({
|
|
472
|
+
type: "sent",
|
|
473
|
+
account,
|
|
474
|
+
id: entry.id,
|
|
475
|
+
kind: entry.kind,
|
|
476
|
+
key,
|
|
477
|
+
thread
|
|
478
|
+
});
|
|
479
|
+
};
|
|
480
|
+
const drain = async (signal) => {
|
|
481
|
+
cancelWait?.();
|
|
482
|
+
cancelWait = void 0;
|
|
483
|
+
while (!signal.aborted && environment.isOnline()) {
|
|
484
|
+
const claim = randomUUID();
|
|
485
|
+
const selected = await update((drafts) => {
|
|
486
|
+
if (signal.aborted) return;
|
|
487
|
+
const entry = next(sortEntries([...drafts.values()]));
|
|
488
|
+
if (!entry || entry.held) return;
|
|
489
|
+
const delay = (entry.retryAt ?? 0) - Date.now();
|
|
490
|
+
if (delay > 0) return { delay };
|
|
491
|
+
const claimed = {
|
|
492
|
+
...entry,
|
|
493
|
+
claim,
|
|
494
|
+
status: "sending",
|
|
495
|
+
uncertain: entry.uncertain || entry.status === "sending"
|
|
496
|
+
};
|
|
497
|
+
drafts.set(entry.id, claimed);
|
|
498
|
+
return { claimed };
|
|
499
|
+
});
|
|
500
|
+
if (!selected) return;
|
|
501
|
+
if (selected.delay !== void 0) {
|
|
502
|
+
wait(selected.delay);
|
|
503
|
+
return;
|
|
504
|
+
}
|
|
505
|
+
await reload();
|
|
506
|
+
await send(selected.claimed);
|
|
507
|
+
}
|
|
508
|
+
};
|
|
509
|
+
/** Sends what waits, in the tab that holds the lock. */
|
|
510
|
+
function kick() {
|
|
511
|
+
if (leading) {
|
|
512
|
+
again = true;
|
|
513
|
+
return;
|
|
514
|
+
}
|
|
515
|
+
const run = { giveUp: () => void 0 };
|
|
516
|
+
leading = run;
|
|
517
|
+
run.giveUp = environment.lead(lockName, async (signal) => {
|
|
518
|
+
try {
|
|
519
|
+
do {
|
|
520
|
+
again = false;
|
|
521
|
+
await drain(signal);
|
|
522
|
+
} while (again && !signal.aborted);
|
|
523
|
+
} finally {
|
|
524
|
+
if (leading === run) leading = void 0;
|
|
525
|
+
}
|
|
526
|
+
});
|
|
527
|
+
}
|
|
528
|
+
const enqueue = async (entry) => {
|
|
529
|
+
const saved = await update((drafts) => {
|
|
530
|
+
const key = keyOf(entry);
|
|
531
|
+
const existing = [...drafts.values()].find((draft) => keyOf(draft) === key);
|
|
532
|
+
if (existing) return existing;
|
|
533
|
+
let order = entry.createdAt;
|
|
534
|
+
for (const draft of drafts.values()) order = Math.max(order, draft.order + 1);
|
|
535
|
+
const ordered = {
|
|
536
|
+
...entry,
|
|
537
|
+
order
|
|
538
|
+
};
|
|
539
|
+
drafts.set(entry.id, ordered);
|
|
540
|
+
return ordered;
|
|
541
|
+
});
|
|
542
|
+
await changed();
|
|
543
|
+
return saved;
|
|
544
|
+
};
|
|
545
|
+
const fields = (requestId, files) => {
|
|
546
|
+
const now = Date.now();
|
|
547
|
+
const id = randomUUID();
|
|
548
|
+
return {
|
|
549
|
+
id,
|
|
550
|
+
requestId: requestId ?? id,
|
|
551
|
+
account,
|
|
552
|
+
files: [...files ?? []],
|
|
553
|
+
createdAt: now,
|
|
554
|
+
order: now,
|
|
555
|
+
status: "pending"
|
|
556
|
+
};
|
|
557
|
+
};
|
|
558
|
+
reload();
|
|
559
|
+
return {
|
|
560
|
+
get entries() {
|
|
561
|
+
return entries;
|
|
562
|
+
},
|
|
563
|
+
get queued() {
|
|
564
|
+
return entries.filter((entry) => entry.status !== "failed");
|
|
565
|
+
},
|
|
566
|
+
get failures() {
|
|
567
|
+
return entries.filter((entry) => entry.status === "failed");
|
|
568
|
+
},
|
|
569
|
+
get persisted() {
|
|
570
|
+
return store.persisted;
|
|
571
|
+
},
|
|
572
|
+
overlay,
|
|
573
|
+
owns: (threadId, messageId) => entries.some((entry) => entry.id === (messageId ?? threadId)),
|
|
574
|
+
async create({ files, requestId, ...input }) {
|
|
575
|
+
const entry = {
|
|
576
|
+
...fields(requestId, files),
|
|
577
|
+
kind: "create",
|
|
578
|
+
input
|
|
579
|
+
};
|
|
580
|
+
const receipt = () => receipts.get(keyOf(entry)) ?? [...host.threads()].find((thread) => matchesCreate(entry, thread));
|
|
581
|
+
const earlier = receipt();
|
|
582
|
+
if (earlier) return earlier;
|
|
583
|
+
const saved = await enqueue(entry);
|
|
584
|
+
kick();
|
|
585
|
+
return receipt() ?? localThread(saved.kind === "create" ? saved : entry);
|
|
586
|
+
},
|
|
587
|
+
async reply({ files, requestId, subject, ...input }) {
|
|
588
|
+
const threadId = realId(input.threadId);
|
|
589
|
+
const parent = entries.find((entry) => entry.kind === "create" && entry.id === threadId);
|
|
590
|
+
const cached = serverThread(threadId);
|
|
591
|
+
const target = subject ?? cached?.subject ?? (parent?.kind === "create" ? parent.input.subject : void 0);
|
|
592
|
+
const entry = {
|
|
593
|
+
...fields(requestId, files),
|
|
594
|
+
kind: "reply",
|
|
595
|
+
input: {
|
|
596
|
+
...input,
|
|
597
|
+
threadId
|
|
598
|
+
},
|
|
599
|
+
...target ? { subject: target } : {}
|
|
600
|
+
};
|
|
601
|
+
const earlier = receipts.get(keyOf(entry));
|
|
602
|
+
if (earlier) return serverThread(earlier.id) ?? earlier;
|
|
603
|
+
const saved = await enqueue(entry);
|
|
604
|
+
kick();
|
|
605
|
+
const thread = serverThread(realId(threadId)) ?? cached;
|
|
606
|
+
if (thread) return overlay([thread]).server[0] ?? thread;
|
|
607
|
+
const local = overlay([]).local.find((local) => local.id === threadId);
|
|
608
|
+
if (local) return local;
|
|
609
|
+
return {
|
|
610
|
+
id: threadId,
|
|
611
|
+
scope: "",
|
|
612
|
+
n: 0,
|
|
613
|
+
subject: target ?? {
|
|
614
|
+
kind: "",
|
|
615
|
+
id: ""
|
|
616
|
+
},
|
|
617
|
+
anchor: { kind: "page" },
|
|
618
|
+
status: "open",
|
|
619
|
+
author: author(),
|
|
620
|
+
messages: [localMessage(saved, threadId)],
|
|
621
|
+
createdAt: entry.createdAt,
|
|
622
|
+
updatedAt: entry.createdAt
|
|
623
|
+
};
|
|
624
|
+
},
|
|
625
|
+
async retry(id) {
|
|
626
|
+
await update((drafts) => {
|
|
627
|
+
for (const entry of drafts.values()) {
|
|
628
|
+
if (entry.status === "sending" || id !== void 0 && entry.id !== id) continue;
|
|
629
|
+
if (entry.status === "failed" && id === void 0) continue;
|
|
630
|
+
drafts.set(entry.id, {
|
|
631
|
+
...entry,
|
|
632
|
+
status: "pending",
|
|
633
|
+
held: false,
|
|
634
|
+
attempts: 0,
|
|
635
|
+
retryAt: void 0,
|
|
636
|
+
error: void 0
|
|
637
|
+
});
|
|
638
|
+
}
|
|
639
|
+
});
|
|
640
|
+
await changed();
|
|
641
|
+
kick();
|
|
642
|
+
},
|
|
643
|
+
async dismiss(id) {
|
|
644
|
+
const removed = await update((drafts) => {
|
|
645
|
+
const entry = drafts.get(id);
|
|
646
|
+
if (!entry || !changeable(entry)) return false;
|
|
647
|
+
if (entry.kind === "create") {
|
|
648
|
+
for (const reply of drafts.values()) if (reply.kind === "reply" && reply.input.threadId === id) drafts.delete(reply.id);
|
|
649
|
+
}
|
|
650
|
+
return drafts.delete(id);
|
|
651
|
+
});
|
|
652
|
+
if (removed) {
|
|
653
|
+
await changed();
|
|
654
|
+
kick();
|
|
655
|
+
}
|
|
656
|
+
return removed;
|
|
657
|
+
},
|
|
658
|
+
async edit(id, body, mentions) {
|
|
659
|
+
const edited = await update((drafts) => {
|
|
660
|
+
const entry = drafts.get(id);
|
|
661
|
+
if (!entry || !changeable(entry)) return false;
|
|
662
|
+
const fields = {
|
|
663
|
+
body,
|
|
664
|
+
...mentions ? { mentions } : {}
|
|
665
|
+
};
|
|
666
|
+
if (entry.kind === "create") drafts.set(id, {
|
|
667
|
+
...entry,
|
|
668
|
+
input: {
|
|
669
|
+
...entry.input,
|
|
670
|
+
...fields
|
|
671
|
+
},
|
|
672
|
+
status: "pending",
|
|
673
|
+
error: void 0
|
|
674
|
+
});
|
|
675
|
+
else drafts.set(id, {
|
|
676
|
+
...entry,
|
|
677
|
+
input: {
|
|
678
|
+
...entry.input,
|
|
679
|
+
...fields
|
|
680
|
+
},
|
|
681
|
+
status: "pending",
|
|
682
|
+
error: void 0
|
|
683
|
+
});
|
|
684
|
+
return true;
|
|
685
|
+
});
|
|
686
|
+
if (edited) {
|
|
687
|
+
await changed();
|
|
688
|
+
kick();
|
|
689
|
+
}
|
|
690
|
+
return edited;
|
|
691
|
+
},
|
|
692
|
+
async clear() {
|
|
693
|
+
cancelWait?.();
|
|
694
|
+
cancelWait = void 0;
|
|
695
|
+
await update((drafts) => drafts.clear());
|
|
696
|
+
sent.clear();
|
|
697
|
+
receipts.clear();
|
|
698
|
+
await changed();
|
|
699
|
+
},
|
|
700
|
+
start() {
|
|
701
|
+
const stops = [environment.listen((event) => {
|
|
702
|
+
if (event !== "online" && event !== "visible") return;
|
|
703
|
+
update((drafts) => {
|
|
704
|
+
for (const entry of drafts.values()) if (entry.status === "pending" && entry.retryAt !== void 0) drafts.set(entry.id, {
|
|
705
|
+
...entry,
|
|
706
|
+
retryAt: void 0
|
|
707
|
+
});
|
|
708
|
+
}).then(changed).then(kick);
|
|
709
|
+
}), environment.receive((message) => {
|
|
710
|
+
if (message.account !== account) return;
|
|
711
|
+
if (message.type === "sent") {
|
|
712
|
+
if (message.kind === "create") sent.set(message.id, message.thread);
|
|
713
|
+
receipts.set(message.key, message.thread);
|
|
714
|
+
host.upsert(message.thread);
|
|
715
|
+
}
|
|
716
|
+
reload();
|
|
717
|
+
})];
|
|
718
|
+
reload();
|
|
719
|
+
kick();
|
|
720
|
+
return () => {
|
|
721
|
+
for (const stop of stops.splice(0)) stop();
|
|
722
|
+
cancelWait?.();
|
|
723
|
+
cancelWait = void 0;
|
|
724
|
+
leading?.giveUp();
|
|
725
|
+
leading = void 0;
|
|
726
|
+
};
|
|
727
|
+
}
|
|
728
|
+
};
|
|
729
|
+
}
|
|
730
|
+
//#endregion
|
|
6
731
|
//#region src/client-anchor.ts
|
|
7
732
|
const defaultContext = 32;
|
|
8
733
|
const clamp = (value) => Math.min(1e3, Math.max(0, value));
|
|
@@ -407,16 +1132,16 @@ function resolveAnchor(anchor, root = document) {
|
|
|
407
1132
|
}
|
|
408
1133
|
//#endregion
|
|
409
1134
|
//#region src/rest-transport.ts
|
|
410
|
-
/** The URL of an attachment key below the Comments API, such as `/
|
|
1135
|
+
/** The URL of an attachment key below the Comments API, such as `/_vitehub/comments/attachments/comments/<id>/shot.png`. */
|
|
411
1136
|
function commentAttachmentUrl(key, base = "") {
|
|
412
1137
|
return `${base.replace(/\/+$/, "")}${commentsRoute}/attachments/${key.split("/").map((part) => encodeURIComponent(part)).join("/")}`;
|
|
413
1138
|
}
|
|
414
1139
|
/**
|
|
415
|
-
* Requests to the Comments API at `<base>/
|
|
1140
|
+
* Requests to the Comments API at `<base>/_vitehub/comments`, with the guest headers and the session cookie. A
|
|
416
1141
|
* failure throws a diagnostic with the `code`, `why`, and `fix` of the error object of the answer, and its `status`.
|
|
417
1142
|
*/
|
|
418
1143
|
function createCommentsRestRequest(options = {}) {
|
|
419
|
-
const route = options.route ?? `${(options.base ?? "").replace(/\/+$/, "")}/
|
|
1144
|
+
const route = options.route ?? `${(options.base ?? "").replace(/\/+$/, "")}/_vitehub/comments`;
|
|
420
1145
|
return async (path, init = {}) => {
|
|
421
1146
|
const send = options.fetch ?? globalThis.fetch;
|
|
422
1147
|
const headers = new Headers(typeof options.headers === "function" ? await options.headers() : options.headers);
|
|
@@ -439,13 +1164,15 @@ function createCommentsRestRequest(options = {}) {
|
|
|
439
1164
|
throw commentsUnansweredError(0, cause);
|
|
440
1165
|
}
|
|
441
1166
|
const value = await response.json().catch(() => void 0);
|
|
1167
|
+
const retryAfter = retryAfterOf(response);
|
|
442
1168
|
if (isRecordOrFunction(value) && isRecordOrFunction(value.error)) {
|
|
443
1169
|
const { code, why, fix } = value.error;
|
|
444
|
-
if (typeof code !== "string" || typeof why !== "string") throw commentsUnansweredError(response.status);
|
|
1170
|
+
if (typeof code !== "string" || typeof why !== "string") throw commentsUnansweredError(response.status, void 0, retryAfter);
|
|
445
1171
|
throw commentsAnswerError({
|
|
446
1172
|
code,
|
|
447
1173
|
why,
|
|
448
1174
|
...typeof fix === "string" ? { fix } : {},
|
|
1175
|
+
...retryAfter === void 0 ? {} : { retryAfter },
|
|
449
1176
|
status: response.status
|
|
450
1177
|
});
|
|
451
1178
|
}
|
|
@@ -458,7 +1185,7 @@ function createCommentsRestRequest(options = {}) {
|
|
|
458
1185
|
options.onAnswer?.(answer);
|
|
459
1186
|
return answer;
|
|
460
1187
|
}
|
|
461
|
-
throw commentsUnansweredError(response.status);
|
|
1188
|
+
throw commentsUnansweredError(response.status, void 0, retryAfter);
|
|
462
1189
|
};
|
|
463
1190
|
}
|
|
464
1191
|
function listSearch(query) {
|
|
@@ -480,12 +1207,13 @@ function listSearch(query) {
|
|
|
480
1207
|
return params;
|
|
481
1208
|
}
|
|
482
1209
|
const thread = (id) => `/threads/${encodeURIComponent(id)}`;
|
|
483
|
-
const post = (body) => ({
|
|
1210
|
+
const post = (body, account) => ({
|
|
484
1211
|
method: "POST",
|
|
485
|
-
...body === void 0 ? {} : { body: JSON.stringify(body) }
|
|
1212
|
+
...body === void 0 ? {} : { body: JSON.stringify(body) },
|
|
1213
|
+
...account === void 0 ? {} : { headers: { [outboxAccountHeader]: encodeURIComponent(account) } }
|
|
486
1214
|
});
|
|
487
1215
|
/**
|
|
488
|
-
* A transport over the Comments API at `<base>/
|
|
1216
|
+
* A transport over the Comments API at `<base>/_vitehub/comments`: the routes that the overlay uses. The server
|
|
489
1217
|
* runs the `authorize` policy of `server/comments.ts` for the user of the request: the app session, or a guest that
|
|
490
1218
|
* `guest` names. Guests and share-link readers can use it. It has no
|
|
491
1219
|
* `subscribe`: the client refreshes when the window gets focus.
|
|
@@ -493,6 +1221,7 @@ const post = (body) => ({
|
|
|
493
1221
|
function createRestTransport(options = {}) {
|
|
494
1222
|
const request = createCommentsRestRequest(options);
|
|
495
1223
|
const base = (options.base ?? "").replace(/\/+$/, "");
|
|
1224
|
+
const upload = async (input, account) => (await request("/attachments", post(input, account))).data;
|
|
496
1225
|
return {
|
|
497
1226
|
async list(query) {
|
|
498
1227
|
return (await request(`/threads?${listSearch(query)}`)).data;
|
|
@@ -501,11 +1230,19 @@ function createRestTransport(options = {}) {
|
|
|
501
1230
|
const path = "id" in input ? thread(input.id) : `/scopes/${encodeURIComponent(input.scope)}/threads/${input.n}`;
|
|
502
1231
|
return (await request(path)).data;
|
|
503
1232
|
},
|
|
504
|
-
async create(input) {
|
|
505
|
-
|
|
1233
|
+
async create({ files, outbox, ...input }) {
|
|
1234
|
+
const body = await withCommentFiles(input, {
|
|
1235
|
+
files,
|
|
1236
|
+
outbox
|
|
1237
|
+
}, input.subject, (data) => upload(data, outbox?.account));
|
|
1238
|
+
return (await request("/threads", post(body, outbox?.account))).data;
|
|
506
1239
|
},
|
|
507
|
-
async reply({ threadId, ...input }) {
|
|
508
|
-
const
|
|
1240
|
+
async reply({ threadId, subject, files, outbox, ...input }) {
|
|
1241
|
+
const body = await withCommentFiles(input, {
|
|
1242
|
+
files,
|
|
1243
|
+
outbox
|
|
1244
|
+
}, subject, (data) => upload(data, outbox?.account));
|
|
1245
|
+
const answer = await request(`${thread(threadId)}/comments`, post(body, outbox?.account));
|
|
509
1246
|
if (!answer.thread) throw commentsUnansweredError(200);
|
|
510
1247
|
return answer.thread;
|
|
511
1248
|
},
|
|
@@ -535,15 +1272,12 @@ function createRestTransport(options = {}) {
|
|
|
535
1272
|
}
|
|
536
1273
|
return (await request(`/people?${params}`)).data;
|
|
537
1274
|
},
|
|
538
|
-
|
|
539
|
-
return (await request("/attachments", post(input))).data;
|
|
540
|
-
},
|
|
1275
|
+
upload: (input) => upload(input),
|
|
541
1276
|
attachmentUrl: (key) => commentAttachmentUrl(key, base)
|
|
542
1277
|
};
|
|
543
1278
|
}
|
|
544
1279
|
//#endregion
|
|
545
1280
|
//#region src/client.ts
|
|
546
|
-
/** A cache and a change signal over a transport. It has no framework in it. */
|
|
547
1281
|
function createCommentsClient(options) {
|
|
548
1282
|
const { transport } = options;
|
|
549
1283
|
const listeners = /* @__PURE__ */ new Set();
|
|
@@ -560,7 +1294,7 @@ function createCommentsClient(options) {
|
|
|
560
1294
|
const notify = () => {
|
|
561
1295
|
for (const listener of listeners) listener();
|
|
562
1296
|
};
|
|
563
|
-
const matchesQuery = (thread) => {
|
|
1297
|
+
const matchesQuery = (thread, waiting = false) => {
|
|
564
1298
|
const user = options.user;
|
|
565
1299
|
if (query.subject && (thread.subject.kind !== query.subject.kind || thread.subject.id !== query.subject.id)) return false;
|
|
566
1300
|
if (query.subjectKind !== void 0 && thread.subject.kind !== query.subjectKind) return false;
|
|
@@ -569,11 +1303,17 @@ function createCommentsClient(options) {
|
|
|
569
1303
|
if (query.status && thread.status !== query.status) return false;
|
|
570
1304
|
if (user && query.mine && thread.author.id !== user.id) return false;
|
|
571
1305
|
if (user && query.mentions && !thread.messages.some((message) => message.mentions.includes(user.id))) return false;
|
|
1306
|
+
if (waiting) return !(query.mentions && !user);
|
|
572
1307
|
if (transportOwnsMembership() && !listed.has(thread.id)) return false;
|
|
573
1308
|
return true;
|
|
574
1309
|
};
|
|
575
1310
|
const publish = () => {
|
|
576
|
-
|
|
1311
|
+
const stored = [...byId.values()];
|
|
1312
|
+
const { server, local } = outbox ? outbox.overlay(stored) : {
|
|
1313
|
+
server: stored,
|
|
1314
|
+
local: []
|
|
1315
|
+
};
|
|
1316
|
+
threads = [...server.filter((thread) => matchesQuery(thread)).sort((a, b) => a.n - b.n), ...local.filter((thread) => matchesQuery(thread, true))];
|
|
577
1317
|
notify();
|
|
578
1318
|
};
|
|
579
1319
|
const upsert = (thread) => {
|
|
@@ -668,6 +1408,22 @@ function createCommentsClient(options) {
|
|
|
668
1408
|
}
|
|
669
1409
|
return threads;
|
|
670
1410
|
};
|
|
1411
|
+
/** The thread as the client shows it, with what waits in the outbox, also when the query leaves it out. */
|
|
1412
|
+
const shown = (threadId) => {
|
|
1413
|
+
const stored = byId.get(threadId);
|
|
1414
|
+
const { server, local } = outbox ? outbox.overlay(stored ? [stored] : []) : {
|
|
1415
|
+
server: stored ? [stored] : [],
|
|
1416
|
+
local: []
|
|
1417
|
+
};
|
|
1418
|
+
return server[0] ?? local.find((thread) => thread.id === threadId) ?? null;
|
|
1419
|
+
};
|
|
1420
|
+
const outbox = options.outbox && createOutbox(options.outbox, {
|
|
1421
|
+
transport,
|
|
1422
|
+
user: options.user,
|
|
1423
|
+
threads: () => byId.values(),
|
|
1424
|
+
upsert: (thread) => void upsert(thread),
|
|
1425
|
+
changed: publish
|
|
1426
|
+
});
|
|
671
1427
|
const refreshQuietly = () => {
|
|
672
1428
|
refresh().catch(() => void 0);
|
|
673
1429
|
};
|
|
@@ -701,19 +1457,37 @@ function createCommentsClient(options) {
|
|
|
701
1457
|
publish();
|
|
702
1458
|
},
|
|
703
1459
|
refresh,
|
|
704
|
-
get: (input) =>
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
1460
|
+
get: async (input) => {
|
|
1461
|
+
const thread = await transport.get(input);
|
|
1462
|
+
if (!thread || !outbox) return thread;
|
|
1463
|
+
upsert(thread);
|
|
1464
|
+
return shown(thread.id) ?? thread;
|
|
1465
|
+
},
|
|
1466
|
+
create: async (input) => outbox ? outbox.create(input) : upsert(await transport.create(input)),
|
|
1467
|
+
reply: (input) => outbox ? outbox.reply(input) : mutate(input.threadId, async () => upsert(await transport.reply(input))),
|
|
1468
|
+
edit: async (input) => {
|
|
1469
|
+
if (outbox?.owns(input.threadId, input.messageId)) {
|
|
1470
|
+
if (!await outbox.edit(input.messageId, input.body, input.mentions)) throw commentsClientDiagnostics.COMMENTS_OUTBOX_SENDING();
|
|
1471
|
+
return shown(input.threadId);
|
|
1472
|
+
}
|
|
1473
|
+
return mutate(input.threadId, async () => upsert(await transport.edit(input)));
|
|
1474
|
+
},
|
|
1475
|
+
delete: async (input) => {
|
|
1476
|
+
if (outbox?.owns(input.threadId, input.messageId)) {
|
|
1477
|
+
if (!await outbox.dismiss(input.messageId ?? input.threadId)) throw commentsClientDiagnostics.COMMENTS_OUTBOX_SENDING();
|
|
1478
|
+
return shown(input.threadId);
|
|
1479
|
+
}
|
|
1480
|
+
return mutate(input.threadId, async () => {
|
|
1481
|
+
const next = await transport.delete(input);
|
|
1482
|
+
if (next) upsert(next);
|
|
1483
|
+
else drop(input.threadId);
|
|
1484
|
+
return next;
|
|
1485
|
+
});
|
|
1486
|
+
},
|
|
714
1487
|
resolve: (input) => mutate(input.threadId, async () => upsert(await transport.resolve(input))),
|
|
715
1488
|
reopen: (input) => mutate(input.threadId, async () => upsert(await transport.reopen(input))),
|
|
716
1489
|
users: (input) => transport.users(input),
|
|
1490
|
+
outbox,
|
|
717
1491
|
onChange(listener) {
|
|
718
1492
|
listeners.add(listener);
|
|
719
1493
|
return () => {
|
|
@@ -727,6 +1501,7 @@ function createCommentsClient(options) {
|
|
|
727
1501
|
window.addEventListener("focus", refreshQuietly);
|
|
728
1502
|
stops.push(() => window.removeEventListener("focus", refreshQuietly));
|
|
729
1503
|
}
|
|
1504
|
+
if (outbox) stops.push(outbox.start());
|
|
730
1505
|
refreshQuietly();
|
|
731
1506
|
return () => {
|
|
732
1507
|
for (const stop of stops.splice(0)) stop();
|
|
@@ -735,4 +1510,4 @@ function createCommentsClient(options) {
|
|
|
735
1510
|
};
|
|
736
1511
|
}
|
|
737
1512
|
//#endregion
|
|
738
|
-
export { anchorRange, commentAttachmentUrl, commentsErrorDiagnostics, createAnchor, createCommentsClient, createCommentsRestRequest, createRestTransport, foldName, resolveAnchor, selectorFor };
|
|
1513
|
+
export { anchorRange, commentAttachmentUrl, commentsErrorDiagnostics, createAnchor, createCommentsClient, createCommentsRestRequest, createIndexedDBOutboxStore, createMemoryOutboxStore, createRestTransport, foldName, replyMessageId, resolveAnchor, selectorFor };
|