@jant/core 0.8.1 → 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.
Files changed (47) hide show
  1. package/dist/{app--uxnaIRV.js → app-B3P2N2vM.js} +1346 -310
  2. package/dist/client/.vite/manifest.json +2 -2
  3. package/dist/client/_assets/{client-auth-begiT1tq.js → client-auth-CeGBe0qG.js} +1 -1
  4. package/dist/client/_assets/{client-BT6kpcuG.js → client-nByuF0Ct.js} +1 -1
  5. package/dist/index.js +1 -1
  6. package/dist/node.js +2 -2
  7. package/package.json +1 -1
  8. package/src/__tests__/helpers/cursor-walk.ts +37 -7
  9. package/src/app.tsx +4 -0
  10. package/src/lib/__tests__/discover.test.ts +24 -0
  11. package/src/lib/__tests__/thread-fold.test.ts +43 -0
  12. package/src/lib/api-posts.ts +110 -2
  13. package/src/lib/api-public-posts.ts +263 -0
  14. package/src/lib/api-threads.ts +207 -0
  15. package/src/lib/discover.ts +34 -0
  16. package/src/lib/thread-fold.ts +83 -41
  17. package/src/lib/thread-query.ts +143 -0
  18. package/src/middleware/cors.ts +8 -1
  19. package/src/middleware/deprecation.ts +48 -0
  20. package/src/routes/api/__tests__/mcp.test.ts +143 -0
  21. package/src/routes/api/__tests__/posts.test.ts +78 -0
  22. package/src/routes/api/__tests__/public-access.test.ts +5 -0
  23. package/src/routes/api/__tests__/search.test.ts +104 -0
  24. package/src/routes/api/__tests__/threads.test.ts +115 -0
  25. package/src/routes/api/posts.ts +15 -104
  26. package/src/routes/api/public/__tests__/threads.test.ts +363 -0
  27. package/src/routes/api/public/archive.ts +82 -81
  28. package/src/routes/api/public/posts.ts +97 -298
  29. package/src/routes/api/public/threads.ts +141 -0
  30. package/src/routes/api/search.ts +31 -2
  31. package/src/routes/api/threads.ts +128 -0
  32. package/src/routes/auth/__tests__/setup-discover.test.ts +29 -1
  33. package/src/routes/auth/__tests__/setup-page.test.tsx +59 -1
  34. package/src/routes/auth/setup.tsx +48 -9
  35. package/src/routes/pages/__tests__/search.test.ts +58 -0
  36. package/src/routes/pages/search.tsx +3 -1
  37. package/src/services/__tests__/search.test.ts +211 -0
  38. package/src/services/__tests__/thread.test.ts +495 -0
  39. package/src/services/index.ts +4 -0
  40. package/src/services/mcp.ts +217 -57
  41. package/src/services/post.ts +222 -43
  42. package/src/services/search.ts +77 -66
  43. package/src/services/thread.ts +392 -0
  44. package/src/types/constants.ts +25 -0
  45. package/src/ui/feed/ThreadPreview.tsx +9 -14
  46. package/src/ui/feed/__tests__/thread-preview.test.ts +18 -22
  47. package/src/ui/feed/thread-preview-state.ts +0 -35
@@ -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
+ });
@@ -12,6 +12,32 @@ import { requirePublicApiAccess } from "../../middleware/public-content-access.j
12
12
 
13
13
  type Env = { Bindings: Bindings; Variables: AppVariables };
14
14
 
