@jant/core 0.8.0 → 0.8.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-CFsmRVJN.js → app--uxnaIRV.js} +427 -245
- package/dist/client/.vite/manifest.json +2 -2
- package/dist/client/_assets/{client-DTLBOHqD.js → client-BT6kpcuG.js} +1 -1
- package/dist/client/_assets/{client-auth-D6Ix6SmE.js → client-auth-begiT1tq.js} +1 -1
- package/dist/index.js +1 -1
- package/dist/node.js +2 -2
- package/package.json +1 -1
- package/src/__tests__/fixtures/releases/0.8.0/site-export/README.md +130 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/2thjq/_index.md +25 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/2u5e5/_index.md +35 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/41q27/0x4nv/index.md +18 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/41q27/_index.md +44 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/41q27/faqyt/index.md +30 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/41q27/the-potting-mix-guide-i-keep-rereading/index.md +20 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/5kayp/_index.md +23 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/5ynfs/_index.md +31 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/5ynfs/hu597/index.md +21 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/_index.md +5 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/a-night-heron-field-guide-for-my-walk-folder/_index.md +31 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/a-quiet-saturday-after-a-busy-week/_index.md +36 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/archive/_index.md +8 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/are-na-is-the-only-bookmark-pile-i-still-revisit/_index.md +30 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/bought-too-many-lemons-again/_index.md +37 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/city-walks/_index.md +12 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/cleanshot-for-screenshots-ocr-and-screen-recording-on-the-mac/_index.md +33 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/cleanshotmac-shang-de-jie-tu-ocr-lu-ping-gong-ju/_index.md +32 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/co344/_index.md +25 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/collections/_index.md +5 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/current-tea-rotation/_index.md +34 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/derek-sivers-still-writes-the-kind-of-internet-i-want-more-of/_index.md +30 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/desk-but-better/_index.md +25 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/e0kcg/_index.md +22 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/egi3c/_index.md +24 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/evf9j/_index.md +152 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/featured/_index.md +8 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/from-basho/_index.md +25 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/from-epictetus/_index.md +25 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/from-marcus-aurelius/_index.md +25 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/from-seneca/_index.md +26 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/from-thoreau/_index.md +25 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/gkbvv/2cl6d/index.md +18 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/gkbvv/3zqv7/index.md +30 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/gkbvv/5oxsv/index.md +18 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/gkbvv/_index.md +39 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/gwob9/_index.md +25 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/home-cooking/_index.md +12 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/hz729/_index.md +24 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/inaturalist-ai-zi-ran-she-qu/_index.md +44 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/j4v0q/_index.md +35 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/k278t/_index.md +25 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/k4eee/_index.md +22 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/k5vh8/_index.md +32 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/k5vh8/a-reading-log-idea-i-might-steal/index.md +20 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/k5vh8/a83qu/index.md +18 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/k5vh8/from-seneca-5hibo/index.md +21 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/kan-qi-lai-bi-jiao-ku-de-gong-si-men/_index.md +41 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/l2m7y/_index.md +29 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/lan-ya-yao-kong-qi-chuang-shang-yue-du-de-fan-ye-shen-qi/_index.md +57 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/mynoise-for-rainy-afternoons/_index.md +30 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/n09le/_index.md +23 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/ni-fu-jin-de-wei-ji-bai-ke-tiao-mu/_index.md +42 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/nts-archives-are-good-cleaning-music/_index.md +30 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/qgfcu/_index.md +32 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/qgfcu/c91j0/index.md +18 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/qgfcu/qyucy/index.md +18 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/qgfcu/wsgck/index.md +18 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/rain-makes-my-neighborhood-look-better/_index.md +46 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/reading-notes/_index.md +12 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/s4r3e/_index.md +25 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/serious-eats-on-tomato-and-egg-soup/_index.md +30 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/te812/_index.md +44 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/te812/cxiu6/index.md +18 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/te812/t77ms/index.md +18 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/te812/the-soup-trick-i-always-forget/index.md +20 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/telk3/_index.md +30 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/telk3/from-thoreau-pawt2/index.md +21 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/the-five-minute-reset-is-still-the-best-habit-i-have/_index.md +36 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/the-hallway-light-took-12-minutes-and-3-months/_index.md +32 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/the-recurse-center-manual-is-weirdly-calming/_index.md +30 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/tiny-projects/_index.md +12 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/tong-wang-xing-fu-zhi-lu/_index.md +68 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/tou-ma-yan-jiang-ju-le-bu-chu-ti-yan/_index.md +62 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/useful-links/_index.md +12 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/weekend-notes/_index.md +12 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/content/what-i-actually-use-my-notebook-for/_index.md +52 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/data/jant.toml +124 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/hugo.toml +45 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/static/_redirects +25 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/static/media/15d51730a4f7-med_01kn8jtq3penf98tfq4gzmh9g9.png +0 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/static/media/med_01kn8jtx8qenf98tm9ahxr48sw.webp +0 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/static/media/med_01kn8jv0jdenf98tp6qr9zgjjc.webp +0 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/static/media/med_01kn8jv452enf98trbkwrvhvgd.webp +0 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/static/media/med_01kn8jvb16enf98txp7p0pf5ge.webp +0 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/static/media/med_01kn8jve5nenf98tzap11gpxzr.webp +0 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/static/media/med_01kn8jvq58enf98v538w5vsqp4.webp +0 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/static/media/med_01kn8jvsaeenf98v6ct4gjzppc.webp +0 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/static/media/med_01kn8jw26cenf98vc7k0ppz5k8.webp +0 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/static/media/med_01m313q1hme4e8cmby77dn66yy.webp +0 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/static/media/med_01m313q2w7e4e8cmc20036yh66.webp +0 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/static/media/med_01m313q7d2e4e8cmdeqjmyafby.webp +0 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/static/media/med_01m313q887e4e8cmdjssych7yh.webp +0 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/static/media/med_01m3167jx2f6ct88pkz45j3ycp-poster.webp +0 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/static/media/med_01m3167jx2f6ct88pkz45j3ycp.mp4 +0 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/static/media/med_01m3167mbdf6ct88pr4ga1b9kf-poster.webp +0 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/static/media/med_01m3167mbdf6ct88pr4ga1b9kf.mp4 +0 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/static/media/med_01m3167nsvf6ct88q1anhz4svb-poster.webp +0 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/static/media/med_01m3167nsvf6ct88q1anhz4svb.mp4 +0 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/static/media/med_01m3167qaxf6ct88qfndwzxx83-poster.webp +0 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/static/media/med_01m3167qaxf6ct88qfndwzxx83.mp4 +0 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/static/media/med_01m3167sm0f6ct88qw5brtsgcw.webp +0 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/static/media/med_01m3167tf8f6ct88r169s51j49.webp +0 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/static/media/med_01m3167vaaf6ct88rbx5ztenwb.webp +0 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/static/media/med_01m3167w51f6ct88rp19pybkw1.webp +0 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/static/media/med_01m3167x0nf6ct88rzajb84tw3.webp +0 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/static/media/med_01m3167xxbf6ct88s5t2300bjk.webp +0 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/static/media/med_01m3167yq5f6ct88s9pjwt5fxg.webp +0 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/static/media/med_01m3167zg4f6ct88sge8jp28ja.webp +0 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/themes/jant/static/apple-touch-icon.png +0 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/themes/jant/static/custom.css +0 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/themes/jant/static/favicon.ico +0 -0
- package/src/__tests__/fixtures/releases/0.8.0/site-export/wrangler.jsonc +24 -0
- package/src/__tests__/fixtures/releases/0.8.0/snapshot/db.sql +322 -0
- package/src/__tests__/fixtures/releases/0.8.0/snapshot/meta.json +11 -0
- package/src/__tests__/helpers/cursor-walk.ts +52 -0
- package/src/__tests__/release-fixtures.test.ts +6 -2
- package/src/lib/__tests__/post-list-cursor.test.ts +105 -0
- package/src/lib/__tests__/timeline.test.ts +14 -14
- package/src/lib/post-list-cursor.ts +211 -0
- package/src/routes/api/__tests__/mcp.test.ts +58 -0
- package/src/routes/api/__tests__/posts.test.ts +79 -0
- package/src/routes/api/posts.ts +5 -9
- package/src/routes/api/public/__tests__/archive.test.ts +261 -0
- package/src/routes/api/public/__tests__/posts.test.ts +344 -19
- package/src/routes/api/public/archive.ts +18 -16
- package/src/routes/api/public/posts.ts +15 -16
- package/src/services/__tests__/post.test.ts +342 -36
- package/src/services/mcp.ts +6 -11
- package/src/services/post.ts +513 -319
- package/src/ui/shared/Pagination.tsx +1 -1
package/src/services/post.ts
CHANGED
|
@@ -39,6 +39,14 @@ import {
|
|
|
39
39
|
} from "../db/thread-activity.js";
|
|
40
40
|
import { buildReaderVisibilityConditions } from "../db/post-visibility.js";
|
|
41
41
|
import { createEntityId } from "../lib/ids.js";
|
|
42
|
+
import {
|
|
43
|
+
decodePostListCursor,
|
|
44
|
+
encodePostListCursor,
|
|
45
|
+
isLegacyPostListCursor,
|
|
46
|
+
MISSING_LEGACY_CURSOR_MESSAGE,
|
|
47
|
+
type PostListCursorKeyKind,
|
|
48
|
+
type PostListCursorValue,
|
|
49
|
+
} from "../lib/post-list-cursor.js";
|
|
42
50
|
import { now } from "../lib/time.js";
|
|
43
51
|
import { trimTiptapBody } from "../lib/tiptap-render.js";
|
|
44
52
|
import {
|
|
@@ -216,10 +224,30 @@ export interface PostFilters {
|
|
|
216
224
|
/** Ignore global pinned ordering when results must remain chronological. */
|
|
217
225
|
ignorePinnedSort?: boolean;
|
|
218
226
|
limit?: number;
|
|
219
|
-
cursor?: string;
|
|
220
227
|
offset?: number; // offset for page-based pagination
|
|
221
228
|
}
|
|
222
229
|
|
|
230
|
+
/** Where `PostService.listPage` starts, and how much it returns. */
|
|
231
|
+
export interface PostListPageOptions {
|
|
232
|
+
/**
|
|
233
|
+
* `nextCursor` from the previous page; omit for the first. A bare post ID,
|
|
234
|
+
* the format earlier releases returned, is still accepted.
|
|
235
|
+
*/
|
|
236
|
+
cursor?: string;
|
|
237
|
+
/** Posts per page. */
|
|
238
|
+
limit: number;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* One page of Posts, and the cursor that continues it. See
|
|
243
|
+
* `PostService.listPage` and `PostService.listCollectionThreadRootPage`.
|
|
244
|
+
*/
|
|
245
|
+
export interface PostListPage {
|
|
246
|
+
posts: Post[];
|
|
247
|
+
/** Opaque. `null` when no further page exists. */
|
|
248
|
+
nextCursor: string | null;
|
|
249
|
+
}
|
|
250
|
+
|
|
223
251
|
/** What one filter measured over a set of Posts. See `aggregateMany`. */
|
|
224
252
|
export interface PostFilterAggregate {
|
|
225
253
|
count: number;
|
|
@@ -258,8 +286,6 @@ interface CollectionFeedEntryOptions extends ThreadRootPageOptions {
|
|
|
258
286
|
|
|
259
287
|
interface CollectionThreadRootPageOptions extends ThreadRootPageOptions {
|
|
260
288
|
sortOrder?: CollectionSortOrder;
|
|
261
|
-
/** Root Post ID returned by the previous page. */
|
|
262
|
-
cursor?: string;
|
|
263
289
|
}
|
|
264
290
|
|
|
265
291
|
interface CursorSortKey {
|
|
@@ -268,6 +294,24 @@ interface CursorSortKey {
|
|
|
268
294
|
value: number | string;
|
|
269
295
|
}
|
|
270
296
|
|
|
297
|
+
/**
|
|
298
|
+
* How `list()` orders Posts: `sortOrder` on the `sortBy` axis, pinned Posts
|
|
299
|
+
* first unless `pinned` is off.
|
|
300
|
+
*/
|
|
301
|
+
interface ListSort {
|
|
302
|
+
order: SortOrder;
|
|
303
|
+
axis: NonNullable<PostFilters["sortBy"]>;
|
|
304
|
+
pinned: boolean;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/** One term of a paged ORDER BY: `list()`'s, or a collection's Threads'. */
|
|
308
|
+
interface ListSortKey {
|
|
309
|
+
/** Selectable as a column, so a page's cursor is read from SQL. */
|
|
310
|
+
expr: SQL;
|
|
311
|
+
direction: "asc" | "desc";
|
|
312
|
+
kind: PostListCursorKeyKind;
|
|
313
|
+
}
|
|
314
|
+
|
|
271
315
|
export interface CollectionFeedEntry {
|
|
272
316
|
post: Post;
|
|
273
317
|
collectedAt: number;
|
|
@@ -383,7 +427,40 @@ export interface PostService {
|
|
|
383
427
|
excludePostId?: string;
|
|
384
428
|
}): Promise<string>;
|
|
385
429
|
checkSlugAvailability(slug: string, excludePostId?: string): Promise<boolean>;
|
|
430
|
+
/**
|
|
431
|
+
* Posts matching `filters`, in the order `sortOrder`, `sortBy`, `featured`,
|
|
432
|
+
* and `ignorePinnedSort` select. Pages by `limit` and `offset`; a caller
|
|
433
|
+
* handing pages to a client that walks them uses {@link listPage} instead.
|
|
434
|
+
*/
|
|
386
435
|
list(filters?: PostFilters): Promise<Post[]>;
|
|
436
|
+
/**
|
|
437
|
+
* One page of {@link list}, and an opaque cursor for the next.
|
|
438
|
+
*
|
|
439
|
+
* The cursor carries the sort mode and the sort-key values of the page's
|
|
440
|
+
* last row, so the next page resumes from that position without looking the
|
|
441
|
+
* row up again. A Post that exists for the whole walk and keeps its place in
|
|
442
|
+
* the order is returned exactly once, whatever else is created, deleted, or
|
|
443
|
+
* edited in between. A Post whose sort key changes mid-walk can be skipped
|
|
444
|
+
* or repeated.
|
|
445
|
+
*
|
|
446
|
+
* @param filters - What to list and in which order, as for {@link list}
|
|
447
|
+
* @param page - The previous page's `nextCursor`, and the page size
|
|
448
|
+
* @returns The page's Posts and the cursor after them, `null` on the last page
|
|
449
|
+
* @throws {ValidationError} When the cursor can't be read, was taken in
|
|
450
|
+
* another sort mode, or is a Post ID the caller can't see
|
|
451
|
+
* @example
|
|
452
|
+
* ```ts
|
|
453
|
+
* const first = await posts.listPage({ status: "published" }, { limit: 20 });
|
|
454
|
+
* const second = await posts.listPage(
|
|
455
|
+
* { status: "published" },
|
|
456
|
+
* { cursor: first.nextCursor ?? undefined, limit: 20 },
|
|
457
|
+
* );
|
|
458
|
+
* ```
|
|
459
|
+
*/
|
|
460
|
+
listPage(
|
|
461
|
+
filters: Omit<PostFilters, "limit" | "offset">,
|
|
462
|
+
page: PostListPageOptions,
|
|
463
|
+
): Promise<PostListPage>;
|
|
387
464
|
/**
|
|
388
465
|
* List minimal fields needed to render sitemap entries, paginated by `id`
|
|
389
466
|
* (ascending). Excludes replies, private posts, deleted posts, and drafts.
|
|
@@ -413,9 +490,9 @@ export interface PostService {
|
|
|
413
490
|
* SQLite/D1 scan only the index for this, not the row data.
|
|
414
491
|
*/
|
|
415
492
|
getSitemapIdAt(offset: number): Promise<string | null>;
|
|
416
|
-
/** Count posts matching filters (ignores
|
|
493
|
+
/** Count posts matching filters (ignores offset, limit) */
|
|
417
494
|
count(filters?: PostFilters): Promise<number>;
|
|
418
|
-
/** Count posts matching filters up to a fixed limit (ignores
|
|
495
|
+
/** Count posts matching filters up to a fixed limit (ignores offset, limit) */
|
|
419
496
|
countUpTo(filters: PostFilters | undefined, limit: number): Promise<number>;
|
|
420
497
|
/**
|
|
421
498
|
* Measure several filters at once, in a single round trip.
|
|
@@ -681,11 +758,39 @@ export interface PostService {
|
|
|
681
758
|
collectionIds: string[],
|
|
682
759
|
options?: CollectionThreadRootPageOptions,
|
|
683
760
|
): Promise<string[]>;
|
|
684
|
-
/**
|
|
685
|
-
|
|
761
|
+
/**
|
|
762
|
+
* One page of a collection's Thread roots, in the order
|
|
763
|
+
* {@link listCollectionThreadRootIdsForCollections} returns them, and an
|
|
764
|
+
* opaque cursor for the next.
|
|
765
|
+
*
|
|
766
|
+
* Pages the way {@link listPage} does: the cursor records the sort order and
|
|
767
|
+
* the sort-key values of the page's last Thread, so a Thread that is
|
|
768
|
+
* deleted, unpublished, or taken out of the collection between requests
|
|
769
|
+
* doesn't end the walk. A Thread that stays listed and keeps its place is
|
|
770
|
+
* returned exactly once; one whose activity, rating, or pin changes mid-walk
|
|
771
|
+
* can be skipped or repeated.
|
|
772
|
+
*
|
|
773
|
+
* @param collectionIds - The collection, or the union of collections, to list
|
|
774
|
+
* @param options - Which Threads to list and in which order
|
|
775
|
+
* @param page - The previous page's `nextCursor`, and the page size
|
|
776
|
+
* @returns The page's Thread roots and the cursor after them, `null` on the
|
|
777
|
+
* last page
|
|
778
|
+
* @throws {ValidationError} When the cursor can't be read, was taken in
|
|
779
|
+
* another order, or is a root ID this request doesn't list
|
|
780
|
+
* @example
|
|
781
|
+
* ```ts
|
|
782
|
+
* const first = await posts.listCollectionThreadRootPage(
|
|
783
|
+
* [collection.id],
|
|
784
|
+
* { status: "published", excludePrivate: true, sortOrder: "newest" },
|
|
785
|
+
* { limit: 20 },
|
|
786
|
+
* );
|
|
787
|
+
* ```
|
|
788
|
+
*/
|
|
789
|
+
listCollectionThreadRootPage(
|
|
686
790
|
collectionIds: string[],
|
|
687
|
-
options
|
|
688
|
-
|
|
791
|
+
options: Omit<CollectionThreadRootPageOptions, "limit" | "offset">,
|
|
792
|
+
page: PostListPageOptions,
|
|
793
|
+
): Promise<PostListPage>;
|
|
689
794
|
/** List collection feed entries ordered by latest added-at timestamp */
|
|
690
795
|
listCollectionFeedEntries(
|
|
691
796
|
collectionId: string,
|
|
@@ -1405,38 +1510,144 @@ export function createPostService(
|
|
|
1405
1510
|
return conditions;
|
|
1406
1511
|
}
|
|
1407
1512
|
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1513
|
+
/**
|
|
1514
|
+
* How `list()` orders a set of filters.
|
|
1515
|
+
*
|
|
1516
|
+
* A Featured list runs newest-published-first whatever `sortOrder` and
|
|
1517
|
+
* `sortBy` say. It is that order and no variant of it, so a cursor moves
|
|
1518
|
+
* between a Featured list and any other newest-published one. Everything
|
|
1519
|
+
* else takes both from the filters, defaulting to newest by announced
|
|
1520
|
+
* activity.
|
|
1521
|
+
*/
|
|
1522
|
+
function resolveListSort(filters: PostFilters): ListSort {
|
|
1523
|
+
const pinned = !filters.ignorePinnedSort;
|
|
1524
|
+
if (filters.featured) {
|
|
1525
|
+
return { order: "newest", axis: "published", pinned };
|
|
1417
1526
|
}
|
|
1418
|
-
return
|
|
1527
|
+
return {
|
|
1528
|
+
order: filters.sortOrder ?? "newest",
|
|
1529
|
+
axis: filters.sortBy ?? "activity",
|
|
1530
|
+
pinned,
|
|
1531
|
+
};
|
|
1532
|
+
}
|
|
1533
|
+
|
|
1534
|
+
/**
|
|
1535
|
+
* The sort mode a cursor records. Two requests share a mode exactly when
|
|
1536
|
+
* their ORDER BY reads the same keys in the same directions, so a position
|
|
1537
|
+
* taken in one resumes the other.
|
|
1538
|
+
*/
|
|
1539
|
+
function listSortMode(sort: ListSort): string {
|
|
1540
|
+
return `${sort.order}:${sort.axis}:${sort.pinned ? "pinned" : "unpinned"}`;
|
|
1419
1541
|
}
|
|
1420
1542
|
|
|
1421
1543
|
/**
|
|
1422
|
-
*
|
|
1544
|
+
* Where a Post sits on one of `list()`'s time axes. See `PostFilters.sortBy`.
|
|
1423
1545
|
*
|
|
1424
|
-
*
|
|
1425
|
-
* the
|
|
1546
|
+
* On the activity axis a draft, which has announced nothing, sits at its last
|
|
1547
|
+
* edit. When the status filter already settles which case applies, only
|
|
1548
|
+
* that column is read.
|
|
1426
1549
|
*/
|
|
1427
|
-
function
|
|
1428
|
-
|
|
1429
|
-
|
|
1550
|
+
function listAxisExpr(
|
|
1551
|
+
axis: ListSort["axis"],
|
|
1552
|
+
status: Status | undefined,
|
|
1553
|
+
): SQL {
|
|
1554
|
+
switch (axis) {
|
|
1555
|
+
case "published":
|
|
1556
|
+
return sql`coalesce(${posts.publishedAt}, ${posts.createdAt})`;
|
|
1557
|
+
case "thread_updated":
|
|
1558
|
+
return sql`${posts.threadUpdatedAt}`;
|
|
1559
|
+
case "activity":
|
|
1560
|
+
if (status === "draft") return sql`${posts.updatedAt}`;
|
|
1561
|
+
if (status === "published") return sql`${posts.lastActivityAt}`;
|
|
1562
|
+
return sql`CASE
|
|
1563
|
+
WHEN ${posts.status} = 'draft' THEN ${posts.updatedAt}
|
|
1564
|
+
ELSE ${posts.lastActivityAt}
|
|
1565
|
+
END`;
|
|
1430
1566
|
}
|
|
1431
|
-
|
|
1432
|
-
|
|
1567
|
+
}
|
|
1568
|
+
|
|
1569
|
+
/**
|
|
1570
|
+
* `list()`'s sort keys, in ORDER BY order — the one definition of it.
|
|
1571
|
+
*
|
|
1572
|
+
* The ORDER BY, the keyset condition that resumes after a cursor, and the
|
|
1573
|
+
* values a cursor records are all built from this list, so they cannot read
|
|
1574
|
+
* different expressions. Two of them used to, and a page boundary then
|
|
1575
|
+
* skipped or repeated Posts.
|
|
1576
|
+
*
|
|
1577
|
+
* Every nullable key is wrapped in `coalesce(…, -1)`. SQLite sorts NULLs
|
|
1578
|
+
* last under DESC and Postgres sorts them first, and a keyset comparison
|
|
1579
|
+
* against NULL is never true on either; with the wrap both engines order,
|
|
1580
|
+
* compare, and return the same values. The Post ID ends every list, so no
|
|
1581
|
+
* two rows share a tuple.
|
|
1582
|
+
*/
|
|
1583
|
+
function listSortKeys(
|
|
1584
|
+
sort: ListSort,
|
|
1585
|
+
status: Status | undefined,
|
|
1586
|
+
): ListSortKey[] {
|
|
1587
|
+
const numberKey = (expr: SQL, direction: "asc" | "desc"): ListSortKey => ({
|
|
1588
|
+
expr: sql<number>`coalesce(${expr}, -1)`.mapWith(Number),
|
|
1589
|
+
direction,
|
|
1590
|
+
kind: "number",
|
|
1591
|
+
});
|
|
1592
|
+
const idKey = (direction: "asc" | "desc"): ListSortKey => ({
|
|
1593
|
+
expr: sql<string>`${posts.id}`,
|
|
1594
|
+
direction,
|
|
1595
|
+
kind: "id",
|
|
1596
|
+
});
|
|
1597
|
+
const pinnedKeys = sort.pinned
|
|
1598
|
+
? [numberKey(sql`${posts.pinnedAt}`, "desc")]
|
|
1599
|
+
: [];
|
|
1600
|
+
const time = listAxisExpr(sort.axis, status);
|
|
1601
|
+
|
|
1602
|
+
// One case per order and no default: an order added to `SortOrder`
|
|
1603
|
+
// doesn't compile until it has keys, rather than falling into another's.
|
|
1604
|
+
switch (sort.order) {
|
|
1605
|
+
case "oldest":
|
|
1606
|
+
return [...pinnedKeys, numberKey(time, "asc"), idKey("asc")];
|
|
1607
|
+
case "rating_desc":
|
|
1608
|
+
// Ratings run 1 to 5, so -1 puts unrated Posts after every rated one.
|
|
1609
|
+
return [
|
|
1610
|
+
...pinnedKeys,
|
|
1611
|
+
numberKey(sql`${posts.rating}`, "desc"),
|
|
1612
|
+
numberKey(time, "desc"),
|
|
1613
|
+
idKey("desc"),
|
|
1614
|
+
];
|
|
1615
|
+
case "newest":
|
|
1616
|
+
return [...pinnedKeys, numberKey(time, "desc"), idKey("desc")];
|
|
1433
1617
|
}
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1618
|
+
}
|
|
1619
|
+
|
|
1620
|
+
function listOrderBy(keys: readonly ListSortKey[]): SQL[] {
|
|
1621
|
+
return keys.map((key) =>
|
|
1622
|
+
key.direction === "desc" ? desc(key.expr) : asc(key.expr),
|
|
1623
|
+
);
|
|
1624
|
+
}
|
|
1625
|
+
|
|
1626
|
+
/** The sort keys as select fields, `k0`, `k1`, …, in ORDER BY order. */
|
|
1627
|
+
function listSortKeyFields(
|
|
1628
|
+
keys: readonly ListSortKey[],
|
|
1629
|
+
): Record<string, SQL> {
|
|
1630
|
+
return Object.fromEntries(
|
|
1631
|
+
keys.map((key, index) => [`k${index}`, key.expr]),
|
|
1632
|
+
);
|
|
1633
|
+
}
|
|
1634
|
+
|
|
1635
|
+
/** Read back the tuple {@link listSortKeyFields} selected. */
|
|
1636
|
+
function readListSortKeyValues(
|
|
1637
|
+
keys: readonly ListSortKey[],
|
|
1638
|
+
fields: Record<string, unknown>,
|
|
1639
|
+
): PostListCursorValue[] {
|
|
1640
|
+
return keys.map((key, index) => {
|
|
1641
|
+
const value = fields[`k${index}`];
|
|
1642
|
+
if (key.kind === "number") {
|
|
1643
|
+
if (typeof value === "number" && Number.isSafeInteger(value)) {
|
|
1644
|
+
return value;
|
|
1645
|
+
}
|
|
1646
|
+
} else if (typeof value === "string") {
|
|
1647
|
+
return value;
|
|
1648
|
+
}
|
|
1649
|
+
throw new Error(`Sort key ${index} read back as ${String(value)}`);
|
|
1650
|
+
});
|
|
1440
1651
|
}
|
|
1441
1652
|
|
|
1442
1653
|
function buildLexicographicCursorCondition(
|
|
@@ -1460,106 +1671,68 @@ export function createPostService(
|
|
|
1460
1671
|
)`;
|
|
1461
1672
|
}
|
|
1462
1673
|
|
|
1463
|
-
|
|
1674
|
+
/** Rows strictly after `values` in the order `keys` describe. */
|
|
1675
|
+
function buildListKeysetCondition(
|
|
1676
|
+
keys: readonly ListSortKey[],
|
|
1677
|
+
values: readonly PostListCursorValue[],
|
|
1678
|
+
): SQL<unknown> {
|
|
1679
|
+
const cursorKeys = keys.map((key, index): CursorSortKey => {
|
|
1680
|
+
const value = values[index];
|
|
1681
|
+
if (value === undefined) {
|
|
1682
|
+
throw new Error(`Cursor holds no value for sort key ${index}`);
|
|
1683
|
+
}
|
|
1684
|
+
return { direction: key.direction, expr: key.expr, value };
|
|
1685
|
+
});
|
|
1686
|
+
const [first, ...rest] = cursorKeys;
|
|
1687
|
+
if (!first) {
|
|
1688
|
+
throw new Error("A list has at least one sort key");
|
|
1689
|
+
}
|
|
1690
|
+
return buildLexicographicCursorCondition([first, ...rest]);
|
|
1691
|
+
}
|
|
1692
|
+
|
|
1693
|
+
/**
|
|
1694
|
+
* The position a cursor names, as the tuple of sort-key values after which
|
|
1695
|
+
* the page starts.
|
|
1696
|
+
*
|
|
1697
|
+
* A bare post ID, the cursor earlier releases returned, is looked up — but
|
|
1698
|
+
* only among Posts the caller could have been shown: the request's status,
|
|
1699
|
+
* and nothing private when the request excludes it. A deleted Post, a draft,
|
|
1700
|
+
* and a private Post all get the same rejection, so the lookup can't be used
|
|
1701
|
+
* to learn whether a Post exists or when it was published. Its values come
|
|
1702
|
+
* from the same key expressions as the ORDER BY.
|
|
1703
|
+
*/
|
|
1704
|
+
async function resolveListCursor(
|
|
1705
|
+
cursor: string,
|
|
1706
|
+
sort: ListSort,
|
|
1707
|
+
keys: readonly ListSortKey[],
|
|
1464
1708
|
filters: PostFilters,
|
|
1465
|
-
): Promise<
|
|
1466
|
-
if (!
|
|
1467
|
-
return
|
|
1709
|
+
): Promise<PostListCursorValue[]> {
|
|
1710
|
+
if (!isLegacyPostListCursor(cursor)) {
|
|
1711
|
+
return decodePostListCursor(cursor, {
|
|
1712
|
+
mode: listSortMode(sort),
|
|
1713
|
+
kinds: keys.map((key) => key.kind),
|
|
1714
|
+
});
|
|
1468
1715
|
}
|
|
1469
1716
|
|
|
1470
|
-
const
|
|
1471
|
-
.select()
|
|
1717
|
+
const rows = await db
|
|
1718
|
+
.select(listSortKeyFields(keys))
|
|
1472
1719
|
.from(posts)
|
|
1473
|
-
.where(
|
|
1720
|
+
.where(
|
|
1721
|
+
and(
|
|
1722
|
+
eq(posts.siteId, siteId),
|
|
1723
|
+
eq(posts.id, cursor),
|
|
1724
|
+
...buildReaderVisibilityConditions(posts, siteId, {
|
|
1725
|
+
status: filters.status,
|
|
1726
|
+
excludePrivate: filters.excludePrivate,
|
|
1727
|
+
}),
|
|
1728
|
+
),
|
|
1729
|
+
)
|
|
1474
1730
|
.limit(1);
|
|
1475
|
-
const
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
return null;
|
|
1479
|
-
}
|
|
1480
|
-
|
|
1481
|
-
const sortTimestampExpr = buildSortTimestampExpr(filters);
|
|
1482
|
-
const pinnedSortExpr = sql<number>`coalesce(${posts.pinnedAt}, -1)`;
|
|
1483
|
-
const featuredPublishedSortExpr = sql<number>`coalesce(
|
|
1484
|
-
${posts.publishedAt}, ${posts.createdAt}, -1
|
|
1485
|
-
)`;
|
|
1486
|
-
const sortTimestampSortExpr = sql<number>`coalesce(${sortTimestampExpr}, -1)`;
|
|
1487
|
-
const ratingPresenceExpr = sql<number>`CASE
|
|
1488
|
-
WHEN ${posts.rating} IS NULL THEN 0
|
|
1489
|
-
ELSE 1
|
|
1490
|
-
END`;
|
|
1491
|
-
const ratingSortExpr = sql<number>`coalesce(${posts.rating}, -1)`;
|
|
1492
|
-
const cursorPinnedAt = cursorPost.pinnedAt ?? -1;
|
|
1493
|
-
const cursorFeaturedPublishedAt =
|
|
1494
|
-
cursorPost.publishedAt ?? cursorPost.createdAt;
|
|
1495
|
-
const cursorSortTimestamp = getCursorSortTimestamp(cursorPost, filters);
|
|
1496
|
-
const cursorRatingPresence = cursorPost.rating === null ? 0 : 1;
|
|
1497
|
-
const cursorRating = cursorPost.rating ?? -1;
|
|
1498
|
-
const withPinnedSortKey = (
|
|
1499
|
-
keys: [CursorSortKey, ...CursorSortKey[]],
|
|
1500
|
-
): [CursorSortKey, ...CursorSortKey[]] =>
|
|
1501
|
-
filters.ignorePinnedSort
|
|
1502
|
-
? keys
|
|
1503
|
-
: [
|
|
1504
|
-
{ direction: "desc", expr: pinnedSortExpr, value: cursorPinnedAt },
|
|
1505
|
-
...keys,
|
|
1506
|
-
];
|
|
1507
|
-
|
|
1508
|
-
if (filters.featured) {
|
|
1509
|
-
return buildLexicographicCursorCondition(
|
|
1510
|
-
withPinnedSortKey([
|
|
1511
|
-
{
|
|
1512
|
-
direction: "desc",
|
|
1513
|
-
expr: featuredPublishedSortExpr,
|
|
1514
|
-
value: cursorFeaturedPublishedAt,
|
|
1515
|
-
},
|
|
1516
|
-
{ direction: "desc", expr: posts.id, value: cursorPost.id },
|
|
1517
|
-
]),
|
|
1518
|
-
);
|
|
1519
|
-
}
|
|
1520
|
-
|
|
1521
|
-
// One case per sort order, mirroring `list()` key for key.
|
|
1522
|
-
switch (filters.sortOrder ?? "newest") {
|
|
1523
|
-
case "oldest":
|
|
1524
|
-
return buildLexicographicCursorCondition(
|
|
1525
|
-
withPinnedSortKey([
|
|
1526
|
-
{
|
|
1527
|
-
direction: "asc",
|
|
1528
|
-
expr: sortTimestampSortExpr,
|
|
1529
|
-
value: cursorSortTimestamp,
|
|
1530
|
-
},
|
|
1531
|
-
{ direction: "asc", expr: posts.id, value: cursorPost.id },
|
|
1532
|
-
]),
|
|
1533
|
-
);
|
|
1534
|
-
case "rating_desc":
|
|
1535
|
-
return buildLexicographicCursorCondition(
|
|
1536
|
-
withPinnedSortKey([
|
|
1537
|
-
{
|
|
1538
|
-
direction: "desc",
|
|
1539
|
-
expr: ratingPresenceExpr,
|
|
1540
|
-
value: cursorRatingPresence,
|
|
1541
|
-
},
|
|
1542
|
-
{ direction: "desc", expr: ratingSortExpr, value: cursorRating },
|
|
1543
|
-
{
|
|
1544
|
-
direction: "desc",
|
|
1545
|
-
expr: sortTimestampSortExpr,
|
|
1546
|
-
value: cursorSortTimestamp,
|
|
1547
|
-
},
|
|
1548
|
-
{ direction: "desc", expr: posts.id, value: cursorPost.id },
|
|
1549
|
-
]),
|
|
1550
|
-
);
|
|
1551
|
-
case "newest":
|
|
1552
|
-
return buildLexicographicCursorCondition(
|
|
1553
|
-
withPinnedSortKey([
|
|
1554
|
-
{
|
|
1555
|
-
direction: "desc",
|
|
1556
|
-
expr: sortTimestampSortExpr,
|
|
1557
|
-
value: cursorSortTimestamp,
|
|
1558
|
-
},
|
|
1559
|
-
{ direction: "desc", expr: posts.id, value: cursorPost.id },
|
|
1560
|
-
]),
|
|
1561
|
-
);
|
|
1731
|
+
const row = rows[0];
|
|
1732
|
+
if (!row) {
|
|
1733
|
+
throw new ValidationError(MISSING_LEGACY_CURSOR_MESSAGE);
|
|
1562
1734
|
}
|
|
1735
|
+
return readListSortKeyValues(keys, row);
|
|
1563
1736
|
}
|
|
1564
1737
|
|
|
1565
1738
|
function toPost(
|
|
@@ -1811,43 +1984,64 @@ export function createPostService(
|
|
|
1811
1984
|
)`.as(alias);
|
|
1812
1985
|
}
|
|
1813
1986
|
|
|
1814
|
-
|
|
1987
|
+
/**
|
|
1988
|
+
* The sort mode a collection cursor records. Distinct from every
|
|
1989
|
+
* {@link listSortMode}: a collection orders Threads by its own pins, so a
|
|
1990
|
+
* position in `list()` never resumes a collection or the other way round.
|
|
1991
|
+
*/
|
|
1992
|
+
function collectionThreadSortMode(order: CollectionSortOrder): string {
|
|
1993
|
+
return `collection:${order}`;
|
|
1994
|
+
}
|
|
1995
|
+
|
|
1996
|
+
/**
|
|
1997
|
+
* A collection's matching Threads, one row per Thread with its sort keys as
|
|
1998
|
+
* columns, those keys in ORDER BY order, and the sort mode a cursor taken
|
|
1999
|
+
* in that order records — the one definition of how a collection orders
|
|
2000
|
+
* its Threads. `options.sortOrder` defaults to `newest`.
|
|
2001
|
+
*
|
|
2002
|
+
* The keys are aggregates over the Thread's matching members, computed once
|
|
2003
|
+
* in the grouped subquery; the ORDER BY, the keyset condition that resumes
|
|
2004
|
+
* after a cursor, and the values a cursor records all read the subquery's
|
|
2005
|
+
* columns through `keys`. As in {@link listSortKeys}, no key is ever NULL,
|
|
2006
|
+
* so SQLite and Postgres order and compare alike: a missing pin, rating, or
|
|
2007
|
+
* publication time folds to -1, and Thread activity falls back to the
|
|
2008
|
+
* member's own times.
|
|
2009
|
+
*
|
|
2010
|
+
* - `newest`: pinned in the collection first, then Thread activity, newest
|
|
2011
|
+
* first.
|
|
2012
|
+
* - `oldest`: pinned first, then the Thread's first publication, oldest
|
|
2013
|
+
* first. A new reply doesn't move a Thread.
|
|
2014
|
+
* - `rating_desc`: pinned first, then the Thread's highest rating (unrated
|
|
2015
|
+
* last, since ratings run 1 to 5), then Thread activity.
|
|
2016
|
+
*
|
|
2017
|
+
* The Thread root ID ends every order, so no two rows share a tuple.
|
|
2018
|
+
*/
|
|
2019
|
+
function buildCollectionThreadSort(
|
|
1815
2020
|
collectionIds: string[],
|
|
1816
2021
|
options: CollectionThreadRootPageOptions,
|
|
1817
2022
|
) {
|
|
2023
|
+
const order = options.sortOrder ?? "newest";
|
|
1818
2024
|
const conditions = [
|
|
1819
2025
|
...buildThreadRootPageConditions(options),
|
|
1820
2026
|
buildCollectionMembershipCondition(collectionIds),
|
|
1821
2027
|
];
|
|
1822
|
-
const sortOrder = options.sortOrder ?? "newest";
|
|
1823
|
-
const publishedAt =
|
|
1824
|
-
sortOrder === "oldest"
|
|
1825
|
-
? sql<number>`MIN(${posts.publishedAt})`.as("published_at")
|
|
1826
|
-
: sql<number>`MAX(${posts.publishedAt})`.as("published_at");
|
|
1827
|
-
const threadActivityAt =
|
|
1828
|
-
buildCollectionThreadActivityExpr("thread_activity_at");
|
|
1829
|
-
const ratingPresence = sql<number>`MAX(
|
|
1830
|
-
CASE
|
|
1831
|
-
WHEN ${posts.rating} IS NULL THEN 0
|
|
1832
|
-
ELSE 1
|
|
1833
|
-
END
|
|
1834
|
-
)`.as("rating_presence");
|
|
1835
|
-
const ratingValue = sql<number>`MAX(coalesce(${posts.rating}, -1))`.as(
|
|
1836
|
-
"rating_value",
|
|
1837
|
-
);
|
|
1838
|
-
const collectionPinnedAt =
|
|
1839
|
-
sql<number>`MAX(coalesce(${threadCollections.pinnedAt}, -1))`.as(
|
|
1840
|
-
"collection_pinned_at",
|
|
1841
|
-
);
|
|
1842
2028
|
|
|
1843
2029
|
const sortedThreads = db
|
|
1844
2030
|
.select({
|
|
1845
2031
|
threadId: posts.threadId,
|
|
1846
|
-
|
|
1847
|
-
|
|
1848
|
-
|
|
1849
|
-
|
|
1850
|
-
|
|
2032
|
+
collectionPinnedAt:
|
|
2033
|
+
sql<number>`MAX(coalesce(${threadCollections.pinnedAt}, -1))`.as(
|
|
2034
|
+
"collection_pinned_at",
|
|
2035
|
+
),
|
|
2036
|
+
firstPublishedAt:
|
|
2037
|
+
sql<number>`coalesce(MIN(${posts.publishedAt}), -1)`.as(
|
|
2038
|
+
"first_published_at",
|
|
2039
|
+
),
|
|
2040
|
+
threadActivityAt:
|
|
2041
|
+
buildCollectionThreadActivityExpr("thread_activity_at"),
|
|
2042
|
+
ratingValue: sql<number>`MAX(coalesce(${posts.rating}, -1))`.as(
|
|
2043
|
+
"rating_value",
|
|
2044
|
+
),
|
|
1851
2045
|
})
|
|
1852
2046
|
.from(posts)
|
|
1853
2047
|
.innerJoin(
|
|
@@ -1861,7 +2055,76 @@ export function createPostService(
|
|
|
1861
2055
|
.groupBy(posts.threadId)
|
|
1862
2056
|
.as("collection_thread_sort");
|
|
1863
2057
|
|
|
1864
|
-
|
|
2058
|
+
const numberKey = (
|
|
2059
|
+
column: SQLWrapper,
|
|
2060
|
+
direction: "asc" | "desc",
|
|
2061
|
+
): ListSortKey => ({
|
|
2062
|
+
expr: sql<number>`${column}`.mapWith(Number),
|
|
2063
|
+
direction,
|
|
2064
|
+
kind: "number",
|
|
2065
|
+
});
|
|
2066
|
+
const idKey = (direction: "asc" | "desc"): ListSortKey => ({
|
|
2067
|
+
expr: sql<string>`${sortedThreads.threadId}`,
|
|
2068
|
+
direction,
|
|
2069
|
+
kind: "id",
|
|
2070
|
+
});
|
|
2071
|
+
const pinned = numberKey(sortedThreads.collectionPinnedAt, "desc");
|
|
2072
|
+
const activity = numberKey(sortedThreads.threadActivityAt, "desc");
|
|
2073
|
+
|
|
2074
|
+
// One case per order and no default, as in `listSortKeys`.
|
|
2075
|
+
const keys = ((): ListSortKey[] => {
|
|
2076
|
+
switch (order) {
|
|
2077
|
+
case "oldest":
|
|
2078
|
+
return [
|
|
2079
|
+
pinned,
|
|
2080
|
+
numberKey(sortedThreads.firstPublishedAt, "asc"),
|
|
2081
|
+
idKey("asc"),
|
|
2082
|
+
];
|
|
2083
|
+
case "rating_desc":
|
|
2084
|
+
return [
|
|
2085
|
+
pinned,
|
|
2086
|
+
numberKey(sortedThreads.ratingValue, "desc"),
|
|
2087
|
+
activity,
|
|
2088
|
+
idKey("desc"),
|
|
2089
|
+
];
|
|
2090
|
+
case "newest":
|
|
2091
|
+
return [pinned, activity, idKey("desc")];
|
|
2092
|
+
}
|
|
2093
|
+
})();
|
|
2094
|
+
|
|
2095
|
+
return { sortedThreads, keys, mode: collectionThreadSortMode(order) };
|
|
2096
|
+
}
|
|
2097
|
+
|
|
2098
|
+
/**
|
|
2099
|
+
* The position a collection cursor names, as the tuple of sort-key values
|
|
2100
|
+
* after which the page starts.
|
|
2101
|
+
*
|
|
2102
|
+
* A bare root ID, the cursor earlier releases returned, is looked up in the
|
|
2103
|
+
* request's own `sortedThreads`, so only among Threads this request could
|
|
2104
|
+
* list. A Thread that is gone, private, a draft, or no longer in the
|
|
2105
|
+
* collection gets the same rejection as an ID that never existed.
|
|
2106
|
+
*/
|
|
2107
|
+
async function resolveCollectionThreadCursor(
|
|
2108
|
+
cursor: string,
|
|
2109
|
+
sort: ReturnType<typeof buildCollectionThreadSort>,
|
|
2110
|
+
): Promise<PostListCursorValue[]> {
|
|
2111
|
+
if (!isLegacyPostListCursor(cursor)) {
|
|
2112
|
+
return decodePostListCursor(cursor, {
|
|
2113
|
+
mode: sort.mode,
|
|
2114
|
+
kinds: sort.keys.map((key) => key.kind),
|
|
2115
|
+
});
|
|
2116
|
+
}
|
|
2117
|
+
|
|
2118
|
+
const rows = await db
|
|
2119
|
+
.select(listSortKeyFields(sort.keys))
|
|
2120
|
+
.from(sort.sortedThreads)
|
|
2121
|
+
.where(eq(sort.sortedThreads.threadId, cursor))
|
|
2122
|
+
.limit(1);
|
|
2123
|
+
const row = rows[0];
|
|
2124
|
+
if (!row) {
|
|
2125
|
+
throw new ValidationError(MISSING_LEGACY_CURSOR_MESSAGE);
|
|
2126
|
+
}
|
|
2127
|
+
return readListSortKeyValues(sort.keys, row);
|
|
1865
2128
|
}
|
|
1866
2129
|
|
|
1867
2130
|
function isMediaAttachmentInput(
|
|
@@ -2101,76 +2364,16 @@ export function createPostService(
|
|
|
2101
2364
|
},
|
|
2102
2365
|
|
|
2103
2366
|
async list(filters = {}) {
|
|
2367
|
+
const keys = listSortKeys(resolveListSort(filters), filters.status);
|
|
2104
2368
|
const conditions = buildFilterConditions(filters);
|
|
2105
|
-
const cursorCondition = await buildListCursorCondition(filters);
|
|
2106
|
-
if (filters.cursor && !cursorCondition) {
|
|
2107
|
-
return [];
|
|
2108
|
-
}
|
|
2109
|
-
const sortTimestamp = buildSortTimestampExpr(filters);
|
|
2110
|
-
|
|
2111
|
-
if (cursorCondition) {
|
|
2112
|
-
conditions.push(cursorCondition);
|
|
2113
|
-
}
|
|
2114
|
-
|
|
2115
|
-
const ratingPresence = sql<number>`CASE
|
|
2116
|
-
WHEN ${posts.rating} IS NULL THEN 0
|
|
2117
|
-
ELSE 1
|
|
2118
|
-
END`;
|
|
2119
2369
|
|
|
2120
|
-
|
|
2121
|
-
// DESC, Postgres puts them first. Wrap nullable sort keys in COALESCE
|
|
2122
|
-
// so pinned and nullable timeline values sort identically under both
|
|
2123
|
-
// engines. Mirrors the
|
|
2124
|
-
// expressions used by `buildListCursorCondition` above.
|
|
2125
|
-
const pinnedSortExpr = sql<number>`coalesce(${posts.pinnedAt}, -1)`;
|
|
2126
|
-
const featuredPublishedSortExpr = sql<number>`coalesce(
|
|
2127
|
-
${posts.publishedAt}, ${posts.createdAt}, -1
|
|
2128
|
-
)`;
|
|
2129
|
-
const sortTimestampSortExpr = sql<number>`coalesce(${sortTimestamp}, -1)`;
|
|
2130
|
-
const pinnedOrder = filters.ignorePinnedSort
|
|
2131
|
-
? []
|
|
2132
|
-
: [desc(pinnedSortExpr)];
|
|
2133
|
-
|
|
2134
|
-
const baseQuery = db
|
|
2370
|
+
let query = db
|
|
2135
2371
|
.select()
|
|
2136
2372
|
.from(posts)
|
|
2137
2373
|
.where(conditions.length > 0 ? and(...conditions) : undefined)
|
|
2374
|
+
.orderBy(...listOrderBy(keys))
|
|
2138
2375
|
.limit(filters.limit ?? 100);
|
|
2139
2376
|
|
|
2140
|
-
// A case per sort order, and no shared fallthrough. This was an if/else
|
|
2141
|
-
// chain whose last branch was written for one order and silently caught
|
|
2142
|
-
// another, so `newest` ordered by rating instead of by time wherever it
|
|
2143
|
-
// was passed explicitly — a smart collection page, most visibly.
|
|
2144
|
-
const buildOrderBy = (): SQL<unknown>[] => {
|
|
2145
|
-
if (filters.featured) {
|
|
2146
|
-
return [
|
|
2147
|
-
...pinnedOrder,
|
|
2148
|
-
desc(featuredPublishedSortExpr),
|
|
2149
|
-
desc(posts.id),
|
|
2150
|
-
];
|
|
2151
|
-
}
|
|
2152
|
-
switch (filters.sortOrder ?? "newest") {
|
|
2153
|
-
case "oldest":
|
|
2154
|
-
return [...pinnedOrder, asc(sortTimestampSortExpr), asc(posts.id)];
|
|
2155
|
-
case "rating_desc":
|
|
2156
|
-
return [
|
|
2157
|
-
...pinnedOrder,
|
|
2158
|
-
desc(ratingPresence),
|
|
2159
|
-
desc(posts.rating),
|
|
2160
|
-
desc(sortTimestampSortExpr),
|
|
2161
|
-
desc(posts.id),
|
|
2162
|
-
];
|
|
2163
|
-
case "newest":
|
|
2164
|
-
return [
|
|
2165
|
-
...pinnedOrder,
|
|
2166
|
-
desc(sortTimestampSortExpr),
|
|
2167
|
-
desc(posts.id),
|
|
2168
|
-
];
|
|
2169
|
-
}
|
|
2170
|
-
};
|
|
2171
|
-
|
|
2172
|
-
let query = baseQuery.orderBy(...buildOrderBy());
|
|
2173
|
-
|
|
2174
2377
|
if (filters.offset !== undefined) {
|
|
2175
2378
|
query = query.offset(filters.offset) as typeof query;
|
|
2176
2379
|
}
|
|
@@ -2179,6 +2382,49 @@ export function createPostService(
|
|
|
2179
2382
|
return hydratePosts(rows);
|
|
2180
2383
|
},
|
|
2181
2384
|
|
|
2385
|
+
async listPage(filters, { cursor, limit }) {
|
|
2386
|
+
const sort = resolveListSort(filters);
|
|
2387
|
+
const keys = listSortKeys(sort, filters.status);
|
|
2388
|
+
const conditions = buildFilterConditions(filters);
|
|
2389
|
+
// An empty cursor has always meant the first page — a client that sends
|
|
2390
|
+
// `?cursor=` before it has one keeps working.
|
|
2391
|
+
if (cursor) {
|
|
2392
|
+
conditions.push(
|
|
2393
|
+
buildListKeysetCondition(
|
|
2394
|
+
keys,
|
|
2395
|
+
await resolveListCursor(cursor, sort, keys, filters),
|
|
2396
|
+
),
|
|
2397
|
+
);
|
|
2398
|
+
}
|
|
2399
|
+
|
|
2400
|
+
// One row past the page says whether another page exists, so the last
|
|
2401
|
+
// page carries a null cursor rather than one that leads to an empty page.
|
|
2402
|
+
const rows = await db
|
|
2403
|
+
.select({ post: posts, sortKey: listSortKeyFields(keys) })
|
|
2404
|
+
.from(posts)
|
|
2405
|
+
.where(conditions.length > 0 ? and(...conditions) : undefined)
|
|
2406
|
+
.orderBy(...listOrderBy(keys))
|
|
2407
|
+
.limit(limit + 1);
|
|
2408
|
+
|
|
2409
|
+
const pageRows = rows.slice(0, limit);
|
|
2410
|
+
// The cursor comes from the last row read, not the last Post returned:
|
|
2411
|
+
// hydration drops a row it can't give a slug, and a page cut short that
|
|
2412
|
+
// way must not end the walk.
|
|
2413
|
+
const lastRow = pageRows[pageRows.length - 1];
|
|
2414
|
+
const nextCursor =
|
|
2415
|
+
rows.length > limit && lastRow
|
|
2416
|
+
? encodePostListCursor(
|
|
2417
|
+
listSortMode(sort),
|
|
2418
|
+
readListSortKeyValues(keys, lastRow.sortKey),
|
|
2419
|
+
)
|
|
2420
|
+
: null;
|
|
2421
|
+
|
|
2422
|
+
return {
|
|
2423
|
+
posts: await hydratePosts(pageRows.map((row) => row.post)),
|
|
2424
|
+
nextCursor,
|
|
2425
|
+
};
|
|
2426
|
+
},
|
|
2427
|
+
|
|
2182
2428
|
async listForSitemap({ afterId, limit }) {
|
|
2183
2429
|
// Share the filter conditions with `list()` so visibility/reply/deleted
|
|
2184
2430
|
// semantics stay consistent if they ever change.
|
|
@@ -4582,103 +4828,15 @@ export function createPostService(
|
|
|
4582
4828
|
collectionIds,
|
|
4583
4829
|
options = {},
|
|
4584
4830
|
) {
|
|
4585
|
-
const {
|
|
4831
|
+
const { sortedThreads, keys } = buildCollectionThreadSort(
|
|
4586
4832
|
collectionIds,
|
|
4587
4833
|
options,
|
|
4588
4834
|
);
|
|
4589
|
-
let cursorCondition: SQL<unknown> | undefined;
|
|
4590
|
-
|
|
4591
|
-
if (options.cursor) {
|
|
4592
|
-
const cursorRows = await db
|
|
4593
|
-
.select()
|
|
4594
|
-
.from(sortedThreads)
|
|
4595
|
-
.where(eq(sortedThreads.threadId, options.cursor))
|
|
4596
|
-
.limit(1);
|
|
4597
|
-
const cursorRow = cursorRows[0];
|
|
4598
|
-
if (!cursorRow) return [];
|
|
4599
|
-
|
|
4600
|
-
const pinnedKey: CursorSortKey = {
|
|
4601
|
-
direction: "desc",
|
|
4602
|
-
expr: sortedThreads.collectionPinnedAt,
|
|
4603
|
-
value: cursorRow.collectionPinnedAt,
|
|
4604
|
-
};
|
|
4605
4835
|
|
|
4606
|
-
|
|
4607
|
-
|
|
4608
|
-
|
|
4609
|
-
|
|
4610
|
-
{
|
|
4611
|
-
direction: "asc",
|
|
4612
|
-
expr: sortedThreads.publishedAt,
|
|
4613
|
-
value: cursorRow.publishedAt,
|
|
4614
|
-
},
|
|
4615
|
-
{
|
|
4616
|
-
direction: "asc",
|
|
4617
|
-
expr: sortedThreads.threadId,
|
|
4618
|
-
value: cursorRow.threadId,
|
|
4619
|
-
},
|
|
4620
|
-
])
|
|
4621
|
-
: sortOrder === "rating_desc"
|
|
4622
|
-
? buildLexicographicCursorCondition([
|
|
4623
|
-
pinnedKey,
|
|
4624
|
-
{
|
|
4625
|
-
direction: "desc",
|
|
4626
|
-
expr: sortedThreads.ratingPresence,
|
|
4627
|
-
value: cursorRow.ratingPresence,
|
|
4628
|
-
},
|
|
4629
|
-
{
|
|
4630
|
-
direction: "desc",
|
|
4631
|
-
expr: sortedThreads.ratingValue,
|
|
4632
|
-
value: cursorRow.ratingValue,
|
|
4633
|
-
},
|
|
4634
|
-
{
|
|
4635
|
-
direction: "desc",
|
|
4636
|
-
expr: sortedThreads.threadActivityAt,
|
|
4637
|
-
value: cursorRow.threadActivityAt,
|
|
4638
|
-
},
|
|
4639
|
-
{
|
|
4640
|
-
direction: "desc",
|
|
4641
|
-
expr: sortedThreads.threadId,
|
|
4642
|
-
value: cursorRow.threadId,
|
|
4643
|
-
},
|
|
4644
|
-
])
|
|
4645
|
-
: buildLexicographicCursorCondition([
|
|
4646
|
-
pinnedKey,
|
|
4647
|
-
{
|
|
4648
|
-
direction: "desc",
|
|
4649
|
-
expr: sortedThreads.threadActivityAt,
|
|
4650
|
-
value: cursorRow.threadActivityAt,
|
|
4651
|
-
},
|
|
4652
|
-
{
|
|
4653
|
-
direction: "desc",
|
|
4654
|
-
expr: sortedThreads.threadId,
|
|
4655
|
-
value: cursorRow.threadId,
|
|
4656
|
-
},
|
|
4657
|
-
]);
|
|
4658
|
-
}
|
|
4659
|
-
|
|
4660
|
-
const baseQuery = db.select().from(sortedThreads).where(cursorCondition);
|
|
4661
|
-
|
|
4662
|
-
let query =
|
|
4663
|
-
sortOrder === "oldest"
|
|
4664
|
-
? baseQuery.orderBy(
|
|
4665
|
-
desc(sortedThreads.collectionPinnedAt),
|
|
4666
|
-
asc(sortedThreads.publishedAt),
|
|
4667
|
-
asc(sortedThreads.threadId),
|
|
4668
|
-
)
|
|
4669
|
-
: sortOrder === "rating_desc"
|
|
4670
|
-
? baseQuery.orderBy(
|
|
4671
|
-
desc(sortedThreads.collectionPinnedAt),
|
|
4672
|
-
desc(sortedThreads.ratingPresence),
|
|
4673
|
-
desc(sortedThreads.ratingValue),
|
|
4674
|
-
desc(sortedThreads.threadActivityAt),
|
|
4675
|
-
desc(sortedThreads.threadId),
|
|
4676
|
-
)
|
|
4677
|
-
: baseQuery.orderBy(
|
|
4678
|
-
desc(sortedThreads.collectionPinnedAt),
|
|
4679
|
-
desc(sortedThreads.threadActivityAt),
|
|
4680
|
-
desc(sortedThreads.threadId),
|
|
4681
|
-
);
|
|
4836
|
+
let query = db
|
|
4837
|
+
.select({ threadId: sortedThreads.threadId })
|
|
4838
|
+
.from(sortedThreads)
|
|
4839
|
+
.orderBy(...listOrderBy(keys));
|
|
4682
4840
|
|
|
4683
4841
|
if (options.limit !== undefined) {
|
|
4684
4842
|
query = query.limit(options.limit) as typeof query;
|
|
@@ -4691,16 +4849,52 @@ export function createPostService(
|
|
|
4691
4849
|
return rows.map((row) => row.threadId);
|
|
4692
4850
|
},
|
|
4693
4851
|
|
|
4694
|
-
async
|
|
4695
|
-
|
|
4696
|
-
|
|
4697
|
-
|
|
4698
|
-
|
|
4852
|
+
async listCollectionThreadRootPage(
|
|
4853
|
+
collectionIds,
|
|
4854
|
+
options,
|
|
4855
|
+
{ cursor, limit },
|
|
4856
|
+
) {
|
|
4857
|
+
const sort = buildCollectionThreadSort(collectionIds, options);
|
|
4858
|
+
const { sortedThreads, keys } = sort;
|
|
4859
|
+
// An empty cursor means the first page, as in `listPage`.
|
|
4860
|
+
const after = cursor
|
|
4861
|
+
? buildListKeysetCondition(
|
|
4862
|
+
keys,
|
|
4863
|
+
await resolveCollectionThreadCursor(cursor, sort),
|
|
4864
|
+
)
|
|
4865
|
+
: undefined;
|
|
4866
|
+
|
|
4867
|
+
// One row past the page says whether another page exists.
|
|
4868
|
+
const rows = await db
|
|
4869
|
+
.select({
|
|
4870
|
+
threadId: sortedThreads.threadId,
|
|
4871
|
+
sortKey: listSortKeyFields(keys),
|
|
4872
|
+
})
|
|
4873
|
+
.from(sortedThreads)
|
|
4874
|
+
.where(after)
|
|
4875
|
+
.orderBy(...listOrderBy(keys))
|
|
4876
|
+
.limit(limit + 1);
|
|
4877
|
+
|
|
4878
|
+
const pageRows = rows.slice(0, limit);
|
|
4879
|
+
// From the last row read, not the last root hydrated, as in `listPage`.
|
|
4880
|
+
const lastRow = pageRows[pageRows.length - 1];
|
|
4881
|
+
const nextCursor =
|
|
4882
|
+
rows.length > limit && lastRow
|
|
4883
|
+
? encodePostListCursor(
|
|
4884
|
+
sort.mode,
|
|
4885
|
+
readListSortKeyValues(keys, lastRow.sortKey),
|
|
4886
|
+
)
|
|
4887
|
+
: null;
|
|
4888
|
+
|
|
4889
|
+
const rootIds = pageRows.map((row) => row.threadId);
|
|
4699
4890
|
const rootsById = await hydratePostsById(rootIds);
|
|
4700
|
-
return
|
|
4701
|
-
|
|
4702
|
-
|
|
4703
|
-
|
|
4891
|
+
return {
|
|
4892
|
+
posts: rootIds.flatMap((rootId) => {
|
|
4893
|
+
const root = rootsById.get(rootId);
|
|
4894
|
+
return root ? [root] : [];
|
|
4895
|
+
}),
|
|
4896
|
+
nextCursor,
|
|
4897
|
+
};
|
|
4704
4898
|
},
|
|
4705
4899
|
|
|
4706
4900
|
async listCollectionFeedEntries(collectionId, options = {}) {
|