@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.
- package/dist/{app--uxnaIRV.js → app-B3P2N2vM.js} +1346 -310
- package/dist/client/.vite/manifest.json +2 -2
- package/dist/client/_assets/{client-auth-begiT1tq.js → client-auth-CeGBe0qG.js} +1 -1
- package/dist/client/_assets/{client-BT6kpcuG.js → client-nByuF0Ct.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__/discover.test.ts +24 -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/discover.ts +34 -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 +143 -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__/search.test.ts +104 -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/search.ts +31 -2
- package/src/routes/api/threads.ts +128 -0
- package/src/routes/auth/__tests__/setup-discover.test.ts +29 -1
- package/src/routes/auth/__tests__/setup-page.test.tsx +59 -1
- package/src/routes/auth/setup.tsx +48 -9
- package/src/routes/pages/__tests__/search.test.ts +58 -0
- package/src/routes/pages/search.tsx +3 -1
- package/src/services/__tests__/search.test.ts +211 -0
- package/src/services/__tests__/thread.test.ts +495 -0
- package/src/services/index.ts +4 -0
- package/src/services/mcp.ts +217 -57
- package/src/services/post.ts +222 -43
- package/src/services/search.ts +77 -66
- 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
|
@@ -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 = {
|
|
@@ -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
|
+
});
|