15
+ const DEFAULT_SEARCH_LIMIT = 20;
16
+ const MAX_SEARCH_LIMIT = 50;
17
+
18
+ /**
19
+ * Read `limit` from the query string, clamped to 1–50.
20
+ *
21
+ * Clamped rather than rejected: the endpoint has always capped a large value
22
+ * at 50, and a small one gets the same treatment. Anything that isn't a number
23
+ * falls back to the default.
24
+ *
25
+ * @param value - Raw `limit` query parameter
26
+ * @returns A page size between 1 and 50
27
+ *
28
+ * @example
29
+ * ```ts
30
+ * parseSearchLimit(undefined); // 20
31
+ * parseSearchLimit("-1"); // 1
32
+ * parseSearchLimit("500"); // 50
33
+ * ```
34
+ */
35
+ function parseSearchLimit(value: string | undefined): number {
36
+ const parsed = value === undefined ? Number.NaN : Number.parseInt(value, 10);
37
+ if (Number.isNaN(parsed)) return DEFAULT_SEARCH_LIMIT;
38
+ return Math.min(Math.max(parsed, 1), MAX_SEARCH_LIMIT);
39
+ }
40
+
15
41
  export const searchApiRoutes = new Hono<Env>();
16
42
 
17
43
  searchApiRoutes.use("*", requirePublicApiAccess());
