birdclaw 0.8.3 → 0.8.5

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.
Files changed (284) hide show
  1. package/CHANGELOG.md +24 -0
  2. package/README.md +10 -8
  3. package/bin/birdclaw.mjs +4 -66
  4. package/dist/cli/birdclaw.js +28164 -0
  5. package/dist/client/assets/AvatarChip-DLoqM5xD.js +1 -0
  6. package/dist/client/assets/ProfilePreview-DQX6_xc3.js +1 -0
  7. package/dist/client/assets/SavedTimelineView-BhSiVyR1.js +1 -0
  8. package/dist/client/assets/SmartTimestamp-BR0-9se2.js +1 -0
  9. package/dist/client/assets/TimelineRouteFrame-9NDLnXzH.js +1 -0
  10. package/dist/client/assets/blocks-CFp_njvG.js +1 -0
  11. package/dist/client/assets/bookmarks-LbYOsoKf.js +1 -0
  12. package/dist/client/assets/chunk-C0phanGV.js +1 -0
  13. package/dist/client/assets/circle-check-C_Vq3ElD.js +1 -0
  14. package/dist/client/assets/client-stream-contracts-sR4xMQxY.js +5 -0
  15. package/dist/client/assets/createLucideIcon-JV8isFFR.js +1 -0
  16. package/dist/client/assets/data-sources-euTr6T_L.js +1 -0
  17. package/dist/client/assets/discuss-BWKu_p7B.js +1 -0
  18. package/dist/client/assets/dms-SLXMtC4v.js +1 -0
  19. package/dist/client/assets/inbox-BybSI5Vf.js +1 -0
  20. package/dist/client/assets/index-DZVisyna.js +31 -0
  21. package/dist/client/assets/index-bpFrQ5yG.css +1 -0
  22. package/dist/client/assets/likes-dyh9p8y_.js +1 -0
  23. package/dist/client/assets/links-CIwqK_dX.js +1 -0
  24. package/dist/client/assets/mapbox-B4zqX7r0.js +2 -0
  25. package/dist/client/assets/mapbox-gl-DYNcLcll.js +3215 -0
  26. package/dist/client/assets/mentions-BoVVAIT7.js +1 -0
  27. package/dist/client/assets/network-map-zRysIY_b.js +2 -0
  28. package/dist/client/assets/profile-analyze-C_yDeX6-.js +1 -0
  29. package/dist/client/assets/profile-hydration-client-YEtJsNTv.js +1 -0
  30. package/dist/client/assets/profiles._handle-DdIVPqU-.js +1 -0
  31. package/dist/client/assets/query-client-fYu2rzRC.js +17 -0
  32. package/dist/client/assets/rate-limits-BWPmPOSh.js +1 -0
  33. package/dist/client/assets/react-dom-DHl6Ug7F.js +1 -0
  34. package/dist/client/assets/react-nBylq1U5.js +1 -0
  35. package/dist/client/assets/repeat-2-xCwHM9hD.js +1 -0
  36. package/dist/client/assets/routes-CiLuYpZr.js +1 -0
  37. package/dist/client/assets/schemas-I9r6F6ya.js +64 -0
  38. package/dist/client/assets/search-Dq5kYsxA.js +1 -0
  39. package/dist/client/assets/sparkles-CQVlvHsZ.js +1 -0
  40. package/dist/client/assets/styles-B8s3tt3r.css +2 -0
  41. package/dist/client/assets/today-BoO64YoY.js +1 -0
  42. package/dist/client/assets/triangle-alert-CBL7s2pf.js +1 -0
  43. package/dist/client/assets/useTimelineRouteData-BaO3Rna1.js +1 -0
  44. package/dist/client/assets/users-CHbKlqUr.js +1 -0
  45. package/dist/server/assets/AvatarChip-zUDAEmjL.js +105 -0
  46. package/dist/server/assets/FeedState-ntW0Q2GB.js +214 -0
  47. package/dist/server/assets/ProfileAnalysisStream-L7bs6IHH.js +198 -0
  48. package/dist/server/assets/ProfilePreview-Cg-AOHBE.js +751 -0
  49. package/dist/server/assets/SavedTimelineView-BtCq1dHk.js +64 -0
  50. package/dist/server/assets/SmartTimestamp-DfwE1NGf.js +45 -0
  51. package/dist/server/assets/SyncNowButton-BOn_IBXS.js +76 -0
  52. package/dist/server/assets/TimelineRouteFrame-CjMIh63G.js +85 -0
  53. package/dist/server/assets/TweetRichText-DDywESRF.js +74 -0
  54. package/dist/server/assets/_tanstack-start-manifest_v-aLWUdbAc.js +148 -0
  55. package/dist/server/assets/account-selection-CTdFGWRk.js +44 -0
  56. package/dist/server/assets/blocks-C1mgGTB3.js +299 -0
  57. package/dist/server/assets/blocks-DSgQghqP.js +17 -0
  58. package/dist/server/assets/bookmarks-D_YkmCK3.js +14 -0
  59. package/dist/server/assets/client-stream-contracts-Bq6kMmhh.js +769 -0
  60. package/dist/server/assets/data-sources-BnA1Q94p.js +180 -0
  61. package/dist/server/assets/discuss-C1QMBBSk.js +338 -0
  62. package/dist/server/assets/discuss-DAq6ehL9.js +15 -0
  63. package/dist/server/assets/dms-BuLj4bp8.js +368 -0
  64. package/dist/server/assets/dms-DAyQrVM2.js +240 -0
  65. package/dist/server/assets/empty-plugin-adapters-DzrEkdBF.js +5 -0
  66. package/dist/server/assets/inbox-CkGOaW1q.js +214 -0
  67. package/dist/server/assets/inbox-DpWd1EzC.js +146 -0
  68. package/dist/server/assets/likes-BMm2JWYI.js +14 -0
  69. package/dist/server/assets/links-Da4tjWtI.js +468 -0
  70. package/dist/server/assets/links-dqXb35YM.js +240 -0
  71. package/dist/server/assets/mentions-CEyEcOFw.js +26 -0
  72. package/dist/server/assets/network-map-D3CzRcC4.js +224 -0
  73. package/dist/server/assets/network-map-D5DoLOwR.js +582 -0
  74. package/dist/server/assets/profile-analyze-CGCj4VFd.js +90 -0
  75. package/dist/server/assets/profile-analyze-DH1TSBkT.js +9 -0
  76. package/dist/server/assets/profile-hydration-client-GJ4kwJS8.js +133 -0
  77. package/dist/server/assets/profiles._handle-B5V-huoo.js +12 -0
  78. package/dist/server/assets/profiles._handle-CJ6rgzt8.js +184 -0
  79. package/dist/server/assets/query-client-BdauHBgd.js +923 -0
  80. package/dist/server/assets/rate-limits-VAr2qzYD.js +262 -0
  81. package/dist/server/assets/route-search-BUlQ5lGM.js +117 -0
  82. package/dist/server/assets/router-C4cz9ypd.js +17026 -0
  83. package/dist/server/assets/routes-B9aikOdB.js +26 -0
  84. package/dist/server/assets/start-FaDOTmto.js +4 -0
  85. package/dist/server/assets/today-B_TA16QG.js +303 -0
  86. package/dist/server/assets/today-JkxZXdBt.js +17 -0
  87. package/dist/server/assets/ui-qbmcMSSX.js +137 -0
  88. package/{src/components/useDebouncedValue.ts → dist/server/assets/useDebouncedValue-DOnA6Xcy.js} +4 -4
  89. package/dist/server/assets/useTimelineRouteData-BLXvMGb2.js +1101 -0
  90. package/dist/server/server.js +5520 -0
  91. package/package.json +26 -34
  92. package/playwright.config.ts +0 -35
  93. package/scripts/browser-perf.mjs +0 -427
  94. package/scripts/build-docs-site.mjs +0 -967
  95. package/scripts/docs-site-assets.mjs +0 -311
  96. package/scripts/run-vitest.mjs +0 -21
  97. package/scripts/sanitize-node-options.mjs +0 -23
  98. package/scripts/start-test-server.mjs +0 -42
  99. package/src/cli/command-context.ts +0 -17
  100. package/src/cli/register-analysis.ts +0 -500
  101. package/src/cli/register-compose.ts +0 -40
  102. package/src/cli/register-graph.ts +0 -132
  103. package/src/cli/register-inbox.ts +0 -41
  104. package/src/cli/register-storage.ts +0 -106
  105. package/src/cli-moderation.ts +0 -210
  106. package/src/cli.ts +0 -1616
  107. package/src/components/AccountSwitcher.tsx +0 -178
  108. package/src/components/AppNav.tsx +0 -118
  109. package/src/components/AvatarChip.tsx +0 -58
  110. package/src/components/AvatarPreload.ts +0 -149
  111. package/src/components/BrandMark.tsx +0 -67
  112. package/src/components/ConversationThread.tsx +0 -131
  113. package/src/components/DmWorkspace.tsx +0 -286
  114. package/src/components/EmbeddedTweetCard.tsx +0 -55
  115. package/src/components/FeedState.tsx +0 -147
  116. package/src/components/FloatingPreview.tsx +0 -382
  117. package/src/components/InboxCard.tsx +0 -152
  118. package/src/components/LinkPreviewCard.tsx +0 -292
  119. package/src/components/MarkdownCitations.tsx +0 -680
  120. package/src/components/MarkdownViewer.tsx +0 -111
  121. package/src/components/ProfileAnalysisClient.ts +0 -191
  122. package/src/components/ProfileAnalysisStream.tsx +0 -259
  123. package/src/components/ProfilePreview.tsx +0 -131
  124. package/src/components/SavedTimelineView.tsx +0 -155
  125. package/src/components/SmartTimestamp.tsx +0 -72
  126. package/src/components/SyncNowButton.tsx +0 -140
  127. package/src/components/ThemeSlider.tsx +0 -80
  128. package/src/components/TimelineCard.tsx +0 -533
  129. package/src/components/TimelineRouteFrame.tsx +0 -186
  130. package/src/components/TweetArticleCard.tsx +0 -66
  131. package/src/components/TweetMediaGrid.tsx +0 -185
  132. package/src/components/TweetRichText.tsx +0 -159
  133. package/src/components/account-selection.ts +0 -64
  134. package/src/components/links-controller.ts +0 -162
  135. package/src/components/links-model.ts +0 -198
  136. package/src/components/network-map-controller.ts +0 -84
  137. package/src/components/network-map-model.ts +0 -255
  138. package/src/components/useTimelineRouteData.ts +0 -193
  139. package/src/lib/account-sync-job.ts +0 -666
  140. package/src/lib/actions-transport.ts +0 -300
  141. package/src/lib/analysis-runtime.ts +0 -238
  142. package/src/lib/api-client.ts +0 -220
  143. package/src/lib/api-contracts.ts +0 -328
  144. package/src/lib/archive-finder.ts +0 -185
  145. package/src/lib/archive-import-plan.ts +0 -102
  146. package/src/lib/archive-import.ts +0 -2695
  147. package/src/lib/authored-live.ts +0 -1027
  148. package/src/lib/avatar-cache.ts +0 -349
  149. package/src/lib/backup.ts +0 -2652
  150. package/src/lib/bird-actions.ts +0 -294
  151. package/src/lib/bird-command.ts +0 -130
  152. package/src/lib/bird.ts +0 -1140
  153. package/src/lib/blocklist.ts +0 -136
  154. package/src/lib/blocks-write.ts +0 -171
  155. package/src/lib/blocks.ts +0 -387
  156. package/src/lib/bookmark-sync-job.ts +0 -536
  157. package/src/lib/config.ts +0 -224
  158. package/src/lib/conversation-surface.ts +0 -205
  159. package/src/lib/data-sources.ts +0 -219
  160. package/src/lib/database-metrics.ts +0 -88
  161. package/src/lib/database-migrations.ts +0 -34
  162. package/src/lib/database-schema.ts +0 -312
  163. package/src/lib/database-writer.ts +0 -69
  164. package/src/lib/db.ts +0 -889
  165. package/src/lib/dm-read-model.ts +0 -533
  166. package/src/lib/dms-live.ts +0 -870
  167. package/src/lib/effect-runtime.ts +0 -45
  168. package/src/lib/follow-graph.ts +0 -1087
  169. package/src/lib/geocoding.ts +0 -296
  170. package/src/lib/http-effect.ts +0 -222
  171. package/src/lib/identity-search-index.ts +0 -369
  172. package/src/lib/import-repository.ts +0 -138
  173. package/src/lib/inbox.ts +0 -242
  174. package/src/lib/link-index.ts +0 -641
  175. package/src/lib/link-insights.ts +0 -860
  176. package/src/lib/link-preview-metadata.ts +0 -754
  177. package/src/lib/live-sync-engine.ts +0 -209
  178. package/src/lib/live-transport-gateway.ts +0 -128
  179. package/src/lib/location.ts +0 -137
  180. package/src/lib/media-fetch.ts +0 -860
  181. package/src/lib/media-includes.ts +0 -165
  182. package/src/lib/mention-threads-live.ts +0 -830
  183. package/src/lib/mentions-export.ts +0 -147
  184. package/src/lib/mentions-live.ts +0 -1114
  185. package/src/lib/moderation-target.ts +0 -227
  186. package/src/lib/moderation-write.ts +0 -131
  187. package/src/lib/mutes-write.ts +0 -171
  188. package/src/lib/mutes.ts +0 -84
  189. package/src/lib/ndjson-stream.ts +0 -106
  190. package/src/lib/network-map.ts +0 -382
  191. package/src/lib/openai-response-runtime.ts +0 -251
  192. package/src/lib/openai.ts +0 -117
  193. package/src/lib/paginated-sync.ts +0 -93
  194. package/src/lib/period-digest.ts +0 -1364
  195. package/src/lib/present.ts +0 -118
  196. package/src/lib/profile-affiliation-hydration.ts +0 -228
  197. package/src/lib/profile-affiliations.ts +0 -262
  198. package/src/lib/profile-analysis.ts +0 -1198
  199. package/src/lib/profile-bio-entities.ts +0 -318
  200. package/src/lib/profile-history.ts +0 -164
  201. package/src/lib/profile-hydration.ts +0 -180
  202. package/src/lib/profile-replies.ts +0 -77
  203. package/src/lib/profile-resolver.ts +0 -650
  204. package/src/lib/queries.ts +0 -6
  205. package/src/lib/query-actions.ts +0 -437
  206. package/src/lib/query-client.tsx +0 -47
  207. package/src/lib/query-read-model-shared.ts +0 -52
  208. package/src/lib/query-read-models.ts +0 -5
  209. package/src/lib/query-resource.ts +0 -41
  210. package/src/lib/query-status.ts +0 -164
  211. package/src/lib/research.ts +0 -652
  212. package/src/lib/runtime-services.ts +0 -20
  213. package/src/lib/search-discussion.ts +0 -812
  214. package/src/lib/seed.ts +0 -613
  215. package/src/lib/server-runtime-services.ts +0 -30
  216. package/src/lib/sqlite.ts +0 -183
  217. package/src/lib/streaming-ingestion.ts +0 -240
  218. package/src/lib/sync-cache.ts +0 -70
  219. package/src/lib/sync-plan.ts +0 -175
  220. package/src/lib/theme-transition.ts +0 -113
  221. package/src/lib/theme.tsx +0 -165
  222. package/src/lib/timeline-collections-live.ts +0 -407
  223. package/src/lib/timeline-live.ts +0 -302
  224. package/src/lib/timeline-read-model.ts +0 -1191
  225. package/src/lib/tweet-account-edges.ts +0 -48
  226. package/src/lib/tweet-lookup.ts +0 -46
  227. package/src/lib/tweet-render.ts +0 -430
  228. package/src/lib/tweet-repository.ts +0 -156
  229. package/src/lib/tweet-search-live.ts +0 -462
  230. package/src/lib/types.ts +0 -723
  231. package/src/lib/ui.ts +0 -529
  232. package/src/lib/url-expansion-store.ts +0 -110
  233. package/src/lib/url-expansion.ts +0 -343
  234. package/src/lib/url-safety.ts +0 -220
  235. package/src/lib/web-sync.ts +0 -533
  236. package/src/lib/whois.ts +0 -1080
  237. package/src/lib/x-profile.ts +0 -365
  238. package/src/lib/x-web.ts +0 -199
  239. package/src/lib/xurl-rate-limits.ts +0 -267
  240. package/src/lib/xurl.ts +0 -1677
  241. package/src/routeTree.gen.ts +0 -723
  242. package/src/router.tsx +0 -20
  243. package/src/routes/__root.tsx +0 -82
  244. package/src/routes/api/action.tsx +0 -147
  245. package/src/routes/api/avatar.tsx +0 -50
  246. package/src/routes/api/blocks.tsx +0 -35
  247. package/src/routes/api/conversation.tsx +0 -45
  248. package/src/routes/api/data-sources.tsx +0 -24
  249. package/src/routes/api/inbox.tsx +0 -43
  250. package/src/routes/api/link-insights.tsx +0 -85
  251. package/src/routes/api/link-preview.tsx +0 -47
  252. package/src/routes/api/network-map.tsx +0 -55
  253. package/src/routes/api/period-digest.tsx +0 -101
  254. package/src/routes/api/profile-analysis.tsx +0 -97
  255. package/src/routes/api/profile-hydrate.tsx +0 -54
  256. package/src/routes/api/query.tsx +0 -121
  257. package/src/routes/api/search-discussion.tsx +0 -113
  258. package/src/routes/api/status.tsx +0 -30
  259. package/src/routes/api/sync.tsx +0 -108
  260. package/src/routes/api/xurl-rate-limits.tsx +0 -24
  261. package/src/routes/blocks.tsx +0 -343
  262. package/src/routes/bookmarks.tsx +0 -18
  263. package/src/routes/data-sources.tsx +0 -249
  264. package/src/routes/discuss.tsx +0 -419
  265. package/src/routes/dms.tsx +0 -415
  266. package/src/routes/inbox.tsx +0 -232
  267. package/src/routes/index.tsx +0 -32
  268. package/src/routes/likes.tsx +0 -18
  269. package/src/routes/links.tsx +0 -631
  270. package/src/routes/mentions.tsx +0 -32
  271. package/src/routes/network-map.tsx +0 -732
  272. package/src/routes/profile-analyze.tsx +0 -112
  273. package/src/routes/profiles.$handle.tsx +0 -228
  274. package/src/routes/rate-limits.tsx +0 -305
  275. package/src/routes/today.tsx +0 -511
  276. package/src/styles.css +0 -257
  277. package/tsconfig.json +0 -29
  278. package/vite.config.ts +0 -29
  279. /package/{public → dist/client}/birdclaw-mark.png +0 -0
  280. /package/{public → dist/client}/favicon.ico +0 -0
  281. /package/{public → dist/client}/logo192.png +0 -0
  282. /package/{public → dist/client}/logo512.png +0 -0
  283. /package/{public → dist/client}/manifest.json +0 -0
  284. /package/{public → dist/client}/robots.txt +0 -0
