@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
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Public API Post responses
|
|
3
|
+
*
|
|
4
|
+
* The reading view of a Post that `/api/public/*` returns: rendered body or
|
|
5
|
+
* Markdown, public permalink, and public collection references.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { Services } from "../services/index.js";
|
|
9
|
+
import type { AppConfig } from "../types/config.js";
|
|
10
|
+
import type { Collection, Media, Post } from "../types.js";
|
|
11
|
+
import { getCollectionPagePath } from "./collection-paths.js";
|
|
12
|
+
import { toApiAttachment } from "./api-posts.js";
|
|
13
|
+
import { tiptapJsonToMarkdown } from "./tiptap-to-markdown.js";
|
|
14
|
+
import { toPublicPath } from "./url.js";
|
|
15
|
+
import { getImageUrl, getMediaUrl, getPublicUrlForProvider } from "./image.js";
|
|
16
|
+
|
|
17
|
+
export type PublicPostBaseResponse = {
|
|
18
|
+
id: string;
|
|
19
|
+
format: Post["format"];
|
|
20
|
+
status: "published";
|
|
21
|
+
visibility: Post["visibility"];
|
|
22
|
+
slug: string;
|
|
23
|
+
permalink: string;
|
|
24
|
+
title?: string | null;
|
|
25
|
+
url?: string | null;
|
|
26
|
+
sourceName?: string | null;
|
|
27
|
+
sourceUrl?: string | null;
|
|
28
|
+
quoteText: string | null;
|
|
29
|
+
summary: string | null;
|
|
30
|
+
rating: number | null;
|
|
31
|
+
previewKind: string | null;
|
|
32
|
+
previewProvider: string | null;
|
|
33
|
+
previewImageUrl: string | null;
|
|
34
|
+
replyToId: string | null;
|
|
35
|
+
threadId: string;
|
|
36
|
+
/**
|
|
37
|
+
* BCP 47 content language, or null on a post written before the site
|
|
38
|
+
* enabled multilingual content.
|
|
39
|
+
*/
|
|
40
|
+
language: string | null;
|
|
41
|
+
/**
|
|
42
|
+
* Shared by every translation of this Thread. Null when the Thread has no
|
|
43
|
+
* translations.
|
|
44
|
+
*/
|
|
45
|
+
translationGroupId: string | null;
|
|
46
|
+
/** Reply published without announcing its Thread. Always false on roots. */
|
|
47
|
+
quietReply: boolean;
|
|
48
|
+
pinnedAt: number | null;
|
|
49
|
+
featuredAt: number | null;
|
|
50
|
+
publishedAt: number | null;
|
|
51
|
+
/** Root only: newest post in the Thread, quiet replies excluded. */
|
|
52
|
+
lastActivityAt: number;
|
|
53
|
+
/** Root only: newest post in the Thread, quiet replies included. */
|
|
54
|
+
threadUpdatedAt: number;
|
|
55
|
+
/**
|
|
56
|
+
* Published Posts in this Post's Thread, its root included: 1 for a Post on
|
|
57
|
+
* its own.
|
|
58
|
+
*/
|
|
59
|
+
threadPostCount: number;
|
|
60
|
+
createdAt: number;
|
|
61
|
+
updatedAt: number;
|
|
62
|
+
attachments: ReturnType<typeof toApiAttachment>[];
|
|
63
|
+
collections: {
|
|
64
|
+
id: string;
|
|
65
|
+
slug: string;
|
|
66
|
+
title: string;
|
|
67
|
+
url: string;
|
|
68
|
+
}[];
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
export type PublicPostRenderedResponse = PublicPostBaseResponse & {
|
|
72
|
+
bodyHtml: string | null;
|
|
73
|
+
bodyText: string | null;
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
export type PublicPostMarkdownResponse = PublicPostBaseResponse & {
|
|
77
|
+
bodyMarkdown: string | null;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
export type PublicPostResponse =
|
|
81
|
+
PublicPostRenderedResponse | PublicPostMarkdownResponse;
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* The public Markdown for a post, or null for a historical body that isn't
|
|
85
|
+
* TipTap JSON. One unreadable row answers null and is logged, rather than
|
|
86
|
+
* failing the whole listing; `bodyHtml` still falls back to stored HTML.
|
|
87
|
+
*/
|
|
88
|
+
function toPublicBodyMarkdown(postId: string, body: string): string | null {
|
|
89
|
+
try {
|
|
90
|
+
return tiptapJsonToMarkdown(body);
|
|
91
|
+
} catch (error) {
|
|
92
|
+
// eslint-disable-next-line no-console -- A skipped body must leave a trace
|
|
93
|
+
console.error(
|
|
94
|
+
`Couldn't convert the body of post ${postId} to Markdown`,
|
|
95
|
+
error,
|
|
96
|
+
);
|
|
97
|
+
return null;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* One Post's reading view, from its attachments, its Thread's collections, and
|
|
103
|
+
* its Thread's Post count.
|
|
104
|
+
*
|
|
105
|
+
* @param post - The Post
|
|
106
|
+
* @param related - What was read about it in the same batch as its page
|
|
107
|
+
* @param appConfig - Media URLs and the site path prefix
|
|
108
|
+
* @param options - `content: "markdown"` returns `bodyMarkdown` instead of the
|
|
109
|
+
* rendered body
|
|
110
|
+
* @returns The public response
|
|
111
|
+
* @example
|
|
112
|
+
* toPublicPost(post, { media: [], collections: [], threadPostCount: 1 }, config);
|
|
113
|
+
*/
|
|
114
|
+
export function toPublicPost(
|
|
115
|
+
post: Post,
|
|
116
|
+
related: {
|
|
117
|
+
media: Media[];
|
|
118
|
+
collections: Collection[];
|
|
119
|
+
threadPostCount: number;
|
|
120
|
+
},
|
|
121
|
+
appConfig: AppConfig,
|
|
122
|
+
options?: { content?: "markdown" },
|
|
123
|
+
): PublicPostResponse {
|
|
124
|
+
const {
|
|
125
|
+
r2PublicUrl,
|
|
126
|
+
imageTransformUrl,
|
|
127
|
+
s3PublicUrl,
|
|
128
|
+
localPublicUrl,
|
|
129
|
+
sitePathPrefix,
|
|
130
|
+
storageDriver,
|
|
131
|
+
} = appConfig;
|
|
132
|
+
|
|
133
|
+
const previewImagePublicUrl = getPublicUrlForProvider(
|
|
134
|
+
storageDriver,
|
|
135
|
+
r2PublicUrl,
|
|
136
|
+
s3PublicUrl,
|
|
137
|
+
localPublicUrl,
|
|
138
|
+
);
|
|
139
|
+
const previewImageUrl = post.previewImageKey
|
|
140
|
+
? getImageUrl(
|
|
141
|
+
getMediaUrl(
|
|
142
|
+
post.previewImageKey,
|
|
143
|
+
previewImagePublicUrl,
|
|
144
|
+
sitePathPrefix,
|
|
145
|
+
),
|
|
146
|
+
imageTransformUrl,
|
|
147
|
+
{ width: 1280, quality: 80, format: "auto", fit: "scale-down" },
|
|
148
|
+
)
|
|
149
|
+
: null;
|
|
150
|
+
|
|
151
|
+
const base = {
|
|
152
|
+
id: post.id,
|
|
153
|
+
format: post.format,
|
|
154
|
+
status: "published" as const,
|
|
155
|
+
visibility: post.visibility,
|
|
156
|
+
slug: post.slug,
|
|
157
|
+
permalink: toPublicPath(`/${post.slug}`, sitePathPrefix),
|
|
158
|
+
quoteText: post.quoteText,
|
|
159
|
+
summary: post.summary,
|
|
160
|
+
rating: post.rating,
|
|
161
|
+
previewKind: post.previewKind,
|
|
162
|
+
previewProvider: post.previewProvider,
|
|
163
|
+
previewImageUrl,
|
|
164
|
+
replyToId: post.replyToId,
|
|
165
|
+
threadId: post.threadId,
|
|
166
|
+
language: post.language,
|
|
167
|
+
translationGroupId: post.translationGroupId,
|
|
168
|
+
quietReply: post.quietReply,
|
|
169
|
+
pinnedAt: post.pinnedAt,
|
|
170
|
+
featuredAt: post.featuredAt,
|
|
171
|
+
publishedAt: post.publishedAt,
|
|
172
|
+
lastActivityAt: post.lastActivityAt,
|
|
173
|
+
threadUpdatedAt: post.threadUpdatedAt,
|
|
174
|
+
threadPostCount: related.threadPostCount,
|
|
175
|
+
createdAt: post.createdAt,
|
|
176
|
+
updatedAt: post.updatedAt,
|
|
177
|
+
attachments: related.media.map((media) =>
|
|
178
|
+
toApiAttachment(
|
|
179
|
+
media,
|
|
180
|
+
r2PublicUrl,
|
|
181
|
+
imageTransformUrl,
|
|
182
|
+
s3PublicUrl,
|
|
183
|
+
localPublicUrl,
|
|
184
|
+
sitePathPrefix,
|
|
185
|
+
),
|
|
186
|
+
),
|
|
187
|
+
collections: related.collections.map((collection) => ({
|
|
188
|
+
id: collection.id,
|
|
189
|
+
slug: collection.slug,
|
|
190
|
+
title: collection.title,
|
|
191
|
+
url: toPublicPath(getCollectionPagePath(collection.slug), sitePathPrefix),
|
|
192
|
+
})),
|
|
193
|
+
};
|
|
194
|
+
const contentFields =
|
|
195
|
+
options?.content === "markdown"
|
|
196
|
+
? {
|
|
197
|
+
bodyMarkdown: post.body
|
|
198
|
+
? toPublicBodyMarkdown(post.id, post.body)
|
|
199
|
+
: null,
|
|
200
|
+
}
|
|
201
|
+
: {
|
|
202
|
+
bodyHtml: post.bodyHtml,
|
|
203
|
+
bodyText: post.bodyText,
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
if (post.format === "quote") {
|
|
207
|
+
return {
|
|
208
|
+
...base,
|
|
209
|
+
...contentFields,
|
|
210
|
+
sourceName: post.title,
|
|
211
|
+
sourceUrl: post.url,
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
return {
|
|
216
|
+
...base,
|
|
217
|
+
...contentFields,
|
|
218
|
+
title: post.title,
|
|
219
|
+
url: post.url,
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Reading-view responses for Posts already loaded. Attachments, Thread
|
|
225
|
+
* collections, and Thread Post counts are each read in one batch.
|
|
226
|
+
*
|
|
227
|
+
* @param deps - Services and app config; a request's `c.var`
|
|
228
|
+
* @param posts - Posts to respond with, in order
|
|
229
|
+
* @param options - `content: "markdown"` returns `bodyMarkdown`
|
|
230
|
+
* @returns One response per Post, in the same order
|
|
231
|
+
* @example
|
|
232
|
+
* const responses = await loadPublicPostResponses(c.var, page.posts, {});
|
|
233
|
+
*/
|
|
234
|
+
export async function loadPublicPostResponses(
|
|
235
|
+
deps: {
|
|
236
|
+
services: Pick<Services, "media" | "posts" | "collections">;
|
|
237
|
+
appConfig: AppConfig;
|
|
238
|
+
},
|
|
239
|
+
posts: Post[],
|
|
240
|
+
options: { content?: "markdown" } = {},
|
|
241
|
+
): Promise<PublicPostResponse[]> {
|
|
242
|
+
if (posts.length === 0) return [];
|
|
243
|
+
const { services, appConfig } = deps;
|
|
244
|
+
const postIds = posts.map((post) => post.id);
|
|
245
|
+
const [mediaMap, collectionsMap, threadPostCounts] = await Promise.all([
|
|
246
|
+
services.media.getByPostIds(postIds),
|
|
247
|
+
services.collections.getCollectionsByPostIds(postIds),
|
|
248
|
+
services.posts.countThreadPosts(posts.map((post) => post.threadId)),
|
|
249
|
+
]);
|
|
250
|
+
|
|
251
|
+
return posts.map((post) =>
|
|
252
|
+
toPublicPost(
|
|
253
|
+
post,
|
|
254
|
+
{
|
|
255
|
+
media: mediaMap.get(post.id) ?? [],
|
|
256
|
+
collections: collectionsMap.get(post.id) ?? [],
|
|
257
|
+
threadPostCount: threadPostCounts.get(post.threadId) ?? 0,
|
|
258
|
+
},
|
|
259
|
+
appConfig,
|
|
260
|
+
options,
|
|
261
|
+
),
|
|
262
|
+
);
|
|
263
|
+
}
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* API Thread responses
|
|
3
|
+
*
|
|
4
|
+
* The Thread object the author API, the public API, and MCP return. One shape
|
|
5
|
+
* for all three, parameterized by how each surface renders a Post and how it
|
|
6
|
+
* names one: the author API's Post is the editing view, the public API's the
|
|
7
|
+
* reading view.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { ThreadSummary } from "../services/thread.js";
|
|
11
|
+
import type { Post } from "../types.js";
|
|
12
|
+
import {
|
|
13
|
+
loadApiPostResponses,
|
|
14
|
+
type ApiPostResponse,
|
|
15
|
+
type ApiPostResponseDeps,
|
|
16
|
+
} from "./api-posts.js";
|
|
17
|
+
import {
|
|
18
|
+
loadPublicPostResponses,
|
|
19
|
+
type PublicPostResponse,
|
|
20
|
+
} from "./api-public-posts.js";
|
|
21
|
+
import { toPublicPath } from "./url.js";
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The homepage fold of one Thread: what it shows either side of the gap, and
|
|
25
|
+
* how much it hides. Which replies it keeps is the site's choice
|
|
26
|
+
* (`lib/thread-fold.ts`) and may change; `hidden` always says how many it left
|
|
27
|
+
* out, so a caller never has to work it out.
|
|
28
|
+
*/
|
|
29
|
+
export interface ApiThreadFold<P, G> {
|
|
30
|
+
/** The earliest replies, as context. */
|
|
31
|
+
leading: P[];
|
|
32
|
+
/** How many replies the fold leaves out, between `leading` and `trailing`. */
|
|
33
|
+
hidden: number;
|
|
34
|
+
/** The first reply left out, where a link to the rest opens; null if none. */
|
|
35
|
+
gap: G | null;
|
|
36
|
+
/** The latest replies, oldest first; the newest reply is the last. */
|
|
37
|
+
trailing: P[];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** One Thread, as a Thread list or lookup returns it. */
|
|
41
|
+
export interface ApiThreadResponse<P, G> {
|
|
42
|
+
/** The Thread's ID, which is its root Post's ID. */
|
|
43
|
+
id: string;
|
|
44
|
+
/** Published Posts in the Thread, the root included. */
|
|
45
|
+
postCount: number;
|
|
46
|
+
/** Newest post in the Thread, quiet replies excluded. */
|
|
47
|
+
lastActivityAt: number;
|
|
48
|
+
/** Newest post in the Thread, quiet replies included. */
|
|
49
|
+
threadUpdatedAt: number;
|
|
50
|
+
root: P;
|
|
51
|
+
/** Present when the request asked for it. */
|
|
52
|
+
fold?: ApiThreadFold<P, G>;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** How the author API and MCP name a Post a fold leaves out. */
|
|
56
|
+
export interface ApiThreadGap {
|
|
57
|
+
id: string;
|
|
58
|
+
slug: string;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** The author API's and MCP's Thread object. */
|
|
62
|
+
export type ApiAuthorThreadResponse = ApiThreadResponse<
|
|
63
|
+
ApiPostResponse,
|
|
64
|
+
ApiThreadGap
|
|
65
|
+
>;
|
|
66
|
+
|
|
67
|
+
/** How the public API names a Post a fold leaves out: with its address. */
|
|
68
|
+
export interface PublicThreadGap extends ApiThreadGap {
|
|
69
|
+
permalink: string;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** The public API's Thread object. */
|
|
73
|
+
export type PublicThreadResponse = ApiThreadResponse<
|
|
74
|
+
PublicPostResponse,
|
|
75
|
+
PublicThreadGap
|
|
76
|
+
>;
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Every Post a set of Thread responses renders, so their attachments and
|
|
80
|
+
* counts can be read in one batch: each root, then the replies its fold shows.
|
|
81
|
+
*
|
|
82
|
+
* @param summaries - Threads to respond with
|
|
83
|
+
* @returns The Posts, each once
|
|
84
|
+
* @example
|
|
85
|
+
* const responses = await render(collectThreadResponsePosts(summaries));
|
|
86
|
+
*/
|
|
87
|
+
export function collectThreadResponsePosts(summaries: ThreadSummary[]): Post[] {
|
|
88
|
+
const seen = new Map<string, Post>();
|
|
89
|
+
for (const { root, fold } of summaries) {
|
|
90
|
+
seen.set(root.id, root);
|
|
91
|
+
if (!fold) continue;
|
|
92
|
+
for (const post of [
|
|
93
|
+
...fold.leadingReplies,
|
|
94
|
+
...fold.trailingReplies,
|
|
95
|
+
fold.latestReply,
|
|
96
|
+
]) {
|
|
97
|
+
seen.set(post.id, post);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return [...seen.values()];
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* One Thread's response, from Post responses already rendered.
|
|
105
|
+
*
|
|
106
|
+
* @param summary - The Thread
|
|
107
|
+
* @param respond - The rendered response for a Post in it
|
|
108
|
+
* @param reference - How the surface names the Post a fold leaves out
|
|
109
|
+
* @returns The Thread object
|
|
110
|
+
* @example
|
|
111
|
+
* toApiThreadResponse(summary, (post) => byId.get(post.id)!, toGap);
|
|
112
|
+
*/
|
|
113
|
+
export function toApiThreadResponse<P, G>(
|
|
114
|
+
summary: ThreadSummary,
|
|
115
|
+
respond: (post: Post) => P,
|
|
116
|
+
reference: (post: Post) => G,
|
|
117
|
+
): ApiThreadResponse<P, G> {
|
|
118
|
+
const { root, fold } = summary;
|
|
119
|
+
const response: ApiThreadResponse<P, G> = {
|
|
120
|
+
id: root.id,
|
|
121
|
+
postCount: summary.postCount,
|
|
122
|
+
lastActivityAt: root.lastActivityAt,
|
|
123
|
+
threadUpdatedAt: root.threadUpdatedAt,
|
|
124
|
+
root: respond(root),
|
|
125
|
+
};
|
|
126
|
+
if (fold === null) {
|
|
127
|
+
response.fold = { leading: [], hidden: 0, gap: null, trailing: [] };
|
|
128
|
+
} else if (fold) {
|
|
129
|
+
response.fold = {
|
|
130
|
+
leading: fold.leadingReplies.map(respond),
|
|
131
|
+
hidden: fold.hiddenCount,
|
|
132
|
+
gap: fold.firstHiddenReply ? reference(fold.firstHiddenReply) : null,
|
|
133
|
+
trailing: [...fold.trailingReplies, fold.latestReply].map(respond),
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
return response;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* The author API's and MCP's Thread objects for Threads already listed.
|
|
141
|
+
*
|
|
142
|
+
* @param deps - Services and app config
|
|
143
|
+
* @param summaries - Threads to respond with, in order
|
|
144
|
+
* @returns One Thread object per summary, in the same order
|
|
145
|
+
* @example
|
|
146
|
+
* const threads = await loadApiThreadResponses(c.var, page.threads);
|
|
147
|
+
*/
|
|
148
|
+
export async function loadApiThreadResponses(
|
|
149
|
+
deps: ApiPostResponseDeps,
|
|
150
|
+
summaries: ThreadSummary[],
|
|
151
|
+
): Promise<ApiAuthorThreadResponse[]> {
|
|
152
|
+
return buildThreadResponses(
|
|
153
|
+
summaries,
|
|
154
|
+
(posts) => loadApiPostResponses(deps, posts),
|
|
155
|
+
(post) => ({ id: post.id, slug: post.slug }),
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* The public API's Thread objects for Threads already listed.
|
|
161
|
+
*
|
|
162
|
+
* @param deps - Services and app config
|
|
163
|
+
* @param summaries - Threads to respond with, in order
|
|
164
|
+
* @param options - `content: "markdown"` returns `bodyMarkdown` on every Post
|
|
165
|
+
* @returns One Thread object per summary, in the same order
|
|
166
|
+
* @example
|
|
167
|
+
* const threads = await loadPublicThreadResponses(c.var, page.threads, {});
|
|
168
|
+
*/
|
|
169
|
+
export async function loadPublicThreadResponses(
|
|
170
|
+
deps: ApiPostResponseDeps,
|
|
171
|
+
summaries: ThreadSummary[],
|
|
172
|
+
options: { content?: "markdown" } = {},
|
|
173
|
+
): Promise<PublicThreadResponse[]> {
|
|
174
|
+
return buildThreadResponses(
|
|
175
|
+
summaries,
|
|
176
|
+
(posts) => loadPublicPostResponses(deps, posts, options),
|
|
177
|
+
(post) => ({
|
|
178
|
+
id: post.id,
|
|
179
|
+
slug: post.slug,
|
|
180
|
+
permalink: toPublicPath(`/${post.slug}`, deps.appConfig.sitePathPrefix),
|
|
181
|
+
}),
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** Render every Post the Threads show in one batch, then assemble them. */
|
|
186
|
+
async function buildThreadResponses<P, G>(
|
|
187
|
+
summaries: ThreadSummary[],
|
|
188
|
+
render: (posts: Post[]) => Promise<P[]>,
|
|
189
|
+
reference: (post: Post) => G,
|
|
190
|
+
): Promise<ApiThreadResponse<P, G>[]> {
|
|
191
|
+
const posts = collectThreadResponsePosts(summaries);
|
|
192
|
+
const responses = await render(posts);
|
|
193
|
+
const byId = new Map(
|
|
194
|
+
posts.map((post, index) => [post.id, responses[index]] as const),
|
|
195
|
+
);
|
|
196
|
+
return summaries.map((summary) =>
|
|
197
|
+
toApiThreadResponse(
|
|
198
|
+
summary,
|
|
199
|
+
(post) => {
|
|
200
|
+
const response = byId.get(post.id);
|
|
201
|
+
if (!response) throw new Error(`No response built for post ${post.id}`);
|
|
202
|
+
return response;
|
|
203
|
+
},
|
|
204
|
+
reference,
|
|
205
|
+
),
|
|
206
|
+
);
|
|
207
|
+
}
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The cursor `posts.listPage()` and `posts.listCollectionThreadRootPage()`
|
|
3
|
+
* hand out as `nextCursor`.
|
|
4
|
+
*
|
|
5
|
+
* A cursor says where the previous page ended: the sort mode it was taken in
|
|
6
|
+
* and the full sort-key tuple of that page's last row, key for key as the
|
|
7
|
+
* ORDER BY reads them. Resuming is a pure keyset comparison against those
|
|
8
|
+
* values, so the post the page ended on can be deleted, unpublished, or edited
|
|
9
|
+
* before the next request without moving the position. The two lists name
|
|
10
|
+
* their sort modes apart, so a cursor from one never resumes the other.
|
|
11
|
+
*
|
|
12
|
+
* Wire format: `base64url(JSON { v, s, k })` — format version, sort mode, and
|
|
13
|
+
* key tuple. It is not signed. A caller who edits one can only move their own
|
|
14
|
+
* position on an axis they may already read; the values are never looked up.
|
|
15
|
+
* Filters stay out: a position on the sort axis is valid under any filter.
|
|
16
|
+
*
|
|
17
|
+
* Callers treat the string as opaque. The one other shape still accepted is
|
|
18
|
+
* the bare post TypeID earlier releases returned; see
|
|
19
|
+
* {@link isLegacyPostListCursor}.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
import { ValidationError } from "./errors.js";
|
|
23
|
+
import { ID_PREFIX, isTypeId } from "./ids.js";
|
|
24
|
+
|
|
25
|
+
/** Bumped when the payload shape changes; older versions are rejected. */
|
|
26
|
+
const POST_LIST_CURSOR_VERSION = 1;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Longest encoded cursor accepted. The largest tuple (pinned, rating, time,
|
|
30
|
+
* id) encodes to under 200 characters; the cap only keeps a hostile value from
|
|
31
|
+
* reaching the decoder.
|
|
32
|
+
*/
|
|
33
|
+
const MAX_ENCODED_LENGTH = 512;
|
|
34
|
+
|
|
35
|
+
const BASE64URL_PATTERN = /^[A-Za-z0-9_-]+$/;
|
|
36
|
+
|
|
37
|
+
const UNREADABLE_MESSAGE =
|
|
38
|
+
"This cursor can't be read. Pass back nextCursor unchanged, or leave cursor out to start from the first page.";
|
|
39
|
+
const VERSION_MESSAGE =
|
|
40
|
+
"This cursor is from a different version of Jant. Leave cursor out to start from the first page.";
|
|
41
|
+
const MODE_MESSAGE =
|
|
42
|
+
"This cursor is from a list in a different order. Leave cursor out to start from the first page.";
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* A legacy cursor naming a Post that is gone, or that the caller can't see.
|
|
46
|
+
* One message for both, so a cursor can't be used to probe for private Posts.
|
|
47
|
+
*/
|
|
48
|
+
export const MISSING_LEGACY_CURSOR_MESSAGE =
|
|
49
|
+
"This cursor names a post that can't be found. Leave cursor out to start from the first page.";
|
|
50
|
+
|
|
51
|
+
/** What one position of the key tuple holds. */
|
|
52
|
+
export type PostListCursorKeyKind = "number" | "id";
|
|
53
|
+
|
|
54
|
+
/** One sort-key value: a Unix timestamp, a rating, a pin, or the post ID. */
|
|
55
|
+
export type PostListCursorValue = number | string;
|
|
56
|
+
|
|
57
|
+
/** What a cursor must match to resume the current request. */
|
|
58
|
+
export interface PostListCursorShape {
|
|
59
|
+
/** The request's sort mode, as the service names it. */
|
|
60
|
+
mode: string;
|
|
61
|
+
/** One kind per ORDER BY key, in ORDER BY order. */
|
|
62
|
+
kinds: readonly PostListCursorKeyKind[];
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
interface PostListCursorPayload {
|
|
66
|
+
v: number;
|
|
67
|
+
s: string;
|
|
68
|
+
k: PostListCursorValue[];
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function encodeBase64Url(text: string): string {
|
|
72
|
+
return btoa(text).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function decodeBase64Url(encoded: string): string | null {
|
|
76
|
+
const padded =
|
|
77
|
+
encoded.replace(/-/g, "+").replace(/_/g, "/") +
|
|
78
|
+
"===".slice((encoded.length + 3) % 4);
|
|
79
|
+
try {
|
|
80
|
+
return atob(padded);
|
|
81
|
+
} catch {
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function isPlainObject(value: unknown): value is Record<string, unknown> {
|
|
87
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function matchesKind(
|
|
91
|
+
value: unknown,
|
|
92
|
+
kind: PostListCursorKeyKind,
|
|
93
|
+
): value is PostListCursorValue {
|
|
94
|
+
return kind === "number"
|
|
95
|
+
? Number.isSafeInteger(value)
|
|
96
|
+
: typeof value === "string" && isTypeId(value, ID_PREFIX.post);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Encode the position after a page's last row.
|
|
101
|
+
*
|
|
102
|
+
* @param mode - The sort mode the page was read in
|
|
103
|
+
* @param values - That row's sort-key tuple, in ORDER BY order
|
|
104
|
+
* @returns The opaque cursor string
|
|
105
|
+
* @example
|
|
106
|
+
* ```ts
|
|
107
|
+
* encodePostListCursor("newest:activity:pinned", [-1, 1706000000, post.id]);
|
|
108
|
+
* ```
|
|
109
|
+
*/
|
|
110
|
+
export function encodePostListCursor(
|
|
111
|
+
mode: string,
|
|
112
|
+
values: readonly PostListCursorValue[],
|
|
113
|
+
): string {
|
|
114
|
+
const payload: PostListCursorPayload = {
|
|
115
|
+
v: POST_LIST_CURSOR_VERSION,
|
|
116
|
+
s: mode,
|
|
117
|
+
k: [...values],
|
|
118
|
+
};
|
|
119
|
+
// Every value is ASCII — digits, a mode name, a TypeID — so `btoa` can take
|
|
120
|
+
// the JSON text as it is.
|
|
121
|
+
return encodeBase64Url(JSON.stringify(payload));
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Decode a cursor for the current request, strictly.
|
|
126
|
+
*
|
|
127
|
+
* Nothing here falls back to the first page: a cursor that can't be read, that
|
|
128
|
+
* an unknown format version wrote, that was taken in another sort mode, or
|
|
129
|
+
* whose tuple doesn't fit the mode's keys is rejected, because answering with
|
|
130
|
+
* page 1 would hand the caller posts it already has.
|
|
131
|
+
*
|
|
132
|
+
* @param raw - The `cursor` the caller sent
|
|
133
|
+
* @param shape - The current request's sort mode and key kinds
|
|
134
|
+
* @returns The key tuple, in ORDER BY order
|
|
135
|
+
* @throws {ValidationError} When the cursor doesn't resume this request
|
|
136
|
+
* @example
|
|
137
|
+
* ```ts
|
|
138
|
+
* const after = decodePostListCursor(cursor, {
|
|
139
|
+
* mode: "newest:published:unpinned",
|
|
140
|
+
* kinds: ["number", "id"],
|
|
141
|
+
* });
|
|
142
|
+
* ```
|
|
143
|
+
*/
|
|
144
|
+
export function decodePostListCursor(
|
|
145
|
+
raw: string,
|
|
146
|
+
shape: PostListCursorShape,
|
|
147
|
+
): PostListCursorValue[] {
|
|
148
|
+
if (raw.length > MAX_ENCODED_LENGTH || !BASE64URL_PATTERN.test(raw)) {
|
|
149
|
+
throw new ValidationError(UNREADABLE_MESSAGE);
|
|
150
|
+
}
|
|
151
|
+
const text = decodeBase64Url(raw);
|
|
152
|
+
if (text === null) {
|
|
153
|
+
throw new ValidationError(UNREADABLE_MESSAGE);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
let parsed: unknown;
|
|
157
|
+
try {
|
|
158
|
+
parsed = JSON.parse(text);
|
|
159
|
+
} catch {
|
|
160
|
+
throw new ValidationError(UNREADABLE_MESSAGE);
|
|
161
|
+
}
|
|
162
|
+
if (!isPlainObject(parsed) || !Number.isSafeInteger(parsed.v)) {
|
|
163
|
+
throw new ValidationError(UNREADABLE_MESSAGE);
|
|
164
|
+
}
|
|
165
|
+
if (parsed.v !== POST_LIST_CURSOR_VERSION) {
|
|
166
|
+
throw new ValidationError(VERSION_MESSAGE);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const { s: mode, k: values } = parsed;
|
|
170
|
+
if (
|
|
171
|
+
Object.keys(parsed).length !== 3 ||
|
|
172
|
+
typeof mode !== "string" ||
|
|
173
|
+
!Array.isArray(values)
|
|
174
|
+
) {
|
|
175
|
+
throw new ValidationError(UNREADABLE_MESSAGE);
|
|
176
|
+
}
|
|
177
|
+
if (mode !== shape.mode) {
|
|
178
|
+
throw new ValidationError(MODE_MESSAGE);
|
|
179
|
+
}
|
|
180
|
+
if (values.length !== shape.kinds.length) {
|
|
181
|
+
throw new ValidationError(UNREADABLE_MESSAGE);
|
|
182
|
+
}
|
|
183
|
+
const tuple: PostListCursorValue[] = [];
|
|
184
|
+
for (const [index, kind] of shape.kinds.entries()) {
|
|
185
|
+
const value: unknown = values[index];
|
|
186
|
+
if (!matchesKind(value, kind)) {
|
|
187
|
+
throw new ValidationError(UNREADABLE_MESSAGE);
|
|
188
|
+
}
|
|
189
|
+
tuple.push(value);
|
|
190
|
+
}
|
|
191
|
+
return tuple;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Whether a cursor is the bare post TypeID that releases before the opaque
|
|
196
|
+
* format returned as `nextCursor`.
|
|
197
|
+
*
|
|
198
|
+
* Accepted indefinitely — a stored cursor, or one a client built from a post
|
|
199
|
+
* ID, must keep resuming. An encoded cursor can never look like one: it opens
|
|
200
|
+
* with the base64url of `{"`, never with `pst_`.
|
|
201
|
+
*
|
|
202
|
+
* @param raw - The `cursor` the caller sent
|
|
203
|
+
* @returns Whether to resolve it as a post ID
|
|
204
|
+
* @example
|
|
205
|
+
* ```ts
|
|
206
|
+
* isLegacyPostListCursor("pst_01jpyx3m7gw4w3h7m4bknq0v1d"); // => true
|
|
207
|
+
* ```
|
|
208
|
+
*/
|
|
209
|
+
export function isLegacyPostListCursor(raw: string): boolean {
|
|
210
|
+
return isTypeId(raw, ID_PREFIX.post);
|
|
211
|
+
}
|