@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
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { Hono } from "hono";
|
|
2
2
|
import { z } from "zod";
|
|
3
|
-
import type { Bindings,
|
|
3
|
+
import type { Bindings, Post } from "../../../types.js";
|
|
4
4
|
import type { AppVariables } from "../../../types/app-context.js";
|
|
5
|
-
import { getCollectionPagePath } from "../../../lib/collection-paths.js";
|
|
6
5
|
import {
|
|
7
6
|
CollectionSortOrderSchema,
|
|
8
7
|
ContentLanguageSchema,
|
|
@@ -10,19 +9,16 @@ import {
|
|
|
10
9
|
parseValidated,
|
|
11
10
|
} from "../../../lib/schemas.js";
|
|
12
11
|
import { NotFoundError } from "../../../lib/errors.js";
|
|
13
|
-
import {
|
|
14
|
-
import { tiptapJsonToMarkdown } from "../../../lib/tiptap-to-markdown.js";
|
|
15
|
-
import { toPublicPath } from "../../../lib/url.js";
|
|
16
|
-
import {
|
|
17
|
-
getImageUrl,
|
|
18
|
-
getMediaUrl,
|
|
19
|
-
getPublicUrlForProvider,
|
|
20
|
-
} from "../../../lib/image.js";
|
|
12
|
+
import { loadPublicPostResponses } from "../../../lib/api-public-posts.js";
|
|
21
13
|
import {
|
|
22
14
|
resolveCollectionSortOrder,
|
|
23
15
|
supportsCollectionRatingSort,
|
|
24
16
|
} from "../../../lib/collection-sort.js";
|
|
25
17
|
import { requirePublicApiEnabled } from "../../../middleware/public-content-access.js";
|
|
18
|
+
import {
|
|
19
|
+
deprecated,
|
|
20
|
+
THREAD_LIST_DEPRECATED_AT,
|
|
21
|
+
} from "../../../middleware/deprecation.js";
|
|
26
22
|
|
|
27
23
|
type Env = { Bindings: Bindings; Variables: AppVariables };
|
|
28
24
|
|
|
@@ -48,85 +44,6 @@ const PublicPostContentQuerySchema = z.object({
|
|
|
48
44
|
content: z.enum(["markdown"]).optional(),
|
|
49
45
|
});
|
|
50
46
|
|
|
51
|
-
export type PublicPostBaseResponse = {
|
|
52
|
-
id: string;
|
|
53
|
-
format: Post["format"];
|
|
54
|
-
status: "published";
|
|
55
|
-
visibility: Post["visibility"];
|
|
56
|
-
slug: string;
|
|
57
|
-
permalink: string;
|
|
58
|
-
title?: string | null;
|
|
59
|
-
url?: string | null;
|
|
60
|
-
sourceName?: string | null;
|
|
61
|
-
sourceUrl?: string | null;
|
|
62
|
-
quoteText: string | null;
|
|
63
|
-
summary: string | null;
|
|
64
|
-
rating: number | null;
|
|
65
|
-
previewKind: string | null;
|
|
66
|
-
previewProvider: string | null;
|
|
67
|
-
previewImageUrl: string | null;
|
|
68
|
-
replyToId: string | null;
|
|
69
|
-
threadId: string;
|
|
70
|
-
/**
|
|
71
|
-
* BCP 47 content language, or null on a post written before the site
|
|
72
|
-
* enabled multilingual content.
|
|
73
|
-
*/
|
|
74
|
-
language: string | null;
|
|
75
|
-
/**
|
|
76
|
-
* Shared by every translation of this Thread. Null when the Thread has no
|
|
77
|
-
* translations.
|
|
78
|
-
*/
|
|
79
|
-
translationGroupId: string | null;
|
|
80
|
-
/** Reply published without announcing its Thread. Always false on roots. */
|
|
81
|
-
quietReply: boolean;
|
|
82
|
-
pinnedAt: number | null;
|
|
83
|
-
featuredAt: number | null;
|
|
84
|
-
publishedAt: number | null;
|
|
85
|
-
/** Root only: newest post in the Thread, quiet replies excluded. */
|
|
86
|
-
lastActivityAt: number;
|
|
87
|
-
/** Root only: newest post in the Thread, quiet replies included. */
|
|
88
|
-
threadUpdatedAt: number;
|
|
89
|
-
createdAt: number;
|
|
90
|
-
updatedAt: number;
|
|
91
|
-
attachments: ReturnType<typeof toApiAttachment>[];
|
|
92
|
-
collections: {
|
|
93
|
-
id: string;
|
|
94
|
-
slug: string;
|
|
95
|
-
title: string;
|
|
96
|
-
url: string;
|
|
97
|
-
}[];
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
export type PublicPostRenderedResponse = PublicPostBaseResponse & {
|
|
101
|
-
bodyHtml: string | null;
|
|
102
|
-
bodyText: string | null;
|
|
103
|
-
};
|
|
104
|
-
|
|
105
|
-
export type PublicPostMarkdownResponse = PublicPostBaseResponse & {
|
|
106
|
-
bodyMarkdown: string | null;
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
export type PublicPostResponse =
|
|
110
|
-
PublicPostRenderedResponse | PublicPostMarkdownResponse;
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* The public Markdown for a post, or null for a historical body that isn't
|
|
114
|
-
* TipTap JSON. One unreadable row answers null and is logged, rather than
|
|
115
|
-
* failing the whole listing; `bodyHtml` still falls back to stored HTML.
|
|
116
|
-
*/
|
|
117
|
-
function toPublicBodyMarkdown(postId: string, body: string): string | null {
|
|
118
|
-
try {
|
|
119
|
-
return tiptapJsonToMarkdown(body);
|
|
120
|
-
} catch (error) {
|
|
121
|
-
// eslint-disable-next-line no-console -- A skipped body must leave a trace
|
|
122
|
-
console.error(
|
|
123
|
-
`Couldn't convert the body of post ${postId} to Markdown`,
|
|
124
|
-
error,
|
|
125
|
-
);
|
|
126
|
-
return null;
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
|
|
130
47
|
function isPublicDetailVisible(post: Post | null): post is Post {
|
|
131
48
|
return (
|
|
132
49
|
post !== null &&
|
|
@@ -135,207 +52,97 @@ function isPublicDetailVisible(post: Post | null): post is Post {
|
|
|
135
52
|
);
|
|
136
53
|
}
|
|
137
54
|
|
|
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
|
-
threadUpdatedAt: post.threadUpdatedAt,
|
|
195
|
-
createdAt: post.createdAt,
|
|
196
|
-
updatedAt: post.updatedAt,
|
|
197
|
-
attachments: mediaList.map((media) =>
|
|
198
|
-
toApiAttachment(
|
|
199
|
-
media,
|
|
200
|
-
r2PublicUrl,
|
|
201
|
-
imageTransformUrl,
|
|
202
|
-
s3PublicUrl,
|
|
203
|
-
localPublicUrl,
|
|
204
|
-
sitePathPrefix,
|
|
205
|
-
),
|
|
206
|
-
),
|
|
207
|
-
collections: threadCollections.map((collection) => ({
|
|
208
|
-
id: collection.id,
|
|
209
|
-
slug: collection.slug,
|
|
210
|
-
title: collection.title,
|
|
211
|
-
url: toPublicPath(getCollectionPagePath(collection.slug), sitePathPrefix),
|
|
212
|
-
})),
|
|
213
|
-
};
|
|
214
|
-
const contentFields =
|
|
215
|
-
options?.content === "markdown"
|
|
216
|
-
? {
|
|
217
|
-
bodyMarkdown: post.body
|
|
218
|
-
? toPublicBodyMarkdown(post.id, post.body)
|
|
219
|
-
: null,
|
|
220
|
-
}
|
|
221
|
-
: {
|
|
222
|
-
bodyHtml: post.bodyHtml,
|
|
223
|
-
bodyText: post.bodyText,
|
|
224
|
-
};
|
|
225
|
-
|
|
226
|
-
if (post.format === "quote") {
|
|
227
|
-
return {
|
|
228
|
-
...base,
|
|
229
|
-
...contentFields,
|
|
230
|
-
sourceName: post.title,
|
|
231
|
-
sourceUrl: post.url,
|
|
232
|
-
};
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
return {
|
|
236
|
-
...base,
|
|
237
|
-
...contentFields,
|
|
238
|
-
title: post.title,
|
|
239
|
-
url: post.url,
|
|
240
|
-
};
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
publicPostsApiRoutes.get("/", async (c) => {
|
|
244
|
-
const { format, lang, collection, sort, cursor, limit, content } =
|
|
245
|
-
parseValidated(ListPublicPostsQuerySchema, c.req.query());
|
|
246
|
-
|
|
247
|
-
// Resolve collection slug(s) — accepts comma-separated (e.g. "tech,art")
|
|
248
|
-
// or "+" separated (e.g. "tech+art"), matching the page URL convention.
|
|
249
|
-
let collectionIds: string[] | undefined;
|
|
250
|
-
let sortOrder: "newest" | "oldest" | "rating_desc" | undefined;
|
|
251
|
-
|
|
252
|
-
if (collection) {
|
|
253
|
-
// Normalize: commas → "+" so resolveSelection handles both forms
|
|
254
|
-
const slugExpression = collection.replace(/,/g, "+");
|
|
255
|
-
const selection =
|
|
256
|
-
await c.var.services.collections.resolveSelection(slugExpression);
|
|
257
|
-
if (!selection) {
|
|
258
|
-
return c.json({ posts: [], nextCursor: null });
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
collectionIds = selection.collections.map((col) => col.id);
|
|
262
|
-
|
|
263
|
-
// Determine sort order: single collection uses its configured default,
|
|
264
|
-
// aggregate selections default to "newest"
|
|
265
|
-
const isAggregate = selection.collections.length > 1;
|
|
266
|
-
const primaryCollection = selection.collections[0];
|
|
267
|
-
if (!primaryCollection) {
|
|
268
|
-
return c.json({ posts: [], nextCursor: null });
|
|
269
|
-
}
|
|
270
|
-
const requestedDefaultSort = isAggregate
|
|
271
|
-
? "newest"
|
|
272
|
-
: primaryCollection.sortOrder;
|
|
273
|
-
|
|
274
|
-
const ratedThreadCount =
|
|
275
|
-
await c.var.services.posts.countCollectionThreadRootsUpToForCollections(
|
|
276
|
-
collectionIds,
|
|
277
|
-
{
|
|
278
|
-
status: "published",
|
|
279
|
-
excludePrivate: true,
|
|
280
|
-
excludeLatestHidden: true,
|
|
281
|
-
rootFormat: format,
|
|
282
|
-
hasRating: true,
|
|
283
|
-
},
|
|
284
|
-
2,
|
|
285
|
-
);
|
|
286
|
-
const showRatingSort = supportsCollectionRatingSort(ratedThreadCount);
|
|
287
|
-
const defaultSort = resolveCollectionSortOrder(
|
|
288
|
-
undefined,
|
|
289
|
-
requestedDefaultSort,
|
|
290
|
-
showRatingSort,
|
|
291
|
-
);
|
|
292
|
-
sortOrder = resolveCollectionSortOrder(sort, defaultSort, showRatingSort);
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
const { posts, nextCursor } = collectionIds
|
|
296
|
-
? await c.var.services.posts.listCollectionThreadRootPage(
|
|
297
|
-
collectionIds,
|
|
298
|
-
{
|
|
299
|
-
status: "published",
|
|
300
|
-
excludePrivate: true,
|
|
301
|
-
excludeLatestHidden: true,
|
|
302
|
-
rootFormat: format,
|
|
303
|
-
lang,
|
|
304
|
-
sortOrder,
|
|
305
|
-
},
|
|
306
|
-
{ cursor, limit },
|
|
307
|
-
)
|
|
308
|
-
: await c.var.services.posts.listPage(
|
|
309
|
-
{
|
|
310
|
-
format,
|
|
311
|
-
lang,
|
|
312
|
-
status: "published",
|
|
313
|
-
excludePrivate: true,
|
|
314
|
-
excludeLatestHidden: true,
|
|
315
|
-
excludeReplies: true,
|
|
316
|
-
},
|
|
317
|
-
{ cursor, limit },
|
|
55
|
+
/**
|
|
56
|
+
* Deprecated in 0.9 for `GET /api/public/threads`, which lists the same Thread
|
|
57
|
+
* roots with the archive's filters and the homepage's defaults. Answers as it
|
|
58
|
+
* always has until 1.0.1 removes it.
|
|
59
|
+
*/
|
|
60
|
+
publicPostsApiRoutes.get(
|
|
61
|
+
"/",
|
|
62
|
+
deprecated(THREAD_LIST_DEPRECATED_AT, "/api/public/threads"),
|
|
63
|
+
async (c) => {
|
|
64
|
+
const { format, lang, collection, sort, cursor, limit, content } =
|
|
65
|
+
parseValidated(ListPublicPostsQuerySchema, c.req.query());
|
|
66
|
+
|
|
67
|
+
// Resolve collection slug(s) — accepts comma-separated (e.g. "tech,art")
|
|
68
|
+
// or "+" separated (e.g. "tech+art"), matching the page URL convention.
|
|
69
|
+
let collectionIds: string[] | undefined;
|
|
70
|
+
let sortOrder: "newest" | "oldest" | "rating_desc" | undefined;
|
|
71
|
+
|
|
72
|
+
if (collection) {
|
|
73
|
+
// Normalize: commas → "+" so resolveSelection handles both forms
|
|
74
|
+
const slugExpression = collection.replace(/,/g, "+");
|
|
75
|
+
const selection =
|
|
76
|
+
await c.var.services.collections.resolveSelection(slugExpression);
|
|
77
|
+
if (!selection) {
|
|
78
|
+
return c.json({ posts: [], nextCursor: null });
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
collectionIds = selection.collections.map((col) => col.id);
|
|
82
|
+
|
|
83
|
+
// Determine sort order: single collection uses its configured default,
|
|
84
|
+
// aggregate selections default to "newest"
|
|
85
|
+
const isAggregate = selection.collections.length > 1;
|
|
86
|
+
const primaryCollection = selection.collections[0];
|
|
87
|
+
if (!primaryCollection) {
|
|
88
|
+
return c.json({ posts: [], nextCursor: null });
|
|
89
|
+
}
|
|
90
|
+
const requestedDefaultSort = isAggregate
|
|
91
|
+
? "newest"
|
|
92
|
+
: primaryCollection.sortOrder;
|
|
93
|
+
|
|
94
|
+
const ratedThreadCount =
|
|
95
|
+
await c.var.services.posts.countCollectionThreadRootsUpToForCollections(
|
|
96
|
+
collectionIds,
|
|
97
|
+
{
|
|
98
|
+
status: "published",
|
|
99
|
+
excludePrivate: true,
|
|
100
|
+
excludeLatestHidden: true,
|
|
101
|
+
rootFormat: format,
|
|
102
|
+
hasRating: true,
|
|
103
|
+
},
|
|
104
|
+
2,
|
|
105
|
+
);
|
|
106
|
+
const showRatingSort = supportsCollectionRatingSort(ratedThreadCount);
|
|
107
|
+
const defaultSort = resolveCollectionSortOrder(
|
|
108
|
+
undefined,
|
|
109
|
+
requestedDefaultSort,
|
|
110
|
+
showRatingSort,
|
|
318
111
|
);
|
|
112
|
+
sortOrder = resolveCollectionSortOrder(sort, defaultSort, showRatingSort);
|
|
113
|
+
}
|
|
319
114
|
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
115
|
+
const { posts, nextCursor } = collectionIds
|
|
116
|
+
? await c.var.services.posts.listCollectionThreadRootPage(
|
|
117
|
+
collectionIds,
|
|
118
|
+
{
|
|
119
|
+
status: "published",
|
|
120
|
+
excludePrivate: true,
|
|
121
|
+
excludeLatestHidden: true,
|
|
122
|
+
rootFormat: format,
|
|
123
|
+
lang,
|
|
124
|
+
sortOrder,
|
|
125
|
+
},
|
|
126
|
+
{ cursor, limit },
|
|
127
|
+
)
|
|
128
|
+
: await c.var.services.posts.listPage(
|
|
129
|
+
{
|
|
130
|
+
format,
|
|
131
|
+
lang,
|
|
132
|
+
status: "published",
|
|
133
|
+
excludePrivate: true,
|
|
134
|
+
excludeLatestHidden: true,
|
|
135
|
+
excludeReplies: true,
|
|
136
|
+
},
|
|
137
|
+
{ cursor, limit },
|
|
138
|
+
);
|
|
139
|
+
|
|
140
|
+
return c.json({
|
|
141
|
+
posts: await loadPublicPostResponses(c.var, posts, { content }),
|
|
142
|
+
nextCursor,
|
|
143
|
+
});
|
|
144
|
+
},
|
|
145
|
+
);
|
|
339
146
|
|
|
340
147
|
publicPostsApiRoutes.get("/:slug", async (c) => {
|
|
341
148
|
const { content } = parseValidated(
|
|
@@ -349,14 +156,6 @@ publicPostsApiRoutes.get("/:slug", async (c) => {
|
|
|
349
156
|
throw new NotFoundError("Post");
|
|
350
157
|
}
|
|
351
158
|
|
|
352
|
-
const [
|
|
353
|
-
|
|
354
|
-
c.var.services.collections.getCollectionsByPostId(post.id),
|
|
355
|
-
]);
|
|
356
|
-
|
|
357
|
-
return c.json(
|
|
358
|
-
toPublicPost(post, mediaList, threadCollections, c.var.appConfig, {
|
|
359
|
-
content,
|
|
360
|
-
}),
|
|
361
|
-
);
|
|
159
|
+
const [response] = await loadPublicPostResponses(c.var, [post], { content });
|
|
160
|
+
return c.json(response);
|
|
362
161
|
});
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Public Threads API Routes
|
|
3
|
+
*
|
|
4
|
+
* The reader's view of Threads: what the site shows anyone, in the reading
|
|
5
|
+
* view. A list left unfiltered is the homepage's — Threads hidden from Latest
|
|
6
|
+
* left out, newest activity first, pins on top — and takes the archive's
|
|
7
|
+
* filters to narrow or widen it.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { Hono } from "hono";
|
|
11
|
+
import { z } from "zod";
|
|
12
|
+
import type { Bindings } from "../../../types.js";
|
|
13
|
+
import { THREAD_SORTS } from "../../../types.js";
|
|
14
|
+
import type { AppVariables } from "../../../types/app-context.js";
|
|
15
|
+
import { ContentLanguageSchema, parseValidated } from "../../../lib/schemas.js";
|
|
16
|
+
import { NotFoundError } from "../../../lib/errors.js";
|
|
17
|
+
import { loadPublicPostResponses } from "../../../lib/api-public-posts.js";
|
|
18
|
+
import { loadPublicThreadResponses } from "../../../lib/api-threads.js";
|
|
19
|
+
import {
|
|
20
|
+
parseThreadInclude,
|
|
21
|
+
parseThreadSelection,
|
|
22
|
+
} from "../../../lib/thread-query.js";
|
|
23
|
+
import { requirePublicApiEnabled } from "../../../middleware/public-content-access.js";
|
|
24
|
+
|
|
25
|
+
type Env = { Bindings: Bindings; Variables: AppVariables };
|
|
26
|
+
|
|
27
|
+
export const publicThreadsApiRoutes = new Hono<Env>();
|
|
28
|
+
|
|
29
|
+
publicThreadsApiRoutes.use("*", requirePublicApiEnabled());
|
|
30
|
+
|
|
31
|
+
const ContentSchema = z.enum(["markdown"]).optional();
|
|
32
|
+
|
|
33
|
+
/** The list's own parameters; everything else is a filter dimension. */
|
|
34
|
+
const ListThreadsQuerySchema = z.object({
|
|
35
|
+
lang: ContentLanguageSchema.optional(),
|
|
36
|
+
sort: z.enum(THREAD_SORTS).optional(),
|
|
37
|
+
include: z.string().optional(),
|
|
38
|
+
cursor: z.string().optional(),
|
|
39
|
+
limit: z.coerce.number().int().min(1).max(100).optional().default(20),
|
|
40
|
+
content: ContentSchema,
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
const LIST_PARAMS = [
|
|
44
|
+
"lang",
|
|
45
|
+
"sort",
|
|
46
|
+
"include",
|
|
47
|
+
"cursor",
|
|
48
|
+
"limit",
|
|
49
|
+
"content",
|
|
50
|
+
] as const;
|
|
51
|
+
|
|
52
|
+
const GetThreadQuerySchema = z.object({
|
|
53
|
+
include: z.string().optional(),
|
|
54
|
+
content: ContentSchema,
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
const ListThreadPostsQuerySchema = z.object({
|
|
58
|
+
cursor: z.string().optional(),
|
|
59
|
+
limit: z.coerce.number().int().min(1).max(100).optional().default(100),
|
|
60
|
+
content: ContentSchema,
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
publicThreadsApiRoutes.get("/", async (c) => {
|
|
64
|
+
const query = parseValidated(ListThreadsQuerySchema, c.req.query());
|
|
65
|
+
const { fold } = parseThreadInclude(query.include);
|
|
66
|
+
const parsed = await parseThreadSelection(
|
|
67
|
+
(key) => c.req.query(key),
|
|
68
|
+
Object.keys(c.req.query()),
|
|
69
|
+
{
|
|
70
|
+
allow: LIST_PARAMS,
|
|
71
|
+
audience: "reader",
|
|
72
|
+
loadCollections: () => c.var.services.collections.list(),
|
|
73
|
+
},
|
|
74
|
+
);
|
|
75
|
+
if (parsed.kind === "empty") {
|
|
76
|
+
return c.json({ threads: [], nextCursor: null });
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const { threads, nextCursor } = await c.var.services.threads.listThreads(
|
|
80
|
+
{
|
|
81
|
+
audience: "reader",
|
|
82
|
+
selection: parsed.selection,
|
|
83
|
+
includeHidden: parsed.includeHidden,
|
|
84
|
+
lang: query.lang,
|
|
85
|
+
sort: query.sort,
|
|
86
|
+
fold,
|
|
87
|
+
},
|
|
88
|
+
{ cursor: query.cursor, limit: query.limit },
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
return c.json({
|
|
92
|
+
threads: await loadPublicThreadResponses(c.var, threads, {
|
|
93
|
+
content: query.content,
|
|
94
|
+
}),
|
|
95
|
+
nextCursor,
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
publicThreadsApiRoutes.get("/:slug", async (c) => {
|
|
100
|
+
const { include, content } = parseValidated(
|
|
101
|
+
GetThreadQuerySchema,
|
|
102
|
+
c.req.query(),
|
|
103
|
+
);
|
|
104
|
+
const root = await c.var.services.threads.findRoot(
|
|
105
|
+
{ slug: c.req.param("slug") },
|
|
106
|
+
"reader",
|
|
107
|
+
);
|
|
108
|
+
if (!root) throw new NotFoundError("Thread");
|
|
109
|
+
|
|
110
|
+
const summaries = await c.var.services.threads.summarize(
|
|
111
|
+
[root],
|
|
112
|
+
parseThreadInclude(include),
|
|
113
|
+
);
|
|
114
|
+
const [thread] = await loadPublicThreadResponses(c.var, summaries, {
|
|
115
|
+
content,
|
|
116
|
+
});
|
|
117
|
+
if (!thread) throw new NotFoundError("Thread");
|
|
118
|
+
return c.json(thread);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
publicThreadsApiRoutes.get("/:slug/posts", async (c) => {
|
|
122
|
+
const { cursor, limit, content } = parseValidated(
|
|
123
|
+
ListThreadPostsQuerySchema,
|
|
124
|
+
c.req.query(),
|
|
125
|
+
);
|
|
126
|
+
const root = await c.var.services.threads.findRoot(
|
|
127
|
+
{ slug: c.req.param("slug") },
|
|
128
|
+
"reader",
|
|
129
|
+
);
|
|
130
|
+
if (!root) throw new NotFoundError("Thread");
|
|
131
|
+
|
|
132
|
+
const { posts, nextCursor } = await c.var.services.threads.listPosts(
|
|
133
|
+
root.id,
|
|
134
|
+
{ audience: "reader" },
|
|
135
|
+
{ cursor, limit },
|
|
136
|
+
);
|
|
137
|
+
return c.json({
|
|
138
|
+
posts: await loadPublicPostResponses(c.var, posts, { content }),
|
|
139
|
+
nextCursor,
|
|
140
|
+
});
|
|
141
|
+
});
|