@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.
Files changed (36) hide show
  1. package/dist/{app--uxnaIRV.js → app-BzH8jmh3.js} +1199 -262
  2. package/dist/client/.vite/manifest.json +2 -2
  3. package/dist/client/_assets/{client-BT6kpcuG.js → client-S8oryJ0m.js} +1 -1
  4. package/dist/client/_assets/{client-auth-begiT1tq.js → client-auth-Ds9G0IgR.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__/thread-fold.test.ts +43 -0
  11. package/src/lib/api-posts.ts +110 -2
  12. package/src/lib/api-public-posts.ts +263 -0
  13. package/src/lib/api-threads.ts +207 -0
  14. package/src/lib/thread-fold.ts +83 -41
  15. package/src/lib/thread-query.ts +143 -0
  16. package/src/middleware/cors.ts +8 -1
  17. package/src/middleware/deprecation.ts +48 -0
  18. package/src/routes/api/__tests__/mcp.test.ts +89 -0
  19. package/src/routes/api/__tests__/posts.test.ts +78 -0
  20. package/src/routes/api/__tests__/public-access.test.ts +5 -0
  21. package/src/routes/api/__tests__/threads.test.ts +115 -0
  22. package/src/routes/api/posts.ts +15 -104
  23. package/src/routes/api/public/__tests__/threads.test.ts +363 -0
  24. package/src/routes/api/public/archive.ts +82 -81
  25. package/src/routes/api/public/posts.ts +97 -298
  26. package/src/routes/api/public/threads.ts +141 -0
  27. package/src/routes/api/threads.ts +128 -0
  28. package/src/services/__tests__/thread.test.ts +495 -0
  29. package/src/services/index.ts +4 -0
  30. package/src/services/mcp.ts +208 -53
  31. package/src/services/post.ts +222 -43
  32. package/src/services/thread.ts +392 -0
  33. package/src/types/constants.ts +25 -0
  34. package/src/ui/feed/ThreadPreview.tsx +9 -14
  35. package/src/ui/feed/__tests__/thread-preview.test.ts +18 -22
  36. package/src/ui/feed/thread-preview-state.ts +0 -35
@@ -222,6 +222,84 @@ describe("Posts API Routes", () => {
222
222
  expect((await unreadable.json()).code).toBe("VALIDATION_ERROR");
223
223
  });
224
224
 
225
+ // The failure this guards: ordered by thread activity with pins first, a
226
+ // reply lifted a long-published root to the top — past a walk's cursor,
227
+ // so the walk never returned it.
228
+ it("orders published posts by publication alone", async () => {
229
+ const { app, services } = createTestApp({ authenticated: true });
230
+ app.route("/api/posts", postsApiRoutes);
231
+
232
+ const root = await services.posts.create({
233
+ format: "note",
234
+ bodyMarkdown: "old root",
235
+ publishedAt: 1000,
236
+ });
237
+ const pinned = await services.posts.create({
238
+ format: "note",
239
+ bodyMarkdown: "pinned",
240
+ publishedAt: 2000,
241
+ pinned: true,
242
+ });
243
+ const recent = await services.posts.create({
244
+ format: "note",
245
+ bodyMarkdown: "recent",
246
+ publishedAt: 3000,
247
+ });
248
+
249
+ const first = await (await app.request("/api/posts?limit=1")).json();
250
+ expect(first.posts[0].id).toBe(recent.id);
251
+ const reply = await services.posts.create({
252
+ format: "note",
253
+ bodyMarkdown: "late reply",
254
+ replyToId: root.id,
255
+ publishedAt: 4000,
256
+ });
257
+
258
+ const rest = await walkPostPages(app, "/api/posts", 1, first.nextCursor);
259
+ expect(rest).toEqual([pinned.id, root.id]);
260
+ const all = await (await app.request("/api/posts")).json();
261
+ expect(all.posts.map((post: { id: string }) => post.id)).toEqual([
262
+ reply.id,
263
+ recent.id,
264
+ pinned.id,
265
+ root.id,
266
+ ]);
267
+ expect(
268
+ all.posts.map(
269
+ (post: { threadPostCount: number }) => post.threadPostCount,
270
+ ),
271
+ ).toEqual([2, 1, 1, 2]);
272
+ });
273
+
274
+ it("keeps drafts in last-edited order", async () => {
275
+ const { app, services } = createTestApp({ authenticated: true });
276
+ app.route("/api/posts", postsApiRoutes);
277
+
278
+ const first = await services.posts.create({
279
+ format: "note",
280
+ bodyMarkdown: "first",
281
+ status: "draft",
282
+ createdAt: 1000,
283
+ updatedAt: 1000,
284
+ });
285
+ const second = await services.posts.create({
286
+ format: "note",
287
+ bodyMarkdown: "second",
288
+ status: "draft",
289
+ createdAt: 2000,
290
+ updatedAt: 2000,
291
+ });
292
+ await services.posts.update(first.id, { bodyMarkdown: "edited" });
293
+
294
+ const res = await app.request("/api/posts?status=draft");
295
+ const body = await res.json();
296
+ expect(body.posts.map((post: { id: string }) => post.id)).toEqual([
297
+ first.id,
298
+ second.id,
299
+ ]);
300
+ expect(body.posts[0].threadPostCount).toBe(0);
301
+ });
302
+
225
303
  it("serializes quote attribution as sourceName/sourceUrl", async () => {
226
304
  const { app, services } = createTestApp({ authenticated: true });
227
305
  app.route("/api/posts", postsApiRoutes);
@@ -4,12 +4,14 @@ import { collectionsApiRoutes } from "../collections.js";
4
4
  import { navItemsApiRoutes } from "../nav-items.js";
5
5
  import { publicArchiveApiRoutes } from "../public/archive.js";
6
6
  import { publicPostsApiRoutes } from "../public/posts.js";
7
+ import { publicThreadsApiRoutes } from "../public/threads.js";
7
8
  import { searchApiRoutes } from "../search.js";
8
9
 
9
10
  function mountPublicReadRoutes(authenticated = false) {
10
11
  const testApp = createTestApp({ authenticated, fts: true });
11
12
  testApp.app.route("/api/public/posts", publicPostsApiRoutes);
12
13
  testApp.app.route("/api/public/archive", publicArchiveApiRoutes);
14
+ testApp.app.route("/api/public/threads", publicThreadsApiRoutes);
13
15
  testApp.app.route("/api/search", searchApiRoutes);
14
16
  testApp.app.route("/api/collections", collectionsApiRoutes);
15
17
  testApp.app.route("/api/nav-items", navItemsApiRoutes);
@@ -21,6 +23,9 @@ describe("public API access setting", () => {
21
23
  "/api/public/posts",
22
24
  "/api/public/posts/missing",
23
25
  "/api/public/archive",
26
+ "/api/public/threads",
27
+ "/api/public/threads/missing",
28
+ "/api/public/threads/missing/posts",
24
29
  ])("returns 404 for %s when the public API is off", async (path) => {
25
30
  const { app, services } = mountPublicReadRoutes();
26
31
  await services.settings.set("PUBLIC_API_ENABLED", "false");
@@ -0,0 +1,115 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { createTestApp } from "../../../__tests__/helpers/app.js";
3
+ import { walkThreadPages } from "../../../__tests__/helpers/cursor-walk.js";
4
+ import type { CreatePost } from "../../../types.js";
5
+ import { threadsApiRoutes } from "../threads.js";
6
+
7
+ const DAY = 86_400;
8
+ const BASE = Date.UTC(2025, 0, 1) / 1000;
9
+ const day = (n: number) => BASE + n * DAY;
10
+
11
+ function setup(authenticated = true) {
12
+ const testApp = createTestApp({ authenticated });
13
+ testApp.app.route("/api/threads", threadsApiRoutes);
14
+ const post = (data: Partial<CreatePost>) =>
15
+ testApp.services.posts.create({
16
+ format: "note",
17
+ bodyMarkdown: "body",
18
+ ...data,
19
+ });
20
+ const get = async (path: string) => {
21
+ const res = await testApp.app.request(path);
22
+ return { status: res.status, body: await res.json() };
23
+ };
24
+ return { ...testApp, post, get };
25
+ }
26
+
27
+ const ids = (items: Array<{ id: string }>) => items.map((item) => item.id);
28
+
29
+ describe("Threads API Routes", () => {
30
+ it.each(["/api/threads", "/api/threads/pst_x", "/api/threads/pst_x/posts"])(
31
+ "requires authentication for %s",
32
+ async (path) => {
33
+ const { app } = setup(false);
34
+ expect((await app.request(path)).status).toBe(401);
35
+ },
36
+ );
37
+
38
+ it("lists every visibility by default, in the editing view", async () => {
39
+ const { post, get } = setup();
40
+ const shown = await post({ publishedAt: day(1) });
41
+ const hidden = await post({
42
+ publishedAt: day(2),
43
+ visibility: "latest_hidden",
44
+ });
45
+ const secret = await post({ publishedAt: day(3), visibility: "private" });
46
+ await post({ status: "draft" });
47
+
48
+ const { status, body } = await get("/api/threads");
49
+ expect(status).toBe(200);
50
+ expect(ids(body.threads)).toEqual([secret.id, hidden.id, shown.id]);
51
+ expect(body.threads[0].root).toMatchObject({
52
+ id: secret.id,
53
+ body: expect.any(String),
54
+ displayTitle: expect.any(String),
55
+ threadPostCount: 1,
56
+ });
57
+
58
+ const privateOnly = await get("/api/threads?visibility=private");
59
+ expect(ids(privateOnly.body.threads)).toEqual([secret.id]);
60
+ });
61
+
62
+ it("lists drafts by status", async () => {
63
+ const { post, get } = setup();
64
+ const draft = await post({ status: "draft" });
65
+ await post({ publishedAt: day(1) });
66
+ const { body } = await get("/api/threads?status=draft");
67
+ expect(ids(body.threads)).toEqual([draft.id]);
68
+ expect(body.threads[0].postCount).toBe(0);
69
+ });
70
+
71
+ it("walks with a cursor", async () => {
72
+ const { post, get, app } = setup();
73
+ for (let i = 0; i < 4; i++) await post({ publishedAt: day(i) });
74
+ const { body } = await get("/api/threads?sort=published&limit=100");
75
+ expect(
76
+ await walkThreadPages(app, "/api/threads?sort=published", 1),
77
+ ).toEqual(ids(body.threads));
78
+ });
79
+
80
+ it("returns a Thread, drafts included, by any of its Posts", async () => {
81
+ const { post, get } = setup();
82
+ const draft = await post({ status: "draft" });
83
+ const root = await post({ publishedAt: day(1) });
84
+ const reply = await post({ replyToId: root.id, publishedAt: day(2) });
85
+
86
+ const found = await get(`/api/threads/${reply.id}?include=fold`);
87
+ expect(found.status).toBe(200);
88
+ expect(found.body).toMatchObject({ id: root.id, postCount: 2 });
89
+ expect(found.body.fold).toEqual({
90
+ leading: [],
91
+ hidden: 0,
92
+ gap: null,
93
+ trailing: [expect.objectContaining({ id: reply.id })],
94
+ });
95
+
96
+ expect((await get(`/api/threads/${draft.id}`)).status).toBe(200);
97
+ expect((await get("/api/threads/not-an-id")).status).toBe(400);
98
+ expect(
99
+ (await get("/api/threads/pst_01jpyx3m7gw4w3h7m4bknq0v1d")).status,
100
+ ).toBe(404);
101
+ });
102
+
103
+ it("lists a Thread's Posts by status", async () => {
104
+ const { post, get } = setup();
105
+ const root = await post({ publishedAt: day(1) });
106
+ const reply = await post({ replyToId: root.id, publishedAt: day(2) });
107
+ const unsent = await post({ replyToId: reply.id, status: "draft" });
108
+
109
+ const published = await get(`/api/threads/${root.id}/posts`);
110
+ expect(ids(published.body.posts)).toEqual([root.id, reply.id]);
111
+ expect(published.body.posts[1].threadPostCount).toBe(2);
112
+ const drafts = await get(`/api/threads/${root.id}/posts?status=draft`);
113
+ expect(ids(drafts.body.posts)).toEqual([unsent.id]);
114
+ });
115
+ });
@@ -16,7 +16,11 @@ import {
16
16
  parseValidated,
17
17
  } from "../../lib/schemas.js";
18
18
  import { requireAuthApi } from "../../middleware/auth.js";
19
- import { toApiAttachment, toApiPost } from "../../lib/api-posts.js";
19
+ import {
20
+ apiPostListOrder,
21
+ loadApiPostResponse,
22
+ loadApiPostResponses,
23
+ } from "../../lib/api-posts.js";
20
24
  import { getPostDisplayTitle } from "../../lib/post-meta.js";
21
25
  import { assertFound, NotFoundError, parseIdParam } from "../../lib/errors.js";
22
26
  import { AddressQuerySchema, requestInternalPath } from "../../lib/address.js";
@@ -59,37 +63,14 @@ postsApiRoutes.get("/", requireAuthApi(), async (c) => {
59
63
  c.req.query(),
60
64
  );
61
65
 
66
+ const listStatus = status ?? "published";
62
67
  const { posts, nextCursor } = await c.var.services.posts.listPage(
63
- { format, status: status ?? "published" },
68
+ { format, status: listStatus, ...apiPostListOrder(listStatus) },
64
69
  { cursor, limit },
65
70
  );
66
71
 
67
- // Batch load media for all posts
68
- const postIds = posts.map((p) => p.id);
69
- const mediaMap = await c.var.services.media.getByPostIds(postIds);
70
- const {
71
- r2PublicUrl,
72
- imageTransformUrl,
73
- s3PublicUrl,
74
- localPublicUrl,
75
- sitePathPrefix,
76
- } = c.var.appConfig;
77
-
78
72
  return c.json({
79
- posts: posts.map((p) =>
80
- toApiPost(p, {
81
- attachments: (mediaMap.get(p.id) ?? []).map((m) =>
82
- toApiAttachment(
83
- m,
84
- r2PublicUrl,
85
- imageTransformUrl,
86
- s3PublicUrl,
87
- localPublicUrl,
88
- sitePathPrefix,
89
- ),
90
- ),
91
- }),
92
- ),
73
+ posts: await loadApiPostResponses(c.var, posts),
93
74
  nextCursor,
94
75
  });
95
76
  });
@@ -131,39 +112,14 @@ postsApiRoutes.get("/:id/content", requireAuthApi(), async (c) => {
131
112
  postsApiRoutes.get("/:id", requireAuthApi(), async (c) => {
132
113
  const id = parseIdParam(c.req.param("id"), ID_PREFIX.post);
133
114
 
134
- // Fetch post, media, collections and thread position in parallel (all keyed
135
- // by the same id)
136
- const [post, mediaList, threadCollections, threadPosition] =
137
- await Promise.all([
138
- c.var.services.posts.getById(id),
139
- c.var.services.media.getByPostId(id),
140
- c.var.services.collections.getCollectionsByPostId(id),
141
- c.var.services.posts.getThreadPosition(id),
142
- ]);
115
+ const [post, threadPosition] = await Promise.all([
116
+ c.var.services.posts.getById(id),
117
+ c.var.services.posts.getThreadPosition(id),
118
+ ]);
143
119
  const foundPost = assertFound(post, "Post");
144
- const {
145
- r2PublicUrl,
146
- imageTransformUrl,
147
- s3PublicUrl,
148
- localPublicUrl,
149
- sitePathPrefix,
150
- } = c.var.appConfig;
151
- const collectionIds = threadCollections.map((col) => col.id);
152
120
 
153
121
  return c.json({
154
- ...toApiPost(foundPost, {
155
- collectionIds,
156
- attachments: mediaList.map((m) =>
157
- toApiAttachment(
158
- m,
159
- r2PublicUrl,
160
- imageTransformUrl,
161
- s3PublicUrl,
162
- localPublicUrl,
163
- sitePathPrefix,
164
- ),
165
- ),
166
- }),
122
+ ...(await loadApiPostResponse(c.var, foundPost, { collectionIds: true })),
167
123
  threadPosition,
168
124
  });
169
125
  });
@@ -215,33 +171,10 @@ postsApiRoutes.post("/", requireAuthApi(), async (c) => {
215
171
  },
216
172
  );
217
173
 
218
- const mediaList = await c.var.services.media.getByPostId(post.id);
219
- const {
220
- r2PublicUrl,
221
- imageTransformUrl,
222
- s3PublicUrl,
223
- localPublicUrl,
224
- sitePathPrefix,
225
- } = c.var.appConfig;
226
-
227
174
  // Trigger GitHub Sync in background (no-op when sync isn't enabled).
228
175
  await triggerGitHubSyncInline(c);
229
176
 
230
- return c.json(
231
- toApiPost(post, {
232
- attachments: mediaList.map((m) =>
233
- toApiAttachment(
234
- m,
235
- r2PublicUrl,
236
- imageTransformUrl,
237
- s3PublicUrl,
238
- localPublicUrl,
239
- sitePathPrefix,
240
- ),
241
- ),
242
- }),
243
- 201,
244
- );
177
+ return c.json(await loadApiPostResponse(c.var, post), 201);
245
178
  });
246
179
 
247
180
  // Update post (requires auth)
@@ -293,29 +226,7 @@ postsApiRoutes.put("/:id", requireAuthApi(), async (c) => {
293
226
  // Trigger GitHub Sync in background (no-op when sync isn't enabled).
294
227
  await triggerGitHubSyncInline(c);
295
228
 
296
- const mediaList = await c.var.services.media.getByPostId(post.id);
297
- const {
298
- r2PublicUrl,
299
- imageTransformUrl,
300
- s3PublicUrl,
301
- localPublicUrl,
302
- sitePathPrefix,
303
- } = c.var.appConfig;
304
-
305
- return c.json(
306
- toApiPost(post, {
307
- attachments: mediaList.map((m) =>
308
- toApiAttachment(
309
- m,
310
- r2PublicUrl,
311
- imageTransformUrl,
312
- s3PublicUrl,
313
- localPublicUrl,
314
- sitePathPrefix,
315
- ),
316
- ),
317
- }),
318
- );
229
+ return c.json(await loadApiPostResponse(c.var, post));
319
230
  });
320
231
 
321
232
  // =============================================================================