@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
package/src/services/mcp.ts
CHANGED
|
@@ -3,6 +3,7 @@ import {
|
|
|
3
3
|
CollectionDescriptionValueSchema,
|
|
4
4
|
CollectionIdSchema,
|
|
5
5
|
CollectionSortOrderSchema,
|
|
6
|
+
ContentLanguageSchema,
|
|
6
7
|
CreateCollectionSchema,
|
|
7
8
|
CreatePostApiSchema,
|
|
8
9
|
FormatSchema,
|
|
@@ -15,7 +16,9 @@ import {
|
|
|
15
16
|
COLLECTION_SORT_ORDERS,
|
|
16
17
|
FORMATS,
|
|
17
18
|
STATUSES,
|
|
19
|
+
THREAD_SORTS,
|
|
18
20
|
VISIBILITIES,
|
|
21
|
+
type Post,
|
|
19
22
|
} from "../types.js";
|
|
20
23
|
import type { AppConfig } from "../types/config.js";
|
|
21
24
|
import type { StorageDriver } from "../lib/storage.js";
|
|
@@ -25,8 +28,14 @@ import {
|
|
|
25
28
|
buildEditableSettingsResponse,
|
|
26
29
|
partitionEditableSettingUpdates,
|
|
27
30
|
} from "../lib/api-settings.js";
|
|
28
|
-
import {
|
|
31
|
+
import {
|
|
32
|
+
apiPostListOrder,
|
|
33
|
+
loadApiPostResponse,
|
|
34
|
+
loadApiPostResponses,
|
|
35
|
+
} from "../lib/api-posts.js";
|
|
29
36
|
import { toSearchApiResult } from "../lib/api-search.js";
|
|
37
|
+
import { loadApiThreadResponses } from "../lib/api-threads.js";
|
|
38
|
+
import { parseThreadSelection } from "../lib/thread-query.js";
|
|
30
39
|
import {
|
|
31
40
|
ConfigurationError,
|
|
32
41
|
ExternalServiceError,
|
|
@@ -140,6 +149,68 @@ const AddCollectionThreadToolSchema = z.object({
|
|
|
140
149
|
|
|
141
150
|
const RemoveCollectionThreadToolSchema = AddCollectionThreadToolSchema;
|
|
142
151
|
|
|
152
|
+
/**
|
|
153
|
+
* The filter dimensions `jant_threads_list` takes, spelled as the HTTP API
|
|
154
|
+
* and the archive spell them. They reach the same registry parser as a query
|
|
155
|
+
* string would, so the vocabularies can't drift.
|
|
156
|
+
*/
|
|
157
|
+
const THREAD_FILTER_TOOL_PROPERTIES = {
|
|
158
|
+
format: { type: "string", enum: [...FORMATS] },
|
|
159
|
+
collection: {
|
|
160
|
+
type: "string",
|
|
161
|
+
description: "Collection slug, or several comma-separated",
|
|
162
|
+
},
|
|
163
|
+
year: { type: "integer", description: "Publication year (UTC)" },
|
|
164
|
+
media: {
|
|
165
|
+
type: "string",
|
|
166
|
+
description:
|
|
167
|
+
"any, none, or comma-separated kinds: image, video, audio, text, document",
|
|
168
|
+
},
|
|
169
|
+
title: { type: "string", enum: ["any", "none"] },
|
|
170
|
+
replies: { type: "string", enum: ["any", "none"] },
|
|
171
|
+
visibility: {
|
|
172
|
+
type: "string",
|
|
173
|
+
enum: ["public", "featured", "hidden", "private"],
|
|
174
|
+
},
|
|
175
|
+
} as const;
|
|
176
|
+
|
|
177
|
+
const ListThreadsToolSchema = z.object({
|
|
178
|
+
cursor: z.string().optional(),
|
|
179
|
+
limit: z.coerce.number().int().min(1).max(100).optional().default(20),
|
|
180
|
+
status: StatusSchema.optional(),
|
|
181
|
+
sort: z.enum(THREAD_SORTS).optional(),
|
|
182
|
+
fold: z.boolean().optional(),
|
|
183
|
+
lang: ContentLanguageSchema.optional(),
|
|
184
|
+
format: z.string().optional(),
|
|
185
|
+
collection: z.string().optional(),
|
|
186
|
+
year: z.number().int().optional(),
|
|
187
|
+
media: z.string().optional(),
|
|
188
|
+
title: z.string().optional(),
|
|
189
|
+
replies: z.string().optional(),
|
|
190
|
+
visibility: z.string().optional(),
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
const LIST_THREADS_OWN_PARAMS = [
|
|
194
|
+
"cursor",
|
|
195
|
+
"limit",
|
|
196
|
+
"status",
|
|
197
|
+
"sort",
|
|
198
|
+
"fold",
|
|
199
|
+
"lang",
|
|
200
|
+
] as const;
|
|
201
|
+
|
|
202
|
+
const GetThreadToolSchema = z.object({
|
|
203
|
+
id: PostIdSchema,
|
|
204
|
+
fold: z.boolean().optional(),
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
const ListThreadPostsToolSchema = z.object({
|
|
208
|
+
id: PostIdSchema,
|
|
209
|
+
cursor: z.string().optional(),
|
|
210
|
+
limit: z.coerce.number().int().min(1).max(100).optional().default(100),
|
|
211
|
+
status: StatusSchema.optional(),
|
|
212
|
+
});
|
|
213
|
+
|
|
143
214
|
const mcpTools: McpToolDefinition[] = [
|
|
144
215
|
{
|
|
145
216
|
name: "jant_posts_list",
|
|
@@ -157,8 +228,9 @@ const mcpTools: McpToolDefinition[] = [
|
|
|
157
228
|
},
|
|
158
229
|
async execute(args, context) {
|
|
159
230
|
const input = ListPostsToolSchema.parse(args ?? {});
|
|
231
|
+
const status = input.status ?? "published";
|
|
160
232
|
const { posts, nextCursor } = await context.services.posts.listPage(
|
|
161
|
-
{ format: input.format, status
|
|
233
|
+
{ format: input.format, status, ...apiPostListOrder(status) },
|
|
162
234
|
{ cursor: input.cursor, limit: input.limit },
|
|
163
235
|
);
|
|
164
236
|
|
|
@@ -211,6 +283,136 @@ const mcpTools: McpToolDefinition[] = [
|
|
|
211
283
|
return content;
|
|
212
284
|
},
|
|
213
285
|
},
|
|
286
|
+
{
|
|
287
|
+
name: "jant_threads_list",
|
|
288
|
+
description:
|
|
289
|
+
"List Threads (a root post and its replies), each with its post count. Takes the archive's filters; sort is activity (default), published, updated, oldest, or rating. Set fold to include the replies the homepage shows. Pass nextCursor back as cursor for the next page.",
|
|
290
|
+
inputSchema: {
|
|
291
|
+
type: "object",
|
|
292
|
+
properties: {
|
|
293
|
+
cursor: { type: "string" },
|
|
294
|
+
limit: { type: "integer", minimum: 1, maximum: 100, default: 20 },
|
|
295
|
+
status: { type: "string", enum: [...STATUSES] },
|
|
296
|
+
sort: { type: "string", enum: [...THREAD_SORTS] },
|
|
297
|
+
fold: { type: "boolean" },
|
|
298
|
+
lang: { type: "string", description: "BCP 47 content language" },
|
|
299
|
+
...THREAD_FILTER_TOOL_PROPERTIES,
|
|
300
|
+
},
|
|
301
|
+
additionalProperties: false,
|
|
302
|
+
},
|
|
303
|
+
async execute(args, context) {
|
|
304
|
+
const input = ListThreadsToolSchema.parse(args ?? {});
|
|
305
|
+
const filterArgs = Object.fromEntries(
|
|
306
|
+
Object.entries(input).filter(
|
|
307
|
+
([key, value]) =>
|
|
308
|
+
value !== undefined &&
|
|
309
|
+
!(LIST_THREADS_OWN_PARAMS as readonly string[]).includes(key),
|
|
310
|
+
),
|
|
311
|
+
);
|
|
312
|
+
const parsed = await parseThreadSelection(
|
|
313
|
+
(key) => {
|
|
314
|
+
const value = filterArgs[key];
|
|
315
|
+
return value === undefined ? undefined : String(value);
|
|
316
|
+
},
|
|
317
|
+
Object.keys(filterArgs),
|
|
318
|
+
{
|
|
319
|
+
allow: [],
|
|
320
|
+
audience: "author",
|
|
321
|
+
loadCollections: () => context.services.collections.list(),
|
|
322
|
+
},
|
|
323
|
+
);
|
|
324
|
+
if (parsed.kind === "empty") {
|
|
325
|
+
return { threads: [], nextCursor: null };
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
const { threads, nextCursor } =
|
|
329
|
+
await context.services.threads.listThreads(
|
|
330
|
+
{
|
|
331
|
+
audience: "author",
|
|
332
|
+
status: input.status,
|
|
333
|
+
selection: parsed.selection,
|
|
334
|
+
lang: input.lang,
|
|
335
|
+
sort: input.sort,
|
|
336
|
+
fold: input.fold,
|
|
337
|
+
},
|
|
338
|
+
{ cursor: input.cursor, limit: input.limit },
|
|
339
|
+
);
|
|
340
|
+
return {
|
|
341
|
+
threads: await loadApiThreadResponses(context, threads),
|
|
342
|
+
nextCursor,
|
|
343
|
+
};
|
|
344
|
+
},
|
|
345
|
+
},
|
|
346
|
+
{
|
|
347
|
+
name: "jant_threads_get",
|
|
348
|
+
description:
|
|
349
|
+
"Get the Thread any post belongs to: its root and post count, and with fold the replies the homepage shows.",
|
|
350
|
+
inputSchema: {
|
|
351
|
+
type: "object",
|
|
352
|
+
properties: {
|
|
353
|
+
id: {
|
|
354
|
+
type: "string",
|
|
355
|
+
description: "TypeID of any post in the Thread",
|
|
356
|
+
},
|
|
357
|
+
fold: { type: "boolean" },
|
|
358
|
+
},
|
|
359
|
+
required: ["id"],
|
|
360
|
+
additionalProperties: false,
|
|
361
|
+
},
|
|
362
|
+
async execute(args, context) {
|
|
363
|
+
const input = GetThreadToolSchema.parse(args ?? {});
|
|
364
|
+
const root = await context.services.threads.findRoot(
|
|
365
|
+
{ id: input.id },
|
|
366
|
+
"author",
|
|
367
|
+
);
|
|
368
|
+
if (!root) {
|
|
369
|
+
throw new NotFoundError("Thread");
|
|
370
|
+
}
|
|
371
|
+
const summaries = await context.services.threads.summarize([root], {
|
|
372
|
+
fold: input.fold,
|
|
373
|
+
});
|
|
374
|
+
const [thread] = await loadApiThreadResponses(context, summaries);
|
|
375
|
+
return thread;
|
|
376
|
+
},
|
|
377
|
+
},
|
|
378
|
+
{
|
|
379
|
+
name: "jant_threads_list_posts",
|
|
380
|
+
description:
|
|
381
|
+
"List the posts of the Thread any post belongs to, root first, in Thread order. Pass nextCursor back as cursor for the next page.",
|
|
382
|
+
inputSchema: {
|
|
383
|
+
type: "object",
|
|
384
|
+
properties: {
|
|
385
|
+
id: {
|
|
386
|
+
type: "string",
|
|
387
|
+
description: "TypeID of any post in the Thread",
|
|
388
|
+
},
|
|
389
|
+
cursor: { type: "string" },
|
|
390
|
+
limit: { type: "integer", minimum: 1, maximum: 100, default: 100 },
|
|
391
|
+
status: { type: "string", enum: [...STATUSES] },
|
|
392
|
+
},
|
|
393
|
+
required: ["id"],
|
|
394
|
+
additionalProperties: false,
|
|
395
|
+
},
|
|
396
|
+
async execute(args, context) {
|
|
397
|
+
const input = ListThreadPostsToolSchema.parse(args ?? {});
|
|
398
|
+
const root = await context.services.threads.findRoot(
|
|
399
|
+
{ id: input.id },
|
|
400
|
+
"author",
|
|
401
|
+
);
|
|
402
|
+
if (!root) {
|
|
403
|
+
throw new NotFoundError("Thread");
|
|
404
|
+
}
|
|
405
|
+
const { posts, nextCursor } = await context.services.threads.listPosts(
|
|
406
|
+
root.id,
|
|
407
|
+
{ audience: "author", status: input.status },
|
|
408
|
+
{ cursor: input.cursor, limit: input.limit },
|
|
409
|
+
);
|
|
410
|
+
return {
|
|
411
|
+
posts: await serializePosts(posts, context),
|
|
412
|
+
nextCursor,
|
|
413
|
+
};
|
|
414
|
+
},
|
|
415
|
+
},
|
|
214
416
|
{
|
|
215
417
|
name: "jant_posts_create",
|
|
216
418
|
description:
|
|
@@ -1281,46 +1483,15 @@ async function uploadMediaFromBase64(
|
|
|
1281
1483
|
return serializeMedia(media, context.appConfig);
|
|
1282
1484
|
}
|
|
1283
1485
|
|
|
1284
|
-
|
|
1486
|
+
function serializePosts(
|
|
1285
1487
|
posts: Awaited<ReturnType<Services["posts"]["list"]>>,
|
|
1286
1488
|
context: McpToolContext,
|
|
1287
1489
|
) {
|
|
1288
|
-
|
|
1289
|
-
return [];
|
|
1290
|
-
}
|
|
1291
|
-
|
|
1292
|
-
const postIds = posts.map((post) => post.id);
|
|
1293
|
-
const [mediaMap, collectionMap] = await Promise.all([
|
|
1294
|
-
context.services.media.getByPostIds(postIds),
|
|
1295
|
-
context.services.collections.getCollectionsByPostIds(postIds),
|
|
1296
|
-
]);
|
|
1297
|
-
|
|
1298
|
-
return posts.map((post) =>
|
|
1299
|
-
toApiPost(post, {
|
|
1300
|
-
attachments: serializeAttachments(
|
|
1301
|
-
mediaMap.get(post.id) ?? [],
|
|
1302
|
-
context.appConfig,
|
|
1303
|
-
),
|
|
1304
|
-
collectionIds: (collectionMap.get(post.id) ?? []).map(
|
|
1305
|
-
(collection) => collection.id,
|
|
1306
|
-
),
|
|
1307
|
-
}),
|
|
1308
|
-
);
|
|
1490
|
+
return loadApiPostResponses(context, posts, { collectionIds: true });
|
|
1309
1491
|
}
|
|
1310
1492
|
|
|
1311
|
-
|
|
1312
|
-
post:
|
|
1313
|
-
context: McpToolContext,
|
|
1314
|
-
) {
|
|
1315
|
-
const [mediaList, threadCollections] = await Promise.all([
|
|
1316
|
-
context.services.media.getByPostId(post.id),
|
|
1317
|
-
context.services.collections.getCollectionsByPostId(post.id),
|
|
1318
|
-
]);
|
|
1319
|
-
|
|
1320
|
-
return toApiPost(post, {
|
|
1321
|
-
attachments: serializeAttachments(mediaList, context.appConfig),
|
|
1322
|
-
collectionIds: threadCollections.map((collection) => collection.id),
|
|
1323
|
-
});
|
|
1493
|
+
function serializePost(post: Post, context: McpToolContext) {
|
|
1494
|
+
return loadApiPostResponse(context, post, { collectionIds: true });
|
|
1324
1495
|
}
|
|
1325
1496
|
|
|
1326
1497
|
function serializeMedia(
|
|
@@ -1331,19 +1502,3 @@ function serializeMedia(
|
|
|
1331
1502
|
) {
|
|
1332
1503
|
return toApiMedia(media, appConfig);
|
|
1333
1504
|
}
|
|
1334
|
-
|
|
1335
|
-
function serializeAttachments(
|
|
1336
|
-
mediaList: Awaited<ReturnType<Services["media"]["getByPostId"]>>,
|
|
1337
|
-
appConfig: AppConfig,
|
|
1338
|
-
) {
|
|
1339
|
-
return mediaList.map((media) =>
|
|
1340
|
-
toApiAttachment(
|
|
1341
|
-
media,
|
|
1342
|
-
appConfig.r2PublicUrl,
|
|
1343
|
-
appConfig.imageTransformUrl,
|
|
1344
|
-
appConfig.s3PublicUrl,
|
|
1345
|
-
appConfig.localPublicUrl,
|
|
1346
|
-
appConfig.sitePathPrefix,
|
|
1347
|
-
),
|
|
1348
|
-
);
|
|
1349
|
-
}
|
package/src/services/post.ts
CHANGED
|
@@ -275,6 +275,13 @@ export interface ThreadRootPageOptions {
|
|
|
275
275
|
rootFormat?: Format;
|
|
276
276
|
/** Restrict to Threads with at least one rated published post. */
|
|
277
277
|
hasRating?: true;
|
|
278
|
+
/**
|
|
279
|
+
* Restrict to Threads whose root matches these filters, the way a list of
|
|
280
|
+
* roots would (`excludeReplies` is implied). Lets a Thread-grouped order,
|
|
281
|
+
* such as a collection's, take the same filter dimensions `list()` does.
|
|
282
|
+
* Sorting and paging fields are ignored.
|
|
283
|
+
*/
|
|
284
|
+
rootFilters?: PostFilters;
|
|
278
285
|
limit?: number;
|
|
279
286
|
offset?: number;
|
|
280
287
|
}
|
|
@@ -304,7 +311,13 @@ interface ListSort {
|
|
|
304
311
|
pinned: boolean;
|
|
305
312
|
}
|
|
306
313
|
|
|
307
|
-
/**
|
|
314
|
+
/**
|
|
315
|
+
* The sort mode a cursor into one Thread's Posts records. Distinct from every
|
|
316
|
+
* `list()` and collection mode, so a position in one never resumes another.
|
|
317
|
+
*/
|
|
318
|
+
const THREAD_POSTS_SORT_MODE = "thread";
|
|
319
|
+
|
|
320
|
+
/** One term of a paged ORDER BY: `list()`'s, a collection's, or a Thread's. */
|
|
308
321
|
interface ListSortKey {
|
|
309
322
|
/** Selectable as a column, so a page's cursor is read from SQL. */
|
|
310
323
|
expr: SQL;
|
|
@@ -587,6 +600,44 @@ export interface PostService {
|
|
|
587
600
|
* then replies by creation time, then ID.
|
|
588
601
|
*/
|
|
589
602
|
getThread(rootId: string): Promise<Post[]>;
|
|
603
|
+
/**
|
|
604
|
+
* One page of a Thread's Posts in Thread order, and an opaque cursor for the
|
|
605
|
+
* next.
|
|
606
|
+
*
|
|
607
|
+
* Pages like {@link listPage}. A new reply joins the end of the Thread, so a
|
|
608
|
+
* walk that is still going when one is published reaches it rather than
|
|
609
|
+
* skipping it. As well as a `nextCursor`, the cursor may be the ID of a Post
|
|
610
|
+
* in this Thread with the requested status: the page then starts right after
|
|
611
|
+
* it, which is how a reader opens the run a fold hides.
|
|
612
|
+
*
|
|
613
|
+
* @param threadId - The Thread root's ID
|
|
614
|
+
* @param options - Which Posts to include; `status` defaults to `published`
|
|
615
|
+
* @param page - The previous page's `nextCursor` or a Post ID, and the page
|
|
616
|
+
* size
|
|
617
|
+
* @returns The page's Posts and the cursor after them, `null` on the last page
|
|
618
|
+
* @throws {ValidationError} When the cursor can't be read, was taken in
|
|
619
|
+
* another order, or names a Post this request doesn't list
|
|
620
|
+
* @example
|
|
621
|
+
* ```ts
|
|
622
|
+
* const first = await posts.listThreadPostsPage(root.id, {}, { limit: 100 });
|
|
623
|
+
* ```
|
|
624
|
+
*/
|
|
625
|
+
listThreadPostsPage(
|
|
626
|
+
threadId: string,
|
|
627
|
+
options: { status?: Status },
|
|
628
|
+
page: PostListPageOptions,
|
|
629
|
+
): Promise<PostListPage>;
|
|
630
|
+
/**
|
|
631
|
+
* How many published Posts each Thread holds, its root included.
|
|
632
|
+
*
|
|
633
|
+
* @param threadIds - Thread root IDs; duplicates are fine
|
|
634
|
+
* @returns Map of Thread root ID to its published Post count; a Thread with
|
|
635
|
+
* nothing published is absent
|
|
636
|
+
* @example
|
|
637
|
+
* const counts = await posts.countThreadPosts([root.id]);
|
|
638
|
+
* counts.get(root.id); // 12
|
|
639
|
+
*/
|
|
640
|
+
countThreadPosts(threadIds: string[]): Promise<Map<string, number>>;
|
|
590
641
|
/**
|
|
591
642
|
* 1-based position of a Post in the reply chain running from its Thread root
|
|
592
643
|
* down to it: a root is 1, a reply to it is 2, a reply to that is 3.
|
|
@@ -1116,14 +1167,33 @@ export function createPostService(
|
|
|
1116
1167
|
* sql`ROW_NUMBER() OVER (ORDER BY ${sql.join(threadOrder(), sql`, `)})`;
|
|
1117
1168
|
*/
|
|
1118
1169
|
function threadOrder(direction: "asc" | "desc" = "asc"): SQL[] {
|
|
1119
|
-
const terms =
|
|
1120
|
-
sql`CASE WHEN ${posts.replyToId} IS NULL THEN 0 ELSE 1 END`,
|
|
1121
|
-
sql`${posts.createdAt}`,
|
|
1122
|
-
sql`${posts.id}`,
|
|
1123
|
-
];
|
|
1170
|
+
const terms = threadSortKeys().map((key) => key.expr);
|
|
1124
1171
|
return direction === "asc" ? terms : terms.map((term) => desc(term));
|
|
1125
1172
|
}
|
|
1126
1173
|
|
|
1174
|
+
/**
|
|
1175
|
+
* {@link threadOrder} as paged sort keys, for walking one Thread with a
|
|
1176
|
+
* cursor. The one definition both read, so a page of a Thread and the
|
|
1177
|
+
* Thread page itself cannot order its posts differently.
|
|
1178
|
+
*/
|
|
1179
|
+
function threadSortKeys(): ListSortKey[] {
|
|
1180
|
+
return [
|
|
1181
|
+
{
|
|
1182
|
+
expr: sql<number>`CASE WHEN ${posts.replyToId} IS NULL THEN 0 ELSE 1 END`.mapWith(
|
|
1183
|
+
Number,
|
|
1184
|
+
),
|
|
1185
|
+
direction: "asc",
|
|
1186
|
+
kind: "number",
|
|
1187
|
+
},
|
|
1188
|
+
{
|
|
1189
|
+
expr: sql<number>`${posts.createdAt}`.mapWith(Number),
|
|
1190
|
+
direction: "asc",
|
|
1191
|
+
kind: "number",
|
|
1192
|
+
},
|
|
1193
|
+
{ expr: sql<string>`${posts.id}`, direction: "asc", kind: "id" },
|
|
1194
|
+
];
|
|
1195
|
+
}
|
|
1196
|
+
|
|
1127
1197
|
function buildYearMonthExpr(column: SQLWrapper): SQL<string> {
|
|
1128
1198
|
return databaseDialect === "pg"
|
|
1129
1199
|
? sql<string>`to_char(timezone('UTC', to_timestamp(${column})), 'YYYY-MM')`
|
|
@@ -1957,6 +2027,26 @@ export function createPostService(
|
|
|
1957
2027
|
if (options?.threadIds !== undefined) {
|
|
1958
2028
|
conditions.push(inArray(posts.threadId, options.threadIds));
|
|
1959
2029
|
}
|
|
2030
|
+
if (options?.rootFilters) {
|
|
2031
|
+
// Uncorrelated: the inner `post` shadows the outer one, so every column
|
|
2032
|
+
// the filters name reads the root row being matched.
|
|
2033
|
+
conditions.push(
|
|
2034
|
+
inArray(
|
|
2035
|
+
posts.threadId,
|
|
2036
|
+
db
|
|
2037
|
+
.select({ id: posts.id })
|
|
2038
|
+
.from(posts)
|
|
2039
|
+
.where(
|
|
2040
|
+
and(
|
|
2041
|
+
...buildFilterConditions({
|
|
2042
|
+
...options.rootFilters,
|
|
2043
|
+
excludeReplies: true,
|
|
2044
|
+
}),
|
|
2045
|
+
),
|
|
2046
|
+
),
|
|
2047
|
+
),
|
|
2048
|
+
);
|
|
2049
|
+
}
|
|
1960
2050
|
|
|
1961
2051
|
return conditions;
|
|
1962
2052
|
}
|
|
@@ -3918,6 +4008,87 @@ export function createPostService(
|
|
|
3918
4008
|
return hydratePosts(rows);
|
|
3919
4009
|
},
|
|
3920
4010
|
|
|
4011
|
+
async listThreadPostsPage(threadId, options, { cursor, limit }) {
|
|
4012
|
+
const keys = threadSortKeys();
|
|
4013
|
+
const conditions: SQL[] = [
|
|
4014
|
+
eq(posts.siteId, siteId),
|
|
4015
|
+
eq(posts.threadId, threadId),
|
|
4016
|
+
eq(posts.status, options.status ?? "published"),
|
|
4017
|
+
];
|
|
4018
|
+
|
|
4019
|
+
if (cursor) {
|
|
4020
|
+
let values: PostListCursorValue[];
|
|
4021
|
+
if (isLegacyPostListCursor(cursor)) {
|
|
4022
|
+
// A Post ID names a position only inside the Thread being listed,
|
|
4023
|
+
// and only among the Posts this request would return, so it can't
|
|
4024
|
+
// be used to learn anything about a Post elsewhere.
|
|
4025
|
+
const rows = await db
|
|
4026
|
+
.select(listSortKeyFields(keys))
|
|
4027
|
+
.from(posts)
|
|
4028
|
+
.where(and(...conditions, eq(posts.id, cursor)))
|
|
4029
|
+
.limit(1);
|
|
4030
|
+
const row = rows[0];
|
|
4031
|
+
if (!row) {
|
|
4032
|
+
throw new ValidationError(MISSING_LEGACY_CURSOR_MESSAGE);
|
|
4033
|
+
}
|
|
4034
|
+
values = readListSortKeyValues(keys, row);
|
|
4035
|
+
} else {
|
|
4036
|
+
values = decodePostListCursor(cursor, {
|
|
4037
|
+
mode: THREAD_POSTS_SORT_MODE,
|
|
4038
|
+
kinds: keys.map((key) => key.kind),
|
|
4039
|
+
});
|
|
4040
|
+
}
|
|
4041
|
+
conditions.push(buildListKeysetCondition(keys, values));
|
|
4042
|
+
}
|
|
4043
|
+
|
|
4044
|
+
const rows = await db
|
|
4045
|
+
.select({ post: posts, sortKey: listSortKeyFields(keys) })
|
|
4046
|
+
.from(posts)
|
|
4047
|
+
.where(and(...conditions))
|
|
4048
|
+
.orderBy(...listOrderBy(keys))
|
|
4049
|
+
.limit(limit + 1);
|
|
4050
|
+
|
|
4051
|
+
const pageRows = rows.slice(0, limit);
|
|
4052
|
+
const lastRow = pageRows[pageRows.length - 1];
|
|
4053
|
+
return {
|
|
4054
|
+
posts: await hydratePosts(pageRows.map((row) => row.post)),
|
|
4055
|
+
nextCursor:
|
|
4056
|
+
rows.length > limit && lastRow
|
|
4057
|
+
? encodePostListCursor(
|
|
4058
|
+
THREAD_POSTS_SORT_MODE,
|
|
4059
|
+
readListSortKeyValues(keys, lastRow.sortKey),
|
|
4060
|
+
)
|
|
4061
|
+
: null,
|
|
4062
|
+
};
|
|
4063
|
+
},
|
|
4064
|
+
|
|
4065
|
+
async countThreadPosts(threadIds) {
|
|
4066
|
+
const counts = new Map<string, number>();
|
|
4067
|
+
if (threadIds.length === 0) return counts;
|
|
4068
|
+
|
|
4069
|
+
const rows = await batchQueryRows([...new Set(threadIds)], (chunk) =>
|
|
4070
|
+
db
|
|
4071
|
+
.select({
|
|
4072
|
+
threadId: posts.threadId,
|
|
4073
|
+
count: sql<number>`CAST(count(*) AS INTEGER)`.as("count"),
|
|
4074
|
+
})
|
|
4075
|
+
.from(posts)
|
|
4076
|
+
.where(
|
|
4077
|
+
and(
|
|
4078
|
+
eq(posts.siteId, siteId),
|
|
4079
|
+
inArray(posts.threadId, chunk),
|
|
4080
|
+
eq(posts.status, "published"),
|
|
4081
|
+
),
|
|
4082
|
+
)
|
|
4083
|
+
.groupBy(posts.threadId),
|
|
4084
|
+
);
|
|
4085
|
+
|
|
4086
|
+
for (const row of rows) {
|
|
4087
|
+
counts.set(row.threadId, row.count);
|
|
4088
|
+
}
|
|
4089
|
+
return counts;
|
|
4090
|
+
},
|
|
4091
|
+
|
|
3921
4092
|
async getThreadPosition(postId) {
|
|
3922
4093
|
const targetRows = await db
|
|
3923
4094
|
.select({ replyToId: posts.replyToId, threadId: posts.threadId })
|
|
@@ -4494,51 +4665,59 @@ export function createPostService(
|
|
|
4494
4665
|
async getThreadTimelineContext(rootIds) {
|
|
4495
4666
|
if (rootIds.length === 0) return new Map();
|
|
4496
4667
|
|
|
4497
|
-
|
|
4498
|
-
|
|
4499
|
-
|
|
4500
|
-
|
|
4501
|
-
|
|
4668
|
+
// A page of Threads can outgrow one statement's parameter budget — the
|
|
4669
|
+
// API lists up to 100. Each Thread lands in one chunk, so every window
|
|
4670
|
+
// below still partitions over a whole Thread.
|
|
4671
|
+
const contextRows = await batchQueryRows(
|
|
4672
|
+
[...new Set(rootIds)],
|
|
4673
|
+
(chunk) => {
|
|
4674
|
+
const rankedReplies = db
|
|
4675
|
+
.select({
|
|
4676
|
+
id: posts.id,
|
|
4677
|
+
threadId: posts.threadId,
|
|
4678
|
+
firstReplyRank: sql<number>`CAST(ROW_NUMBER() OVER (
|
|
4502
4679
|
PARTITION BY ${posts.threadId}
|
|
4503
4680
|
ORDER BY ${sql.join(threadOrder(), sql`, `)}
|
|
4504
4681
|
) AS INTEGER)`.as("first_reply_rank"),
|
|
4505
|
-
|
|
4682
|
+
latestReplyRank: sql<number>`CAST(ROW_NUMBER() OVER (
|
|
4506
4683
|
PARTITION BY ${posts.threadId}
|
|
4507
4684
|
ORDER BY ${sql.join(threadOrder("desc"), sql`, `)}
|
|
4508
4685
|
) AS INTEGER)`.as("latest_reply_rank"),
|
|
4509
|
-
|
|
4686
|
+
totalReplyCount: sql<number>`CAST(COUNT(*) OVER (
|
|
4510
4687
|
PARTITION BY ${posts.threadId}
|
|
4511
4688
|
) AS INTEGER)`.as("total_reply_count"),
|
|
4512
|
-
|
|
4513
|
-
|
|
4514
|
-
|
|
4515
|
-
|
|
4516
|
-
|
|
4517
|
-
|
|
4518
|
-
|
|
4519
|
-
|
|
4520
|
-
|
|
4521
|
-
|
|
4522
|
-
|
|
4523
|
-
|
|
4524
|
-
|
|
4525
|
-
|
|
4526
|
-
|
|
4527
|
-
|
|
4528
|
-
|
|
4529
|
-
|
|
4530
|
-
|
|
4531
|
-
|
|
4532
|
-
|
|
4533
|
-
|
|
4534
|
-
|
|
4535
|
-
|
|
4536
|
-
|
|
4537
|
-
|
|
4538
|
-
|
|
4539
|
-
|
|
4540
|
-
|
|
4541
|
-
|
|
4689
|
+
})
|
|
4690
|
+
.from(posts)
|
|
4691
|
+
.where(
|
|
4692
|
+
and(
|
|
4693
|
+
eq(posts.siteId, siteId),
|
|
4694
|
+
inArray(posts.threadId, chunk),
|
|
4695
|
+
eq(posts.status, "published"),
|
|
4696
|
+
isNotNull(posts.replyToId),
|
|
4697
|
+
),
|
|
4698
|
+
)
|
|
4699
|
+
.as("ranked_replies");
|
|
4700
|
+
|
|
4701
|
+
return db
|
|
4702
|
+
.select({
|
|
4703
|
+
threadId: rankedReplies.threadId,
|
|
4704
|
+
id: rankedReplies.id,
|
|
4705
|
+
firstReplyRank: rankedReplies.firstReplyRank,
|
|
4706
|
+
latestReplyRank: rankedReplies.latestReplyRank,
|
|
4707
|
+
totalReplyCount: rankedReplies.totalReplyCount,
|
|
4708
|
+
})
|
|
4709
|
+
.from(rankedReplies)
|
|
4710
|
+
.where(
|
|
4711
|
+
or(
|
|
4712
|
+
// One past the leading window: that extra row is never rendered,
|
|
4713
|
+
// it is only where the gap link points. Without it the gap has to
|
|
4714
|
+
// aim at a reply already on screen.
|
|
4715
|
+
lte(rankedReplies.firstReplyRank, THREAD_LEADING_REPLIES + 1),
|
|
4716
|
+
lte(rankedReplies.latestReplyRank, THREAD_TRAILING_REPLIES),
|
|
4717
|
+
),
|
|
4718
|
+
);
|
|
4719
|
+
},
|
|
4720
|
+
);
|
|
4542
4721
|
|
|
4543
4722
|
const hydratedPosts = await hydratePostsById(
|
|
4544
4723
|
contextRows.map((row) => row.id),
|