@@ -40,13 +66,16 @@ searchApiRoutes.get("/", async (c) => {
40
66
  throw new ValidationError("Query too long");
41
67
  }
42
68
 
43
- const limitParam = c.req.query("limit");
44
- const limit = limitParam ? Math.min(parseInt(limitParam, 10) || 20, 50) : 20;
69
+ const limit = parseSearchLimit(c.req.query("limit"));
45
70
 
46
71
  try {
72
+ // One answer for every caller, as on `/api/public/*`: this endpoint is
73
+ // documented as public, so a session or token does not widen it. The
74
+ // author searches private posts on `/search` or through MCP.
47
75
  const results = await c.var.services.search.search(query, {
48
76
  limit,
49
77
  status: ["published"],
78
+ includePrivate: false,
50
79
  });
51
80
 
52
81
  return c.json({
@@ -0,0 +1,128 @@
1
+ /**
2
+ * Threads API Routes
3
+ *
4
+ * The author's view of Threads: every status and visibility, Posts in the
5
+ * editing view. The reader's view is `/api/public/threads`.
6
+ */
7
+
8
+ import { Hono } from "hono";
9
+ import { z } from "zod";
10
+ import type { Bindings } from "../../types.js";
11
+ import { THREAD_SORTS } from "../../types.js";
12
+ import type { AppVariables } from "../../types/app-context.js";
13
+ import {
14
+ ContentLanguageSchema,
15
+ StatusSchema,
16
+ parseValidated,
17
+ } from "../../lib/schemas.js";
18
+ import { requireAuthApi } from "../../middleware/auth.js";
19
+ import { loadApiPostResponses } from "../../lib/api-posts.js";
20
+ import { loadApiThreadResponses } from "../../lib/api-threads.js";
21
+ import {
22
+ parseThreadInclude,
23
+ parseThreadSelection,
24
+ } from "../../lib/thread-query.js";
25
+ import { NotFoundError, parseIdParam } from "../../lib/errors.js";
26
+ import { ID_PREFIX } from "../../lib/ids.js";
27
+
28
+ type Env = { Bindings: Bindings; Variables: AppVariables };
29
+
30
+ export const threadsApiRoutes = new Hono<Env>();
31
+
32
+ threadsApiRoutes.use("*", requireAuthApi());
33
+
34
+ /** The list's own parameters; everything else is a filter dimension. */
35
+ const ListThreadsQuerySchema = z.object({
36
+ status: StatusSchema.optional(),
37
+ lang: ContentLanguageSchema.optional(),
38
+ sort: z.enum(THREAD_SORTS).optional(),
39
+ include: z.string().optional(),
40
+ cursor: z.string().optional(),
41
+ limit: z.coerce.number().int().min(1).max(100).optional().default(20),
42
+ });
43
+
44
+ const LIST_PARAMS = [
45
+ "status",
46
+ "lang",
47
+ "sort",
48
+ "include",
49
+ "cursor",
50
+ "limit",
51
+ ] as const;
52
+
53
+ const GetThreadQuerySchema = z.object({ include: z.string().optional() });
54
+
55
+ const ListThreadPostsQuerySchema = z.object({
56
+ status: StatusSchema.optional(),
57
+ cursor: z.string().optional(),
58
+ limit: z.coerce.number().int().min(1).max(100).optional().default(100),
59
+ });
60
+
61
+ threadsApiRoutes.get("/", async (c) => {
62
+ const query = parseValidated(ListThreadsQuerySchema, c.req.query());
63
+ const { fold } = parseThreadInclude(query.include);
64
+ const parsed = await parseThreadSelection(
65
+ (key) => c.req.query(key),
66
+ Object.keys(c.req.query()),
67
+ {
68
+ allow: LIST_PARAMS,
69
+ audience: "author",
70
+ loadCollections: () => c.var.services.collections.list(),
71
+ },
72
+ );
73
+ if (parsed.kind === "empty") {
74
+ return c.json({ threads: [], nextCursor: null });
75
+ }
76
+
77
+ const { threads, nextCursor } = await c.var.services.threads.listThreads(
78
+ {
79
+ audience: "author",
80
+ status: query.status,
81
+ selection: parsed.selection,
82
+ lang: query.lang,
83
+ sort: query.sort,
84
+ fold,
85
+ },
86
+ { cursor: query.cursor, limit: query.limit },
87
+ );
88
+
89
+ return c.json({
90
+ threads: await loadApiThreadResponses(c.var, threads),
91
+ nextCursor,
92
+ });
93
+ });
94
+
95
+ threadsApiRoutes.get("/:id", async (c) => {
96
+ const id = parseIdParam(c.req.param("id"), ID_PREFIX.post);
97
+ const { include } = parseValidated(GetThreadQuerySchema, c.req.query());
98
+ const root = await c.var.services.threads.findRoot({ id }, "author");
99
+ if (!root) throw new NotFoundError("Thread");
100
+
101
+ const summaries = await c.var.services.threads.summarize(
102
+ [root],
103
+ parseThreadInclude(include),
104
+ );
105
+ const [thread] = await loadApiThreadResponses(c.var, summaries);
106
+ if (!thread) throw new NotFoundError("Thread");
107
+ return c.json(thread);
108
+ });
109
+
110
+ threadsApiRoutes.get("/:id/posts", async (c) => {
111
+ const id = parseIdParam(c.req.param("id"), ID_PREFIX.post);
112
+ const { status, cursor, limit } = parseValidated(
113
+ ListThreadPostsQuerySchema,
114
+ c.req.query(),
115
+ );
116
+ const root = await c.var.services.threads.findRoot({ id }, "author");
117
+ if (!root) throw new NotFoundError("Thread");
118
+
119
+ const { posts, nextCursor } = await c.var.services.threads.listPosts(
120
+ root.id,
121
+ { audience: "author", status },
122
+ { cursor, limit },
123
+ );
124
+ return c.json({
125
+ posts: await loadApiPostResponses(c.var, posts),
126
+ nextCursor,
127
+ });
128
+ });
@@ -6,7 +6,8 @@
6
6
  * field at all writes nothing, since that last case is an older client or a
7
7
  * scripted setup and reading it as a refusal would opt sites out that never
8
8
  * said so — and then acts on it, on the same terms as the settings page's own
9
- * switch.
9
+ * switch. The one exception is a language the directory does not list: the
10
+ * question was not on screen, so whatever the form sent is not an answer.
10
11
  */
11
12
 
12
13
  import { describe, expect, it, vi } from "vitest";
@@ -144,6 +145,33 @@ describe("POST /setup, the Discover answer", () => {
144
145
  expect(updateDiscoverSetting).not.toHaveBeenCalled();
145
146
  });
146
147
 
148
+ // The box is off screen for a language the directory does not list, but its
149
+ // signal still goes out holding the starting state — ticked on hosted Jant,
150
+ // clear on a self-hosted install. Neither is an answer anyone gave.
151
+ it.each([true, false])(
152
+ "writes nothing for a language the directory does not list (box %s)",
153
+ async (discover) => {
154
+ const { app, updateDiscoverSetting, announceToDiscover, pending } =
155
+ createSetupApp();
156
+
157
+ await post(app, { contentLanguage: "en", discover });
158
+ await Promise.all(pending);
159
+
160
+ expect(updateDiscoverSetting).not.toHaveBeenCalled();
161
+ expect(announceToDiscover).not.toHaveBeenCalled();
162
+ },
163
+ );
164
+
165
+ it("takes the answer for Traditional Chinese as well", async () => {
166
+ const { app, updateDiscoverSetting } = createSetupApp();
167
+
168
+ await post(app, { contentLanguage: "zh-Hant", discover: true });
169
+
170
+ expect(updateDiscoverSetting).toHaveBeenCalledWith("latest", {
171
+ demoMode: false,
172
+ });
173
+ });
174
+
147
175
  // The answer takes effect now, not when the author next happens to open a
148
176
  // settings page they have no reason to open. A directory decides eligibility
149
177
  // for itself and re-reads the feed on its own schedule, so a site with
@@ -13,7 +13,11 @@ import { renderToString } from "hono/jsx/dom/server";
13
13
  import { describe, expect, it } from "vitest";
14
14
  import { I18nProvider } from "../../../i18n/context.js";
15
15
  import { createI18n } from "../../../i18n/i18n.js";
16
- import type { DiscoverPageUrls } from "../../../lib/discover.js";
16
+ import {
17
+ isDiscoverLanguage,
18
+ type DiscoverPageUrls,
19
+ } from "../../../lib/discover.js";
20
+ import { SUPPORTED_LOCALE_TAGS } from "../../../i18n/supported-locales.js";
17
21
  import { getDiscoverCopy } from "../../../ui/dash/settings/discover-copy.js";
18
22
  import { SetupContent } from "../setup.js";
19
23
 
@@ -26,6 +30,23 @@ const PAGES: DiscoverPageUrls = {
26
30
  rules: "https://jant.me/discover/about",
27
31
  };
28
32
 
33
+ /** The Discover field's opening tag, which carries whether it is on screen. */
34
+ function discoverFieldTag(html: string): string {
35
+ const tag = html.match(/<div class="field mt-2"[^>]*>/)?.[0];
36
+ if (!tag) throw new Error("No Discover field in the rendered form");
37
+ return tag;
38
+ }
39
+
40
+ /** Undo the attribute escaping the renderer applies. */
41
+ function unescapeAttribute(value: string): string {
42
+ return value
43
+ .replace(/&quot;/g, '"')
44
+ .replace(/&#39;/g, "'")
45
+ .replace(/&lt;/g, "<")
46
+ .replace(/&gt;/g, ">")
47
+ .replace(/&amp;/g, "&");
48
+ }
49
+
29
50
  function render(props: SetupProps): string {
30
51
  const i18n = createI18n("en");
31
52
  const c = {
@@ -150,6 +171,43 @@ describe("SetupContent — the Discover question", () => {
150
171
  expect(html).toContain("discover: false");
151
172
  });
152
173
 
174
+ // The directory lists Chinese only. Asking anyone else leads nowhere, and on
175
+ // hosted Jant the box would arrive ticked.
176
+ it("stays off screen for a language the directory does not list", () => {
177
+ const tag = discoverFieldTag(render({ ...base, contentLanguage: "en" }));
178
+
179
+ expect(tag).toContain('style="display:none"');
180
+ });
181
+
182
+ it("is on screen from the start for a language the directory lists", () => {
183
+ const tag = discoverFieldTag(
184
+ render({ ...base, contentLanguage: "zh-Hant" }),
185
+ );
186
+
187
+ expect(tag).not.toContain("display:none");
188
+ });
189
+
190
+ // The field follows the picker on the client and the answer is filtered on
191
+ // the server, so the two have to read the language the same way. The
192
+ // expression is plain JavaScript once Datastar has swapped its signal in, and
193
+ // `$contentLanguage` is a valid parameter name, so it runs here as written.
194
+ it("follows the picker the way the server reads the language", () => {
195
+ const tag = discoverFieldTag(render(base));
196
+ const expression = unescapeAttribute(
197
+ tag.match(/data-show="([^"]*)"/)?.[1] ?? "",
198
+ );
199
+ const shows = new Function(
200
+ "$contentLanguage",
201
+ `return (${expression});`,
202
+ ) as (language: string) => boolean;
203
+
204
+ for (const language of [...SUPPORTED_LOCALE_TAGS, "zh", "zh-TW"]) {
205
+ expect(shows(language), language).toBe(isDiscoverLanguage(language));
206
+ }
207
+ expect(shows("zh-Hans")).toBe(true);
208
+ expect(shows("en")).toBe(false);
209
+ });
210
+
153
211
  // One control appearing twice should not describe itself two ways: the
154
212
  // label is the settings page's, and the help line is the settings page's
155
213
  // opening sentence rather than a wording of setup's own. The delay, the
@@ -67,8 +67,10 @@ import { toPublicPath } from "../../lib/url.js";
67
67
  import { ONBOARDING_STATUS } from "../../lib/constants.js";
68
68
  import { announceInBackground } from "../discover-announce.js";
69
69
  import {
70
+ DISCOVER_LANGUAGES,
70
71
  discoverIntroRuns,
71
72
  getDiscoverPageUrls,
73
+ isDiscoverLanguage,
72
74
  resolveDiscoverMode,
73
75
  type DiscoverPageUrls,
74
76
  } from "../../lib/discover.js";
@@ -142,6 +144,14 @@ const LocaleField: FC<{
142
144
  );
143
145
  };
144
146
 
147
+ /**
148
+ * `isDiscoverLanguage` as a Datastar expression over the language signal, so
149
+ * the Discover question follows the picker without a round trip. The list is
150
+ * the same constant the server reads, and a test holds the two readings of it
151
+ * together.
152
+ */
153
+ const DISCOVER_LANGUAGE_EXPRESSION = `${JSON.stringify(DISCOVER_LANGUAGES)}.includes($contentLanguage.split('-')[0].toLowerCase())`;
154
+
145
155
  /**
146
156
  * The one question setup asks about the world outside this site.
147
157
  *
@@ -151,6 +161,11 @@ const LocaleField: FC<{
151
161
  * settings page's own: one control appearing twice should not describe itself
152
162
  * two ways.
153
163
  *
164
+ * Only on screen while the chosen language is one the directory lists. For any
165
+ * other, learning the directory exists leads nowhere, and on hosted Jant the box
166
+ * would arrive ticked — a promise nobody keeps. It follows the language picker
167
+ * above it, since both are answered on the same screen.
168
+ *
154
169
  * The words come from the same `getDiscoverCopy` the settings page uses,
155
170
  * which is what keeps the two surfaces from drifting when either is edited.
156
171
  * The label is the settings page's; the help line is the settings page's
@@ -164,14 +179,22 @@ const DiscoverField: FC<{
164
179
  copy: DiscoverCopy;
165
180
  /** The directory's pages, or null when this deployment announces to none. */
166
181
  pages: DiscoverPageUrls | null;
167
- }> = ({ copy, pages }) => {
182
+ /** Whether the language the form opens with is one the directory lists. */
183
+ shown: boolean;
184
+ }> = ({ copy, pages, shown }) => {
168
185
  const runs = discoverIntroRuns(copy.about, copy, pages);
169
186
 
170
187
  return (
171
188
  // Held a little further from the field above it than the form's own gap:
172
189
  // every other field on this screen is about the site itself, and this one
173
- // is about the world outside it.
174
- <div class="field mt-2">
190
+ // is about the world outside it. Hidden in the markup as well as by
191
+ // `data-show`, so a form opening in English never flashes the question
192
+ // before Datastar starts.
193
+ <div
194
+ class="field mt-2"
195
+ data-show={DISCOVER_LANGUAGE_EXPRESSION}
196
+ style={shown ? undefined : "display:none"}
197
+ >
175
198
  {/* Classes copied from the settings page's own Discover checkbox
176
199
  (`jant-settings-general.ts`), so the control a hosted author meets here
177
200
  and the one they find later in Settings are the same object. */}
@@ -410,6 +433,8 @@ export type SetupContentProps = {
410
433
  *
411
434
  * False where the answer could not be honoured — a demo site, or feeds
412
435
  * switched off — and a control nobody can act on is worse than none.
436
+ * Where it is true, the question still waits for a language the
437
+ * directory lists; see `DiscoverField`.
413
438
  */
414
439
  discoverAvailable: boolean;
415
440
  /**
@@ -474,11 +499,15 @@ export const SetupContent: FC<SetupContentProps> = (props) => {
474
499
  }),
475
500
  );
476
501
 
477
- // Rendered even when the question is not asked, so the form can name the
478
- // signal unconditionally; `discoverAvailable` decides whether the control
502
+ // The signal exists even when the question is not asked, so the form can
503
+ // name it unconditionally; `discoverAvailable` decides whether the control
479
504
  // appears, and an absent field simply sends the default back.
480
505
  const discoverField = discoverAvailable ? (
481
- <DiscoverField copy={getDiscoverCopy(i18n)} pages={discoverPages} />
506
+ <DiscoverField
507
+ copy={getDiscoverCopy(i18n)}
508
+ pages={discoverPages}
509
+ shown={isDiscoverLanguage(contentLanguage)}
510
+ />
482
511
  ) : null;
483
512
 
484
513
  const signals = [
@@ -630,6 +659,12 @@ export const SetupContent: FC<SetupContentProps> = (props) => {
630
659
  * is the whole point of asking. An absent field is not a refusal — an older
631
660
  * client or a scripted setup sends none — and leaves the default in force.
632
661
  *
662
+ * Neither is a value sent with a language the directory does not list. The
663
+ * question was not on screen for it, but the signal behind the hidden box still
664
+ * went out holding its starting state, and storing that would record an answer
665
+ * to a question nobody was asked. It is dropped, and the default stays in
666
+ * force — the same as sending nothing.
667
+ *
633
668
  * A yes is announced immediately, on the same terms as the settings page's own
634
669
  * switch. A directory decides eligibility for itself and re-reads the feed on
635
670
  * its own schedule, so a site with nothing published yet loses nothing by
@@ -644,12 +679,16 @@ export const SetupContent: FC<SetupContentProps> = (props) => {
644
679
  * page's status block reads it back with a Retry beside it.
645
680
  *
646
681
  * @param c - The setup request, for its services and its config
647
- * @param answer - The checkbox, or undefined when the form carried no field
682
+ * @param answers - The validated answers: the checkbox, or undefined when the
683
+ * form carried no field, and the language it was answered for
648
684
  */
649
685
  async function storeDiscoverAnswer(
650
686
  c: Context<Env>,
651
- answer: boolean | undefined,
687
+ answers: SetupLanguageAnswers,
652
688
  ): Promise<void> {
689
+ const answer = isDiscoverLanguage(answers.contentLanguage)
690
+ ? answers.discover
691
+ : undefined;
653
692
  if (answer === undefined) return;
654
693
  const stored = answer ? "latest" : "off";
655
694
  const { shouldAnnounce } =
@@ -959,7 +998,7 @@ async function storeSiteAnswers(
959
998
  },
960
999
  );
961
1000
 
962
- await storeDiscoverAnswer(c, answers.discover);
1001
+ await storeDiscoverAnswer(c, answers);
963
1002
 
964
1003
  return dsRedirect(toPublicPath("/", c.var.appConfig.sitePathPrefix));
965
1004
  }
@@ -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