@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
|
@@ -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
|
+
});
|
|
@@ -0,0 +1,495 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { createTestApp } from "../../__tests__/helpers/app.js";
|
|
3
|
+
import { ValidationError } from "../../lib/errors.js";
|
|
4
|
+
import type { CreatePost, Post } from "../../types.js";
|
|
5
|
+
import type { ThreadListQuery } from "../thread.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() {
|
|
12
|
+
const { services } = createTestApp();
|
|
13
|
+
const post = (data: Partial<CreatePost> & { title?: string }) =>
|
|
14
|
+
services.posts.create({ format: "note", bodyMarkdown: "body", ...data });
|
|
15
|
+
const reply = (parent: Post, at: number, data: Partial<CreatePost> = {}) =>
|
|
16
|
+
post({ replyToId: parent.id, publishedAt: at, createdAt: at, ...data });
|
|
17
|
+
const list = async (
|
|
18
|
+
query: Partial<ThreadListQuery> = {},
|
|
19
|
+
limit = 50,
|
|
20
|
+
): Promise<string[]> => {
|
|
21
|
+
const page = await services.threads.listThreads(
|
|
22
|
+
{ audience: "reader", selection: {}, ...query },
|
|
23
|
+
{ limit },
|
|
24
|
+
);
|
|
25
|
+
return page.threads.map((thread) => thread.root.id);
|
|
26
|
+
};
|
|
27
|
+
return { services, post, reply, list };
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
describe("ThreadService.listThreads", () => {
|
|
31
|
+
it("lists what the homepage lists by default", async () => {
|
|
32
|
+
const { post, reply, list } = setup();
|
|
33
|
+
const quiet = await post({ publishedAt: day(1) });
|
|
34
|
+
const pinned = await post({ publishedAt: day(0), pinned: true });
|
|
35
|
+
const active = await post({ publishedAt: day(2) });
|
|
36
|
+
await reply(active, day(9));
|
|
37
|
+
await post({ publishedAt: day(3), visibility: "latest_hidden" });
|
|
38
|
+
await post({ publishedAt: day(4), visibility: "private" });
|
|
39
|
+
await post({ status: "draft" });
|
|
40
|
+
|
|
41
|
+
// Pins first, then newest activity; hidden, private, and drafts left out.
|
|
42
|
+
expect(await list()).toEqual([pinned.id, active.id, quiet.id]);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("widens to Threads hidden from Latest on request", async () => {
|
|
46
|
+
const { post, list } = setup();
|
|
47
|
+
const shown = await post({ publishedAt: day(1) });
|
|
48
|
+
const hidden = await post({
|
|
49
|
+
publishedAt: day(2),
|
|
50
|
+
visibility: "latest_hidden",
|
|
51
|
+
});
|
|
52
|
+
await post({ publishedAt: day(3), visibility: "private" });
|
|
53
|
+
|
|
54
|
+
expect(await list({ includeHidden: true })).toEqual([hidden.id, shown.id]);
|
|
55
|
+
expect(await list({ selection: { visibility: "latest_hidden" } })).toEqual([
|
|
56
|
+
hidden.id,
|
|
57
|
+
]);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
// Each order is one the site already shows, and only the homepage's puts
|
|
61
|
+
// pins first.
|
|
62
|
+
it("orders by each named axis", async () => {
|
|
63
|
+
const { post, reply, list } = setup();
|
|
64
|
+
const first = await post({ publishedAt: day(1), rating: 3 });
|
|
65
|
+
const second = await post({ publishedAt: day(2), pinned: true });
|
|
66
|
+
const third = await post({ publishedAt: day(3), rating: 5 });
|
|
67
|
+
await reply(first, day(8));
|
|
68
|
+
await reply(third, day(9), { quietReply: true });
|
|
69
|
+
|
|
70
|
+
expect(await list({ sort: "activity" })).toEqual([
|
|
71
|
+
second.id,
|
|
72
|
+
first.id,
|
|
73
|
+
third.id,
|
|
74
|
+
]);
|
|
75
|
+
expect(await list({ sort: "published" })).toEqual([
|
|
76
|
+
third.id,
|
|
77
|
+
second.id,
|
|
78
|
+
first.id,
|
|
79
|
+
]);
|
|
80
|
+
// A quiet reply moves a Thread on `updated`, never on `activity`.
|
|
81
|
+
expect(await list({ sort: "updated" })).toEqual([
|
|
82
|
+
third.id,
|
|
83
|
+
first.id,
|
|
84
|
+
second.id,
|
|
85
|
+
]);
|
|
86
|
+
expect(await list({ sort: "oldest" })).toEqual([
|
|
87
|
+
first.id,
|
|
88
|
+
second.id,
|
|
89
|
+
third.id,
|
|
90
|
+
]);
|
|
91
|
+
expect(await list({ sort: "rating" })).toEqual([
|
|
92
|
+
third.id,
|
|
93
|
+
first.id,
|
|
94
|
+
second.id,
|
|
95
|
+
]);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it("lists featured Threads newest-published first and refuses another order", async () => {
|
|
99
|
+
const { services, post, reply, list } = setup();
|
|
100
|
+
const older = await post({ publishedAt: day(1), featured: true });
|
|
101
|
+
const newer = await post({ publishedAt: day(2), featured: true });
|
|
102
|
+
await post({ publishedAt: day(3) });
|
|
103
|
+
await reply(older, day(9));
|
|
104
|
+
|
|
105
|
+
expect(await list({ selection: { visibility: "featured" } })).toEqual([
|
|
106
|
+
newer.id,
|
|
107
|
+
older.id,
|
|
108
|
+
]);
|
|
109
|
+
await expect(
|
|
110
|
+
services.threads.listThreads(
|
|
111
|
+
{
|
|
112
|
+
audience: "reader",
|
|
113
|
+
selection: { visibility: "featured" },
|
|
114
|
+
sort: "activity",
|
|
115
|
+
},
|
|
116
|
+
{ limit: 10 },
|
|
117
|
+
),
|
|
118
|
+
).rejects.toBeInstanceOf(ValidationError);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("uses a collection's own order and pins when it is named", async () => {
|
|
122
|
+
const { services, post, reply, list } = setup();
|
|
123
|
+
const collection = await services.collections.create({
|
|
124
|
+
slug: "reading",
|
|
125
|
+
title: "Reading",
|
|
126
|
+
sortOrder: "oldest",
|
|
127
|
+
});
|
|
128
|
+
const early = await post({
|
|
129
|
+
publishedAt: day(1),
|
|
130
|
+
collectionIds: [collection.id],
|
|
131
|
+
});
|
|
132
|
+
const late = await post({
|
|
133
|
+
publishedAt: day(2),
|
|
134
|
+
collectionIds: [collection.id],
|
|
135
|
+
});
|
|
136
|
+
const pinnedInCollection = await post({
|
|
137
|
+
publishedAt: day(3),
|
|
138
|
+
collectionIds: [collection.id],
|
|
139
|
+
});
|
|
140
|
+
await post({ publishedAt: day(4) });
|
|
141
|
+
await reply(early, day(9));
|
|
142
|
+
await services.collections.pinThread(collection.id, pinnedInCollection.id);
|
|
143
|
+
|
|
144
|
+
const selection = { collection: [collection.id] };
|
|
145
|
+
// The collection's configured order, with its own pin on top.
|
|
146
|
+
expect(await list({ selection })).toEqual([
|
|
147
|
+
pinnedInCollection.id,
|
|
148
|
+
early.id,
|
|
149
|
+
late.id,
|
|
150
|
+
]);
|
|
151
|
+
expect(await list({ selection, sort: "activity" })).toEqual([
|
|
152
|
+
pinnedInCollection.id,
|
|
153
|
+
early.id,
|
|
154
|
+
late.id,
|
|
155
|
+
]);
|
|
156
|
+
// `published` reads the collection as a plain filter: no collection pins.
|
|
157
|
+
expect(await list({ selection, sort: "published" })).toEqual([
|
|
158
|
+
pinnedInCollection.id,
|
|
159
|
+
late.id,
|
|
160
|
+
early.id,
|
|
161
|
+
]);
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it("applies the other dimensions to a collection's own order", async () => {
|
|
165
|
+
const { services, post, list } = setup();
|
|
166
|
+
const collection = await services.collections.create({
|
|
167
|
+
slug: "log",
|
|
168
|
+
title: "Log",
|
|
169
|
+
});
|
|
170
|
+
const titled = await post({
|
|
171
|
+
title: "Titled",
|
|
172
|
+
publishedAt: day(1),
|
|
173
|
+
collectionIds: [collection.id],
|
|
174
|
+
});
|
|
175
|
+
await post({ publishedAt: day(2), collectionIds: [collection.id] });
|
|
176
|
+
const lastYear = await post({
|
|
177
|
+
title: "Last year",
|
|
178
|
+
publishedAt: BASE - 10 * DAY,
|
|
179
|
+
collectionIds: [collection.id],
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
expect(
|
|
183
|
+
await list({ selection: { collection: [collection.id], title: true } }),
|
|
184
|
+
).toEqual([titled.id, lastYear.id]);
|
|
185
|
+
expect(
|
|
186
|
+
await list({
|
|
187
|
+
selection: { collection: [collection.id], title: true, year: 2025 },
|
|
188
|
+
}),
|
|
189
|
+
).toEqual([titled.id]);
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
it("walks every order with a cursor, one Thread per page", async () => {
|
|
193
|
+
const { services, post, reply } = setup();
|
|
194
|
+
const collection = await services.collections.create({
|
|
195
|
+
slug: "all",
|
|
196
|
+
title: "All",
|
|
197
|
+
});
|
|
198
|
+
const roots: Post[] = [];
|
|
199
|
+
for (let i = 0; i < 5; i++) {
|
|
200
|
+
roots.push(
|
|
201
|
+
await post({
|
|
202
|
+
publishedAt: day(i),
|
|
203
|
+
rating: (i % 3) + 1,
|
|
204
|
+
collectionIds: [collection.id],
|
|
205
|
+
}),
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
await reply(roots[1] as Post, day(20));
|
|
209
|
+
await services.collections.pinThread(collection.id, roots[3]?.id ?? "");
|
|
210
|
+
|
|
211
|
+
const cases: Partial<ThreadListQuery>[] = [
|
|
212
|
+
{ sort: "activity" },
|
|
213
|
+
{ sort: "published" },
|
|
214
|
+
{ sort: "updated" },
|
|
215
|
+
{ sort: "oldest" },
|
|
216
|
+
{ sort: "rating" },
|
|
217
|
+
{ selection: { collection: [collection.id] } },
|
|
218
|
+
{ selection: { collection: [collection.id] }, sort: "rating" },
|
|
219
|
+
];
|
|
220
|
+
for (const query of cases) {
|
|
221
|
+
const full = await services.threads.listThreads(
|
|
222
|
+
{ audience: "reader", selection: {}, ...query },
|
|
223
|
+
{ limit: 50 },
|
|
224
|
+
);
|
|
225
|
+
const walked: string[] = [];
|
|
226
|
+
let cursor: string | undefined;
|
|
227
|
+
for (let page = 0; page < 10; page++) {
|
|
228
|
+
const result = await services.threads.listThreads(
|
|
229
|
+
{ audience: "reader", selection: {}, ...query },
|
|
230
|
+
{ limit: 1, cursor },
|
|
231
|
+
);
|
|
232
|
+
walked.push(...result.threads.map((thread) => thread.root.id));
|
|
233
|
+
if (!result.nextCursor) break;
|
|
234
|
+
cursor = result.nextCursor;
|
|
235
|
+
}
|
|
236
|
+
expect(walked).toEqual(full.threads.map((thread) => thread.root.id));
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
it("shows the author every status and visibility", async () => {
|
|
241
|
+
const { services, post } = setup();
|
|
242
|
+
const hidden = await post({
|
|
243
|
+
publishedAt: day(1),
|
|
244
|
+
visibility: "latest_hidden",
|
|
245
|
+
});
|
|
246
|
+
const secret = await post({ publishedAt: day(2), visibility: "private" });
|
|
247
|
+
const draft = await post({ status: "draft" });
|
|
248
|
+
|
|
249
|
+
const published = await services.threads.listThreads(
|
|
250
|
+
{ audience: "author", selection: {} },
|
|
251
|
+
{ limit: 10 },
|
|
252
|
+
);
|
|
253
|
+
expect(published.threads.map((thread) => thread.root.id)).toEqual([
|
|
254
|
+
secret.id,
|
|
255
|
+
hidden.id,
|
|
256
|
+
]);
|
|
257
|
+
const drafts = await services.threads.listThreads(
|
|
258
|
+
{ audience: "author", status: "draft", selection: {} },
|
|
259
|
+
{ limit: 10 },
|
|
260
|
+
);
|
|
261
|
+
expect(drafts.threads.map((thread) => thread.root.id)).toEqual([draft.id]);
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
it("never shows a reader private Threads", async () => {
|
|
265
|
+
const { services } = setup();
|
|
266
|
+
await expect(
|
|
267
|
+
services.threads.listThreads(
|
|
268
|
+
{ audience: "reader", selection: { visibility: "private" } },
|
|
269
|
+
{ limit: 10 },
|
|
270
|
+
),
|
|
271
|
+
).rejects.toBeInstanceOf(ValidationError);
|
|
272
|
+
});
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
describe("ThreadService.summarize", () => {
|
|
276
|
+
it("counts published Posts and folds the way the homepage does", async () => {
|
|
277
|
+
const { services, post, reply } = setup();
|
|
278
|
+
const root = await post({ publishedAt: day(0), createdAt: day(0) });
|
|
279
|
+
const replies: Post[] = [];
|
|
280
|
+
let parent = root;
|
|
281
|
+
for (let i = 1; i <= 8; i++) {
|
|
282
|
+
parent = await reply(parent, day(i));
|
|
283
|
+
replies.push(parent);
|
|
284
|
+
}
|
|
285
|
+
await services.posts.create({
|
|
286
|
+
format: "note",
|
|
287
|
+
bodyMarkdown: "unsent",
|
|
288
|
+
replyToId: parent.id,
|
|
289
|
+
status: "draft",
|
|
290
|
+
});
|
|
291
|
+
const lone = await post({ publishedAt: day(1) });
|
|
292
|
+
|
|
293
|
+
const [thread, single] = await services.threads.summarize([root, lone], {
|
|
294
|
+
fold: true,
|
|
295
|
+
});
|
|
296
|
+
expect(thread?.postCount).toBe(9);
|
|
297
|
+
expect(thread?.fold?.leadingReplies.map((p) => p.id)).toEqual(
|
|
298
|
+
replies.slice(0, 2).map((p) => p.id),
|
|
299
|
+
);
|
|
300
|
+
expect(thread?.fold?.trailingReplies.map((p) => p.id)).toEqual(
|
|
301
|
+
replies.slice(5, 7).map((p) => p.id),
|
|
302
|
+
);
|
|
303
|
+
expect(thread?.fold?.latestReply.id).toBe(replies[7]?.id);
|
|
304
|
+
expect(thread?.fold?.hiddenCount).toBe(3);
|
|
305
|
+
expect(thread?.fold?.firstHiddenReply?.id).toBe(replies[2]?.id);
|
|
306
|
+
|
|
307
|
+
expect(single?.postCount).toBe(1);
|
|
308
|
+
expect(single?.fold).toBeNull();
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
it("leaves the fold out unless asked", async () => {
|
|
312
|
+
const { services, post } = setup();
|
|
313
|
+
const root = await post({ publishedAt: day(0) });
|
|
314
|
+
const [summary] = await services.threads.summarize([root], {});
|
|
315
|
+
expect(summary).toEqual({ root, postCount: 1 });
|
|
316
|
+
});
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
describe("ThreadService.findRoot", () => {
|
|
320
|
+
it("finds a Thread by any of its Posts", async () => {
|
|
321
|
+
const { services, post, reply } = setup();
|
|
322
|
+
const root = await post({ publishedAt: day(0) });
|
|
323
|
+
const child = await reply(root, day(1));
|
|
324
|
+
|
|
325
|
+
expect(
|
|
326
|
+
(await services.threads.findRoot({ slug: child.slug }, "reader"))?.id,
|
|
327
|
+
).toBe(root.id);
|
|
328
|
+
expect(
|
|
329
|
+
(await services.threads.findRoot({ id: child.id }, "author"))?.id,
|
|
330
|
+
).toBe(root.id);
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
it("answers a reader only for what the site shows anyone", async () => {
|
|
334
|
+
const { services, post, reply } = setup();
|
|
335
|
+
const secret = await post({ publishedAt: day(0), visibility: "private" });
|
|
336
|
+
const hidden = await post({
|
|
337
|
+
publishedAt: day(1),
|
|
338
|
+
visibility: "latest_hidden",
|
|
339
|
+
});
|
|
340
|
+
const draft = await post({ status: "draft" });
|
|
341
|
+
const open = await post({ publishedAt: day(2) });
|
|
342
|
+
const tail = await reply(open, day(3));
|
|
343
|
+
const unsent = await services.posts.create({
|
|
344
|
+
format: "note",
|
|
345
|
+
bodyMarkdown: "unsent",
|
|
346
|
+
replyToId: tail.id,
|
|
347
|
+
status: "draft",
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
const find = (slug: string) =>
|
|
351
|
+
services.threads.findRoot({ slug }, "reader");
|
|
352
|
+
expect(await find(secret.slug)).toBeNull();
|
|
353
|
+
expect(await find(draft.slug)).toBeNull();
|
|
354
|
+
expect(await find(unsent.slug)).toBeNull();
|
|
355
|
+
expect((await find(hidden.slug))?.id).toBe(hidden.id);
|
|
356
|
+
expect(
|
|
357
|
+
(await services.threads.findRoot({ id: draft.id }, "author"))?.id,
|
|
358
|
+
).toBe(draft.id);
|
|
359
|
+
});
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
describe("posts.listThreadPostsPage", () => {
|
|
363
|
+
async function thread(count: number) {
|
|
364
|
+
const { services, post, reply } = setup();
|
|
365
|
+
const root = await post({ publishedAt: day(0), createdAt: day(0) });
|
|
366
|
+
const all: Post[] = [root];
|
|
367
|
+
let parent = root;
|
|
368
|
+
for (let i = 1; i < count; i++) {
|
|
369
|
+
parent = await reply(parent, day(i));
|
|
370
|
+
all.push(parent);
|
|
371
|
+
}
|
|
372
|
+
return { services, root, all, reply };
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
it("walks a Thread in Thread order, one Post per page", async () => {
|
|
376
|
+
const { services, root, all } = await thread(5);
|
|
377
|
+
const walked: string[] = [];
|
|
378
|
+
let cursor: string | undefined;
|
|
379
|
+
for (let page = 0; page < 10; page++) {
|
|
380
|
+
const result = await services.posts.listThreadPostsPage(
|
|
381
|
+
root.id,
|
|
382
|
+
{},
|
|
383
|
+
{ limit: 1, cursor },
|
|
384
|
+
);
|
|
385
|
+
walked.push(...result.posts.map((p) => p.id));
|
|
386
|
+
if (!result.nextCursor) break;
|
|
387
|
+
cursor = result.nextCursor;
|
|
388
|
+
}
|
|
389
|
+
expect(walked).toEqual(all.map((p) => p.id));
|
|
390
|
+
});
|
|
391
|
+
|
|
392
|
+
it("reaches a reply published while the walk is under way", async () => {
|
|
393
|
+
const { services, root, all, reply } = await thread(3);
|
|
394
|
+
const first = await services.posts.listThreadPostsPage(
|
|
395
|
+
root.id,
|
|
396
|
+
{},
|
|
397
|
+
{ limit: 2 },
|
|
398
|
+
);
|
|
399
|
+
const added = await reply(all[2] as Post, day(10));
|
|
400
|
+
const rest = await services.posts.listThreadPostsPage(
|
|
401
|
+
root.id,
|
|
402
|
+
{},
|
|
403
|
+
{ limit: 10, cursor: first.nextCursor ?? undefined },
|
|
404
|
+
);
|
|
405
|
+
expect(rest.posts.map((p) => p.id)).toEqual([all[2]?.id, added.id]);
|
|
406
|
+
});
|
|
407
|
+
|
|
408
|
+
it("starts after a Post of the Thread named by ID", async () => {
|
|
409
|
+
const { services, root, all } = await thread(6);
|
|
410
|
+
const page = await services.posts.listThreadPostsPage(
|
|
411
|
+
root.id,
|
|
412
|
+
{},
|
|
413
|
+
{ limit: 2, cursor: all[2]?.id },
|
|
414
|
+
);
|
|
415
|
+
expect(page.posts.map((p) => p.id)).toEqual([all[3]?.id, all[4]?.id]);
|
|
416
|
+
});
|
|
417
|
+
|
|
418
|
+
it("refuses an ID from another Thread or one it wouldn't list", async () => {
|
|
419
|
+
const { services, root, all } = await thread(2);
|
|
420
|
+
const other = await services.posts.create({
|
|
421
|
+
format: "note",
|
|
422
|
+
bodyMarkdown: "elsewhere",
|
|
423
|
+
});
|
|
424
|
+
const unsent = await services.posts.create({
|
|
425
|
+
format: "note",
|
|
426
|
+
bodyMarkdown: "unsent",
|
|
427
|
+
replyToId: all[1]?.id,
|
|
428
|
+
status: "draft",
|
|
429
|
+
});
|
|
430
|
+
for (const cursor of [other.id, unsent.id, "not-a-cursor"]) {
|
|
431
|
+
await expect(
|
|
432
|
+
services.posts.listThreadPostsPage(root.id, {}, { limit: 5, cursor }),
|
|
433
|
+
).rejects.toBeInstanceOf(ValidationError);
|
|
434
|
+
}
|
|
435
|
+
// A cursor from a different list names a position on another axis.
|
|
436
|
+
const listCursor = (
|
|
437
|
+
await services.posts.listPage({ status: "published" }, { limit: 1 })
|
|
438
|
+
).nextCursor;
|
|
439
|
+
await expect(
|
|
440
|
+
services.posts.listThreadPostsPage(
|
|
441
|
+
root.id,
|
|
442
|
+
{},
|
|
443
|
+
{ limit: 5, cursor: listCursor ?? undefined },
|
|
444
|
+
),
|
|
445
|
+
).rejects.toBeInstanceOf(ValidationError);
|
|
446
|
+
});
|
|
447
|
+
|
|
448
|
+
it("lists drafts only when asked", async () => {
|
|
449
|
+
const { services, root, all } = await thread(2);
|
|
450
|
+
const unsent = await services.posts.create({
|
|
451
|
+
format: "note",
|
|
452
|
+
bodyMarkdown: "unsent",
|
|
453
|
+
replyToId: all[1]?.id,
|
|
454
|
+
status: "draft",
|
|
455
|
+
});
|
|
456
|
+
const published = await services.posts.listThreadPostsPage(
|
|
457
|
+
root.id,
|
|
458
|
+
{},
|
|
459
|
+
{ limit: 10 },
|
|
460
|
+
);
|
|
461
|
+
expect(published.posts.map((p) => p.id)).toEqual(all.map((p) => p.id));
|
|
462
|
+
const drafts = await services.posts.listThreadPostsPage(
|
|
463
|
+
root.id,
|
|
464
|
+
{ status: "draft" },
|
|
465
|
+
{ limit: 10 },
|
|
466
|
+
);
|
|
467
|
+
expect(drafts.posts.map((p) => p.id)).toEqual([unsent.id]);
|
|
468
|
+
});
|
|
469
|
+
});
|
|
470
|
+
|
|
471
|
+
describe("posts.countThreadPosts", () => {
|
|
472
|
+
it("counts each Thread's published Posts, the root included", async () => {
|
|
473
|
+
const { services, post, reply } = setup();
|
|
474
|
+
const root = await post({ publishedAt: day(0) });
|
|
475
|
+
const child = await reply(root, day(1));
|
|
476
|
+
await services.posts.create({
|
|
477
|
+
format: "note",
|
|
478
|
+
bodyMarkdown: "unsent",
|
|
479
|
+
replyToId: child.id,
|
|
480
|
+
status: "draft",
|
|
481
|
+
});
|
|
482
|
+
const lone = await post({ publishedAt: day(2) });
|
|
483
|
+
const draft = await post({ status: "draft" });
|
|
484
|
+
|
|
485
|
+
const counts = await services.posts.countThreadPosts([
|
|
486
|
+
root.id,
|
|
487
|
+
lone.id,
|
|
488
|
+
draft.id,
|
|
489
|
+
root.id,
|
|
490
|
+
]);
|
|
491
|
+
expect(counts.get(root.id)).toBe(2);
|
|
492
|
+
expect(counts.get(lone.id)).toBe(1);
|
|
493
|
+
expect(counts.has(draft.id)).toBe(false);
|
|
494
|
+
});
|
|
495
|
+
});
|
package/src/services/index.ts
CHANGED
|
@@ -26,6 +26,7 @@ import {
|
|
|
26
26
|
type SmartCollectionService,
|
|
27
27
|
} from "./smart-collection.js";
|
|
28
28
|
import { createSearchService, type SearchService } from "./search.js";
|
|
29
|
+
import { createThreadService, type ThreadService } from "./thread.js";
|
|
29
30
|
import { createNavItemService, type NavItemService } from "./navigation.js";
|
|
30
31
|
import { createAuthService, type AuthService } from "./auth.js";
|
|
31
32
|
import { createApiTokenService, type ApiTokenService } from "./api-token.js";
|
|
@@ -58,6 +59,7 @@ export interface Services {
|
|
|
58
59
|
site: SiteService;
|
|
59
60
|
paths: PathService;
|
|
60
61
|
posts: PostService;
|
|
62
|
+
threads: ThreadService;
|
|
61
63
|
customUrls: CustomUrlService;
|
|
62
64
|
media: MediaService;
|
|
63
65
|
uploads: UploadSessionService;
|
|
@@ -135,6 +137,7 @@ export function createServices(
|
|
|
135
137
|
site,
|
|
136
138
|
paths,
|
|
137
139
|
posts,
|
|
140
|
+
threads: createThreadService({ posts, collections }),
|
|
138
141
|
customUrls: createCustomUrlService(db, siteId, paths, databaseSchema),
|
|
139
142
|
media,
|
|
140
143
|
uploads: createUploadSessionService(db, siteId, media, databaseSchema),
|
|
@@ -193,6 +196,7 @@ export type { SettingsService } from "./settings.js";
|
|
|
193
196
|
export type { SiteService } from "./site.js";
|
|
194
197
|
export type { PathService } from "./path.js";
|
|
195
198
|
export type { PostService, PostFilters, PostDeleteDeps } from "./post.js";
|
|
199
|
+
export type { ThreadService } from "./thread.js";
|
|
196
200
|
export type { CustomUrlService } from "./custom-url.js";
|
|
197
201
|
export type {
|
|
198
202
|
SmartCollectionService,
|