@jant/core 0.9.0 → 0.9.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.
@@ -16,6 +16,64 @@ describe("Search Page Routes", () => {
16
16
  expect(response.status).toBe(200);
17
17
  });
18
18
 
19
+ describe("private posts", () => {
20
+ async function seedPrivateThread(
21
+ services: ReturnType<typeof createTestApp>["services"],
22
+ ) {
23
+ const root = await services.posts.create({
24
+ format: "note",
25
+ title: "Secret diary",
26
+ bodyMarkdown: "Private root about lanterns",
27
+ visibility: "private",
28
+ });
29
+ await services.posts.create({
30
+ format: "note",
31
+ bodyMarkdown: "Private reply about lanterns",
32
+ replyToId: root.id,
33
+ });
34
+ await services.posts.create({
35
+ format: "note",
36
+ title: "Public notes",
37
+ bodyMarkdown: "Public post about lanterns",
38
+ });
39
+ }
40
+
41
+ it("leaves out private posts and replies in a private Thread for a signed-out reader", async () => {
42
+ const { app, services } = createTestApp({
43
+ authenticated: false,
44
+ fts: true,
45
+ });
46
+ app.route("/search", searchRoutes);
47
+ await seedPrivateThread(services);
48
+
49
+ const response = await app.request("/search?q=lanterns");
50
+
51
+ expect(response.status).toBe(200);
52
+ const html = await response.text();
53
+ expect(html).toContain("Public notes");
54
+ expect(html).not.toContain("Secret diary");
55
+ expect(html).not.toContain("Private root");
56
+ expect(html).not.toContain("Private reply");
57
+ });
58
+
59
+ it("includes them for the signed-in author", async () => {
60
+ const { app, services } = createTestApp({
61
+ authenticated: true,
62
+ fts: true,
63
+ });
64
+ app.route("/search", searchRoutes);
65
+ await seedPrivateThread(services);
66
+
67
+ const response = await app.request("/search?q=lanterns");
68
+
69
+ expect(response.status).toBe(200);
70
+ const html = await response.text();
71
+ expect(html).toContain("Public notes");
72
+ expect(html).toContain("Secret diary");
73
+ expect(html).toContain("Private reply");
74
+ });
75
+ });
76
+
19
77
  it("hides visible Thread collection tags on a matching child post", async () => {
20
78
  const { app, services } = createTestApp({ fts: true });
21
79
  app.route("/search", searchRoutes);
@@ -42,11 +42,13 @@ export async function renderSearchPage(c: Context<Env>): Promise<Response> {
42
42
 
43
43
  if (query.trim()) {
44
44
  try {
45
- // Fetch one extra to check for more
45
+ // Fetch one extra to check for more. Private posts match for the
46
+ // signed-in author only, as on the archive and collection pages.
46
47
  results = await c.var.services.search.search(query, {
47
48
  limit: pageSize + 1,
48
49
  offset: (page - 1) * pageSize,
49
50
  status: ["published"],
51
+ includePrivate: c.var.isAuthenticated,
50
52
  lang: getViewLang(c) ?? undefined,
51
53
  });
52
54
 
@@ -323,6 +323,217 @@ describe("SearchService", () => {
323
323
  expect(results).toHaveLength(0);
324
324
  });
325
325
 
326
+ describe("private posts", () => {
327
+ async function createPrivateThread() {
328
+ const root = await postService.create({
329
+ format: "note",
330
+ title: "Secret diary",
331
+ body: tiptapDoc("Private root about lanterns"),
332
+ visibility: "private",
333
+ });
334
+ const reply = await postService.create({
335
+ format: "note",
336
+ body: tiptapDoc("Private reply about lanterns"),
337
+ replyToId: root.id,
338
+ });
339
+ return { root, reply };
340
+ }
341
+
342
+ it("leaves out private posts and replies in a private Thread by default", async () => {
343
+ const { root, reply } = await createPrivateThread();
344
+ const publicPost = await postService.create({
345
+ format: "note",
346
+ body: tiptapDoc("Public post about lanterns"),
347
+ });
348
+ // A reply stores no visibility of its own; it inherits the root's.
349
+ const [storedReply] = await db
350
+ .select({ visibility: posts.visibility })
351
+ .from(posts)
352
+ .where(eq(posts.id, reply.id));
353
+ expect(storedReply?.visibility).toBeNull();
354
+
355
+ const searchService = createSearchService(
356
+ createMockD1(sqlite),
357
+ DEFAULT_TEST_SITE_ID,
358
+ );
359
+ const results = await searchService.search("lanterns");
360
+
361
+ expect(results.map((r) => r.post.id)).toEqual([publicPost.id]);
362
+ expect(results.map((r) => r.post.id)).not.toContain(root.id);
363
+ });
364
+
365
+ it("leaves them out on the short-query LIKE path too", async () => {
366
+ await postService.create({
367
+ format: "note",
368
+ body: tiptapDoc("私密的灯笼"),
369
+ visibility: "private",
370
+ });
371
+
372
+ const searchService = createSearchService(
373
+ createMockD1(sqlite),
374
+ DEFAULT_TEST_SITE_ID,
375
+ );
376
+
377
+ expect(await searchService.search("灯笼")).toEqual([]);
378
+ expect(
379
+ await searchService.search("灯笼", { includePrivate: true }),
380
+ ).toHaveLength(1);
381
+ });
382
+
383
+ it("finds nothing when the only FTS match is private, instead of falling back to it", async () => {
384
+ await createPrivateThread();
385
+
386
+ const searchService = createSearchService(
387
+ createMockD1(sqlite),
388
+ DEFAULT_TEST_SITE_ID,
389
+ );
390
+
391
+ // No public match sends the query on to the LIKE statement, which has
392
+ // to apply the same rule.
393
+ expect(await searchService.search("Secret diary")).toEqual([]);
394
+ });
395
+
396
+ it("includes them, with the inherited visibility, when asked", async () => {
397
+ const { root, reply } = await createPrivateThread();
398
+
399
+ const searchService = createSearchService(
400
+ createMockD1(sqlite),
401
+ DEFAULT_TEST_SITE_ID,
402
+ );
403
+ const results = await searchService.search("lanterns", {
404
+ includePrivate: true,
405
+ });
406
+
407
+ const visibilityById = new Map(
408
+ results.map((r) => [r.post.id, r.post.visibility]),
409
+ );
410
+ expect(visibilityById.get(root.id)).toBe("private");
411
+ expect(visibilityById.get(reply.id)).toBe("private");
412
+ });
413
+
414
+ it("filters inside the query, so private matches don't use up the limit", async () => {
415
+ for (let i = 0; i < 3; i++) {
416
+ await postService.create({
417
+ format: "note",
418
+ body: tiptapDoc(`Private lantern note ${i}`),
419
+ visibility: "private",
420
+ });
421
+ }
422
+ for (let i = 0; i < 2; i++) {
423
+ await postService.create({
424
+ format: "note",
425
+ body: tiptapDoc(`Public lantern note ${i}`),
426
+ });
427
+ }
428
+
429
+ const searchService = createSearchService(
430
+ createMockD1(sqlite),
431
+ DEFAULT_TEST_SITE_ID,
432
+ );
433
+ const firstPage = await searchService.search("lantern", { limit: 2 });
434
+ const secondPage = await searchService.search("lantern", {
435
+ limit: 2,
436
+ offset: 2,
437
+ });
438
+
439
+ expect(firstPage).toHaveLength(2);
440
+ expect(firstPage.every((r) => r.post.visibility === "public")).toBe(true);
441
+ expect(secondPage).toEqual([]);
442
+ });
443
+ });
444
+
445
+ describe("private posts on Postgres", () => {
446
+ const VISIBILITY_CLAUSE =
447
+ "COALESCE(post.visibility, root_post.visibility) != 'private'";
448
+
449
+ /** Records every statement; returns no rows so FTS falls back to LIKE. */
450
+ function createCapturingRawQuery() {
451
+ const calls: { params: unknown[]; query: string }[] = [];
452
+ const rawQuery: RawQueryClient = {
453
+ prepare(query) {
454
+ const call = { params: [] as unknown[], query };
455
+ calls.push(call);
456
+ return {
457
+ bind(...params: unknown[]) {
458
+ call.params = params;
459
+ return this;
460
+ },
461
+ async all() {
462
+ return { results: [] };
463
+ },
464
+ };
465
+ },
466
+ };
467
+ return { calls, rawQuery };
468
+ }
469
+
470
+ it("applies the visibility rule in both the FTS and the LIKE statement", async () => {
471
+ const { calls, rawQuery } = createCapturingRawQuery();
472
+ const searchService = createSearchService(
473
+ rawQuery,
474
+ DEFAULT_TEST_SITE_ID,
475
+ "pg",
476
+ );
477
+
478
+ await searchService.search("lanterns", {
479
+ format: "note",
480
+ lang: "en",
481
+ limit: 5,
482
+ offset: 10,
483
+ });
484
+
485
+ expect(calls).toHaveLength(2);
486
+ expect(calls[0]?.query).toContain("search_document @@");
487
+ expect(calls[1]?.query).toContain("search_text ILIKE");
488
+ for (const call of calls) {
489
+ expect(call.query).toContain(VISIBILITY_CLAUSE);
490
+ }
491
+ // The rule binds no value, so every placeholder still lines up.
492
+ expect(calls[0]?.params).toEqual([
493
+ "lanterns:*",
494
+ DEFAULT_TEST_SITE_ID,
495
+ "published",
496
+ "note",
497
+ "en",
498
+ 5,
499
+ 10,
500
+ ]);
501
+ expect(calls[1]?.params).toEqual([
502
+ "lanterns",
503
+ "lanterns",
504
+ "lanterns",
505
+ "lanterns",
506
+ "%lanterns%",
507
+ DEFAULT_TEST_SITE_ID,
508
+ "published",
509
+ "note",
510
+ "en",
511
+ 5,
512
+ 10,
513
+ ]);
514
+ for (const call of calls) {
515
+ expect(call.query.split("?").length - 1).toBe(call.params.length);
516
+ }
517
+ });
518
+
519
+ it("drops the rule only when private posts are asked for", async () => {
520
+ const { calls, rawQuery } = createCapturingRawQuery();
521
+ const searchService = createSearchService(
522
+ rawQuery,
523
+ DEFAULT_TEST_SITE_ID,
524
+ "pg",
525
+ );
526
+
527
+ await searchService.search("lanterns", { includePrivate: true });
528
+ await searchService.search("灯笼", { includePrivate: true });
529
+
530
+ expect(calls).toHaveLength(3);
531
+ for (const call of calls) {
532
+ expect(call.query).not.toContain(VISIBILITY_CLAUSE);
533
+ }
534
+ });
535
+ });
536
+
326
537
  it("uses weighted FTS for Postgres searches with ts_headline snippets", async () => {
327
538
  const calls: { params: unknown[]; query: string }[] = [];
328
539
  const rawQuery: RawQueryClient = {
@@ -1028,7 +1028,8 @@ const mcpTools: McpToolDefinition[] = [
1028
1028
  },
1029
1029
  {
1030
1030
  name: "jant_search_posts",
1031
- description: "Search published posts.",
1031
+ description:
1032
+ "Search published posts, including private ones. Each result carries its visibility.",
1032
1033
  inputSchema: {
1033
1034
  type: "object",
1034
1035
  properties: {
@@ -1040,21 +1041,25 @@ const mcpTools: McpToolDefinition[] = [
1040
1041
  },
1041
1042
  async execute(args, context) {
1042
1043
  const input = SearchPostsToolSchema.parse(args ?? {});
1044
+ // MCP is the author's tool, so private posts match. Each result says
1045
+ // which visibility it has, so an agent can tell before linking one.
1043
1046
  const results = await context.services.search.search(input.query, {
1044
1047
  limit: input.limit,
1045
1048
  status: ["published"],
1049
+ includePrivate: true,
1046
1050
  });
1047
1051
 
1048
1052
  return {
1049
1053
  count: results.length,
1050
1054
  query: input.query,
1051
- results: results.map((result) =>
1052
- toSearchApiResult(
1055
+ results: results.map((result) => ({
1056
+ ...toSearchApiResult(
1053
1057
  result.post,
1054
1058
  result.snippet,
1055
1059
  context.appConfig.sitePathPrefix,
1056
1060
  ),
1057
- ),
1061
+ visibility: result.post.visibility,
1062
+ })),
1058
1063
  };
1059
1064
  },
1060
1065
  },
@@ -25,6 +25,13 @@ export interface SearchOptions {
25
25
  offset?: number;
26
26
  /** Filter by status */
27
27
  status?: Status[];
28
+ /**
29
+ * Whether private posts, and replies in a private Thread, may match.
30
+ *
31
+ * Defaults to `false`, so a caller that doesn't say who is asking gets what a
32
+ * signed-out reader would. Pass `true` only for the signed-in author.
33
+ */
34
+ includePrivate?: boolean;
28
35
  /** Filter by format */
29
36
  format?: Format;
30
37
  /**
@@ -167,6 +174,62 @@ function buildPgPrefixTsQuery(query: string): string | null {
167
174
  return terms.map((term) => `${term}:*`).join(" & ");
168
175
  }
169
176
 
177
+ /**
178
+ * A post's visibility after Thread-root inheritance, in the raw-SQL form every
179
+ * search statement shares. Each one joins the root as `root_post`. This is the
180
+ * same rule as `buildEffectiveVisibilityExpr` in `db/post-visibility.ts`.
181
+ */
182
+ const EFFECTIVE_VISIBILITY_SQL =
183
+ "COALESCE(post.visibility, root_post.visibility)";
184
+
185
+ /**
186
+ * The conditions every search statement applies after its own match clause:
187
+ * site, status, audience, format, and language.
188
+ *
189
+ * Built once so that no statement can leave one out. Each dialect and each
190
+ * match strategy has its own SQL, and a statement missing the visibility
191
+ * clause does not fail — it returns private posts.
192
+ *
193
+ * @param siteId - Site the search is scoped to
194
+ * @param options - Caller's search options
195
+ * @returns `AND …` lines to follow the match clause, and their bound values in order
196
+ *
197
+ * @example
198
+ * ```ts
199
+ * const filters = buildSearchFilters(siteId, { status: ["published"] });
200
+ * // filters.sql: "AND post.site_id = ?\nAND post.status IN (?)\nAND COALESCE(…) != 'private'"
201
+ * // filters.params: [siteId, "published"]
202
+ * ```
203
+ */
204
+ function buildSearchFilters(
205
+ siteId: string,
206
+ options: SearchOptions,
207
+ ): { sql: string; params: unknown[] } {
208
+ const status = options.status ?? ["published"];
209
+ const conditions = [
210
+ "post.site_id = ?",
211
+ `post.status IN (${status.map(() => "?").join(", ")})`,
212
+ ];
213
+ const params: unknown[] = [siteId, ...status];
214
+
215
+ if (!options.includePrivate) {
216
+ conditions.push(`${EFFECTIVE_VISIBILITY_SQL} != 'private'`);
217
+ }
218
+ if (options.format) {
219
+ conditions.push("post.format = ?");
220
+ params.push(options.format);
221
+ }
222
+ if (options.lang) {
223
+ conditions.push("post.language = ?");
224
+ params.push(options.lang);
225
+ }
226
+
227
+ return {
228
+ sql: conditions.map((condition) => `AND ${condition}`).join("\n"),
229
+ params,
230
+ };
231
+ }
232
+
170
233
  const PG_TS_HEADLINE_OPTIONS = [
171
234
  "MaxWords=18",
172
235
  "MinWords=6",
@@ -188,12 +251,7 @@ export function createSearchService(
188
251
  ): Promise<SearchResult[]> {
189
252
  const limit = options.limit ?? 20;
190
253
  const offset = options.offset ?? 0;
191
- const status = options.status ?? ["published"];
192
- const statusPlaceholders = status.map(() => "?").join(", ");
193
- const formatFilter = options.format ? "AND post.format = ?" : "";
194
- const formatParams = options.format ? [options.format] : [];
195
- const langFilter = options.lang ? "AND post.language = ?" : "";
196
- const langParams = options.lang ? [options.lang] : [];
254
+ const filters = buildSearchFilters(siteId, options);
197
255
 
198
256
  if (databaseDialect === "sqlite") {
199
257
  const ftsQuery = buildSqliteFtsQuery(query);
@@ -202,7 +260,7 @@ export function createSearchService(
202
260
  const stmt = rawQuery.prepare(`
203
261
  SELECT
204
262
  post.*,
205
- COALESCE(post.visibility, root_post.visibility) AS effective_visibility,
263
+ ${EFFECTIVE_VISIBILITY_SQL} AS effective_visibility,
206
264
  path_registry.path AS slug,
207
265
  post_fts.rank AS rank,
208
266
  snippet(post_fts, 1, char(2), char(3), '...', 32) AS snippet
@@ -214,24 +272,13 @@ export function createSearchService(
214
272
  AND path_registry.site_id = post.site_id
215
273
  AND path_registry.kind = 'slug'
216
274
  WHERE post_fts MATCH ?
217
- AND post.site_id = ?
218
- AND post.status IN (${statusPlaceholders})
219
- ${formatFilter}
220
- ${langFilter}
275
+ ${filters.sql}
221
276
  ORDER BY post_fts.rank
222
277
  LIMIT ? OFFSET ?
223
278
  `);
224
279
 
225
280
  const { results } = await stmt
226
- .bind(
227
- ftsQuery,
228
- siteId,
229
- ...status,
230
- ...formatParams,
231
- ...langParams,
232
- limit,
233
- offset,
234
- )
281
+ .bind(ftsQuery, ...filters.params, limit, offset)
235
282
  .all<RawSearchRow>();
236
283
 
237
284
  return withSnippetFallback((results || []).map(mapRow), query);
@@ -250,7 +297,7 @@ export function createSearchService(
250
297
  )
251
298
  SELECT
252
299
  post.*,
253
- COALESCE(post.visibility, root_post.visibility) AS effective_visibility,
300
+ ${EFFECTIVE_VISIBILITY_SQL} AS effective_visibility,
254
301
  path_registry.path AS slug,
255
302
  ts_rank_cd(post.search_document, search_query.tsq, 32) AS rank,
256
303
  NULLIF(
@@ -297,24 +344,13 @@ export function createSearchService(
297
344
  AND path_registry.site_id = post.site_id
298
345
  AND path_registry.kind = 'slug'
299
346
  WHERE post.search_document @@ search_query.tsq
300
- AND post.site_id = ?
301
- AND post.status IN (${statusPlaceholders})
302
- ${formatFilter}
303
- ${langFilter}
347
+ ${filters.sql}
304
348
  ORDER BY rank DESC, post.published_at DESC NULLS LAST, post.id DESC
305
349
  LIMIT ? OFFSET ?
306
350
  `);
307
351
 
308
352
  const { results } = await stmt
309
- .bind(
310
- tsQuery,
311
- siteId,
312
- ...status,
313
- ...formatParams,
314
- ...langParams,
315
- limit,
316
- offset,
317
- )
353
+ .bind(tsQuery, ...filters.params, limit, offset)
318
354
  .all<RawSearchRow>();
319
355
 
320
356
  return withSnippetFallback((results || []).map(mapRow), query);
@@ -326,19 +362,14 @@ export function createSearchService(
326
362
  ): Promise<SearchResult[]> {
327
363
  const limit = options.limit ?? 20;
328
364
  const offset = options.offset ?? 0;
329
- const status = options.status ?? ["published"];
330
365
  const like = `%${query}%`;
331
- const statusPlaceholders = status.map(() => "?").join(", ");
332
- const formatFilter = options.format ? "AND post.format = ?" : "";
333
- const formatParams = options.format ? [options.format] : [];
334
- const langFilter = options.lang ? "AND post.language = ?" : "";
335
- const langParams = options.lang ? [options.lang] : [];
366
+ const filters = buildSearchFilters(siteId, options);
336
367
 
337
368
  if (databaseDialect === "pg") {
338
369
  const stmt = rawQuery.prepare(`
339
370
  SELECT
340
371
  post.*,
341
- COALESCE(post.visibility, root_post.visibility) AS effective_visibility,
372
+ ${EFFECTIVE_VISIBILITY_SQL} AS effective_visibility,
342
373
  path_registry.path AS slug,
343
374
  GREATEST(
344
375
  similarity(coalesce(post.title, ''), ?),
@@ -354,10 +385,7 @@ export function createSearchService(
354
385
  AND path_registry.site_id = post.site_id
355
386
  AND path_registry.kind = 'slug'
356
387
  WHERE post.search_text ILIKE ?
357
- AND post.site_id = ?
358
- AND post.status IN (${statusPlaceholders})
359
- ${formatFilter}
360
- ${langFilter}
388
+ ${filters.sql}
361
389
  ORDER BY rank DESC, post.published_at DESC NULLS LAST, post.id DESC
362
390
  LIMIT ? OFFSET ?
363
391
  `);
@@ -369,10 +397,7 @@ export function createSearchService(
369
397
  query,
370
398
  query,
371
399
  like,
372
- siteId,
373
- ...status,
374
- ...formatParams,
375
- ...langParams,
400
+ ...filters.params,
376
401
  limit,
377
402
  offset,
378
403
  )
@@ -387,7 +412,7 @@ export function createSearchService(
387
412
  const stmt = rawQuery.prepare(`
388
413
  SELECT
389
414
  post.*,
390
- COALESCE(post.visibility, root_post.visibility) AS effective_visibility,
415
+ ${EFFECTIVE_VISIBILITY_SQL} AS effective_visibility,
391
416
  path_registry.path AS slug,
392
417
  0 AS rank,
393
418
  NULL AS snippet
@@ -403,27 +428,13 @@ export function createSearchService(
403
428
  post.quote_text ${likeOperator} ? OR
404
429
  post.url ${likeOperator} ?
405
430
  )
406
- AND post.site_id = ?
407
- AND post.status IN (${statusPlaceholders})
408
- ${formatFilter}
409
- ${langFilter}
431
+ ${filters.sql}
410
432
  ${likeOrderBy}
411
433
  LIMIT ? OFFSET ?
412
434
  `);
413
435
 
414
436
  const { results } = await stmt
415
- .bind(
416
- like,
417
- like,
418
- like,
419
- like,
420
- siteId,
421
- ...status,
422
- ...formatParams,
423
- ...langParams,
424
- limit,
425
- offset,
426
- )
437
+ .bind(like, like, like, like, ...filters.params, limit, offset)
427
438
  .all<RawSearchRow>();
428
439
 
429
440
  return withSnippetFallback((results || []).map(mapRow), query);