@jant/core 0.8.0 → 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-CFsmRVJN.js → app-BzH8jmh3.js} +1619 -500
- package/dist/client/.vite/manifest.json +2 -2
- package/dist/client/_assets/{client-DTLBOHqD.js → client-S8oryJ0m.js} +1 -1
- package/dist/client/_assets/{client-auth-D6Ix6SmE.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__/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 +82 -0
- package/src/__tests__/release-fixtures.test.ts +6 -2
- package/src/app.tsx +4 -0
- package/src/lib/__tests__/post-list-cursor.test.ts +105 -0
- package/src/lib/__tests__/thread-fold.test.ts +43 -0
- package/src/lib/__tests__/timeline.test.ts +14 -14
- 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/post-list-cursor.ts +211 -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 +147 -0
- package/src/routes/api/__tests__/posts.test.ts +157 -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 +19 -112
- 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/__tests__/threads.test.ts +363 -0
- package/src/routes/api/public/archive.ts +82 -79
- package/src/routes/api/public/posts.ts +97 -299
- package/src/routes/api/public/threads.ts +141 -0
- package/src/routes/api/threads.ts +128 -0
- package/src/services/__tests__/post.test.ts +342 -36
- package/src/services/__tests__/thread.test.ts +495 -0
- package/src/services/index.ts +4 -0
- package/src/services/mcp.ts +213 -63
- package/src/services/post.ts +734 -361
- 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
- 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;
|
|
@@ -247,6 +275,13 @@ export interface ThreadRootPageOptions {
|
|
|
247
275
|
rootFormat?: Format;
|
|
248
276
|
/** Restrict to Threads with at least one rated published post. */
|
|
249
277
|
hasRating?: true;
|
|
278
|
+
/**
|
|
279
|
+
* Restrict to Threads whose root matches these filters, the way a list of
|
|
280
|
+
* roots would (`excludeReplies` is implied). Lets a Thread-grouped order,
|
|
281
|
+
* such as a collection's, take the same filter dimensions `list()` does.
|
|
282
|
+
* Sorting and paging fields are ignored.
|
|
283
|
+
*/
|
|
284
|
+
rootFilters?: PostFilters;
|
|
250
285
|
limit?: number;
|
|
251
286
|
offset?: number;
|
|
252
287
|
}
|
|
@@ -258,8 +293,6 @@ interface CollectionFeedEntryOptions extends ThreadRootPageOptions {
|
|
|
258
293
|
|
|
259
294
|
interface CollectionThreadRootPageOptions extends ThreadRootPageOptions {
|
|
260
295
|
sortOrder?: CollectionSortOrder;
|
|
261
|
-
/** Root Post ID returned by the previous page. */
|
|
262
|
-
cursor?: string;
|
|
263
296
|
}
|
|
264
297
|
|
|
265
298
|
interface CursorSortKey {
|
|
@@ -268,6 +301,30 @@ interface CursorSortKey {
|
|
|
268
301
|
value: number | string;
|
|
269
302
|
}
|
|
270
303
|
|
|
304
|
+
/**
|
|
305
|
+
* How `list()` orders Posts: `sortOrder` on the `sortBy` axis, pinned Posts
|
|
306
|
+
* first unless `pinned` is off.
|
|
307
|
+
*/
|
|
308
|
+
interface ListSort {
|
|
309
|
+
order: SortOrder;
|
|
310
|
+
axis: NonNullable<PostFilters["sortBy"]>;
|
|
311
|
+
pinned: boolean;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* The sort mode a cursor into one Thread's Posts records. Distinct from every
|
|
316
|
+
* `list()` and collection mode, so a position in one never resumes another.
|
|
317
|
+
*/
|
|
318
|
+
const THREAD_POSTS_SORT_MODE = "thread";
|
|
319
|
+
|
|
320
|
+
/** One term of a paged ORDER BY: `list()`'s, a collection's, or a Thread's. */
|
|
321
|
+
interface ListSortKey {
|
|
322
|
+
/** Selectable as a column, so a page's cursor is read from SQL. */
|
|
323
|
+
expr: SQL;
|
|
324
|
+
direction: "asc" | "desc";
|
|
325
|
+
kind: PostListCursorKeyKind;
|
|
326
|
+
}
|
|
327
|
+
|
|
271
328
|
export interface CollectionFeedEntry {
|
|
272
329
|
post: Post;
|
|
273
330
|
collectedAt: number;
|
|
@@ -383,7 +440,40 @@ export interface PostService {
|
|
|
383
440
|
excludePostId?: string;
|
|
384
441
|
}): Promise<string>;
|
|
385
442
|
checkSlugAvailability(slug: string, excludePostId?: string): Promise<boolean>;
|
|
443
|
+
/**
|
|
444
|
+
* Posts matching `filters`, in the order `sortOrder`, `sortBy`, `featured`,
|
|
445
|
+
* and `ignorePinnedSort` select. Pages by `limit` and `offset`; a caller
|
|
446
|
+
* handing pages to a client that walks them uses {@link listPage} instead.
|
|
447
|
+
*/
|
|
386
448
|
list(filters?: PostFilters): Promise<Post[]>;
|
|
449
|
+
/**
|
|
450
|
+
* One page of {@link list}, and an opaque cursor for the next.
|
|
451
|
+
*
|
|
452
|
+
* The cursor carries the sort mode and the sort-key values of the page's
|
|
453
|
+
* last row, so the next page resumes from that position without looking the
|
|
454
|
+
* row up again. A Post that exists for the whole walk and keeps its place in
|
|
455
|
+
* the order is returned exactly once, whatever else is created, deleted, or
|
|
456
|
+
* edited in between. A Post whose sort key changes mid-walk can be skipped
|
|
457
|
+
* or repeated.
|
|
458
|
+
*
|
|
459
|
+
* @param filters - What to list and in which order, as for {@link list}
|
|
460
|
+
* @param page - The previous page's `nextCursor`, and the page size
|
|
461
|
+
* @returns The page's Posts and the cursor after them, `null` on the last page
|
|
462
|
+
* @throws {ValidationError} When the cursor can't be read, was taken in
|
|
463
|
+
* another sort mode, or is a Post ID the caller can't see
|
|
464
|
+
* @example
|
|
465
|
+
* ```ts
|
|
466
|
+
* const first = await posts.listPage({ status: "published" }, { limit: 20 });
|
|
467
|
+
* const second = await posts.listPage(
|
|
468
|
+
* { status: "published" },
|
|
469
|
+
* { cursor: first.nextCursor ?? undefined, limit: 20 },
|
|
470
|
+
* );
|
|
471
|
+
* ```
|
|
472
|
+
*/
|
|
473
|
+
listPage(
|
|
474
|
+
filters: Omit<PostFilters, "limit" | "offset">,
|
|
475
|
+
page: PostListPageOptions,
|
|
476
|
+
): Promise<PostListPage>;
|
|
387
477
|
/**
|
|
388
478
|
* List minimal fields needed to render sitemap entries, paginated by `id`
|
|
389
479
|
* (ascending). Excludes replies, private posts, deleted posts, and drafts.
|
|
@@ -413,9 +503,9 @@ export interface PostService {
|
|
|
413
503
|
* SQLite/D1 scan only the index for this, not the row data.
|
|
414
504
|
*/
|
|
415
505
|
getSitemapIdAt(offset: number): Promise<string | null>;
|
|
416
|
-
/** Count posts matching filters (ignores
|
|
506
|
+
/** Count posts matching filters (ignores offset, limit) */
|
|
417
507
|
count(filters?: PostFilters): Promise<number>;
|
|
418
|
-
/** Count posts matching filters up to a fixed limit (ignores
|
|
508
|
+
/** Count posts matching filters up to a fixed limit (ignores offset, limit) */
|
|
419
509
|
countUpTo(filters: PostFilters | undefined, limit: number): Promise<number>;
|
|
420
510
|
/**
|
|
421
511
|
* Measure several filters at once, in a single round trip.
|
|
@@ -510,6 +600,44 @@ export interface PostService {
|
|
|
510
600
|
* then replies by creation time, then ID.
|
|
511
601
|
*/
|
|
512
602
|
getThread(rootId: string): Promise<Post[]>;
|
|
603
|
+
/**
|
|
604
|
+
* One page of a Thread's Posts in Thread order, and an opaque cursor for the
|
|
605
|
+
* next.
|
|
606
|
+
*
|
|
607
|
+
* Pages like {@link listPage}. A new reply joins the end of the Thread, so a
|
|
608
|
+
* walk that is still going when one is published reaches it rather than
|
|
609
|
+
* skipping it. As well as a `nextCursor`, the cursor may be the ID of a Post
|
|
610
|
+
* in this Thread with the requested status: the page then starts right after
|
|
611
|
+
* it, which is how a reader opens the run a fold hides.
|
|
612
|
+
*
|
|
613
|
+
* @param threadId - The Thread root's ID
|
|
614
|
+
* @param options - Which Posts to include; `status` defaults to `published`
|
|
615
|
+
* @param page - The previous page's `nextCursor` or a Post ID, and the page
|
|
616
|
+
* size
|
|
617
|
+
* @returns The page's Posts and the cursor after them, `null` on the last page
|
|
618
|
+
* @throws {ValidationError} When the cursor can't be read, was taken in
|
|
619
|
+
* another order, or names a Post this request doesn't list
|
|
620
|
+
* @example
|
|
621
|
+
* ```ts
|
|
622
|
+
* const first = await posts.listThreadPostsPage(root.id, {}, { limit: 100 });
|
|
623
|
+
* ```
|
|
624
|
+
*/
|
|
625
|
+
listThreadPostsPage(
|
|
626
|
+
threadId: string,
|
|
627
|
+
options: { status?: Status },
|
|
628
|
+
page: PostListPageOptions,
|
|
629
|
+
): Promise<PostListPage>;
|
|
630
|
+
/**
|
|
631
|
+
* How many published Posts each Thread holds, its root included.
|
|
632
|
+
*
|
|
633
|
+
* @param threadIds - Thread root IDs; duplicates are fine
|
|
634
|
+
* @returns Map of Thread root ID to its published Post count; a Thread with
|
|
635
|
+
* nothing published is absent
|
|
636
|
+
* @example
|
|
637
|
+
* const counts = await posts.countThreadPosts([root.id]);
|
|
638
|
+
* counts.get(root.id); // 12
|
|
639
|
+
*/
|
|
640
|
+
countThreadPosts(threadIds: string[]): Promise<Map<string, number>>;
|
|
513
641
|
/**
|
|
514
642
|
* 1-based position of a Post in the reply chain running from its Thread root
|
|
515
643
|
* down to it: a root is 1, a reply to it is 2, a reply to that is 3.
|
|
@@ -681,11 +809,39 @@ export interface PostService {
|
|
|
681
809
|
collectionIds: string[],
|
|
682
810
|
options?: CollectionThreadRootPageOptions,
|
|
683
811
|
): Promise<string[]>;
|
|
684
|
-
/**
|
|
685
|
-
|
|
812
|
+
/**
|
|
813
|
+
* One page of a collection's Thread roots, in the order
|
|
814
|
+
* {@link listCollectionThreadRootIdsForCollections} returns them, and an
|
|
815
|
+
* opaque cursor for the next.
|
|
816
|
+
*
|
|
817
|
+
* Pages the way {@link listPage} does: the cursor records the sort order and
|
|
818
|
+
* the sort-key values of the page's last Thread, so a Thread that is
|
|
819
|
+
* deleted, unpublished, or taken out of the collection between requests
|
|
820
|
+
* doesn't end the walk. A Thread that stays listed and keeps its place is
|
|
821
|
+
* returned exactly once; one whose activity, rating, or pin changes mid-walk
|
|
822
|
+
* can be skipped or repeated.
|
|
823
|
+
*
|
|
824
|
+
* @param collectionIds - The collection, or the union of collections, to list
|
|
825
|
+
* @param options - Which Threads to list and in which order
|
|
826
|
+
* @param page - The previous page's `nextCursor`, and the page size
|
|
827
|
+
* @returns The page's Thread roots and the cursor after them, `null` on the
|
|
828
|
+
* last page
|
|
829
|
+
* @throws {ValidationError} When the cursor can't be read, was taken in
|
|
830
|
+
* another order, or is a root ID this request doesn't list
|
|
831
|
+
* @example
|
|
832
|
+
* ```ts
|
|
833
|
+
* const first = await posts.listCollectionThreadRootPage(
|
|
834
|
+
* [collection.id],
|
|
835
|
+
* { status: "published", excludePrivate: true, sortOrder: "newest" },
|
|
836
|
+
* { limit: 20 },
|
|
837
|
+
* );
|
|
838
|
+
* ```
|
|
839
|
+
*/
|
|
840
|
+
listCollectionThreadRootPage(
|
|
686
841
|
collectionIds: string[],
|
|
687
|
-
options
|
|
688
|
-
|
|
842
|
+
options: Omit<CollectionThreadRootPageOptions, "limit" | "offset">,
|
|
843
|
+
page: PostListPageOptions,
|
|
844
|
+
): Promise<PostListPage>;
|
|
689
845
|
/** List collection feed entries ordered by latest added-at timestamp */
|
|
690
846
|
listCollectionFeedEntries(
|
|
691
847
|
collectionId: string,
|
|
@@ -1011,14 +1167,33 @@ export function createPostService(
|
|
|
1011
1167
|
* sql`ROW_NUMBER() OVER (ORDER BY ${sql.join(threadOrder(), sql`, `)})`;
|
|
1012
1168
|
*/
|
|
1013
1169
|
function threadOrder(direction: "asc" | "desc" = "asc"): SQL[] {
|
|
1014
|
-
const terms =
|
|
1015
|
-
sql`CASE WHEN ${posts.replyToId} IS NULL THEN 0 ELSE 1 END`,
|
|
1016
|
-
sql`${posts.createdAt}`,
|
|
1017
|
-
sql`${posts.id}`,
|
|
1018
|
-
];
|
|
1170
|
+
const terms = threadSortKeys().map((key) => key.expr);
|
|
1019
1171
|
return direction === "asc" ? terms : terms.map((term) => desc(term));
|
|
1020
1172
|
}
|
|
1021
1173
|
|
|
1174
|
+
/**
|
|
1175
|
+
* {@link threadOrder} as paged sort keys, for walking one Thread with a
|
|
1176
|
+
* cursor. The one definition both read, so a page of a Thread and the
|
|
1177
|
+
* Thread page itself cannot order its posts differently.
|
|
1178
|
+
*/
|
|
1179
|
+
function threadSortKeys(): ListSortKey[] {
|
|
1180
|
+
return [
|
|
1181
|
+
{
|
|
1182
|
+
expr: sql<number>`CASE WHEN ${posts.replyToId} IS NULL THEN 0 ELSE 1 END`.mapWith(
|
|
1183
|
+
Number,
|
|
1184
|
+
),
|
|
1185
|
+
direction: "asc",
|
|
1186
|
+
kind: "number",
|
|
1187
|
+
},
|
|
1188
|
+
{
|
|
1189
|
+
expr: sql<number>`${posts.createdAt}`.mapWith(Number),
|
|
1190
|
+
direction: "asc",
|
|
1191
|
+
kind: "number",
|
|
1192
|
+
},
|
|
1193
|
+
{ expr: sql<string>`${posts.id}`, direction: "asc", kind: "id" },
|
|
1194
|
+
];
|
|
1195
|
+
}
|
|
1196
|
+
|
|
1022
1197
|
function buildYearMonthExpr(column: SQLWrapper): SQL<string> {
|
|
1023
1198
|
return databaseDialect === "pg"
|
|
1024
1199
|
? sql<string>`to_char(timezone('UTC', to_timestamp(${column})), 'YYYY-MM')`
|
|
@@ -1405,38 +1580,144 @@ export function createPostService(
|
|
|
1405
1580
|
return conditions;
|
|
1406
1581
|
}
|
|
1407
1582
|
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1583
|
+
/**
|
|
1584
|
+
* How `list()` orders a set of filters.
|
|
1585
|
+
*
|
|
1586
|
+
* A Featured list runs newest-published-first whatever `sortOrder` and
|
|
1587
|
+
* `sortBy` say. It is that order and no variant of it, so a cursor moves
|
|
1588
|
+
* between a Featured list and any other newest-published one. Everything
|
|
1589
|
+
* else takes both from the filters, defaulting to newest by announced
|
|
1590
|
+
* activity.
|
|
1591
|
+
*/
|
|
1592
|
+
function resolveListSort(filters: PostFilters): ListSort {
|
|
1593
|
+
const pinned = !filters.ignorePinnedSort;
|
|
1594
|
+
if (filters.featured) {
|
|
1595
|
+
return { order: "newest", axis: "published", pinned };
|
|
1417
1596
|
}
|
|
1418
|
-
return
|
|
1597
|
+
return {
|
|
1598
|
+
order: filters.sortOrder ?? "newest",
|
|
1599
|
+
axis: filters.sortBy ?? "activity",
|
|
1600
|
+
pinned,
|
|
1601
|
+
};
|
|
1602
|
+
}
|
|
1603
|
+
|
|
1604
|
+
/**
|
|
1605
|
+
* The sort mode a cursor records. Two requests share a mode exactly when
|
|
1606
|
+
* their ORDER BY reads the same keys in the same directions, so a position
|
|
1607
|
+
* taken in one resumes the other.
|
|
1608
|
+
*/
|
|
1609
|
+
function listSortMode(sort: ListSort): string {
|
|
1610
|
+
return `${sort.order}:${sort.axis}:${sort.pinned ? "pinned" : "unpinned"}`;
|
|
1419
1611
|
}
|
|
1420
1612
|
|
|
1421
1613
|
/**
|
|
1422
|
-
*
|
|
1614
|
+
* Where a Post sits on one of `list()`'s time axes. See `PostFilters.sortBy`.
|
|
1423
1615
|
*
|
|
1424
|
-
*
|
|
1425
|
-
* the
|
|
1616
|
+
* On the activity axis a draft, which has announced nothing, sits at its last
|
|
1617
|
+
* edit. When the status filter already settles which case applies, only
|
|
1618
|
+
* that column is read.
|
|
1426
1619
|
*/
|
|
1427
|
-
function
|
|
1428
|
-
|
|
1429
|
-
|
|
1620
|
+
function listAxisExpr(
|
|
1621
|
+
axis: ListSort["axis"],
|
|
1622
|
+
status: Status | undefined,
|
|
1623
|
+
): SQL {
|
|
1624
|
+
switch (axis) {
|
|
1625
|
+
case "published":
|
|
1626
|
+
return sql`coalesce(${posts.publishedAt}, ${posts.createdAt})`;
|
|
1627
|
+
case "thread_updated":
|
|
1628
|
+
return sql`${posts.threadUpdatedAt}`;
|
|
1629
|
+
case "activity":
|
|
1630
|
+
if (status === "draft") return sql`${posts.updatedAt}`;
|
|
1631
|
+
if (status === "published") return sql`${posts.lastActivityAt}`;
|
|
1632
|
+
return sql`CASE
|
|
1633
|
+
WHEN ${posts.status} = 'draft' THEN ${posts.updatedAt}
|
|
1634
|
+
ELSE ${posts.lastActivityAt}
|
|
1635
|
+
END`;
|
|
1430
1636
|
}
|
|
1431
|
-
|
|
1432
|
-
|
|
1637
|
+
}
|
|
1638
|
+
|
|
1639
|
+
/**
|
|
1640
|
+
* `list()`'s sort keys, in ORDER BY order — the one definition of it.
|
|
1641
|
+
*
|
|
1642
|
+
* The ORDER BY, the keyset condition that resumes after a cursor, and the
|
|
1643
|
+
* values a cursor records are all built from this list, so they cannot read
|
|
1644
|
+
* different expressions. Two of them used to, and a page boundary then
|
|
1645
|
+
* skipped or repeated Posts.
|
|
1646
|
+
*
|
|
1647
|
+
* Every nullable key is wrapped in `coalesce(…, -1)`. SQLite sorts NULLs
|
|
1648
|
+
* last under DESC and Postgres sorts them first, and a keyset comparison
|
|
1649
|
+
* against NULL is never true on either; with the wrap both engines order,
|
|
1650
|
+
* compare, and return the same values. The Post ID ends every list, so no
|
|
1651
|
+
* two rows share a tuple.
|
|
1652
|
+
*/
|
|
1653
|
+
function listSortKeys(
|
|
1654
|
+
sort: ListSort,
|
|
1655
|
+
status: Status | undefined,
|
|
1656
|
+
): ListSortKey[] {
|
|
1657
|
+
const numberKey = (expr: SQL, direction: "asc" | "desc"): ListSortKey => ({
|
|
1658
|
+
expr: sql<number>`coalesce(${expr}, -1)`.mapWith(Number),
|
|
1659
|
+
direction,
|
|
1660
|
+
kind: "number",
|
|
1661
|
+
});
|
|
1662
|
+
const idKey = (direction: "asc" | "desc"): ListSortKey => ({
|
|
1663
|
+
expr: sql<string>`${posts.id}`,
|
|
1664
|
+
direction,
|
|
1665
|
+
kind: "id",
|
|
1666
|
+
});
|
|
1667
|
+
const pinnedKeys = sort.pinned
|
|
1668
|
+
? [numberKey(sql`${posts.pinnedAt}`, "desc")]
|
|
1669
|
+
: [];
|
|
1670
|
+
const time = listAxisExpr(sort.axis, status);
|
|
1671
|
+
|
|
1672
|
+
// One case per order and no default: an order added to `SortOrder`
|
|
1673
|
+
// doesn't compile until it has keys, rather than falling into another's.
|
|
1674
|
+
switch (sort.order) {
|
|
1675
|
+
case "oldest":
|
|
1676
|
+
return [...pinnedKeys, numberKey(time, "asc"), idKey("asc")];
|
|
1677
|
+
case "rating_desc":
|
|
1678
|
+
// Ratings run 1 to 5, so -1 puts unrated Posts after every rated one.
|
|
1679
|
+
return [
|
|
1680
|
+
...pinnedKeys,
|
|
1681
|
+
numberKey(sql`${posts.rating}`, "desc"),
|
|
1682
|
+
numberKey(time, "desc"),
|
|
1683
|
+
idKey("desc"),
|
|
1684
|
+
];
|
|
1685
|
+
case "newest":
|
|
1686
|
+
return [...pinnedKeys, numberKey(time, "desc"), idKey("desc")];
|
|
1433
1687
|
}
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1688
|
+
}
|
|
1689
|
+
|
|
1690
|
+
function listOrderBy(keys: readonly ListSortKey[]): SQL[] {
|
|
1691
|
+
return keys.map((key) =>
|
|
1692
|
+
key.direction === "desc" ? desc(key.expr) : asc(key.expr),
|
|
1693
|
+
);
|
|
1694
|
+
}
|
|
1695
|
+
|
|
1696
|
+
/** The sort keys as select fields, `k0`, `k1`, …, in ORDER BY order. */
|
|
1697
|
+
function listSortKeyFields(
|
|
1698
|
+
keys: readonly ListSortKey[],
|
|
1699
|
+
): Record<string, SQL> {
|
|
1700
|
+
return Object.fromEntries(
|
|
1701
|
+
keys.map((key, index) => [`k${index}`, key.expr]),
|
|
1702
|
+
);
|
|
1703
|
+
}
|
|
1704
|
+
|
|
1705
|
+
/** Read back the tuple {@link listSortKeyFields} selected. */
|
|
1706
|
+
function readListSortKeyValues(
|
|
1707
|
+
keys: readonly ListSortKey[],
|
|
1708
|
+
fields: Record<string, unknown>,
|
|
1709
|
+
): PostListCursorValue[] {
|
|
1710
|
+
return keys.map((key, index) => {
|
|
1711
|
+
const value = fields[`k${index}`];
|
|
1712
|
+
if (key.kind === "number") {
|
|
1713
|
+
if (typeof value === "number" && Number.isSafeInteger(value)) {
|
|
1714
|
+
return value;
|
|
1715
|
+
}
|
|
1716
|
+
} else if (typeof value === "string") {
|
|
1717
|
+
return value;
|
|
1718
|
+
}
|
|
1719
|
+
throw new Error(`Sort key ${index} read back as ${String(value)}`);
|
|
1720
|
+
});
|
|
1440
1721
|
}
|
|
1441
1722
|
|
|
1442
1723
|
function buildLexicographicCursorCondition(
|
|
@@ -1460,106 +1741,68 @@ export function createPostService(
|
|
|
1460
1741
|
)`;
|
|
1461
1742
|
}
|
|
1462
1743
|
|
|
1463
|
-
|
|
1744
|
+
/** Rows strictly after `values` in the order `keys` describe. */
|
|
1745
|
+
function buildListKeysetCondition(
|
|
1746
|
+
keys: readonly ListSortKey[],
|
|
1747
|
+
values: readonly PostListCursorValue[],
|
|
1748
|
+
): SQL<unknown> {
|
|
1749
|
+
const cursorKeys = keys.map((key, index): CursorSortKey => {
|
|
1750
|
+
const value = values[index];
|
|
1751
|
+
if (value === undefined) {
|
|
1752
|
+
throw new Error(`Cursor holds no value for sort key ${index}`);
|
|
1753
|
+
}
|
|
1754
|
+
return { direction: key.direction, expr: key.expr, value };
|
|
1755
|
+
});
|
|
1756
|
+
const [first, ...rest] = cursorKeys;
|
|
1757
|
+
if (!first) {
|
|
1758
|
+
throw new Error("A list has at least one sort key");
|
|
1759
|
+
}
|
|
1760
|
+
return buildLexicographicCursorCondition([first, ...rest]);
|
|
1761
|
+
}
|
|
1762
|
+
|
|
1763
|
+
/**
|
|
1764
|
+
* The position a cursor names, as the tuple of sort-key values after which
|
|
1765
|
+
* the page starts.
|
|
1766
|
+
*
|
|
1767
|
+
* A bare post ID, the cursor earlier releases returned, is looked up — but
|
|
1768
|
+
* only among Posts the caller could have been shown: the request's status,
|
|
1769
|
+
* and nothing private when the request excludes it. A deleted Post, a draft,
|
|
1770
|
+
* and a private Post all get the same rejection, so the lookup can't be used
|
|
1771
|
+
* to learn whether a Post exists or when it was published. Its values come
|
|
1772
|
+
* from the same key expressions as the ORDER BY.
|
|
1773
|
+
*/
|
|
1774
|
+
async function resolveListCursor(
|
|
1775
|
+
cursor: string,
|
|
1776
|
+
sort: ListSort,
|
|
1777
|
+
keys: readonly ListSortKey[],
|
|
1464
1778
|
filters: PostFilters,
|
|
1465
|
-
): Promise<
|
|
1466
|
-
if (!
|
|
1467
|
-
return
|
|
1779
|
+
): Promise<PostListCursorValue[]> {
|
|
1780
|
+
if (!isLegacyPostListCursor(cursor)) {
|
|
1781
|
+
return decodePostListCursor(cursor, {
|
|
1782
|
+
mode: listSortMode(sort),
|
|
1783
|
+
kinds: keys.map((key) => key.kind),
|
|
1784
|
+
});
|
|
1468
1785
|
}
|
|
1469
1786
|
|
|
1470
|
-
const
|
|
1471
|
-
.select()
|
|
1787
|
+
const rows = await db
|
|
1788
|
+
.select(listSortKeyFields(keys))
|
|
1472
1789
|
.from(posts)
|
|
1473
|
-
.where(
|
|
1790
|
+
.where(
|
|
1791
|
+
and(
|
|
1792
|
+
eq(posts.siteId, siteId),
|
|
1793
|
+
eq(posts.id, cursor),
|
|
1794
|
+
...buildReaderVisibilityConditions(posts, siteId, {
|
|
1795
|
+
status: filters.status,
|
|
1796
|
+
excludePrivate: filters.excludePrivate,
|
|
1797
|
+
}),
|
|
1798
|
+
),
|
|
1799
|
+
)
|
|
1474
1800
|
.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
|
-
);
|
|
1801
|
+
const row = rows[0];
|
|
1802
|
+
if (!row) {
|
|
1803
|
+
throw new ValidationError(MISSING_LEGACY_CURSOR_MESSAGE);
|
|
1562
1804
|
}
|
|
1805
|
+
return readListSortKeyValues(keys, row);
|
|
1563
1806
|
}
|
|
1564
1807
|
|
|
1565
1808
|
function toPost(
|
|
@@ -1784,6 +2027,26 @@ export function createPostService(
|
|
|
1784
2027
|
if (options?.threadIds !== undefined) {
|
|
1785
2028
|
conditions.push(inArray(posts.threadId, options.threadIds));
|
|
1786
2029
|
}
|
|
2030
|
+
if (options?.rootFilters) {
|
|
2031
|
+
// Uncorrelated: the inner `post` shadows the outer one, so every column
|
|
2032
|
+
// the filters name reads the root row being matched.
|
|
2033
|
+
conditions.push(
|
|
2034
|
+
inArray(
|
|
2035
|
+
posts.threadId,
|
|
2036
|
+
db
|
|
2037
|
+
.select({ id: posts.id })
|
|
2038
|
+
.from(posts)
|
|
2039
|
+
.where(
|
|
2040
|
+
and(
|
|
2041
|
+
...buildFilterConditions({
|
|
2042
|
+
...options.rootFilters,
|
|
2043
|
+
excludeReplies: true,
|
|
2044
|
+
}),
|
|
2045
|
+
),
|
|
2046
|
+
),
|
|
2047
|
+
),
|
|
2048
|
+
);
|
|
2049
|
+
}
|
|
1787
2050
|
|
|
1788
2051
|
return conditions;
|
|
1789
2052
|
}
|
|
@@ -1811,43 +2074,64 @@ export function createPostService(
|
|
|
1811
2074
|
)`.as(alias);
|
|
1812
2075
|
}
|
|
1813
2076
|
|
|
1814
|
-
|
|
2077
|
+
/**
|
|
2078
|
+
* The sort mode a collection cursor records. Distinct from every
|
|
2079
|
+
* {@link listSortMode}: a collection orders Threads by its own pins, so a
|
|
2080
|
+
* position in `list()` never resumes a collection or the other way round.
|
|
2081
|
+
*/
|
|
2082
|
+
function collectionThreadSortMode(order: CollectionSortOrder): string {
|
|
2083
|
+
return `collection:${order}`;
|
|
2084
|
+
}
|
|
2085
|
+
|
|
2086
|
+
/**
|
|
2087
|
+
* A collection's matching Threads, one row per Thread with its sort keys as
|
|
2088
|
+
* columns, those keys in ORDER BY order, and the sort mode a cursor taken
|
|
2089
|
+
* in that order records — the one definition of how a collection orders
|
|
2090
|
+
* its Threads. `options.sortOrder` defaults to `newest`.
|
|
2091
|
+
*
|
|
2092
|
+
* The keys are aggregates over the Thread's matching members, computed once
|
|
2093
|
+
* in the grouped subquery; the ORDER BY, the keyset condition that resumes
|
|
2094
|
+
* after a cursor, and the values a cursor records all read the subquery's
|
|
2095
|
+
* columns through `keys`. As in {@link listSortKeys}, no key is ever NULL,
|
|
2096
|
+
* so SQLite and Postgres order and compare alike: a missing pin, rating, or
|
|
2097
|
+
* publication time folds to -1, and Thread activity falls back to the
|
|
2098
|
+
* member's own times.
|
|
2099
|
+
*
|
|
2100
|
+
* - `newest`: pinned in the collection first, then Thread activity, newest
|
|
2101
|
+
* first.
|
|
2102
|
+
* - `oldest`: pinned first, then the Thread's first publication, oldest
|
|
2103
|
+
* first. A new reply doesn't move a Thread.
|
|
2104
|
+
* - `rating_desc`: pinned first, then the Thread's highest rating (unrated
|
|
2105
|
+
* last, since ratings run 1 to 5), then Thread activity.
|
|
2106
|
+
*
|
|
2107
|
+
* The Thread root ID ends every order, so no two rows share a tuple.
|
|
2108
|
+
*/
|
|
2109
|
+
function buildCollectionThreadSort(
|
|
1815
2110
|
collectionIds: string[],
|
|
1816
2111
|
options: CollectionThreadRootPageOptions,
|
|
1817
2112
|
) {
|
|
2113
|
+
const order = options.sortOrder ?? "newest";
|
|
1818
2114
|
const conditions = [
|
|
1819
2115
|
...buildThreadRootPageConditions(options),
|
|
1820
2116
|
buildCollectionMembershipCondition(collectionIds),
|
|
1821
2117
|
];
|
|
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
2118
|
|
|
1843
2119
|
const sortedThreads = db
|
|
1844
2120
|
.select({
|
|
1845
2121
|
threadId: posts.threadId,
|
|
1846
|
-
|
|
1847
|
-
|
|
1848
|
-
|
|
1849
|
-
|
|
1850
|
-
|
|
2122
|
+
collectionPinnedAt:
|
|
2123
|
+
sql<number>`MAX(coalesce(${threadCollections.pinnedAt}, -1))`.as(
|
|
2124
|
+
"collection_pinned_at",
|
|
2125
|
+
),
|
|
2126
|
+
firstPublishedAt:
|
|
2127
|
+
sql<number>`coalesce(MIN(${posts.publishedAt}), -1)`.as(
|
|
2128
|
+
"first_published_at",
|
|
2129
|
+
),
|
|
2130
|
+
threadActivityAt:
|
|
2131
|
+
buildCollectionThreadActivityExpr("thread_activity_at"),
|
|
2132
|
+
ratingValue: sql<number>`MAX(coalesce(${posts.rating}, -1))`.as(
|
|
2133
|
+
"rating_value",
|
|
2134
|
+
),
|
|
1851
2135
|
})
|
|
1852
2136
|
.from(posts)
|
|
1853
2137
|
.innerJoin(
|
|
@@ -1861,7 +2145,76 @@ export function createPostService(
|
|
|
1861
2145
|
.groupBy(posts.threadId)
|
|
1862
2146
|
.as("collection_thread_sort");
|
|
1863
2147
|
|
|
1864
|
-
|
|
2148
|
+
const numberKey = (
|
|
2149
|
+
column: SQLWrapper,
|
|
2150
|
+
direction: "asc" | "desc",
|
|
2151
|
+
): ListSortKey => ({
|
|
2152
|
+
expr: sql<number>`${column}`.mapWith(Number),
|
|
2153
|
+
direction,
|
|
2154
|
+
kind: "number",
|
|
2155
|
+
});
|
|
2156
|
+
const idKey = (direction: "asc" | "desc"): ListSortKey => ({
|
|
2157
|
+
expr: sql<string>`${sortedThreads.threadId}`,
|
|
2158
|
+
direction,
|
|
2159
|
+
kind: "id",
|
|
2160
|
+
});
|
|
2161
|
+
const pinned = numberKey(sortedThreads.collectionPinnedAt, "desc");
|
|
2162
|
+
const activity = numberKey(sortedThreads.threadActivityAt, "desc");
|
|
2163
|
+
|
|
2164
|
+
// One case per order and no default, as in `listSortKeys`.
|
|
2165
|
+
const keys = ((): ListSortKey[] => {
|
|
2166
|
+
switch (order) {
|
|
2167
|
+
case "oldest":
|
|
2168
|
+
return [
|
|
2169
|
+
pinned,
|
|
2170
|
+
numberKey(sortedThreads.firstPublishedAt, "asc"),
|
|
2171
|
+
idKey("asc"),
|
|
2172
|
+
];
|
|
2173
|
+
case "rating_desc":
|
|
2174
|
+
return [
|
|
2175
|
+
pinned,
|
|
2176
|
+
numberKey(sortedThreads.ratingValue, "desc"),
|
|
2177
|
+
activity,
|
|
2178
|
+
idKey("desc"),
|
|
2179
|
+
];
|
|
2180
|
+
case "newest":
|
|
2181
|
+
return [pinned, activity, idKey("desc")];
|
|
2182
|
+
}
|
|
2183
|
+
})();
|
|
2184
|
+
|
|
2185
|
+
return { sortedThreads, keys, mode: collectionThreadSortMode(order) };
|
|
2186
|
+
}
|
|
2187
|
+
|
|
2188
|
+
/**
|
|
2189
|
+
* The position a collection cursor names, as the tuple of sort-key values
|
|
2190
|
+
* after which the page starts.
|
|
2191
|
+
*
|
|
2192
|
+
* A bare root ID, the cursor earlier releases returned, is looked up in the
|
|
2193
|
+
* request's own `sortedThreads`, so only among Threads this request could
|
|
2194
|
+
* list. A Thread that is gone, private, a draft, or no longer in the
|
|
2195
|
+
* collection gets the same rejection as an ID that never existed.
|
|
2196
|
+
*/
|
|
2197
|
+
async function resolveCollectionThreadCursor(
|
|
2198
|
+
cursor: string,
|
|
2199
|
+
sort: ReturnType<typeof buildCollectionThreadSort>,
|
|
2200
|
+
): Promise<PostListCursorValue[]> {
|
|
2201
|
+
if (!isLegacyPostListCursor(cursor)) {
|
|
2202
|
+
return decodePostListCursor(cursor, {
|
|
2203
|
+
mode: sort.mode,
|
|
2204
|
+
kinds: sort.keys.map((key) => key.kind),
|
|
2205
|
+
});
|
|
2206
|
+
}
|
|
2207
|
+
|
|
2208
|
+
const rows = await db
|
|
2209
|
+
.select(listSortKeyFields(sort.keys))
|
|
2210
|
+
.from(sort.sortedThreads)
|
|
2211
|
+
.where(eq(sort.sortedThreads.threadId, cursor))
|
|
2212
|
+
.limit(1);
|
|
2213
|
+
const row = rows[0];
|
|
2214
|
+
if (!row) {
|
|
2215
|
+
throw new ValidationError(MISSING_LEGACY_CURSOR_MESSAGE);
|
|
2216
|
+
}
|
|
2217
|
+
return readListSortKeyValues(sort.keys, row);
|
|
1865
2218
|
}
|
|
1866
2219
|
|
|
1867
2220
|
function isMediaAttachmentInput(
|
|
@@ -2101,76 +2454,16 @@ export function createPostService(
|
|
|
2101
2454
|
},
|
|
2102
2455
|
|
|
2103
2456
|
async list(filters = {}) {
|
|
2457
|
+
const keys = listSortKeys(resolveListSort(filters), filters.status);
|
|
2104
2458
|
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
2459
|
|
|
2115
|
-
|
|
2116
|
-
WHEN ${posts.rating} IS NULL THEN 0
|
|
2117
|
-
ELSE 1
|
|
2118
|
-
END`;
|
|
2119
|
-
|
|
2120
|
-
// NULL-sort order differs between dialects: SQLite puts NULLs last for
|
|
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
|
|
2460
|
+
let query = db
|
|
2135
2461
|
.select()
|
|
2136
2462
|
.from(posts)
|
|
2137
2463
|
.where(conditions.length > 0 ? and(...conditions) : undefined)
|
|
2464
|
+
.orderBy(...listOrderBy(keys))
|
|
2138
2465
|
.limit(filters.limit ?? 100);
|
|
2139
2466
|
|
|
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
2467
|
if (filters.offset !== undefined) {
|
|
2175
2468
|
query = query.offset(filters.offset) as typeof query;
|
|
2176
2469
|
}
|
|
@@ -2179,6 +2472,49 @@ export function createPostService(
|
|
|
2179
2472
|
return hydratePosts(rows);
|
|
2180
2473
|
},
|
|
2181
2474
|
|
|
2475
|
+
async listPage(filters, { cursor, limit }) {
|
|
2476
|
+
const sort = resolveListSort(filters);
|
|
2477
|
+
const keys = listSortKeys(sort, filters.status);
|
|
2478
|
+
const conditions = buildFilterConditions(filters);
|
|
2479
|
+
// An empty cursor has always meant the first page — a client that sends
|
|
2480
|
+
// `?cursor=` before it has one keeps working.
|
|
2481
|
+
if (cursor) {
|
|
2482
|
+
conditions.push(
|
|
2483
|
+
buildListKeysetCondition(
|
|
2484
|
+
keys,
|
|
2485
|
+
await resolveListCursor(cursor, sort, keys, filters),
|
|
2486
|
+
),
|
|
2487
|
+
);
|
|
2488
|
+
}
|
|
2489
|
+
|
|
2490
|
+
// One row past the page says whether another page exists, so the last
|
|
2491
|
+
// page carries a null cursor rather than one that leads to an empty page.
|
|
2492
|
+
const rows = await db
|
|
2493
|
+
.select({ post: posts, sortKey: listSortKeyFields(keys) })
|
|
2494
|
+
.from(posts)
|
|
2495
|
+
.where(conditions.length > 0 ? and(...conditions) : undefined)
|
|
2496
|
+
.orderBy(...listOrderBy(keys))
|
|
2497
|
+
.limit(limit + 1);
|
|
2498
|
+
|
|
2499
|
+
const pageRows = rows.slice(0, limit);
|
|
2500
|
+
// The cursor comes from the last row read, not the last Post returned:
|
|
2501
|
+
// hydration drops a row it can't give a slug, and a page cut short that
|
|
2502
|
+
// way must not end the walk.
|
|
2503
|
+
const lastRow = pageRows[pageRows.length - 1];
|
|
2504
|
+
const nextCursor =
|
|
2505
|
+
rows.length > limit && lastRow
|
|
2506
|
+
? encodePostListCursor(
|
|
2507
|
+
listSortMode(sort),
|
|
2508
|
+
readListSortKeyValues(keys, lastRow.sortKey),
|
|
2509
|
+
)
|
|
2510
|
+
: null;
|
|
2511
|
+
|
|
2512
|
+
return {
|
|
2513
|
+
posts: await hydratePosts(pageRows.map((row) => row.post)),
|
|
2514
|
+
nextCursor,
|
|
2515
|
+
};
|
|
2516
|
+
},
|
|
2517
|
+
|
|
2182
2518
|
async listForSitemap({ afterId, limit }) {
|
|
2183
2519
|
// Share the filter conditions with `list()` so visibility/reply/deleted
|
|
2184
2520
|
// semantics stay consistent if they ever change.
|
|
@@ -3672,6 +4008,87 @@ export function createPostService(
|
|
|
3672
4008
|
return hydratePosts(rows);
|
|
3673
4009
|
},
|
|
3674
4010
|
|
|
4011
|
+
async listThreadPostsPage(threadId, options, { cursor, limit }) {
|
|
4012
|
+
const keys = threadSortKeys();
|
|
4013
|
+
const conditions: SQL[] = [
|
|
4014
|
+
eq(posts.siteId, siteId),
|
|
4015
|
+
eq(posts.threadId, threadId),
|
|
4016
|
+
eq(posts.status, options.status ?? "published"),
|
|
4017
|
+
];
|
|
4018
|
+
|
|
4019
|
+
if (cursor) {
|
|
4020
|
+
let values: PostListCursorValue[];
|
|
4021
|
+
if (isLegacyPostListCursor(cursor)) {
|
|
4022
|
+
// A Post ID names a position only inside the Thread being listed,
|
|
4023
|
+
// and only among the Posts this request would return, so it can't
|
|
4024
|
+
// be used to learn anything about a Post elsewhere.
|
|
4025
|
+
const rows = await db
|
|
4026
|
+
.select(listSortKeyFields(keys))
|
|
4027
|
+
.from(posts)
|
|
4028
|
+
.where(and(...conditions, eq(posts.id, cursor)))
|
|
4029
|
+
.limit(1);
|
|
4030
|
+
const row = rows[0];
|
|
4031
|
+
if (!row) {
|
|
4032
|
+
throw new ValidationError(MISSING_LEGACY_CURSOR_MESSAGE);
|
|
4033
|
+
}
|
|
4034
|
+
values = readListSortKeyValues(keys, row);
|
|
4035
|
+
} else {
|
|
4036
|
+
values = decodePostListCursor(cursor, {
|
|
4037
|
+
mode: THREAD_POSTS_SORT_MODE,
|
|
4038
|
+
kinds: keys.map((key) => key.kind),
|
|
4039
|
+
});
|
|
4040
|
+
}
|
|
4041
|
+
conditions.push(buildListKeysetCondition(keys, values));
|
|
4042
|
+
}
|
|
4043
|
+
|
|
4044
|
+
const rows = await db
|
|
4045
|
+
.select({ post: posts, sortKey: listSortKeyFields(keys) })
|
|
4046
|
+
.from(posts)
|
|
4047
|
+
.where(and(...conditions))
|
|
4048
|
+
.orderBy(...listOrderBy(keys))
|
|
4049
|
+
.limit(limit + 1);
|
|
4050
|
+
|
|
4051
|
+
const pageRows = rows.slice(0, limit);
|
|
4052
|
+
const lastRow = pageRows[pageRows.length - 1];
|
|
4053
|
+
return {
|
|
4054
|
+
posts: await hydratePosts(pageRows.map((row) => row.post)),
|
|
4055
|
+
nextCursor:
|
|
4056
|
+
rows.length > limit && lastRow
|
|
4057
|
+
? encodePostListCursor(
|
|
4058
|
+
THREAD_POSTS_SORT_MODE,
|
|
4059
|
+
readListSortKeyValues(keys, lastRow.sortKey),
|
|
4060
|
+
)
|
|
4061
|
+
: null,
|
|
4062
|
+
};
|
|
4063
|
+
},
|
|
4064
|
+
|
|
4065
|
+
async countThreadPosts(threadIds) {
|
|
4066
|
+
const counts = new Map<string, number>();
|
|
4067
|
+
if (threadIds.length === 0) return counts;
|
|
4068
|
+
|
|
4069
|
+
const rows = await batchQueryRows([...new Set(threadIds)], (chunk) =>
|
|
4070
|
+
db
|
|
4071
|
+
.select({
|
|
4072
|
+
threadId: posts.threadId,
|
|
4073
|
+
count: sql<number>`CAST(count(*) AS INTEGER)`.as("count"),
|
|
4074
|
+
})
|
|
4075
|
+
.from(posts)
|
|
4076
|
+
.where(
|
|
4077
|
+
and(
|
|
4078
|
+
eq(posts.siteId, siteId),
|
|
4079
|
+
inArray(posts.threadId, chunk),
|
|
4080
|
+
eq(posts.status, "published"),
|
|
4081
|
+
),
|
|
4082
|
+
)
|
|
4083
|
+
.groupBy(posts.threadId),
|
|
4084
|
+
);
|
|
4085
|
+
|
|
4086
|
+
for (const row of rows) {
|
|
4087
|
+
counts.set(row.threadId, row.count);
|
|
4088
|
+
}
|
|
4089
|
+
return counts;
|
|
4090
|
+
},
|
|
4091
|
+
|
|
3675
4092
|
async getThreadPosition(postId) {
|
|
3676
4093
|
const targetRows = await db
|
|
3677
4094
|
.select({ replyToId: posts.replyToId, threadId: posts.threadId })
|
|
@@ -4248,51 +4665,59 @@ export function createPostService(
|
|
|
4248
4665
|
async getThreadTimelineContext(rootIds) {
|
|
4249
4666
|
if (rootIds.length === 0) return new Map();
|
|
4250
4667
|
|
|
4251
|
-
|
|
4252
|
-
|
|
4253
|
-
|
|
4254
|
-
|
|
4255
|
-
|
|
4668
|
+
// A page of Threads can outgrow one statement's parameter budget — the
|
|
4669
|
+
// API lists up to 100. Each Thread lands in one chunk, so every window
|
|
4670
|
+
// below still partitions over a whole Thread.
|
|
4671
|
+
const contextRows = await batchQueryRows(
|
|
4672
|
+
[...new Set(rootIds)],
|
|
4673
|
+
(chunk) => {
|
|
4674
|
+
const rankedReplies = db
|
|
4675
|
+
.select({
|
|
4676
|
+
id: posts.id,
|
|
4677
|
+
threadId: posts.threadId,
|
|
4678
|
+
firstReplyRank: sql<number>`CAST(ROW_NUMBER() OVER (
|
|
4256
4679
|
PARTITION BY ${posts.threadId}
|
|
4257
4680
|
ORDER BY ${sql.join(threadOrder(), sql`, `)}
|
|
4258
4681
|
) AS INTEGER)`.as("first_reply_rank"),
|
|
4259
|
-
|
|
4682
|
+
latestReplyRank: sql<number>`CAST(ROW_NUMBER() OVER (
|
|
4260
4683
|
PARTITION BY ${posts.threadId}
|
|
4261
4684
|
ORDER BY ${sql.join(threadOrder("desc"), sql`, `)}
|
|
4262
4685
|
) AS INTEGER)`.as("latest_reply_rank"),
|
|
4263
|
-
|
|
4686
|
+
totalReplyCount: sql<number>`CAST(COUNT(*) OVER (
|
|
4264
4687
|
PARTITION BY ${posts.threadId}
|
|
4265
4688
|
) AS INTEGER)`.as("total_reply_count"),
|
|
4266
|
-
|
|
4267
|
-
|
|
4268
|
-
|
|
4269
|
-
|
|
4270
|
-
|
|
4271
|
-
|
|
4272
|
-
|
|
4273
|
-
|
|
4274
|
-
|
|
4275
|
-
|
|
4276
|
-
|
|
4277
|
-
|
|
4278
|
-
|
|
4279
|
-
|
|
4280
|
-
|
|
4281
|
-
|
|
4282
|
-
|
|
4283
|
-
|
|
4284
|
-
|
|
4285
|
-
|
|
4286
|
-
|
|
4287
|
-
|
|
4288
|
-
|
|
4289
|
-
|
|
4290
|
-
|
|
4291
|
-
|
|
4292
|
-
|
|
4293
|
-
|
|
4294
|
-
|
|
4295
|
-
|
|
4689
|
+
})
|
|
4690
|
+
.from(posts)
|
|
4691
|
+
.where(
|
|
4692
|
+
and(
|
|
4693
|
+
eq(posts.siteId, siteId),
|
|
4694
|
+
inArray(posts.threadId, chunk),
|
|
4695
|
+
eq(posts.status, "published"),
|
|
4696
|
+
isNotNull(posts.replyToId),
|
|
4697
|
+
),
|
|
4698
|
+
)
|
|
4699
|
+
.as("ranked_replies");
|
|
4700
|
+
|
|
4701
|
+
return db
|
|
4702
|
+
.select({
|
|
4703
|
+
threadId: rankedReplies.threadId,
|
|
4704
|
+
id: rankedReplies.id,
|
|
4705
|
+
firstReplyRank: rankedReplies.firstReplyRank,
|
|
4706
|
+
latestReplyRank: rankedReplies.latestReplyRank,
|
|
4707
|
+
totalReplyCount: rankedReplies.totalReplyCount,
|
|
4708
|
+
})
|
|
4709
|
+
.from(rankedReplies)
|
|
4710
|
+
.where(
|
|
4711
|
+
or(
|
|
4712
|
+
// One past the leading window: that extra row is never rendered,
|
|
4713
|
+
// it is only where the gap link points. Without it the gap has to
|
|
4714
|
+
// aim at a reply already on screen.
|
|
4715
|
+
lte(rankedReplies.firstReplyRank, THREAD_LEADING_REPLIES + 1),
|
|
4716
|
+
lte(rankedReplies.latestReplyRank, THREAD_TRAILING_REPLIES),
|
|
4717
|
+
),
|
|
4718
|
+
);
|
|
4719
|
+
},
|
|
4720
|
+
);
|
|
4296
4721
|
|
|
4297
4722
|
const hydratedPosts = await hydratePostsById(
|
|
4298
4723
|
contextRows.map((row) => row.id),
|
|
@@ -4582,103 +5007,15 @@ export function createPostService(
|
|
|
4582
5007
|
collectionIds,
|
|
4583
5008
|
options = {},
|
|
4584
5009
|
) {
|
|
4585
|
-
const {
|
|
5010
|
+
const { sortedThreads, keys } = buildCollectionThreadSort(
|
|
4586
5011
|
collectionIds,
|
|
4587
5012
|
options,
|
|
4588
5013
|
);
|
|
4589
|
-
let cursorCondition: SQL<unknown> | undefined;
|
|
4590
5014
|
|
|
4591
|
-
|
|
4592
|
-
|
|
4593
|
-
|
|
4594
|
-
|
|
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
|
-
|
|
4606
|
-
cursorCondition =
|
|
4607
|
-
sortOrder === "oldest"
|
|
4608
|
-
? buildLexicographicCursorCondition([
|
|
4609
|
-
pinnedKey,
|
|
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
|
-
);
|
|
5015
|
+
let query = db
|
|
5016
|
+
.select({ threadId: sortedThreads.threadId })
|
|
5017
|
+
.from(sortedThreads)
|
|
5018
|
+
.orderBy(...listOrderBy(keys));
|
|
4682
5019
|
|
|
4683
5020
|
if (options.limit !== undefined) {
|
|
4684
5021
|
query = query.limit(options.limit) as typeof query;
|
|
@@ -4691,16 +5028,52 @@ export function createPostService(
|
|
|
4691
5028
|
return rows.map((row) => row.threadId);
|
|
4692
5029
|
},
|
|
4693
5030
|
|
|
4694
|
-
async
|
|
4695
|
-
|
|
4696
|
-
|
|
4697
|
-
|
|
4698
|
-
|
|
5031
|
+
async listCollectionThreadRootPage(
|
|
5032
|
+
collectionIds,
|
|
5033
|
+
options,
|
|
5034
|
+
{ cursor, limit },
|
|
5035
|
+
) {
|
|
5036
|
+
const sort = buildCollectionThreadSort(collectionIds, options);
|
|
5037
|
+
const { sortedThreads, keys } = sort;
|
|
5038
|
+
// An empty cursor means the first page, as in `listPage`.
|
|
5039
|
+
const after = cursor
|
|
5040
|
+
? buildListKeysetCondition(
|
|
5041
|
+
keys,
|
|
5042
|
+
await resolveCollectionThreadCursor(cursor, sort),
|
|
5043
|
+
)
|
|
5044
|
+
: undefined;
|
|
5045
|
+
|
|
5046
|
+
// One row past the page says whether another page exists.
|
|
5047
|
+
const rows = await db
|
|
5048
|
+
.select({
|
|
5049
|
+
threadId: sortedThreads.threadId,
|
|
5050
|
+
sortKey: listSortKeyFields(keys),
|
|
5051
|
+
})
|
|
5052
|
+
.from(sortedThreads)
|
|
5053
|
+
.where(after)
|
|
5054
|
+
.orderBy(...listOrderBy(keys))
|
|
5055
|
+
.limit(limit + 1);
|
|
5056
|
+
|
|
5057
|
+
const pageRows = rows.slice(0, limit);
|
|
5058
|
+
// From the last row read, not the last root hydrated, as in `listPage`.
|
|
5059
|
+
const lastRow = pageRows[pageRows.length - 1];
|
|
5060
|
+
const nextCursor =
|
|
5061
|
+
rows.length > limit && lastRow
|
|
5062
|
+
? encodePostListCursor(
|
|
5063
|
+
sort.mode,
|
|
5064
|
+
readListSortKeyValues(keys, lastRow.sortKey),
|
|
5065
|
+
)
|
|
5066
|
+
: null;
|
|
5067
|
+
|
|
5068
|
+
const rootIds = pageRows.map((row) => row.threadId);
|
|
4699
5069
|
const rootsById = await hydratePostsById(rootIds);
|
|
4700
|
-
return
|
|
4701
|
-
|
|
4702
|
-
|
|
4703
|
-
|
|
5070
|
+
return {
|
|
5071
|
+
posts: rootIds.flatMap((rootId) => {
|
|
5072
|
+
const root = rootsById.get(rootId);
|
|
5073
|
+
return root ? [root] : [];
|
|
5074
|
+
}),
|
|
5075
|
+
nextCursor,
|
|
5076
|
+
};
|
|
4704
5077
|
},
|
|
4705
5078
|
|
|
4706
5079
|
async listCollectionFeedEntries(collectionId, options = {}) {
|