@@ -1,1114 +0,0 @@
1
- import type { Database } from "./sqlite";
2
- import { Effect } from "effect";
3
- import type { MentionsDataSource } from "./config";
4
- import { databaseWriteEffect } from "./database-writer";
5
- import { getNativeDb } from "./db";
6
- import { runEffectPromise } from "./effect-runtime";
7
- import { liveTransportGateway } from "./live-transport-gateway";
8
- import {
9
- assertLiveAccountMatches,
10
- resolveLiveSyncAccount,
11
- type LiveSyncAccount,
12
- } from "./live-sync-engine";
13
- import { serializeMentionItemsAsXurlCompatible } from "./mentions-export";
14
- import { listTimelineItems } from "./timeline-read-model";
15
- import { deleteSyncCache, readSyncCache, writeSyncCache } from "./sync-cache";
16
- import type {
17
- ReplyFilter,
18
- XurlMentionData,
19
- XurlMentionsResponse,
20
- XurlMediaItem,
21
- XurlMentionUser,
22
- } from "./types";
23
- import { ingestTweetPayload } from "./tweet-repository";
24
-
25
- export const DEFAULT_MENTIONS_CACHE_TTL_MS = 2 * 60_000;
26
- const MIN_XURL_MENTIONS_LIMIT = 5;
27
- const MAX_XURL_MENTIONS_LIMIT = 100;
28
- type MentionSyncMode = Exclude<MentionsDataSource, "birdclaw">;
29
- type MentionLiveSource = Exclude<MentionSyncMode, "auto">;
30
- export interface MentionsProgress {
31
- source: "bird" | "xurl" | "cache";
32
- fetched: number;
33
- total?: number;
34
- page?: number;
35
- maxPages?: number;
36
- pageSize?: number;
37
- done: boolean;
38
- }
39
- export interface SyncMentionsOptions {
40
- account?: string;
41
- mode?: string;
42
- limit?: number;
43
- maxPages?: number;
44
- refresh?: boolean;
45
- cacheTtlMs?: number;
46
- sinceId?: string;
47
- startTime?: string;
48
- onProgress?: (progress: MentionsProgress) => void;
49
- }
50
- interface ExportMentionsViaCachedLiveSourceOptions {
51
- mode: MentionSyncMode;
52
- account?: string;
53
- search?: string;
54
- replyFilter?: ReplyFilter;
55
- limit?: number;
56
- all?: boolean;
57
- maxPages?: number;
58
- refresh?: boolean;
59
- cacheTtlMs?: number;
60
- }
61
- type MentionScanBoundary =
62
- | { kind: "auto" }
63
- | { kind: "since"; sinceId: string }
64
- | { kind: "start"; startTime: string }
65
- | { kind: "unbounded" };
66
- interface MentionScanShape {
67
- endpoint: "mentions";
68
- mode: MentionLiveSource;
69
- accountId: string;
70
- pageSize: number;
71
- boundary: MentionScanBoundary;
72
- }
73
- interface MentionCursorValue extends XurlMentionsResponse {
74
- birdclaw?: {
75
- boundary?: MentionScanBoundary;
76
- pendingNewestId?: string | null;
77
- };
78
- }
79
- interface MentionHighWaterValue {
80
- sinceId: string;
81
- }
82
-
83
- function toError(error: unknown) {
84
- return error instanceof Error ? error : new Error(String(error));
85
- }
86
-
87
- function trySync<T>(try_: () => T) {
88
- return Effect.try({
89
- try: try_,
90
- catch: toError,
91
- });
92
- }
93
-
94
- function getMentionsFetchModeKey({
95
- scope,
96
- mode,
97
- accountId,
98
- pageSize,
99
- all,
100
- maxPages,
101
- sinceId,
102
- startTime,
103
- }: {
104
- scope: "sync" | "export";
105
- mode: MentionsDataSource;
106
- accountId: string;
107
- pageSize: number;
108
- all: boolean;
109
- maxPages: number | null;
110
- sinceId: string | null;
111
- startTime: string | null;
112
- }) {
113
- return `mentions:${scope}:${mode}:${accountId}:${String(pageSize)}:${all ? "all" : "single"}:${maxPages === null ? "all-pages" : String(maxPages)}:${sinceId ?? "no-since"}:${startTime ?? "no-start"}`;
114
- }
115
-
116
- function getLegacyMentionsFetchModeKeyWithoutStart({
117
- scope,
118
- mode,
119
- accountId,
120
- pageSize,
121
- all,
122
- maxPages,
123
- sinceId,
124
- }: {
125
- scope: "sync" | "export";
126
- mode: MentionsDataSource;
127
- accountId: string;
128
- pageSize: number;
129
- all: boolean;
130
- maxPages: number | null;
131
- sinceId: string | null;
132
- }) {
133
- return `mentions:${scope}:${mode}:${accountId}:${String(pageSize)}:${all ? "all" : "single"}:${maxPages === null ? "all-pages" : String(maxPages)}:${sinceId ?? "no-since"}`;
134
- }
135
-
136
- function encodeCacheKeyPart(value: string) {
137
- return encodeURIComponent(value);
138
- }
139
-
140
- function getMentionScanBoundaryKey(boundary: MentionScanBoundary) {
141
- switch (boundary.kind) {
142
- case "auto":
143
- return "auto";
144
- case "since":
145
- return `since=${encodeCacheKeyPart(boundary.sinceId)}`;
146
- case "start":
147
- return `start=${encodeCacheKeyPart(boundary.startTime)}`;
148
- case "unbounded":
149
- return "unbounded";
150
- }
151
- }
152
-
153
- function getMentionScanShapeKey(shape: MentionScanShape) {
154
- return [
155
- `endpoint=${shape.endpoint}`,
156
- `mode=${shape.mode}`,
157
- `account=${encodeCacheKeyPart(shape.accountId)}`,
158
- `page=${String(shape.pageSize)}`,
159
- `boundary=${getMentionScanBoundaryKey(shape.boundary)}`,
160
- ].join(":");
161
- }
162
-
163
- function getMentionCursorKey(shape: MentionScanShape) {
164
- return `mentions:sync:cursor:v2:${getMentionScanShapeKey(shape)}`;
165
- }
166
-
167
- function getMentionResultCacheKey({
168
- shape,
169
- all,
170
- maxPages,
171
- }: {
172
- shape: MentionScanShape;
173
- all: boolean;
174
- maxPages: number | null;
175
- }) {
176
- return `mentions:sync:result:v2:${getMentionScanShapeKey(shape)}:${all ? "all" : "single"}:${maxPages === null ? "all-pages" : String(maxPages)}`;
177
- }
178
-
179
- function getMentionHighWaterKey({
180
- mode,
181
- accountId,
182
- }: {
183
- mode: MentionLiveSource;
184
- accountId: string;
185
- }) {
186
- return `mentions:sync:high-water:v1:mode=${mode}:account=${encodeCacheKeyPart(accountId)}`;
187
- }
188
-
189
- function getMentionCursorBoundary({
190
- explicitSinceId,
191
- explicitStartTime,
192
- }: {
193
- explicitSinceId?: string;
194
- explicitStartTime?: string;
195
- }): MentionScanBoundary {
196
- if (explicitSinceId) {
197
- return { kind: "since", sinceId: explicitSinceId };
198
- }
199
- if (explicitStartTime) {
200
- return { kind: "start", startTime: explicitStartTime };
201
- }
202
- return { kind: "auto" };
203
- }
204
-
205
- function getMentionRequestBoundary({
206
- sinceId,
207
- startTime,
208
- }: {
209
- sinceId?: string;
210
- startTime?: string;
211
- }): MentionScanBoundary {
212
- if (sinceId) {
213
- return { kind: "since", sinceId };
214
- }
215
- if (startTime) {
216
- return { kind: "start", startTime };
217
- }
218
- return { kind: "unbounded" };
219
- }
220
-
221
- function getLegacyMentionCursorKeys(shape: MentionScanShape) {
222
- const sinceId =
223
- shape.boundary.kind === "since" ? shape.boundary.sinceId : null;
224
- const startTime =
225
- shape.boundary.kind === "start" ? shape.boundary.startTime : null;
226
- const keys = [
227
- getMentionsFetchModeKey({
228
- scope: "sync",
229
- mode: shape.mode,
230
- accountId: shape.accountId,
231
- pageSize: shape.pageSize,
232
- all: false,
233
- maxPages: null,
234
- sinceId,
235
- startTime,
236
- }),
237
- ];
238
- if (!startTime) {
239
- keys.push(
240
- getLegacyMentionsFetchModeKeyWithoutStart({
241
- scope: "sync",
242
- mode: shape.mode,
243
- accountId: shape.accountId,
244
- pageSize: shape.pageSize,
245
- all: false,
246
- maxPages: null,
247
- sinceId,
248
- }),
249
- );
250
- }
251
- return [...new Set(keys)];
252
- }
253
-
254
- function parseCacheTtlMs(value?: number) {
255
- if (typeof value !== "number" || !Number.isFinite(value) || value < 0) {
256
- return DEFAULT_MENTIONS_CACHE_TTL_MS;
257
- }
258
- return Math.floor(value);
259
- }
260
-
261
- function assertXurlLimit(limit: number) {
262
- if (limit < MIN_XURL_MENTIONS_LIMIT || limit > MAX_XURL_MENTIONS_LIMIT) {
263
- throw new Error("xurl mode requires --limit between 5 and 100");
264
- }
265
- }
266
-
267
- function assertBirdLimit(limit: number) {
268
- if (!Number.isFinite(limit) || limit < 1) {
269
- throw new Error("bird mode requires --limit of at least 1");
270
- }
271
- }
272
-
273
- function parseMaxPages(value?: number) {
274
- if (value === undefined) {
275
- return null;
276
- }
277
- if (!Number.isFinite(value) || value < 1) {
278
- throw new Error("--max-pages must be at least 1");
279
- }
280
- return Math.floor(value);
281
- }
282
-
283
- function parseSyncMode(value?: string): MentionSyncMode {
284
- const mode = value ?? "auto";
285
- if (mode !== "auto" && mode !== "bird" && mode !== "xurl") {
286
- throw new Error("--mode must be auto, bird, or xurl");
287
- }
288
- return mode;
289
- }
290
-
291
- function getCachedPaginationToken(
292
- cached?: { value: XurlMentionsResponse } | null,
293
- ) {
294
- return typeof cached?.value.meta?.next_token === "string" &&
295
- cached.value.meta.next_token.length > 0
296
- ? cached.value.meta.next_token
297
- : undefined;
298
- }
299
-
300
- function parseCachedMentionBoundary(
301
- value: MentionCursorValue | XurlMentionsResponse,
302
- fallbackBoundary?: MentionScanBoundary,
303
- ) {
304
- const boundary = (value as MentionCursorValue).birdclaw?.boundary;
305
- if (!boundary || typeof boundary !== "object") {
306
- return fallbackBoundary;
307
- }
308
- if (boundary.kind === "unbounded" || boundary.kind === "auto") {
309
- return boundary;
310
- }
311
- if (boundary.kind === "since" && typeof boundary.sinceId === "string") {
312
- return boundary;
313
- }
314
- if (boundary.kind === "start" && typeof boundary.startTime === "string") {
315
- return boundary;
316
- }
317
- return fallbackBoundary;
318
- }
319
-
320
- function getCachedMentionPendingNewestId(
321
- value: MentionCursorValue | XurlMentionsResponse | undefined,
322
- ) {
323
- const pendingNewestId = (value as MentionCursorValue | undefined)?.birdclaw
324
- ?.pendingNewestId;
325
- return isNumericTweetId(pendingNewestId) ? pendingNewestId : undefined;
326
- }
327
-
328
- function addMentionCursorState(
329
- payload: XurlMentionsResponse,
330
- boundary: MentionScanBoundary,
331
- pendingNewestId: string | undefined,
332
- ): MentionCursorValue {
333
- return {
334
- ...payload,
335
- birdclaw: { boundary, pendingNewestId: pendingNewestId ?? null },
336
- };
337
- }
338
-
339
- function readMentionCursor({
340
- db,
341
- shape,
342
- cursorKey,
343
- legacyCursorKeys,
344
- }: {
345
- db: Database;
346
- shape: MentionScanShape;
347
- cursorKey: string;
348
- legacyCursorKeys: string[];
349
- }) {
350
- const fallbackBoundary =
351
- shape.boundary.kind === "auto" ? undefined : shape.boundary;
352
- const current = readSyncCache<MentionCursorValue>(cursorKey, db);
353
- const currentToken = getCachedPaginationToken(current);
354
- if (current && currentToken) {
355
- return {
356
- key: cursorKey,
357
- token: currentToken,
358
- boundary: parseCachedMentionBoundary(current.value, fallbackBoundary),
359
- pendingNewestId: getCachedMentionPendingNewestId(current.value),
360
- legacyKeys: [] as string[],
361
- };
362
- }
363
-
364
- for (const legacyKey of legacyCursorKeys) {
365
- const legacy = readSyncCache<MentionCursorValue>(legacyKey, db);
366
- const legacyToken = getCachedPaginationToken(legacy);
367
- if (legacy && legacyToken) {
368
- return {
369
- key: legacyKey,
370
- token: legacyToken,
371
- boundary: parseCachedMentionBoundary(legacy.value, fallbackBoundary),
372
- pendingNewestId: getCachedMentionPendingNewestId(legacy.value),
373
- legacyKeys: [legacyKey],
374
- };
375
- }
376
- }
377
-
378
- return undefined;
379
- }
380
-
381
- function isNumericTweetId(value: string | undefined | null): value is string {
382
- return typeof value === "string" && /^[0-9]+$/.test(value);
383
- }
384
-
385
- function maxNumericTweetId(...ids: Array<string | undefined | null>) {
386
- return ids.filter(isNumericTweetId).reduce<string | undefined>((max, id) => {
387
- if (!max) {
388
- return id;
389
- }
390
- if (id.length !== max.length) {
391
- return id.length > max.length ? id : max;
392
- }
393
- return id > max ? id : max;
394
- }, undefined);
395
- }
396
-
397
- function getNewestMentionId(payload: XurlMentionsResponse) {
398
- return maxNumericTweetId(
399
- typeof payload.meta?.newest_id === "string"
400
- ? payload.meta.newest_id
401
- : undefined,
402
- ...payload.data.map((tweet) => tweet.id),
403
- );
404
- }
405
-
406
- function readMentionHighWaterId(
407
- db: Database,
408
- mode: MentionLiveSource,
409
- accountId: string,
410
- ) {
411
- const cached = readSyncCache<MentionHighWaterValue>(
412
- getMentionHighWaterKey({ mode, accountId }),
413
- db,
414
- );
415
- return isNumericTweetId(cached?.value.sinceId)
416
- ? cached.value.sinceId
417
- : undefined;
418
- }
419
-
420
- function writeMentionHighWaterId(
421
- db: Database,
422
- mode: MentionLiveSource,
423
- accountId: string,
424
- sinceId: string | undefined,
425
- ) {
426
- if (!isNumericTweetId(sinceId)) {
427
- return;
428
- }
429
- writeSyncCache(getMentionHighWaterKey({ mode, accountId }), { sinceId }, db);
430
- }
431
-
432
- function verifyBirdAccountMatchesEffect(account: LiveSyncAccount) {
433
- return Effect.gen(function* () {
434
- const authenticated =
435
- yield* liveTransportGateway.bird.getAuthenticatedAccount();
436
- return yield* Effect.try({
437
- try: () =>
438
- assertLiveAccountMatches({
439
- source: "bird",
440
- account,
441
- liveUsername: authenticated.username,
442
- liveExternalUserId: authenticated.id,
443
- }),
444
- catch: (error) =>
445
- error instanceof Error ? error : new Error(String(error)),
446
- });
447
- });
448
- }
449
-
450
- function findNewestArchiveMentionId(db: Database, accountId: string) {
451
- const row = db
452
- .prepare(
453
- `
454
- select t.id
455
- from tweets t
456
- join tweet_account_edges e
457
- on e.tweet_id = t.id
458
- where e.account_id = ?
459
- and e.kind = 'mention'
460
- and e.source in ('archive', 'legacy')
461
- and length(t.id) > 0
462
- and t.id glob '[0-9]*'
463
- and t.id not glob '*[^0-9]*'
464
- order by length(t.id) desc, t.id desc
465
- limit 1
466
- `,
467
- )
468
- .get(accountId) as { id: string } | undefined;
469
- return row?.id;
470
- }
471
-
472
- function mergeMentionsIntoLocalStore(
473
- db: Database,
474
- accountId: string,
475
- payload: XurlMentionsResponse,
476
- source: MentionsDataSource,
477
- ) {
478
- ingestTweetPayload(db, {
479
- accountId,
480
- payload,
481
- kind: "mention",
482
- edgeKind: "mention",
483
- replaceSecondaryKind: true,
484
- source,
485
- });
486
- }
487
-
488
- function shouldReturnFilteredLocalPayload({
489
- search,
490
- replyFilter,
491
- }: {
492
- search?: string;
493
- replyFilter?: ReplyFilter;
494
- }) {
495
- return (
496
- Boolean(search?.trim()) ||
497
- replyFilter === "replied" ||
498
- replyFilter === "unreplied"
499
- );
500
- }
501
-
502
- function readLocalXurlCompatiblePayload({
503
- accountId,
504
- search,
505
- replyFilter,
506
- limit,
507
- }: {
508
- accountId?: string;
509
- search?: string;
510
- replyFilter?: ReplyFilter;
511
- limit: number;
512
- }) {
513
- return serializeMentionItemsAsXurlCompatible(
514
- listTimelineItems({
515
- resource: "mentions",
516
- account: accountId,
517
- search,
518
- replyFilter,
519
- limit,
520
- }),
521
- );
522
- }
523
-
524
- function mergeMentionPayloads(
525
- pages: XurlMentionsResponse[],
526
- ): XurlMentionsResponse {
527
- const tweets: XurlMentionData[] = [];
528
- const seenTweetIds = new Set<string>();
529
- const users: XurlMentionUser[] = [];
530
- const seenUserIds = new Set<string>();
531
- const media: XurlMediaItem[] = [];
532
- const seenMediaKeys = new Set<string>();
533
-
534
- for (const page of pages) {
535
- for (const tweet of page.data) {
536
- if (seenTweetIds.has(tweet.id)) {
537
- continue;
538
- }
539
- seenTweetIds.add(tweet.id);
540
- tweets.push(tweet);
541
- }
542
-
543
- for (const user of page.includes?.users ?? []) {
544
- if (seenUserIds.has(user.id)) {
545
- continue;
546
- }
547
- seenUserIds.add(user.id);
548
- users.push(user);
549
- }
550
-
551
- for (const item of page.includes?.media ?? []) {
552
- if (seenMediaKeys.has(item.media_key)) {
553
- continue;
554
- }
555
- seenMediaKeys.add(item.media_key);
556
- media.push(item);
557
- }
558
- }
559
-
560
- const lastMeta = pages.at(-1)?.meta;
561
- const includes = {
562
- ...(users.length > 0 ? { users } : {}),
563
- ...(media.length > 0 ? { media } : {}),
564
- };
565
- return {
566
- data: tweets,
567
- includes: users.length > 0 || media.length > 0 ? includes : undefined,
568
- meta: {
569
- ...lastMeta,
570
- result_count: tweets.length,
571
- page_count: pages.length,
572
- next_token:
573
- typeof lastMeta?.next_token === "string" ? lastMeta.next_token : null,
574
- },
575
- };
576
- }
577
-
578
- function fetchMentionsViaXurlEffect({
579
- resolvedAccount,
580
- limit,
581
- all,
582
- parsedMaxPages,
583
- sinceId,
584
- startPaginationToken,
585
- startTime,
586
- onProgress,
587
- }: {
588
- resolvedAccount: LiveSyncAccount;
589
- limit: number;
590
- all: boolean;
591
- parsedMaxPages: number | null;
592
- sinceId?: string;
593
- startPaginationToken?: string;
594
- startTime?: string;
595
- onProgress?: (progress: MentionsProgress) => void;
596
- }) {
597
- return Effect.gen(function* () {
598
- const accountUserId =
599
- resolvedAccount.externalUserId ??
600
- (yield* liveTransportGateway.xurl
601
- .lookupUsersByHandles([resolvedAccount.username])
602
- .pipe(Effect.map((users) => users[0]?.id)));
603
- if (!accountUserId) {
604
- return yield* Effect.fail(
605
- new Error(
606
- `Could not resolve Twitter user id for @${resolvedAccount.username}`,
607
- ),
608
- );
609
- }
610
-
611
- const pages: XurlMentionsResponse[] = [];
612
- let nextToken: string | undefined = startPaginationToken;
613
- let pageCount = 0;
614
- do {
615
- const payload = yield* liveTransportGateway.xurl.listMentions({
616
- maxResults: limit,
617
- username: resolvedAccount.username,
618
- userId: String(accountUserId),
619
- paginationToken: nextToken,
620
- ...(sinceId ? { sinceId } : {}),
621
- ...(startTime ? { startTime } : {}),
622
- });
623
- pages.push(payload);
624
- const metaNextToken =
625
- typeof payload.meta?.next_token === "string"
626
- ? payload.meta.next_token
627
- : undefined;
628
- nextToken = metaNextToken;
629
- pageCount += 1;
630
- const fetched = pages.reduce((sum, item) => sum + item.data.length, 0);
631
- const done =
632
- !all ||
633
- !nextToken ||
634
- (parsedMaxPages !== null && pageCount >= parsedMaxPages);
635
- yield* Effect.sync(() =>
636
- onProgress?.({
637
- source: "xurl",
638
- fetched,
639
- total: parsedMaxPages === null ? undefined : parsedMaxPages * limit,
640
- page: pageCount,
641
- maxPages: parsedMaxPages ?? undefined,
642
- pageSize: limit,
643
- done,
644
- }),
645
- );
646
- } while (
647
- all &&
648
- nextToken &&
649
- (parsedMaxPages === null || pageCount < parsedMaxPages)
650
- );
651
-
652
- return mergeMentionPayloads(pages);
653
- });
654
- }
655
-
656
- function fetchMentionsViaBirdEffect({ limit }: { limit: number }) {
657
- return liveTransportGateway.bird.listMentions({ maxResults: limit });
658
- }
659
-
660
- function isMaxPagesPartial({
661
- payload,
662
- maxPages,
663
- }: {
664
- payload: XurlMentionsResponse;
665
- maxPages: number | null;
666
- }) {
667
- return (
668
- maxPages !== null &&
669
- typeof payload.meta?.next_token === "string" &&
670
- payload.meta.next_token.length > 0
671
- );
672
- }
673
-
674
- export function syncMentionsEffect({
675
- account,
676
- mode,
677
- limit = 20,
678
- maxPages,
679
- refresh = false,
680
- cacheTtlMs,
681
- sinceId,
682
- startTime,
683
- onProgress,
684
- }: SyncMentionsOptions) {
685
- return Effect.gen(function* () {
686
- const parsedMode = yield* trySync(() => parseSyncMode(mode));
687
- const primaryMode: MentionLiveSource =
688
- parsedMode === "auto" ? "xurl" : parsedMode;
689
- const explicitSinceId = sinceId?.trim() || undefined;
690
- const explicitStartTime = startTime?.trim() || undefined;
691
- if (primaryMode === "bird" && (explicitSinceId || explicitStartTime)) {
692
- return yield* Effect.fail(
693
- new Error("bird mode does not support --since-id or --start-time"),
694
- );
695
- }
696
- if (primaryMode === "xurl") {
697
- yield* trySync(() => assertXurlLimit(limit));
698
- } else {
699
- yield* trySync(() => assertBirdLimit(limit));
700
- }
701
- const parsedMaxPages = yield* trySync(() => parseMaxPages(maxPages));
702
- const fetchAll =
703
- primaryMode === "xurl" &&
704
- (parsedMaxPages !== null ||
705
- Boolean(explicitSinceId || explicitStartTime));
706
- const db = yield* trySync(() => getNativeDb());
707
- const resolvedAccount = yield* trySync(() =>
708
- resolveLiveSyncAccount(db, account),
709
- );
710
- const cursorShape: MentionScanShape = {
711
- endpoint: "mentions",
712
- mode: primaryMode,
713
- accountId: resolvedAccount.accountId,
714
- pageSize: limit,
715
- boundary: getMentionCursorBoundary({
716
- explicitSinceId,
717
- explicitStartTime,
718
- }),
719
- };
720
- const cursorKey = getMentionCursorKey(cursorShape);
721
- const legacyCursorKeys = getLegacyMentionCursorKeys(cursorShape);
722
- const cursor =
723
- primaryMode === "xurl"
724
- ? yield* trySync(() =>
725
- readMentionCursor({
726
- db,
727
- shape: cursorShape,
728
- cursorKey,
729
- legacyCursorKeys,
730
- }),
731
- )
732
- : undefined;
733
- const startPaginationToken = cursor?.token;
734
- const cursorSinceId =
735
- cursor?.boundary?.kind === "since" ? cursor.boundary.sinceId : undefined;
736
- const cursorStartTime =
737
- cursor?.boundary?.kind === "start"
738
- ? cursor.boundary.startTime
739
- : undefined;
740
- const committedSinceId =
741
- primaryMode === "xurl" &&
742
- cursorShape.boundary.kind === "auto" &&
743
- !startPaginationToken
744
- ? yield* trySync(() =>
745
- readMentionHighWaterId(db, primaryMode, resolvedAccount.accountId),
746
- )
747
- : undefined;
748
- const seededSinceId =
749
- primaryMode === "xurl" &&
750
- !explicitSinceId &&
751
- !explicitStartTime &&
752
- !startPaginationToken
753
- ? (committedSinceId ??
754
- (yield* trySync(() =>
755
- findNewestArchiveMentionId(db, resolvedAccount.accountId),
756
- )))
757
- : undefined;
758
- const resolvedSinceId = startPaginationToken
759
- ? cursorSinceId
760
- : (explicitSinceId ?? seededSinceId);
761
- const resolvedStartTime = startPaginationToken
762
- ? cursorStartTime
763
- : !resolvedSinceId
764
- ? explicitStartTime
765
- : undefined;
766
- const resultShape: MentionScanShape = {
767
- endpoint: "mentions",
768
- mode: primaryMode,
769
- accountId: resolvedAccount.accountId,
770
- pageSize: limit,
771
- boundary: getMentionRequestBoundary({
772
- sinceId: resolvedSinceId,
773
- startTime: resolvedStartTime,
774
- }),
775
- };
776
- const resultCacheKey = getMentionResultCacheKey({
777
- shape: resultShape,
778
- all: fetchAll,
779
- maxPages: parsedMaxPages,
780
- });
781
- const ttlMs = parseCacheTtlMs(cacheTtlMs);
782
- const cached = startPaginationToken
783
- ? null
784
- : yield* trySync(() =>
785
- readSyncCache<XurlMentionsResponse>(resultCacheKey, db),
786
- );
787
- const cachedPaginationToken = getCachedPaginationToken(cached);
788
- const cacheAgeMs = cached
789
- ? Date.now() - new Date(cached.updatedAt).getTime()
790
- : Number.POSITIVE_INFINITY;
791
-
792
- if (
793
- !startPaginationToken &&
794
- !cachedPaginationToken &&
795
- !refresh &&
796
- cached &&
797
- cacheAgeMs <= ttlMs
798
- ) {
799
- yield* databaseWriteEffect((writeDb) =>
800
- mergeMentionsIntoLocalStore(
801
- writeDb,
802
- resolvedAccount.accountId,
803
- cached.value,
804
- primaryMode,
805
- ),
806
- );
807
- yield* Effect.sync(() =>
808
- onProgress?.({
809
- source: "cache",
810
- fetched: cached.value.data.length,
811
- total: parsedMaxPages === null ? undefined : parsedMaxPages * limit,
812
- done: true,
813
- }),
814
- );
815
- return {
816
- ok: true,
817
- source: "cache",
818
- kind: "mentions",
819
- accountId: resolvedAccount.accountId,
820
- count: cached.value.data.length,
821
- partial: isMaxPagesPartial({
822
- payload: cached.value,
823
- maxPages: parsedMaxPages,
824
- }),
825
- payload: cached.value,
826
- };
827
- }
828
-
829
- if (
830
- primaryMode === "xurl" &&
831
- !explicitSinceId &&
832
- !explicitStartTime &&
833
- !startPaginationToken &&
834
- !seededSinceId
835
- ) {
836
- console.error(
837
- "No local mention baseline found; syncing mentions from the newest page backwards.",
838
- );
839
- }
840
-
841
- let source: MentionLiveSource = primaryMode;
842
- const canFallbackToBird =
843
- primaryMode === "xurl" &&
844
- parsedMode === "auto" &&
845
- !explicitSinceId &&
846
- !explicitStartTime &&
847
- !startPaginationToken;
848
- const payload =
849
- primaryMode === "bird"
850
- ? yield* fetchMentionsViaBirdEffect({ limit })
851
- : yield* fetchMentionsViaXurlEffect({
852
- resolvedAccount,
853
- limit,
854
- all: fetchAll,
855
- parsedMaxPages,
856
- sinceId: resolvedSinceId,
857
- startPaginationToken,
858
- startTime: resolvedStartTime,
859
- onProgress,
860
- }).pipe(
861
- Effect.catchAll((error) => {
862
- if (!canFallbackToBird) return Effect.fail(error);
863
- source = "bird";
864
- return verifyBirdAccountMatchesEffect(resolvedAccount).pipe(
865
- Effect.flatMap(() => fetchMentionsViaBirdEffect({ limit })),
866
- );
867
- }),
868
- );
869
- if (source === "bird") {
870
- yield* Effect.sync(() =>
871
- onProgress?.({
872
- source: "bird",
873
- fetched: payload.data.length,
874
- total: limit,
875
- done: true,
876
- }),
877
- );
878
- }
879
- yield* databaseWriteEffect((writeDb) =>
880
- mergeMentionsIntoLocalStore(
881
- writeDb,
882
- resolvedAccount.accountId,
883
- payload,
884
- source,
885
- ),
886
- );
887
- const payloadPaginationToken = getCachedPaginationToken({ value: payload });
888
- if (source === "xurl") {
889
- if (payloadPaginationToken) {
890
- yield* trySync(() => {
891
- writeSyncCache(
892
- cursorKey,
893
- addMentionCursorState(
894
- payload,
895
- getMentionRequestBoundary({
896
- sinceId: resolvedSinceId,
897
- startTime: resolvedStartTime,
898
- }),
899
- maxNumericTweetId(resolvedSinceId, getNewestMentionId(payload)),
900
- ),
901
- db,
902
- );
903
- deleteSyncCache(resultCacheKey, db);
904
- deleteSyncCache(
905
- getMentionResultCacheKey({
906
- shape: cursorShape,
907
- all: fetchAll,
908
- maxPages: parsedMaxPages,
909
- }),
910
- db,
911
- );
912
- for (const legacyKey of cursor?.legacyKeys ?? []) {
913
- deleteSyncCache(legacyKey, db);
914
- }
915
- });
916
- } else {
917
- yield* trySync(() => {
918
- deleteSyncCache(cursorKey, db);
919
- for (const legacyKey of legacyCursorKeys) {
920
- deleteSyncCache(legacyKey, db);
921
- }
922
- if (cursorShape.boundary.kind === "auto") {
923
- writeMentionHighWaterId(
924
- db,
925
- source,
926
- resolvedAccount.accountId,
927
- maxNumericTweetId(
928
- resolvedSinceId,
929
- cursor?.pendingNewestId,
930
- getNewestMentionId(payload),
931
- ),
932
- );
933
- }
934
- });
935
- }
936
- }
937
- if (!payloadPaginationToken && !startPaginationToken) {
938
- const writeCacheKey =
939
- source === primaryMode
940
- ? resultCacheKey
941
- : getMentionResultCacheKey({
942
- shape: {
943
- ...resultShape,
944
- mode: source,
945
- },
946
- all: false,
947
- maxPages: null,
948
- });
949
- yield* trySync(() => writeSyncCache(writeCacheKey, payload, db));
950
- }
951
-
952
- return {
953
- ok: true,
954
- source,
955
- kind: "mentions",
956
- accountId: resolvedAccount.accountId,
957
- count: payload.data.length,
958
- partial: isMaxPagesPartial({ payload, maxPages: parsedMaxPages }),
959
- payload,
960
- };
961
- });
962
- }
963
-
964
- export function syncMentions(options: SyncMentionsOptions) {
965
- return runEffectPromise(syncMentionsEffect(options));
966
- }
967
-
968
- function exportMentionsViaCachedLiveSourceEffect({
969
- mode,
970
- account,
971
- search,
972
- replyFilter = "all",
973
- limit = 20,
974
- all = false,
975
- maxPages,
976
- refresh = false,
977
- cacheTtlMs,
978
- }: ExportMentionsViaCachedLiveSourceOptions) {
979
- return Effect.gen(function* () {
980
- const primaryMode: MentionLiveSource = mode === "auto" ? "xurl" : mode;
981
- if (primaryMode === "xurl") {
982
- yield* trySync(() => assertXurlLimit(limit));
983
- } else {
984
- yield* trySync(() => assertBirdLimit(limit));
985
- }
986
- const parsedMaxPages = yield* trySync(() => parseMaxPages(maxPages));
987
- const fetchAll = primaryMode === "xurl" && (all || parsedMaxPages !== null);
988
-
989
- const db = yield* trySync(() => getNativeDb());
990
- const resolvedAccount = yield* trySync(() =>
991
- resolveLiveSyncAccount(db, account),
992
- );
993
- const cacheKey = getMentionsFetchModeKey({
994
- scope: "export",
995
- mode,
996
- accountId: resolvedAccount.accountId,
997
- pageSize: limit,
998
- all: fetchAll,
999
- maxPages: parsedMaxPages,
1000
- sinceId: null,
1001
- startTime: null,
1002
- });
1003
- const ttlMs = parseCacheTtlMs(cacheTtlMs);
1004
- const cached = yield* trySync(() =>
1005
- readSyncCache<XurlMentionsResponse>(cacheKey, db),
1006
- );
1007
- const cacheAgeMs = cached
1008
- ? Date.now() - new Date(cached.updatedAt).getTime()
1009
- : Number.POSITIVE_INFINITY;
1010
- const readFilteredOrRaw = (payload: XurlMentionsResponse) => {
1011
- if (
1012
- shouldReturnFilteredLocalPayload({
1013
- search,
1014
- replyFilter,
1015
- })
1016
- ) {
1017
- return readLocalXurlCompatiblePayload({
1018
- accountId: resolvedAccount.accountId,
1019
- search,
1020
- replyFilter,
1021
- limit: fetchAll ? payload.data.length : limit,
1022
- });
1023
- }
1024
- return payload;
1025
- };
1026
-
1027
- if (!refresh && cached && cacheAgeMs <= ttlMs) {
1028
- return yield* trySync(() => readFilteredOrRaw(cached.value));
1029
- }
1030
-
1031
- let source: MentionLiveSource = primaryMode;
1032
- const liveResult = yield* (
1033
- primaryMode === "bird"
1034
- ? fetchMentionsViaBirdEffect({ limit })
1035
- : fetchMentionsViaXurlEffect({
1036
- resolvedAccount,
1037
- limit,
1038
- all: fetchAll,
1039
- parsedMaxPages,
1040
- })
1041
- ).pipe(
1042
- Effect.catchAll((error) => {
1043
- if (mode !== "auto" || fetchAll) return Effect.fail(error);
1044
- source = "bird";
1045
- return verifyBirdAccountMatchesEffect(resolvedAccount).pipe(
1046
- Effect.flatMap(() => fetchMentionsViaBirdEffect({ limit })),
1047
- );
1048
- }),
1049
- Effect.flatMap((payload) =>
1050
- databaseWriteEffect((writeDb) => {
1051
- mergeMentionsIntoLocalStore(
1052
- writeDb,
1053
- resolvedAccount.accountId,
1054
- payload,
1055
- source,
1056
- );
1057
- writeSyncCache(cacheKey, payload, writeDb);
1058
- return readFilteredOrRaw(payload);
1059
- }),
1060
- ),
1061
- Effect.map((payload) => ({ ok: true as const, payload })),
1062
- Effect.catchAll((error) => {
1063
- if (!refresh && cached) {
1064
- return Effect.succeed({ ok: false as const });
1065
- }
1066
- return Effect.fail(error);
1067
- }),
1068
- );
1069
-
1070
- if (!liveResult.ok) {
1071
- if (!cached) {
1072
- return yield* Effect.fail(
1073
- new Error("Mention export failed without cache"),
1074
- );
1075
- }
1076
- return yield* trySync(() => readFilteredOrRaw(cached.value));
1077
- }
1078
-
1079
- return liveResult.payload;
1080
- });
1081
- }
1082
-
1083
- export function exportMentionsViaCachedXurl(
1084
- options: Omit<ExportMentionsViaCachedLiveSourceOptions, "mode">,
1085
- ) {
1086
- return runEffectPromise(
1087
- exportMentionsViaCachedLiveSourceEffect({
1088
- ...options,
1089
- mode: "xurl",
1090
- }),
1091
- );
1092
- }
1093
-
1094
- export function exportMentionsViaCachedBird(
1095
- options: Omit<ExportMentionsViaCachedLiveSourceOptions, "mode">,
1096
- ) {
1097
- return runEffectPromise(
1098
- exportMentionsViaCachedLiveSourceEffect({
1099
- ...options,
1100
- mode: "bird",
1101
- }),
1102
- );
1103
- }
1104
-
1105
- export function exportMentionsViaCachedAuto(
1106
- options: Omit<ExportMentionsViaCachedLiveSourceOptions, "mode">,
1107
- ) {
1108
- return runEffectPromise(
1109
- exportMentionsViaCachedLiveSourceEffect({
1110
- ...options,
1111
- mode: "auto",
1112
- }),
1113
- );
1114
- }