@jant/core 0.8.1 → 0.9.0
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/dist/{app--uxnaIRV.js → app-BzH8jmh3.js} +1199 -262
- package/dist/client/.vite/manifest.json +2 -2
- package/dist/client/_assets/{client-BT6kpcuG.js → client-S8oryJ0m.js} +1 -1
- package/dist/client/_assets/{client-auth-begiT1tq.js → client-auth-Ds9G0IgR.js} +1 -1
- package/dist/index.js +1 -1
- package/dist/node.js +2 -2
- package/package.json +1 -1
- package/src/__tests__/helpers/cursor-walk.ts +37 -7
- package/src/app.tsx +4 -0
- package/src/lib/__tests__/thread-fold.test.ts +43 -0
- package/src/lib/api-posts.ts +110 -2
- package/src/lib/api-public-posts.ts +263 -0
- package/src/lib/api-threads.ts +207 -0
- package/src/lib/thread-fold.ts +83 -41
- package/src/lib/thread-query.ts +143 -0
- package/src/middleware/cors.ts +8 -1
- package/src/middleware/deprecation.ts +48 -0
- package/src/routes/api/__tests__/mcp.test.ts +89 -0
- package/src/routes/api/__tests__/posts.test.ts +78 -0
- package/src/routes/api/__tests__/public-access.test.ts +5 -0
- package/src/routes/api/__tests__/threads.test.ts +115 -0
- package/src/routes/api/posts.ts +15 -104
- package/src/routes/api/public/__tests__/threads.test.ts +363 -0
- package/src/routes/api/public/archive.ts +82 -81
- package/src/routes/api/public/posts.ts +97 -298
- package/src/routes/api/public/threads.ts +141 -0
- package/src/routes/api/threads.ts +128 -0
- package/src/services/__tests__/thread.test.ts +495 -0
- package/src/services/index.ts +4 -0
- package/src/services/mcp.ts +208 -53
- package/src/services/post.ts +222 -43
- package/src/services/thread.ts +392 -0
- package/src/types/constants.ts +25 -0
- package/src/ui/feed/ThreadPreview.tsx +9 -14
- package/src/ui/feed/__tests__/thread-preview.test.ts +18 -22
- package/src/ui/feed/thread-preview-state.ts +0 -35
|
@@ -0,0 +1,363 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { createTestApp } from "../../../../__tests__/helpers/app.js";
|
|
3
|
+
import { enforceD1BoundParameterLimit } from "../../../../__tests__/helpers/db.js";
|
|
4
|
+
import {
|
|
5
|
+
walkPostPages,
|
|
6
|
+
walkThreadPages,
|
|
7
|
+
} from "../../../../__tests__/helpers/cursor-walk.js";
|
|
8
|
+
import type { CreatePost, Post } from "../../../../types.js";
|
|
9
|
+
import { publicArchiveApiRoutes } from "../archive.js";
|
|
10
|
+
import { publicPostsApiRoutes } from "../posts.js";
|
|
11
|
+
import { publicThreadsApiRoutes } from "../threads.js";
|
|
12
|
+
|
|
13
|
+
const DAY = 86_400;
|
|
14
|
+
const BASE = Date.UTC(2025, 0, 1) / 1000;
|
|
15
|
+
const day = (n: number) => BASE + n * DAY;
|
|
16
|
+
|
|
17
|
+
function setup() {
|
|
18
|
+
const testApp = createTestApp({ authenticated: false });
|
|
19
|
+
testApp.app.route("/api/public/threads", publicThreadsApiRoutes);
|
|
20
|
+
testApp.app.route("/api/public/posts", publicPostsApiRoutes);
|
|
21
|
+
testApp.app.route("/api/public/archive", publicArchiveApiRoutes);
|
|
22
|
+
const { services } = testApp;
|
|
23
|
+
const post = (data: Partial<CreatePost>) =>
|
|
24
|
+
services.posts.create({ format: "note", bodyMarkdown: "body", ...data });
|
|
25
|
+
/** A root and `replies` replies, one a day, each created when published. */
|
|
26
|
+
const thread = async (replies: number, root: Partial<CreatePost> = {}) => {
|
|
27
|
+
const first = await post({
|
|
28
|
+
publishedAt: day(0),
|
|
29
|
+
createdAt: day(0),
|
|
30
|
+
...root,
|
|
31
|
+
});
|
|
32
|
+
const all: Post[] = [first];
|
|
33
|
+
let parent = first;
|
|
34
|
+
for (let i = 1; i <= replies; i++) {
|
|
35
|
+
parent = await post({
|
|
36
|
+
replyToId: parent.id,
|
|
37
|
+
publishedAt: day(i),
|
|
38
|
+
createdAt: day(i),
|
|
39
|
+
});
|
|
40
|
+
all.push(parent);
|
|
41
|
+
}
|
|
42
|
+
return all;
|
|
43
|
+
};
|
|
44
|
+
const get = async (path: string) => {
|
|
45
|
+
const res = await testApp.app.request(path);
|
|
46
|
+
return { status: res.status, headers: res.headers, body: await res.json() };
|
|
47
|
+
};
|
|
48
|
+
return { ...testApp, post, thread, get };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const ids = (items: Array<{ id: string }>) => items.map((item) => item.id);
|
|
52
|
+
|
|
53
|
+
describe("GET /api/public/threads", () => {
|
|
54
|
+
it("lists what the homepage lists, as Thread objects", async () => {
|
|
55
|
+
const { post, thread, get } = setup();
|
|
56
|
+
const [root, reply] = await thread(1);
|
|
57
|
+
const lone = await post({ publishedAt: day(5) });
|
|
58
|
+
await post({ publishedAt: day(6), visibility: "latest_hidden" });
|
|
59
|
+
await post({ publishedAt: day(7), visibility: "private" });
|
|
60
|
+
|
|
61
|
+
const { status, body } = await get("/api/public/threads");
|
|
62
|
+
expect(status).toBe(200);
|
|
63
|
+
expect(ids(body.threads)).toEqual([lone.id, root?.id]);
|
|
64
|
+
expect(body.nextCursor).toBeNull();
|
|
65
|
+
|
|
66
|
+
const [loneThread, rootThread] = body.threads;
|
|
67
|
+
expect(rootThread).toMatchObject({
|
|
68
|
+
id: root?.id,
|
|
69
|
+
postCount: 2,
|
|
70
|
+
lastActivityAt: reply?.publishedAt,
|
|
71
|
+
root: { id: root?.id, threadPostCount: 2, permalink: `/${root?.slug}` },
|
|
72
|
+
});
|
|
73
|
+
expect(rootThread.fold).toBeUndefined();
|
|
74
|
+
expect(loneThread.postCount).toBe(1);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("attaches the homepage fold on request", async () => {
|
|
78
|
+
const { post, thread, get } = setup();
|
|
79
|
+
const all = await thread(8);
|
|
80
|
+
const lone = await post({ publishedAt: day(1) });
|
|
81
|
+
|
|
82
|
+
const { body } = await get("/api/public/threads?include=fold&sort=oldest");
|
|
83
|
+
const [long, single] = body.threads;
|
|
84
|
+
expect(long.id).toBe(all[0]?.id);
|
|
85
|
+
expect(ids(long.fold.leading)).toEqual(ids(all.slice(1, 3)));
|
|
86
|
+
expect(long.fold.hidden).toBe(3);
|
|
87
|
+
expect(long.fold.gap).toEqual({
|
|
88
|
+
id: all[3]?.id,
|
|
89
|
+
slug: all[3]?.slug,
|
|
90
|
+
permalink: `/${all[3]?.slug}`,
|
|
91
|
+
});
|
|
92
|
+
// Oldest first, the newest reply last.
|
|
93
|
+
expect(ids(long.fold.trailing)).toEqual(ids(all.slice(6)));
|
|
94
|
+
expect(long.fold.trailing[2].threadPostCount).toBe(9);
|
|
95
|
+
|
|
96
|
+
expect(single.id).toBe(lone.id);
|
|
97
|
+
expect(single.fold).toEqual({
|
|
98
|
+
leading: [],
|
|
99
|
+
hidden: 0,
|
|
100
|
+
gap: null,
|
|
101
|
+
trailing: [],
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("opens the run a fold hides with one request", async () => {
|
|
106
|
+
const { thread, get, app } = setup();
|
|
107
|
+
const all = await thread(10);
|
|
108
|
+
const {
|
|
109
|
+
body: { threads },
|
|
110
|
+
} = await get("/api/public/threads?include=fold");
|
|
111
|
+
const fold = threads[0].fold;
|
|
112
|
+
const lastLeading = fold.leading[fold.leading.length - 1].id;
|
|
113
|
+
|
|
114
|
+
const { body } = await get(
|
|
115
|
+
`/api/public/threads/${all[0]?.slug}/posts?cursor=${lastLeading}&limit=${fold.hidden}`,
|
|
116
|
+
);
|
|
117
|
+
expect(body.posts[0].id).toBe(fold.gap.id);
|
|
118
|
+
expect(ids(body.posts)).toEqual(ids(all.slice(3, 3 + fold.hidden)));
|
|
119
|
+
|
|
120
|
+
expect(
|
|
121
|
+
await walkPostPages(app, `/api/public/threads/${all[0]?.slug}/posts`, 3),
|
|
122
|
+
).toEqual(ids(all));
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it("widens to every visibility a reader may see with visibility=any", async () => {
|
|
126
|
+
const { post, get } = setup();
|
|
127
|
+
const shown = await post({ publishedAt: day(1) });
|
|
128
|
+
const hidden = await post({
|
|
129
|
+
publishedAt: day(2),
|
|
130
|
+
visibility: "latest_hidden",
|
|
131
|
+
});
|
|
132
|
+
await post({ publishedAt: day(3), visibility: "private" });
|
|
133
|
+
|
|
134
|
+
const any = await get("/api/public/threads?visibility=any");
|
|
135
|
+
expect(ids(any.body.threads)).toEqual([hidden.id, shown.id]);
|
|
136
|
+
const only = await get("/api/public/threads?visibility=hidden");
|
|
137
|
+
expect(ids(only.body.threads)).toEqual([hidden.id]);
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it("walks a whole site in publication order", async () => {
|
|
141
|
+
const { post, thread, app } = setup();
|
|
142
|
+
const [old] = await thread(1);
|
|
143
|
+
await post({ publishedAt: day(4), visibility: "latest_hidden" });
|
|
144
|
+
await post({ publishedAt: day(3), pinned: true });
|
|
145
|
+
await post({ publishedAt: day(2) });
|
|
146
|
+
|
|
147
|
+
const full = await app.request(
|
|
148
|
+
"/api/public/threads?visibility=any&sort=published&limit=100",
|
|
149
|
+
);
|
|
150
|
+
const expected = ids((await full.json()).threads);
|
|
151
|
+
expect(expected).toHaveLength(4);
|
|
152
|
+
expect(expected[3]).toBe(old?.id);
|
|
153
|
+
expect(
|
|
154
|
+
await walkThreadPages(
|
|
155
|
+
app,
|
|
156
|
+
"/api/public/threads?visibility=any&sort=published",
|
|
157
|
+
1,
|
|
158
|
+
),
|
|
159
|
+
).toEqual(expected);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it("takes the archive's filters", async () => {
|
|
163
|
+
const { services, post, get } = setup();
|
|
164
|
+
const collection = await services.collections.create({
|
|
165
|
+
slug: "books",
|
|
166
|
+
title: "Books",
|
|
167
|
+
});
|
|
168
|
+
const titled = await post({
|
|
169
|
+
title: "A title",
|
|
170
|
+
publishedAt: day(1),
|
|
171
|
+
collectionIds: [collection.id],
|
|
172
|
+
});
|
|
173
|
+
await post({ publishedAt: day(2), collectionIds: [collection.id] });
|
|
174
|
+
await post({ title: "Elsewhere", publishedAt: day(3) });
|
|
175
|
+
|
|
176
|
+
const { body } = await get(
|
|
177
|
+
"/api/public/threads?collection=books&title=any",
|
|
178
|
+
);
|
|
179
|
+
expect(ids(body.threads)).toEqual([titled.id]);
|
|
180
|
+
const missing = await get("/api/public/threads?collection=nope");
|
|
181
|
+
expect(missing.status).toBe(200);
|
|
182
|
+
expect(missing.body).toEqual({ threads: [], nextCursor: null });
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it.each([
|
|
186
|
+
["an unknown parameter", "?sorting=published"],
|
|
187
|
+
["visibility=all", "?visibility=all"],
|
|
188
|
+
["visibility=private", "?visibility=private"],
|
|
189
|
+
["an unknown include", "?include=replies"],
|
|
190
|
+
["an unknown sort", "?sort=newest"],
|
|
191
|
+
["featured with another order", "?visibility=featured&sort=activity"],
|
|
192
|
+
])("refuses %s", async (_label, query) => {
|
|
193
|
+
const { get } = setup();
|
|
194
|
+
const { status, body } = await get(`/api/public/threads${query}`);
|
|
195
|
+
expect(status).toBe(400);
|
|
196
|
+
expect(typeof body.error).toBe("string");
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
// D1 refuses a statement with more than 100 bound parameters, and a full
|
|
200
|
+
// page with the fold asks about 100 Threads at once.
|
|
201
|
+
it("serves a full page with the fold within D1's parameter limit", async () => {
|
|
202
|
+
const { services, sqlite, get } = setup();
|
|
203
|
+
for (let i = 0; i < 101; i++) {
|
|
204
|
+
const root = await services.posts.create({
|
|
205
|
+
format: "note",
|
|
206
|
+
bodyMarkdown: `root ${i}`,
|
|
207
|
+
publishedAt: day(i),
|
|
208
|
+
});
|
|
209
|
+
if (i % 10 === 0) {
|
|
210
|
+
await services.posts.create({
|
|
211
|
+
format: "note",
|
|
212
|
+
bodyMarkdown: `reply ${i}`,
|
|
213
|
+
replyToId: root.id,
|
|
214
|
+
publishedAt: day(i) + 1,
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
enforceD1BoundParameterLimit(sqlite);
|
|
219
|
+
|
|
220
|
+
const { status, body } = await get(
|
|
221
|
+
"/api/public/threads?limit=100&include=fold",
|
|
222
|
+
);
|
|
223
|
+
expect(status).toBe(200);
|
|
224
|
+
expect(body.threads).toHaveLength(100);
|
|
225
|
+
expect(
|
|
226
|
+
body.threads.filter(
|
|
227
|
+
(thread: { fold: { trailing: unknown[] } }) =>
|
|
228
|
+
thread.fold.trailing.length === 1,
|
|
229
|
+
),
|
|
230
|
+
).toHaveLength(10);
|
|
231
|
+
expect(body.nextCursor).not.toBeNull();
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
it("returns Markdown on request", async () => {
|
|
235
|
+
const { post, get } = setup();
|
|
236
|
+
await post({ publishedAt: day(1), bodyMarkdown: "Some *text*" });
|
|
237
|
+
const { body } = await get("/api/public/threads?content=markdown");
|
|
238
|
+
expect(body.threads[0].root.bodyMarkdown).toBe("Some *text*");
|
|
239
|
+
expect(body.threads[0].root.bodyHtml).toBeUndefined();
|
|
240
|
+
});
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
describe("GET /api/public/threads/:slug", () => {
|
|
244
|
+
it("returns the Thread any of its Posts names", async () => {
|
|
245
|
+
const { thread, get } = setup();
|
|
246
|
+
const all = await thread(2);
|
|
247
|
+
const { status, body } = await get(
|
|
248
|
+
`/api/public/threads/${all[2]?.slug}?include=fold`,
|
|
249
|
+
);
|
|
250
|
+
expect(status).toBe(200);
|
|
251
|
+
expect(body).toMatchObject({ id: all[0]?.id, postCount: 3 });
|
|
252
|
+
expect(ids(body.fold.leading)).toEqual(ids(all.slice(1, 2)));
|
|
253
|
+
expect(ids(body.fold.trailing)).toEqual(ids(all.slice(2)));
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
it("finds a Thread hidden from Latest", async () => {
|
|
257
|
+
const { thread, get } = setup();
|
|
258
|
+
const [root] = await thread(0, { visibility: "latest_hidden" });
|
|
259
|
+
expect((await get(`/api/public/threads/${root?.slug}`)).status).toBe(200);
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
it("returns 404 for what a reader can't see", async () => {
|
|
263
|
+
const { services, post, thread, get } = setup();
|
|
264
|
+
const secret = await post({ publishedAt: day(1), visibility: "private" });
|
|
265
|
+
const draft = await post({ status: "draft" });
|
|
266
|
+
const all = await thread(1);
|
|
267
|
+
const unsent = await services.posts.create({
|
|
268
|
+
format: "note",
|
|
269
|
+
bodyMarkdown: "unsent",
|
|
270
|
+
replyToId: all[1]?.id,
|
|
271
|
+
status: "draft",
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
for (const slug of [secret.slug, draft.slug, unsent.slug, "missing"]) {
|
|
275
|
+
expect((await get(`/api/public/threads/${slug}`)).status).toBe(404);
|
|
276
|
+
expect((await get(`/api/public/threads/${slug}/posts`)).status).toBe(404);
|
|
277
|
+
}
|
|
278
|
+
});
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
describe("GET /api/public/threads/:slug/posts", () => {
|
|
282
|
+
it("lists published Posts in Thread order", async () => {
|
|
283
|
+
const { services, thread, get } = setup();
|
|
284
|
+
const all = await thread(3);
|
|
285
|
+
await services.posts.create({
|
|
286
|
+
format: "note",
|
|
287
|
+
bodyMarkdown: "unsent",
|
|
288
|
+
replyToId: all[3]?.id,
|
|
289
|
+
status: "draft",
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
const { body } = await get(`/api/public/threads/${all[1]?.slug}/posts`);
|
|
293
|
+
expect(ids(body.posts)).toEqual(ids(all));
|
|
294
|
+
expect(body.posts[0].threadPostCount).toBe(4);
|
|
295
|
+
expect(body.nextCursor).toBeNull();
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
it("pages a Thread longer than one page", async () => {
|
|
299
|
+
const { thread, get, sqlite } = setup();
|
|
300
|
+
const all = await thread(104);
|
|
301
|
+
enforceD1BoundParameterLimit(sqlite);
|
|
302
|
+
|
|
303
|
+
const path = `/api/public/threads/${all[0]?.slug}/posts`;
|
|
304
|
+
const first = await get(path);
|
|
305
|
+
expect(first.status).toBe(200);
|
|
306
|
+
expect(first.body.posts).toHaveLength(100);
|
|
307
|
+
expect(first.body.posts[0].threadPostCount).toBe(105);
|
|
308
|
+
const second = await get(`${path}?cursor=${first.body.nextCursor}`);
|
|
309
|
+
expect(second.body.nextCursor).toBeNull();
|
|
310
|
+
expect([...ids(first.body.posts), ...ids(second.body.posts)]).toEqual(
|
|
311
|
+
ids(all),
|
|
312
|
+
);
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
it("refuses a cursor that names a Post outside the Thread", async () => {
|
|
316
|
+
const { post, thread, get } = setup();
|
|
317
|
+
const all = await thread(1);
|
|
318
|
+
const other = await post({ publishedAt: day(3) });
|
|
319
|
+
const { status } = await get(
|
|
320
|
+
`/api/public/threads/${all[0]?.slug}/posts?cursor=${other.id}`,
|
|
321
|
+
);
|
|
322
|
+
expect(status).toBe(400);
|
|
323
|
+
});
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
describe("deprecated Thread lists", () => {
|
|
327
|
+
it.each([
|
|
328
|
+
["/api/public/posts", "</api/public/threads>"],
|
|
329
|
+
[
|
|
330
|
+
"/api/public/archive",
|
|
331
|
+
"</api/public/threads?visibility=any&sort=published>",
|
|
332
|
+
],
|
|
333
|
+
])(
|
|
334
|
+
"%s names its replacement and still answers as before",
|
|
335
|
+
async (path, successor) => {
|
|
336
|
+
const { thread, get } = setup();
|
|
337
|
+
const [root] = await thread(2);
|
|
338
|
+
|
|
339
|
+
const { status, headers, body } = await get(path);
|
|
340
|
+
expect(status).toBe(200);
|
|
341
|
+
expect(headers.get("Deprecation")).toMatch(/^@\d+$/);
|
|
342
|
+
expect(headers.get("Link")).toBe(`${successor}; rel="successor-version"`);
|
|
343
|
+
expect(ids(body.posts)).toEqual([root?.id]);
|
|
344
|
+
expect(body.posts[0].threadPostCount).toBe(3);
|
|
345
|
+
|
|
346
|
+
// Errors carry the headers too.
|
|
347
|
+
const refused = await get(`${path}?limit=0`);
|
|
348
|
+
expect(refused.status).toBe(400);
|
|
349
|
+
expect(refused.headers.get("Deprecation")).toMatch(/^@\d+$/);
|
|
350
|
+
},
|
|
351
|
+
);
|
|
352
|
+
|
|
353
|
+
it("leaves the single-post endpoint undeprecated", async () => {
|
|
354
|
+
const { thread, get } = setup();
|
|
355
|
+
const all = await thread(1);
|
|
356
|
+
const { status, headers, body } = await get(
|
|
357
|
+
`/api/public/posts/${all[1]?.slug}`,
|
|
358
|
+
);
|
|
359
|
+
expect(status).toBe(200);
|
|
360
|
+
expect(headers.get("Deprecation")).toBeNull();
|
|
361
|
+
expect(body.threadPostCount).toBe(2);
|
|
362
|
+
});
|
|
363
|
+
});
|
|
@@ -13,8 +13,12 @@ import {
|
|
|
13
13
|
toPostFilters,
|
|
14
14
|
} from "../../../lib/filter-dimensions.js";
|
|
15
15
|
import { ValidationError } from "../../../lib/errors.js";
|
|
16
|
-
import {
|
|
16
|
+
import { loadPublicPostResponses } from "../../../lib/api-public-posts.js";
|
|
17
17
|
import { requirePublicApiEnabled } from "../../../middleware/public-content-access.js";
|
|
18
|
+
import {
|
|
19
|
+
deprecated,
|
|
20
|
+
THREAD_LIST_DEPRECATED_AT,
|
|
21
|
+
} from "../../../middleware/deprecation.js";
|
|
18
22
|
|
|
19
23
|
type Env = { Bindings: Bindings; Variables: AppVariables };
|
|
20
24
|
|
|
@@ -45,90 +49,87 @@ const INVALID_VISIBILITY_MESSAGE =
|
|
|
45
49
|
value === "latest_hidden" ? "hidden" : value,
|
|
46
50
|
).join(", ");
|
|
47
51
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
52
|
+
/**
|
|
53
|
+
* Deprecated in 0.9 for `GET /api/public/threads?visibility=any&sort=published`,
|
|
54
|
+
* which takes the same filters. Answers as it always has until 1.0.1 removes it.
|
|
55
|
+
*/
|
|
56
|
+
publicArchiveApiRoutes.get(
|
|
57
|
+
"/",
|
|
58
|
+
deprecated(
|
|
59
|
+
THREAD_LIST_DEPRECATED_AT,
|
|
60
|
+
"/api/public/threads?visibility=any&sort=published",
|
|
61
|
+
),
|
|
62
|
+
async (c) => {
|
|
63
|
+
const { lang, cursor, limit, content } = parseValidated(
|
|
64
|
+
ListPublicArchiveQuerySchema,
|
|
65
|
+
c.req.query(),
|
|
66
|
+
);
|
|
55
67
|
|
|
56
|
-
|
|
57
|
-
// the caller named one.
|
|
58
|
-
const slugs = readCollectionSlugs(read);
|
|
59
|
-
const ctx: DimensionContext = {
|
|
60
|
-
collections:
|
|
61
|
-
slugs.length > 0
|
|
62
|
-
? buildCollectionVocabulary(await c.var.services.collections.list())
|
|
63
|
-
: EMPTY_COLLECTION_VOCABULARY,
|
|
64
|
-
};
|
|
68
|
+
const read = (key: string): string | undefined => c.req.query(key);
|
|
65
69
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
ctx
|
|
70
|
-
|
|
71
|
-
|
|
70
|
+
// Resolving a collection slug costs a round trip, so it is paid for only when
|
|
71
|
+
// the caller named one.
|
|
72
|
+
const slugs = readCollectionSlugs(read);
|
|
73
|
+
const ctx: DimensionContext = {
|
|
74
|
+
collections:
|
|
75
|
+
slugs.length > 0
|
|
76
|
+
? buildCollectionVocabulary(await c.var.services.collections.list())
|
|
77
|
+
: EMPTY_COLLECTION_VOCABULARY,
|
|
78
|
+
};
|
|
72
79
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
) {
|
|
80
|
-
return c.json({ posts: [], nextCursor: null });
|
|
81
|
-
}
|
|
82
|
-
if (!parsed.ok) {
|
|
83
|
-
const first = parsed.issues[0];
|
|
84
|
-
throw new ValidationError(first?.message ?? "Validation failed");
|
|
85
|
-
}
|
|
80
|
+
const parsed = parsePostFilterSelectionStrict(
|
|
81
|
+
read,
|
|
82
|
+
Object.keys(c.req.query()),
|
|
83
|
+
ctx,
|
|
84
|
+
{ allow: ENDPOINT_PARAMS },
|
|
85
|
+
);
|
|
86
86
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
87
|
+
// A collection slug that names nothing is answered as an empty result rather
|
|
88
|
+
// than a rejection: the caller's words were understood, and this site simply
|
|
89
|
+
// holds no such collection.
|
|
90
|
+
if (
|
|
91
|
+
!parsed.ok &&
|
|
92
|
+
parsed.issues.every((issue) => issue.param === "collection")
|
|
93
|
+
) {
|
|
94
|
+
return c.json({ posts: [], nextCursor: null });
|
|
95
|
+
}
|
|
96
|
+
if (!parsed.ok) {
|
|
97
|
+
const first = parsed.issues[0];
|
|
98
|
+
throw new ValidationError(first?.message ?? "Validation failed");
|
|
99
|
+
}
|
|
97
100
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
// and so does the cursor.
|
|
109
|
-
...toPostFilters(parsed.selection, { yearAxis: "published" }),
|
|
110
|
-
sortBy: "published",
|
|
111
|
-
ignorePinnedSort: true,
|
|
112
|
-
},
|
|
113
|
-
{ cursor, limit },
|
|
114
|
-
);
|
|
101
|
+
// `private` names a set no anonymous caller may see. Rejected rather than
|
|
102
|
+
// dropped: silently widening the result to the whole archive would answer a
|
|
103
|
+
// different question under the caller's own words.
|
|
104
|
+
const visibility = parsed.selection.visibility;
|
|
105
|
+
if (
|
|
106
|
+
visibility !== undefined &&
|
|
107
|
+
!(PUBLIC_ARCHIVE_VISIBILITIES as readonly string[]).includes(visibility)
|
|
108
|
+
) {
|
|
109
|
+
throw new ValidationError(INVALID_VISIBILITY_MESSAGE);
|
|
110
|
+
}
|
|
115
111
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
112
|
+
const { posts, nextCursor } = await c.var.services.posts.listPage(
|
|
113
|
+
{
|
|
114
|
+
lang,
|
|
115
|
+
status: "published",
|
|
116
|
+
excludePrivate: true,
|
|
117
|
+
excludeLatestHidden: false,
|
|
118
|
+
excludeReplies: true,
|
|
119
|
+
// Newest-published-first, the page's default order. Named explicitly:
|
|
120
|
+
// left unset, a published list sorts by activity, and a reply would
|
|
121
|
+
// lift an old root to the top. The year filter reads the same column,
|
|
122
|
+
// and so does the cursor.
|
|
123
|
+
...toPostFilters(parsed.selection, { yearAxis: "published" }),
|
|
124
|
+
sortBy: "published",
|
|
125
|
+
ignorePinnedSort: true,
|
|
126
|
+
},
|
|
127
|
+
{ cursor, limit },
|
|
128
|
+
);
|
|
121
129
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
c.var.appConfig,
|
|
129
|
-
{ content },
|
|
130
|
-
),
|
|
131
|
-
),
|
|
132
|
-
nextCursor,
|
|
133
|
-
});
|
|
134
|
-
});
|
|
130
|
+
return c.json({
|
|
131
|
+
posts: await loadPublicPostResponses(c.var, posts, { content }),
|
|
132
|
+
nextCursor,
|
|
133
|
+
});
|
|
134
|
+
},
|
|
135
|
+
);
|