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.
- package/CHANGELOG.md +24 -0
- package/README.md +10 -8
- package/bin/birdclaw.mjs +4 -66
- package/dist/cli/birdclaw.js +28164 -0
- package/dist/client/assets/AvatarChip-DLoqM5xD.js +1 -0
- package/dist/client/assets/ProfilePreview-DQX6_xc3.js +1 -0
- package/dist/client/assets/SavedTimelineView-BhSiVyR1.js +1 -0
- package/dist/client/assets/SmartTimestamp-BR0-9se2.js +1 -0
- package/dist/client/assets/TimelineRouteFrame-9NDLnXzH.js +1 -0
- package/dist/client/assets/blocks-CFp_njvG.js +1 -0
- package/dist/client/assets/bookmarks-LbYOsoKf.js +1 -0
- package/dist/client/assets/chunk-C0phanGV.js +1 -0
- package/dist/client/assets/circle-check-C_Vq3ElD.js +1 -0
- package/dist/client/assets/client-stream-contracts-sR4xMQxY.js +5 -0
- package/dist/client/assets/createLucideIcon-JV8isFFR.js +1 -0
- package/dist/client/assets/data-sources-euTr6T_L.js +1 -0
- package/dist/client/assets/discuss-BWKu_p7B.js +1 -0
- package/dist/client/assets/dms-SLXMtC4v.js +1 -0
- package/dist/client/assets/inbox-BybSI5Vf.js +1 -0
- package/dist/client/assets/index-DZVisyna.js +31 -0
- package/dist/client/assets/index-bpFrQ5yG.css +1 -0
- package/dist/client/assets/likes-dyh9p8y_.js +1 -0
- package/dist/client/assets/links-CIwqK_dX.js +1 -0
- package/dist/client/assets/mapbox-B4zqX7r0.js +2 -0
- package/dist/client/assets/mapbox-gl-DYNcLcll.js +3215 -0
- package/dist/client/assets/mentions-BoVVAIT7.js +1 -0
- package/dist/client/assets/network-map-zRysIY_b.js +2 -0
- package/dist/client/assets/profile-analyze-C_yDeX6-.js +1 -0
- package/dist/client/assets/profile-hydration-client-YEtJsNTv.js +1 -0
- package/dist/client/assets/profiles._handle-DdIVPqU-.js +1 -0
- package/dist/client/assets/query-client-fYu2rzRC.js +17 -0
- package/dist/client/assets/rate-limits-BWPmPOSh.js +1 -0
- package/dist/client/assets/react-dom-DHl6Ug7F.js +1 -0
- package/dist/client/assets/react-nBylq1U5.js +1 -0
- package/dist/client/assets/repeat-2-xCwHM9hD.js +1 -0
- package/dist/client/assets/routes-CiLuYpZr.js +1 -0
- package/dist/client/assets/schemas-I9r6F6ya.js +64 -0
- package/dist/client/assets/search-Dq5kYsxA.js +1 -0
- package/dist/client/assets/sparkles-CQVlvHsZ.js +1 -0
- package/dist/client/assets/styles-B8s3tt3r.css +2 -0
- package/dist/client/assets/today-BoO64YoY.js +1 -0
- package/dist/client/assets/triangle-alert-CBL7s2pf.js +1 -0
- package/dist/client/assets/useTimelineRouteData-BaO3Rna1.js +1 -0
- package/dist/client/assets/users-CHbKlqUr.js +1 -0
- package/dist/server/assets/AvatarChip-zUDAEmjL.js +105 -0
- package/dist/server/assets/FeedState-ntW0Q2GB.js +214 -0
- package/dist/server/assets/ProfileAnalysisStream-L7bs6IHH.js +198 -0
- package/dist/server/assets/ProfilePreview-Cg-AOHBE.js +751 -0
- package/dist/server/assets/SavedTimelineView-BtCq1dHk.js +64 -0
- package/dist/server/assets/SmartTimestamp-DfwE1NGf.js +45 -0
- package/dist/server/assets/SyncNowButton-BOn_IBXS.js +76 -0
- package/dist/server/assets/TimelineRouteFrame-CjMIh63G.js +85 -0
- package/dist/server/assets/TweetRichText-DDywESRF.js +74 -0
- package/dist/server/assets/_tanstack-start-manifest_v-aLWUdbAc.js +148 -0
- package/dist/server/assets/account-selection-CTdFGWRk.js +44 -0
- package/dist/server/assets/blocks-C1mgGTB3.js +299 -0
- package/dist/server/assets/blocks-DSgQghqP.js +17 -0
- package/dist/server/assets/bookmarks-D_YkmCK3.js +14 -0
- package/dist/server/assets/client-stream-contracts-Bq6kMmhh.js +769 -0
- package/dist/server/assets/data-sources-BnA1Q94p.js +180 -0
- package/dist/server/assets/discuss-C1QMBBSk.js +338 -0
- package/dist/server/assets/discuss-DAq6ehL9.js +15 -0
- package/dist/server/assets/dms-BuLj4bp8.js +368 -0
- package/dist/server/assets/dms-DAyQrVM2.js +240 -0
- package/dist/server/assets/empty-plugin-adapters-DzrEkdBF.js +5 -0
- package/dist/server/assets/inbox-CkGOaW1q.js +214 -0
- package/dist/server/assets/inbox-DpWd1EzC.js +146 -0
- package/dist/server/assets/likes-BMm2JWYI.js +14 -0
- package/dist/server/assets/links-Da4tjWtI.js +468 -0
- package/dist/server/assets/links-dqXb35YM.js +240 -0
- package/dist/server/assets/mentions-CEyEcOFw.js +26 -0
- package/dist/server/assets/network-map-D3CzRcC4.js +224 -0
- package/dist/server/assets/network-map-D5DoLOwR.js +582 -0
- package/dist/server/assets/profile-analyze-CGCj4VFd.js +90 -0
- package/dist/server/assets/profile-analyze-DH1TSBkT.js +9 -0
- package/dist/server/assets/profile-hydration-client-GJ4kwJS8.js +133 -0
- package/dist/server/assets/profiles._handle-B5V-huoo.js +12 -0
- package/dist/server/assets/profiles._handle-CJ6rgzt8.js +184 -0
- package/dist/server/assets/query-client-BdauHBgd.js +923 -0
- package/dist/server/assets/rate-limits-VAr2qzYD.js +262 -0
- package/dist/server/assets/route-search-BUlQ5lGM.js +117 -0
- package/dist/server/assets/router-C4cz9ypd.js +17026 -0
- package/dist/server/assets/routes-B9aikOdB.js +26 -0
- package/dist/server/assets/start-FaDOTmto.js +4 -0
- package/dist/server/assets/today-B_TA16QG.js +303 -0
- package/dist/server/assets/today-JkxZXdBt.js +17 -0
- package/dist/server/assets/ui-qbmcMSSX.js +137 -0
- package/{src/components/useDebouncedValue.ts → dist/server/assets/useDebouncedValue-DOnA6Xcy.js} +4 -4
- package/dist/server/assets/useTimelineRouteData-BLXvMGb2.js +1101 -0
- package/dist/server/server.js +5520 -0
- package/package.json +26 -34
- package/playwright.config.ts +0 -35
- package/scripts/browser-perf.mjs +0 -427
- package/scripts/build-docs-site.mjs +0 -967
- package/scripts/docs-site-assets.mjs +0 -311
- package/scripts/run-vitest.mjs +0 -21
- package/scripts/sanitize-node-options.mjs +0 -23
- package/scripts/start-test-server.mjs +0 -42
- package/src/cli/command-context.ts +0 -17
- package/src/cli/register-analysis.ts +0 -500
- package/src/cli/register-compose.ts +0 -40
- package/src/cli/register-graph.ts +0 -132
- package/src/cli/register-inbox.ts +0 -41
- package/src/cli/register-storage.ts +0 -106
- package/src/cli-moderation.ts +0 -210
- package/src/cli.ts +0 -1616
- package/src/components/AccountSwitcher.tsx +0 -178
- package/src/components/AppNav.tsx +0 -118
- package/src/components/AvatarChip.tsx +0 -58
- package/src/components/AvatarPreload.ts +0 -149
- package/src/components/BrandMark.tsx +0 -67
- package/src/components/ConversationThread.tsx +0 -131
- package/src/components/DmWorkspace.tsx +0 -286
- package/src/components/EmbeddedTweetCard.tsx +0 -55
- package/src/components/FeedState.tsx +0 -147
- package/src/components/FloatingPreview.tsx +0 -382
- package/src/components/InboxCard.tsx +0 -152
- package/src/components/LinkPreviewCard.tsx +0 -292
- package/src/components/MarkdownCitations.tsx +0 -680
- package/src/components/MarkdownViewer.tsx +0 -111
- package/src/components/ProfileAnalysisClient.ts +0 -191
- package/src/components/ProfileAnalysisStream.tsx +0 -259
- package/src/components/ProfilePreview.tsx +0 -131
- package/src/components/SavedTimelineView.tsx +0 -155
- package/src/components/SmartTimestamp.tsx +0 -72
- package/src/components/SyncNowButton.tsx +0 -140
- package/src/components/ThemeSlider.tsx +0 -80
- package/src/components/TimelineCard.tsx +0 -533
- package/src/components/TimelineRouteFrame.tsx +0 -186
- package/src/components/TweetArticleCard.tsx +0 -66
- package/src/components/TweetMediaGrid.tsx +0 -185
- package/src/components/TweetRichText.tsx +0 -159
- package/src/components/account-selection.ts +0 -64
- package/src/components/links-controller.ts +0 -162
- package/src/components/links-model.ts +0 -198
- package/src/components/network-map-controller.ts +0 -84
- package/src/components/network-map-model.ts +0 -255
- package/src/components/useTimelineRouteData.ts +0 -193
- package/src/lib/account-sync-job.ts +0 -666
- package/src/lib/actions-transport.ts +0 -300
- package/src/lib/analysis-runtime.ts +0 -238
- package/src/lib/api-client.ts +0 -220
- package/src/lib/api-contracts.ts +0 -328
- package/src/lib/archive-finder.ts +0 -185
- package/src/lib/archive-import-plan.ts +0 -102
- package/src/lib/archive-import.ts +0 -2695
- package/src/lib/authored-live.ts +0 -1027
- package/src/lib/avatar-cache.ts +0 -349
- package/src/lib/backup.ts +0 -2652
- package/src/lib/bird-actions.ts +0 -294
- package/src/lib/bird-command.ts +0 -130
- package/src/lib/bird.ts +0 -1140
- package/src/lib/blocklist.ts +0 -136
- package/src/lib/blocks-write.ts +0 -171
- package/src/lib/blocks.ts +0 -387
- package/src/lib/bookmark-sync-job.ts +0 -536
- package/src/lib/config.ts +0 -224
- package/src/lib/conversation-surface.ts +0 -205
- package/src/lib/data-sources.ts +0 -219
- package/src/lib/database-metrics.ts +0 -88
- package/src/lib/database-migrations.ts +0 -34
- package/src/lib/database-schema.ts +0 -312
- package/src/lib/database-writer.ts +0 -69
- package/src/lib/db.ts +0 -889
- package/src/lib/dm-read-model.ts +0 -533
- package/src/lib/dms-live.ts +0 -870
- package/src/lib/effect-runtime.ts +0 -45
- package/src/lib/follow-graph.ts +0 -1087
- package/src/lib/geocoding.ts +0 -296
- package/src/lib/http-effect.ts +0 -222
- package/src/lib/identity-search-index.ts +0 -369
- package/src/lib/import-repository.ts +0 -138
- package/src/lib/inbox.ts +0 -242
- package/src/lib/link-index.ts +0 -641
- package/src/lib/link-insights.ts +0 -860
- package/src/lib/link-preview-metadata.ts +0 -754
- package/src/lib/live-sync-engine.ts +0 -209
- package/src/lib/live-transport-gateway.ts +0 -128
- package/src/lib/location.ts +0 -137
- package/src/lib/media-fetch.ts +0 -860
- package/src/lib/media-includes.ts +0 -165
- package/src/lib/mention-threads-live.ts +0 -830
- package/src/lib/mentions-export.ts +0 -147
- package/src/lib/mentions-live.ts +0 -1114
- package/src/lib/moderation-target.ts +0 -227
- package/src/lib/moderation-write.ts +0 -131
- package/src/lib/mutes-write.ts +0 -171
- package/src/lib/mutes.ts +0 -84
- package/src/lib/ndjson-stream.ts +0 -106
- package/src/lib/network-map.ts +0 -382
- package/src/lib/openai-response-runtime.ts +0 -251
- package/src/lib/openai.ts +0 -117
- package/src/lib/paginated-sync.ts +0 -93
- package/src/lib/period-digest.ts +0 -1364
- package/src/lib/present.ts +0 -118
- package/src/lib/profile-affiliation-hydration.ts +0 -228
- package/src/lib/profile-affiliations.ts +0 -262
- package/src/lib/profile-analysis.ts +0 -1198
- package/src/lib/profile-bio-entities.ts +0 -318
- package/src/lib/profile-history.ts +0 -164
- package/src/lib/profile-hydration.ts +0 -180
- package/src/lib/profile-replies.ts +0 -77
- package/src/lib/profile-resolver.ts +0 -650
- package/src/lib/queries.ts +0 -6
- package/src/lib/query-actions.ts +0 -437
- package/src/lib/query-client.tsx +0 -47
- package/src/lib/query-read-model-shared.ts +0 -52
- package/src/lib/query-read-models.ts +0 -5
- package/src/lib/query-resource.ts +0 -41
- package/src/lib/query-status.ts +0 -164
- package/src/lib/research.ts +0 -652
- package/src/lib/runtime-services.ts +0 -20
- package/src/lib/search-discussion.ts +0 -812
- package/src/lib/seed.ts +0 -613
- package/src/lib/server-runtime-services.ts +0 -30
- package/src/lib/sqlite.ts +0 -183
- package/src/lib/streaming-ingestion.ts +0 -240
- package/src/lib/sync-cache.ts +0 -70
- package/src/lib/sync-plan.ts +0 -175
- package/src/lib/theme-transition.ts +0 -113
- package/src/lib/theme.tsx +0 -165
- package/src/lib/timeline-collections-live.ts +0 -407
- package/src/lib/timeline-live.ts +0 -302
- package/src/lib/timeline-read-model.ts +0 -1191
- package/src/lib/tweet-account-edges.ts +0 -48
- package/src/lib/tweet-lookup.ts +0 -46
- package/src/lib/tweet-render.ts +0 -430
- package/src/lib/tweet-repository.ts +0 -156
- package/src/lib/tweet-search-live.ts +0 -462
- package/src/lib/types.ts +0 -723
- package/src/lib/ui.ts +0 -529
- package/src/lib/url-expansion-store.ts +0 -110
- package/src/lib/url-expansion.ts +0 -343
- package/src/lib/url-safety.ts +0 -220
- package/src/lib/web-sync.ts +0 -533
- package/src/lib/whois.ts +0 -1080
- package/src/lib/x-profile.ts +0 -365
- package/src/lib/x-web.ts +0 -199
- package/src/lib/xurl-rate-limits.ts +0 -267
- package/src/lib/xurl.ts +0 -1677
- package/src/routeTree.gen.ts +0 -723
- package/src/router.tsx +0 -20
- package/src/routes/__root.tsx +0 -82
- package/src/routes/api/action.tsx +0 -147
- package/src/routes/api/avatar.tsx +0 -50
- package/src/routes/api/blocks.tsx +0 -35
- package/src/routes/api/conversation.tsx +0 -45
- package/src/routes/api/data-sources.tsx +0 -24
- package/src/routes/api/inbox.tsx +0 -43
- package/src/routes/api/link-insights.tsx +0 -85
- package/src/routes/api/link-preview.tsx +0 -47
- package/src/routes/api/network-map.tsx +0 -55
- package/src/routes/api/period-digest.tsx +0 -101
- package/src/routes/api/profile-analysis.tsx +0 -97
- package/src/routes/api/profile-hydrate.tsx +0 -54
- package/src/routes/api/query.tsx +0 -121
- package/src/routes/api/search-discussion.tsx +0 -113
- package/src/routes/api/status.tsx +0 -30
- package/src/routes/api/sync.tsx +0 -108
- package/src/routes/api/xurl-rate-limits.tsx +0 -24
- package/src/routes/blocks.tsx +0 -343
- package/src/routes/bookmarks.tsx +0 -18
- package/src/routes/data-sources.tsx +0 -249
- package/src/routes/discuss.tsx +0 -419
- package/src/routes/dms.tsx +0 -415
- package/src/routes/inbox.tsx +0 -232
- package/src/routes/index.tsx +0 -32
- package/src/routes/likes.tsx +0 -18
- package/src/routes/links.tsx +0 -631
- package/src/routes/mentions.tsx +0 -32
- package/src/routes/network-map.tsx +0 -732
- package/src/routes/profile-analyze.tsx +0 -112
- package/src/routes/profiles.$handle.tsx +0 -228
- package/src/routes/rate-limits.tsx +0 -305
- package/src/routes/today.tsx +0 -511
- package/src/styles.css +0 -257
- package/tsconfig.json +0 -29
- package/vite.config.ts +0 -29
- /package/{public → dist/client}/birdclaw-mark.png +0 -0
- /package/{public → dist/client}/favicon.ico +0 -0
- /package/{public → dist/client}/logo192.png +0 -0
- /package/{public → dist/client}/logo512.png +0 -0
- /package/{public → dist/client}/manifest.json +0 -0
- /package/{public → dist/client}/robots.txt +0 -0
package/src/lib/follow-graph.ts
DELETED
|
@@ -1,1087 +0,0 @@
|
|
|
1
|
-
import { randomUUID } from "node:crypto";
|
|
2
|
-
import { Effect } from "effect";
|
|
3
|
-
import { getNativeDb } from "./db";
|
|
4
|
-
import { runEffectPromise } from "./effect-runtime";
|
|
5
|
-
import { liveTransportGateway } from "./live-transport-gateway";
|
|
6
|
-
import type { Database } from "./sqlite";
|
|
7
|
-
import { readSyncCache, writeSyncCache } from "./sync-cache";
|
|
8
|
-
import { runSyncPlanEffect } from "./sync-plan";
|
|
9
|
-
import type {
|
|
10
|
-
FollowEventKind,
|
|
11
|
-
FollowDirection,
|
|
12
|
-
FollowGraphEvent,
|
|
13
|
-
FollowGraphProfile,
|
|
14
|
-
FollowGraphSummary,
|
|
15
|
-
XurlFollowUsersResponse,
|
|
16
|
-
XurlMentionUser,
|
|
17
|
-
XurlPublicMetrics,
|
|
18
|
-
} from "./types";
|
|
19
|
-
import { upsertProfileFromXUser } from "./x-profile";
|
|
20
|
-
|
|
21
|
-
const DEFAULT_FOLLOW_CACHE_TTL_MS = 24 * 60 * 60_000;
|
|
22
|
-
const DEFAULT_FOLLOW_PAGE_LIMIT = 1000;
|
|
23
|
-
const MIN_FOLLOW_PAGE_LIMIT = 1;
|
|
24
|
-
const MAX_FOLLOW_PAGE_LIMIT = 1000;
|
|
25
|
-
const BIRD_FOLLOW_PAGE_LIMIT = 100;
|
|
26
|
-
|
|
27
|
-
export interface SyncFollowGraphOptions {
|
|
28
|
-
direction: FollowDirection;
|
|
29
|
-
account?: string;
|
|
30
|
-
mode?: FollowGraphSyncMode;
|
|
31
|
-
limit?: number;
|
|
32
|
-
maxPages?: number;
|
|
33
|
-
maxResources?: number;
|
|
34
|
-
yes?: boolean;
|
|
35
|
-
refresh?: boolean;
|
|
36
|
-
allowPartial?: boolean;
|
|
37
|
-
cacheTtlMs?: number;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
type FollowGraphSyncMode = "auto" | "bird" | "xurl";
|
|
41
|
-
type FollowGraphLiveSource = "bird" | "xurl";
|
|
42
|
-
|
|
43
|
-
function toError(error: unknown) {
|
|
44
|
-
return error instanceof Error ? error : new Error(String(error));
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function trySync<T>(try_: () => T) {
|
|
48
|
-
return Effect.try({
|
|
49
|
-
try: try_,
|
|
50
|
-
catch: toError,
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
interface ResolvedAccount {
|
|
55
|
-
accountId: string;
|
|
56
|
-
username: string;
|
|
57
|
-
externalUserId?: string;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
interface MergedFollowPayload {
|
|
61
|
-
data: XurlMentionUser[];
|
|
62
|
-
meta: Record<string, unknown>;
|
|
63
|
-
complete: boolean;
|
|
64
|
-
pageCount: number;
|
|
65
|
-
truncatedByMaxResources: boolean;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
function parseLimit(value = DEFAULT_FOLLOW_PAGE_LIMIT) {
|
|
69
|
-
if (
|
|
70
|
-
!Number.isFinite(value) ||
|
|
71
|
-
value < MIN_FOLLOW_PAGE_LIMIT ||
|
|
72
|
-
value > MAX_FOLLOW_PAGE_LIMIT
|
|
73
|
-
) {
|
|
74
|
-
throw new Error("--limit must be between 1 and 1000 for follow sync");
|
|
75
|
-
}
|
|
76
|
-
return Math.floor(value);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
function parseOptionalPositiveInteger(name: string, value?: number) {
|
|
80
|
-
if (value === undefined) {
|
|
81
|
-
return undefined;
|
|
82
|
-
}
|
|
83
|
-
if (!Number.isFinite(value) || value < 1) {
|
|
84
|
-
throw new Error(`${name} must be at least 1`);
|
|
85
|
-
}
|
|
86
|
-
return Math.floor(value);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
function parseCacheTtlMs(value?: number) {
|
|
90
|
-
if (typeof value !== "number" || !Number.isFinite(value) || value < 0) {
|
|
91
|
-
return DEFAULT_FOLLOW_CACHE_TTL_MS;
|
|
92
|
-
}
|
|
93
|
-
return Math.floor(value);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
function parseJsonField<T>(value: unknown, fallback: T): T {
|
|
97
|
-
if (typeof value !== "string" || value.length === 0) {
|
|
98
|
-
return fallback;
|
|
99
|
-
}
|
|
100
|
-
try {
|
|
101
|
-
return JSON.parse(value) as T;
|
|
102
|
-
} catch {
|
|
103
|
-
return fallback;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
function resolveAccount(db: Database, accountId?: string): ResolvedAccount {
|
|
108
|
-
const row = accountId
|
|
109
|
-
? (db
|
|
110
|
-
.prepare(
|
|
111
|
-
"select id, handle, external_user_id from accounts where id = ?",
|
|
112
|
-
)
|
|
113
|
-
.get(accountId) as
|
|
114
|
-
| { id: string; handle: string; external_user_id: string | null }
|
|
115
|
-
| undefined)
|
|
116
|
-
: (db
|
|
117
|
-
.prepare(
|
|
118
|
-
`
|
|
119
|
-
select id, handle, external_user_id
|
|
120
|
-
from accounts
|
|
121
|
-
order by is_default desc, created_at asc
|
|
122
|
-
limit 1
|
|
123
|
-
`,
|
|
124
|
-
)
|
|
125
|
-
.get() as
|
|
126
|
-
| { id: string; handle: string; external_user_id: string | null }
|
|
127
|
-
| undefined);
|
|
128
|
-
|
|
129
|
-
if (!row) {
|
|
130
|
-
throw new Error(`Unknown account: ${accountId ?? "default"}`);
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
return {
|
|
134
|
-
accountId: row.id,
|
|
135
|
-
username: row.handle.replace(/^@/, ""),
|
|
136
|
-
externalUserId:
|
|
137
|
-
typeof row.external_user_id === "string" &&
|
|
138
|
-
row.external_user_id.length > 0
|
|
139
|
-
? row.external_user_id
|
|
140
|
-
: undefined,
|
|
141
|
-
};
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
function buildCacheKey({
|
|
145
|
-
direction,
|
|
146
|
-
accountId,
|
|
147
|
-
mode,
|
|
148
|
-
limit,
|
|
149
|
-
maxPages,
|
|
150
|
-
maxResources,
|
|
151
|
-
}: {
|
|
152
|
-
direction: FollowDirection;
|
|
153
|
-
accountId: string;
|
|
154
|
-
mode: FollowGraphSyncMode;
|
|
155
|
-
limit: number;
|
|
156
|
-
maxPages?: number;
|
|
157
|
-
maxResources?: number;
|
|
158
|
-
}) {
|
|
159
|
-
return [
|
|
160
|
-
"follow-graph",
|
|
161
|
-
direction,
|
|
162
|
-
accountId,
|
|
163
|
-
`mode:${mode}`,
|
|
164
|
-
`limit:${String(limit)}`,
|
|
165
|
-
`pages:${maxPages === undefined ? "all" : String(maxPages)}`,
|
|
166
|
-
`resources:${maxResources === undefined ? "all" : String(maxResources)}`,
|
|
167
|
-
].join(":");
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
function readCurrentCount(
|
|
171
|
-
db: Database,
|
|
172
|
-
accountId: string,
|
|
173
|
-
direction: FollowDirection,
|
|
174
|
-
) {
|
|
175
|
-
const row = db
|
|
176
|
-
.prepare(
|
|
177
|
-
`
|
|
178
|
-
select count(*) as count
|
|
179
|
-
from follow_edges
|
|
180
|
-
where account_id = ? and direction = ? and current = 1
|
|
181
|
-
`,
|
|
182
|
-
)
|
|
183
|
-
.get(accountId, direction) as { count: number };
|
|
184
|
-
return Number(row.count);
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
function getLastSnapshot(
|
|
188
|
-
db: Database,
|
|
189
|
-
accountId: string,
|
|
190
|
-
direction: FollowDirection,
|
|
191
|
-
status: "complete" | "incomplete",
|
|
192
|
-
) {
|
|
193
|
-
const row = db
|
|
194
|
-
.prepare(
|
|
195
|
-
`
|
|
196
|
-
select completed_at
|
|
197
|
-
from follow_snapshots
|
|
198
|
-
where account_id = ? and direction = ? and status = ?
|
|
199
|
-
order by completed_at desc
|
|
200
|
-
limit 1
|
|
201
|
-
`,
|
|
202
|
-
)
|
|
203
|
-
.get(accountId, direction, status) as { completed_at: string } | undefined;
|
|
204
|
-
return row?.completed_at;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
function mergePages(
|
|
208
|
-
pages: XurlFollowUsersResponse[],
|
|
209
|
-
nextToken: string | undefined,
|
|
210
|
-
maxResources?: number,
|
|
211
|
-
): MergedFollowPayload {
|
|
212
|
-
const users: XurlMentionUser[] = [];
|
|
213
|
-
const seen = new Set<string>();
|
|
214
|
-
let truncatedByMaxResources = false;
|
|
215
|
-
|
|
216
|
-
for (const page of pages) {
|
|
217
|
-
for (const user of page.data) {
|
|
218
|
-
if (seen.has(user.id)) {
|
|
219
|
-
continue;
|
|
220
|
-
}
|
|
221
|
-
if (maxResources !== undefined && users.length >= maxResources) {
|
|
222
|
-
truncatedByMaxResources = true;
|
|
223
|
-
break;
|
|
224
|
-
}
|
|
225
|
-
seen.add(user.id);
|
|
226
|
-
users.push(user);
|
|
227
|
-
}
|
|
228
|
-
if (truncatedByMaxResources) {
|
|
229
|
-
break;
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
const lastPage = pages.at(-1);
|
|
234
|
-
const payloadPageCount = Number(lastPage?.meta?.page_count);
|
|
235
|
-
const pageCount =
|
|
236
|
-
Number.isFinite(payloadPageCount) && payloadPageCount > pages.length
|
|
237
|
-
? payloadPageCount
|
|
238
|
-
: pages.length;
|
|
239
|
-
return {
|
|
240
|
-
data: users,
|
|
241
|
-
meta: {
|
|
242
|
-
...lastPage?.meta,
|
|
243
|
-
result_count: users.length,
|
|
244
|
-
page_count: pageCount,
|
|
245
|
-
next_token: nextToken ?? null,
|
|
246
|
-
truncated_by_max_resources: truncatedByMaxResources,
|
|
247
|
-
},
|
|
248
|
-
complete: !nextToken && !truncatedByMaxResources,
|
|
249
|
-
pageCount,
|
|
250
|
-
truncatedByMaxResources,
|
|
251
|
-
};
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
function fetchFollowGraphViaXurlEffect({
|
|
255
|
-
direction,
|
|
256
|
-
username,
|
|
257
|
-
userId,
|
|
258
|
-
limit,
|
|
259
|
-
maxPages,
|
|
260
|
-
maxResources,
|
|
261
|
-
}: {
|
|
262
|
-
direction: FollowDirection;
|
|
263
|
-
username: string;
|
|
264
|
-
userId?: string;
|
|
265
|
-
limit: number;
|
|
266
|
-
maxPages?: number;
|
|
267
|
-
maxResources?: number;
|
|
268
|
-
}): Effect.Effect<MergedFollowPayload, unknown> {
|
|
269
|
-
return Effect.gen(function* () {
|
|
270
|
-
const result = yield* runSyncPlanEffect({
|
|
271
|
-
fetchPage: ({ cursor }) =>
|
|
272
|
-
liveTransportGateway.xurl.listFollowUsers({
|
|
273
|
-
direction,
|
|
274
|
-
username,
|
|
275
|
-
userId,
|
|
276
|
-
maxResults: limit,
|
|
277
|
-
...(cursor ? { paginationToken: cursor } : {}),
|
|
278
|
-
}),
|
|
279
|
-
getItemCount: (page) => page.data.length,
|
|
280
|
-
getNextCursor: (page) =>
|
|
281
|
-
typeof page.meta?.next_token === "string"
|
|
282
|
-
? String(page.meta.next_token)
|
|
283
|
-
: undefined,
|
|
284
|
-
maxItems: maxResources,
|
|
285
|
-
maxPages,
|
|
286
|
-
});
|
|
287
|
-
|
|
288
|
-
return mergePages(result.pages, result.nextCursor, maxResources);
|
|
289
|
-
});
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
function fetchFollowGraphViaBirdEffect({
|
|
293
|
-
direction,
|
|
294
|
-
userId,
|
|
295
|
-
limit,
|
|
296
|
-
maxPages,
|
|
297
|
-
maxResources,
|
|
298
|
-
}: {
|
|
299
|
-
direction: FollowDirection;
|
|
300
|
-
userId?: string;
|
|
301
|
-
limit: number;
|
|
302
|
-
maxPages?: number;
|
|
303
|
-
maxResources?: number;
|
|
304
|
-
}): Effect.Effect<MergedFollowPayload, unknown> {
|
|
305
|
-
return Effect.gen(function* () {
|
|
306
|
-
const birdLimit = Math.min(limit, BIRD_FOLLOW_PAGE_LIMIT);
|
|
307
|
-
const cappedMaxPages =
|
|
308
|
-
maxResources === undefined
|
|
309
|
-
? maxPages
|
|
310
|
-
: Math.min(
|
|
311
|
-
maxPages ?? Number.POSITIVE_INFINITY,
|
|
312
|
-
Math.ceil(maxResources / birdLimit),
|
|
313
|
-
);
|
|
314
|
-
const payload = yield* liveTransportGateway.bird.listFollowUsers({
|
|
315
|
-
direction,
|
|
316
|
-
userId,
|
|
317
|
-
maxResults: Math.min(birdLimit, maxResources ?? birdLimit),
|
|
318
|
-
all: true,
|
|
319
|
-
maxPages: Number.isFinite(cappedMaxPages) ? cappedMaxPages : undefined,
|
|
320
|
-
});
|
|
321
|
-
return mergePages(
|
|
322
|
-
[payload],
|
|
323
|
-
typeof payload.meta?.next_token === "string"
|
|
324
|
-
? String(payload.meta.next_token)
|
|
325
|
-
: undefined,
|
|
326
|
-
maxResources,
|
|
327
|
-
);
|
|
328
|
-
});
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
function fetchFollowGraphEffect({
|
|
332
|
-
mode,
|
|
333
|
-
direction,
|
|
334
|
-
username,
|
|
335
|
-
userId,
|
|
336
|
-
limit,
|
|
337
|
-
maxPages,
|
|
338
|
-
maxResources,
|
|
339
|
-
}: {
|
|
340
|
-
mode: FollowGraphSyncMode;
|
|
341
|
-
direction: FollowDirection;
|
|
342
|
-
username: string;
|
|
343
|
-
userId?: string;
|
|
344
|
-
limit: number;
|
|
345
|
-
maxPages?: number;
|
|
346
|
-
maxResources?: number;
|
|
347
|
-
}): Effect.Effect<
|
|
348
|
-
{ source: FollowGraphLiveSource; payload: MergedFollowPayload },
|
|
349
|
-
unknown
|
|
350
|
-
> {
|
|
351
|
-
return Effect.gen(function* () {
|
|
352
|
-
if (mode === "bird") {
|
|
353
|
-
return {
|
|
354
|
-
source: "bird",
|
|
355
|
-
payload: yield* fetchFollowGraphViaBirdEffect({
|
|
356
|
-
direction,
|
|
357
|
-
userId,
|
|
358
|
-
limit,
|
|
359
|
-
maxPages,
|
|
360
|
-
maxResources,
|
|
361
|
-
}),
|
|
362
|
-
};
|
|
363
|
-
}
|
|
364
|
-
if (mode === "xurl") {
|
|
365
|
-
return {
|
|
366
|
-
source: "xurl",
|
|
367
|
-
payload: yield* fetchFollowGraphViaXurlEffect({
|
|
368
|
-
direction,
|
|
369
|
-
username,
|
|
370
|
-
userId,
|
|
371
|
-
limit,
|
|
372
|
-
maxPages,
|
|
373
|
-
maxResources,
|
|
374
|
-
}),
|
|
375
|
-
};
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
const birdResult = yield* fetchFollowGraphViaBirdEffect({
|
|
379
|
-
direction,
|
|
380
|
-
userId,
|
|
381
|
-
limit,
|
|
382
|
-
maxPages,
|
|
383
|
-
maxResources,
|
|
384
|
-
}).pipe(
|
|
385
|
-
Effect.map((payload) => ({ ok: true as const, payload })),
|
|
386
|
-
Effect.catchAll((error) => Effect.succeed({ ok: false as const, error })),
|
|
387
|
-
);
|
|
388
|
-
if (birdResult.ok) {
|
|
389
|
-
return {
|
|
390
|
-
source: "bird",
|
|
391
|
-
payload: birdResult.payload,
|
|
392
|
-
};
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
const xurlResult = yield* fetchFollowGraphViaXurlEffect({
|
|
396
|
-
direction,
|
|
397
|
-
username,
|
|
398
|
-
userId,
|
|
399
|
-
limit,
|
|
400
|
-
maxPages,
|
|
401
|
-
maxResources,
|
|
402
|
-
}).pipe(
|
|
403
|
-
Effect.map((payload) => ({ ok: true as const, payload })),
|
|
404
|
-
Effect.catchAll((error) => Effect.succeed({ ok: false as const, error })),
|
|
405
|
-
);
|
|
406
|
-
if (xurlResult.ok) {
|
|
407
|
-
return {
|
|
408
|
-
source: "xurl",
|
|
409
|
-
payload: xurlResult.payload,
|
|
410
|
-
};
|
|
411
|
-
}
|
|
412
|
-
|
|
413
|
-
return yield* Effect.fail(
|
|
414
|
-
new Error(
|
|
415
|
-
`follow graph sync failed via bird and xurl: bird: ${errorMessage(
|
|
416
|
-
birdResult.error,
|
|
417
|
-
)}; xurl: ${errorMessage(xurlResult.error)}`,
|
|
418
|
-
),
|
|
419
|
-
);
|
|
420
|
-
});
|
|
421
|
-
}
|
|
422
|
-
|
|
423
|
-
function getExistingEdges(
|
|
424
|
-
db: Database,
|
|
425
|
-
accountId: string,
|
|
426
|
-
direction: FollowDirection,
|
|
427
|
-
) {
|
|
428
|
-
const rows = db
|
|
429
|
-
.prepare(
|
|
430
|
-
`
|
|
431
|
-
select profile_id, external_user_id, current
|
|
432
|
-
from follow_edges
|
|
433
|
-
where account_id = ? and direction = ?
|
|
434
|
-
`,
|
|
435
|
-
)
|
|
436
|
-
.all(accountId, direction) as Array<{
|
|
437
|
-
profile_id: string;
|
|
438
|
-
external_user_id: string;
|
|
439
|
-
current: number;
|
|
440
|
-
}>;
|
|
441
|
-
return new Map(rows.map((row) => [row.profile_id, row]));
|
|
442
|
-
}
|
|
443
|
-
|
|
444
|
-
function mergeFollowPayloadIntoLocalStore({
|
|
445
|
-
db,
|
|
446
|
-
accountId,
|
|
447
|
-
direction,
|
|
448
|
-
payload,
|
|
449
|
-
source,
|
|
450
|
-
}: {
|
|
451
|
-
db: Database;
|
|
452
|
-
accountId: string;
|
|
453
|
-
direction: FollowDirection;
|
|
454
|
-
payload: MergedFollowPayload;
|
|
455
|
-
source: FollowGraphLiveSource | "cache";
|
|
456
|
-
}) {
|
|
457
|
-
const now = new Date().toISOString();
|
|
458
|
-
const snapshotId = `follow_snapshot_${randomUUID()}`;
|
|
459
|
-
const status = payload.complete ? "complete" : "incomplete";
|
|
460
|
-
|
|
461
|
-
const insertSnapshot = db.prepare(`
|
|
462
|
-
insert into follow_snapshots (
|
|
463
|
-
id, account_id, direction, source, status, page_count, result_count,
|
|
464
|
-
started_at, completed_at, raw_meta_json
|
|
465
|
-
) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
466
|
-
`);
|
|
467
|
-
const insertMember = db.prepare(`
|
|
468
|
-
insert into follow_snapshot_members (
|
|
469
|
-
snapshot_id, profile_id, external_user_id, position
|
|
470
|
-
) values (?, ?, ?, ?)
|
|
471
|
-
`);
|
|
472
|
-
const insertEdge = db.prepare(`
|
|
473
|
-
insert into follow_edges (
|
|
474
|
-
account_id, direction, profile_id, external_user_id, source, current,
|
|
475
|
-
first_seen_at, last_seen_at, ended_at, updated_at
|
|
476
|
-
) values (?, ?, ?, ?, ?, 1, ?, ?, null, ?)
|
|
477
|
-
on conflict(account_id, direction, profile_id) do update set
|
|
478
|
-
external_user_id = excluded.external_user_id,
|
|
479
|
-
source = excluded.source,
|
|
480
|
-
current = 1,
|
|
481
|
-
last_seen_at = excluded.last_seen_at,
|
|
482
|
-
ended_at = null,
|
|
483
|
-
updated_at = excluded.updated_at
|
|
484
|
-
`);
|
|
485
|
-
const endEdge = db.prepare(`
|
|
486
|
-
update follow_edges
|
|
487
|
-
set current = 0, ended_at = ?, updated_at = ?
|
|
488
|
-
where account_id = ? and direction = ? and profile_id = ?
|
|
489
|
-
`);
|
|
490
|
-
const insertEvent = db.prepare(`
|
|
491
|
-
insert into follow_events (
|
|
492
|
-
id, account_id, direction, profile_id, external_user_id, kind, event_at, snapshot_id
|
|
493
|
-
) values (?, ?, ?, ?, ?, ?, ?, ?)
|
|
494
|
-
`);
|
|
495
|
-
|
|
496
|
-
return db.transaction(() => {
|
|
497
|
-
const existingEdges = getExistingEdges(db, accountId, direction);
|
|
498
|
-
const currentProfileIds = new Set<string>();
|
|
499
|
-
|
|
500
|
-
insertSnapshot.run(
|
|
501
|
-
snapshotId,
|
|
502
|
-
accountId,
|
|
503
|
-
direction,
|
|
504
|
-
source,
|
|
505
|
-
status,
|
|
506
|
-
payload.pageCount,
|
|
507
|
-
payload.data.length,
|
|
508
|
-
now,
|
|
509
|
-
now,
|
|
510
|
-
JSON.stringify(payload.meta),
|
|
511
|
-
);
|
|
512
|
-
|
|
513
|
-
payload.data.forEach((user, index) => {
|
|
514
|
-
const resolved = upsertProfileFromXUser(db, user);
|
|
515
|
-
currentProfileIds.add(resolved.profile.id);
|
|
516
|
-
insertMember.run(
|
|
517
|
-
snapshotId,
|
|
518
|
-
resolved.profile.id,
|
|
519
|
-
resolved.externalUserId,
|
|
520
|
-
index,
|
|
521
|
-
);
|
|
522
|
-
|
|
523
|
-
if (!payload.complete) {
|
|
524
|
-
return;
|
|
525
|
-
}
|
|
526
|
-
|
|
527
|
-
const previous = existingEdges.get(resolved.profile.id);
|
|
528
|
-
insertEdge.run(
|
|
529
|
-
accountId,
|
|
530
|
-
direction,
|
|
531
|
-
resolved.profile.id,
|
|
532
|
-
resolved.externalUserId,
|
|
533
|
-
source,
|
|
534
|
-
now,
|
|
535
|
-
now,
|
|
536
|
-
now,
|
|
537
|
-
);
|
|
538
|
-
if (!previous || previous.current === 0) {
|
|
539
|
-
insertEvent.run(
|
|
540
|
-
`follow_event_${randomUUID()}`,
|
|
541
|
-
accountId,
|
|
542
|
-
direction,
|
|
543
|
-
resolved.profile.id,
|
|
544
|
-
resolved.externalUserId,
|
|
545
|
-
"started",
|
|
546
|
-
now,
|
|
547
|
-
snapshotId,
|
|
548
|
-
);
|
|
549
|
-
}
|
|
550
|
-
});
|
|
551
|
-
|
|
552
|
-
if (payload.complete) {
|
|
553
|
-
for (const [profileId, previous] of existingEdges) {
|
|
554
|
-
if (previous.current === 1 && !currentProfileIds.has(profileId)) {
|
|
555
|
-
endEdge.run(now, now, accountId, direction, profileId);
|
|
556
|
-
insertEvent.run(
|
|
557
|
-
`follow_event_${randomUUID()}`,
|
|
558
|
-
accountId,
|
|
559
|
-
direction,
|
|
560
|
-
profileId,
|
|
561
|
-
previous.external_user_id,
|
|
562
|
-
"ended",
|
|
563
|
-
now,
|
|
564
|
-
snapshotId,
|
|
565
|
-
);
|
|
566
|
-
}
|
|
567
|
-
}
|
|
568
|
-
}
|
|
569
|
-
|
|
570
|
-
return {
|
|
571
|
-
snapshotId,
|
|
572
|
-
status,
|
|
573
|
-
count: payload.data.length,
|
|
574
|
-
pageCount: payload.pageCount,
|
|
575
|
-
};
|
|
576
|
-
})();
|
|
577
|
-
}
|
|
578
|
-
|
|
579
|
-
function makeDryRunResponse({
|
|
580
|
-
db,
|
|
581
|
-
account,
|
|
582
|
-
direction,
|
|
583
|
-
mode,
|
|
584
|
-
limit,
|
|
585
|
-
maxPages,
|
|
586
|
-
maxResources,
|
|
587
|
-
cacheKey,
|
|
588
|
-
cacheTtlMs,
|
|
589
|
-
}: {
|
|
590
|
-
db: Database;
|
|
591
|
-
account: ResolvedAccount;
|
|
592
|
-
direction: FollowDirection;
|
|
593
|
-
mode: FollowGraphSyncMode;
|
|
594
|
-
limit: number;
|
|
595
|
-
maxPages?: number;
|
|
596
|
-
maxResources?: number;
|
|
597
|
-
cacheKey: string;
|
|
598
|
-
cacheTtlMs: number;
|
|
599
|
-
}) {
|
|
600
|
-
const cached = readSyncCache<MergedFollowPayload>(cacheKey, db);
|
|
601
|
-
const cacheAgeMs = cached
|
|
602
|
-
? Date.now() - new Date(cached.updatedAt).getTime()
|
|
603
|
-
: Number.POSITIVE_INFINITY;
|
|
604
|
-
const wouldCallLive = !cached || cacheAgeMs > cacheTtlMs;
|
|
605
|
-
return {
|
|
606
|
-
ok: true,
|
|
607
|
-
dryRun: true,
|
|
608
|
-
direction,
|
|
609
|
-
accountId: account.accountId,
|
|
610
|
-
mode,
|
|
611
|
-
wouldCallLive,
|
|
612
|
-
wouldCallX: mode === "bird" ? false : wouldCallLive,
|
|
613
|
-
requiredFlag: "--yes",
|
|
614
|
-
estimate: {
|
|
615
|
-
maxResultsPerPage: limit,
|
|
616
|
-
maxPages: maxPages ?? null,
|
|
617
|
-
maxResources: maxResources ?? null,
|
|
618
|
-
cacheTtlSeconds: Math.floor(cacheTtlMs / 1000),
|
|
619
|
-
},
|
|
620
|
-
cache: {
|
|
621
|
-
key: cacheKey,
|
|
622
|
-
hit: Boolean(cached),
|
|
623
|
-
fresh: Boolean(cached && cacheAgeMs <= cacheTtlMs),
|
|
624
|
-
updatedAt: cached?.updatedAt ?? null,
|
|
625
|
-
ageSeconds: Number.isFinite(cacheAgeMs)
|
|
626
|
-
? Math.floor(cacheAgeMs / 1000)
|
|
627
|
-
: null,
|
|
628
|
-
count: cached?.value.data.length ?? 0,
|
|
629
|
-
},
|
|
630
|
-
currentCount: readCurrentCount(db, account.accountId, direction),
|
|
631
|
-
message:
|
|
632
|
-
"Dry run only. Pass --yes to use a fresh cache or perform live follow graph sync.",
|
|
633
|
-
};
|
|
634
|
-
}
|
|
635
|
-
|
|
636
|
-
function parseMode(value?: FollowGraphSyncMode) {
|
|
637
|
-
if (!value || value === "auto" || value === "bird" || value === "xurl") {
|
|
638
|
-
return value ?? "auto";
|
|
639
|
-
}
|
|
640
|
-
throw new Error("--mode must be auto, bird, or xurl");
|
|
641
|
-
}
|
|
642
|
-
|
|
643
|
-
function errorMessage(error: unknown) {
|
|
644
|
-
return error instanceof Error ? error.message : String(error);
|
|
645
|
-
}
|
|
646
|
-
|
|
647
|
-
export function syncFollowGraphEffect(options: SyncFollowGraphOptions) {
|
|
648
|
-
return Effect.gen(function* () {
|
|
649
|
-
const db = yield* trySync(() => getNativeDb());
|
|
650
|
-
const mode = yield* trySync(() => parseMode(options.mode));
|
|
651
|
-
const limit = yield* trySync(() => parseLimit(options.limit));
|
|
652
|
-
const maxPages = yield* trySync(() =>
|
|
653
|
-
parseOptionalPositiveInteger("--max-pages", options.maxPages),
|
|
654
|
-
);
|
|
655
|
-
const maxResources = yield* trySync(() =>
|
|
656
|
-
parseOptionalPositiveInteger("--max-resources", options.maxResources),
|
|
657
|
-
);
|
|
658
|
-
const cacheTtlMs = parseCacheTtlMs(options.cacheTtlMs);
|
|
659
|
-
const account = yield* trySync(() => resolveAccount(db, options.account));
|
|
660
|
-
const cacheKey = buildCacheKey({
|
|
661
|
-
direction: options.direction,
|
|
662
|
-
accountId: account.accountId,
|
|
663
|
-
mode,
|
|
664
|
-
limit,
|
|
665
|
-
maxPages,
|
|
666
|
-
maxResources,
|
|
667
|
-
});
|
|
668
|
-
|
|
669
|
-
if (!options.yes) {
|
|
670
|
-
return yield* trySync(() =>
|
|
671
|
-
makeDryRunResponse({
|
|
672
|
-
db,
|
|
673
|
-
account,
|
|
674
|
-
direction: options.direction,
|
|
675
|
-
mode,
|
|
676
|
-
limit,
|
|
677
|
-
maxPages,
|
|
678
|
-
maxResources,
|
|
679
|
-
cacheKey,
|
|
680
|
-
cacheTtlMs,
|
|
681
|
-
}),
|
|
682
|
-
);
|
|
683
|
-
}
|
|
684
|
-
|
|
685
|
-
const cached = yield* trySync(() =>
|
|
686
|
-
readSyncCache<MergedFollowPayload>(cacheKey, db),
|
|
687
|
-
);
|
|
688
|
-
const cacheAgeMs = cached
|
|
689
|
-
? Date.now() - new Date(cached.updatedAt).getTime()
|
|
690
|
-
: Number.POSITIVE_INFINITY;
|
|
691
|
-
const useCache = Boolean(
|
|
692
|
-
!options.refresh && cached && cacheAgeMs <= cacheTtlMs,
|
|
693
|
-
);
|
|
694
|
-
|
|
695
|
-
const liveResult = useCache
|
|
696
|
-
? undefined
|
|
697
|
-
: yield* fetchFollowGraphEffect({
|
|
698
|
-
mode,
|
|
699
|
-
direction: options.direction,
|
|
700
|
-
username: account.username,
|
|
701
|
-
userId: account.externalUserId,
|
|
702
|
-
limit,
|
|
703
|
-
maxPages,
|
|
704
|
-
maxResources,
|
|
705
|
-
});
|
|
706
|
-
const payload = useCache ? cached!.value : liveResult!.payload;
|
|
707
|
-
|
|
708
|
-
if (!useCache) {
|
|
709
|
-
yield* trySync(() => writeSyncCache(cacheKey, payload, db));
|
|
710
|
-
}
|
|
711
|
-
|
|
712
|
-
const mergeResult = yield* trySync(() =>
|
|
713
|
-
mergeFollowPayloadIntoLocalStore({
|
|
714
|
-
db,
|
|
715
|
-
accountId: account.accountId,
|
|
716
|
-
direction: options.direction,
|
|
717
|
-
payload,
|
|
718
|
-
source: useCache ? "cache" : liveResult!.source,
|
|
719
|
-
}),
|
|
720
|
-
);
|
|
721
|
-
|
|
722
|
-
return {
|
|
723
|
-
ok: true,
|
|
724
|
-
dryRun: false,
|
|
725
|
-
source: useCache ? "cache" : liveResult!.source,
|
|
726
|
-
mode,
|
|
727
|
-
direction: options.direction,
|
|
728
|
-
accountId: account.accountId,
|
|
729
|
-
status: mergeResult.status,
|
|
730
|
-
count: mergeResult.count,
|
|
731
|
-
pageCount: mergeResult.pageCount,
|
|
732
|
-
snapshotId: mergeResult.snapshotId,
|
|
733
|
-
partial: mergeResult.status === "incomplete",
|
|
734
|
-
cache: {
|
|
735
|
-
key: cacheKey,
|
|
736
|
-
reused: useCache,
|
|
737
|
-
updatedAt: useCache ? cached?.updatedAt : new Date().toISOString(),
|
|
738
|
-
},
|
|
739
|
-
warning:
|
|
740
|
-
mergeResult.status === "incomplete" && !options.allowPartial
|
|
741
|
-
? "Snapshot is incomplete because a page/resource cap stopped pagination. It was recorded but not used for churn events."
|
|
742
|
-
: undefined,
|
|
743
|
-
};
|
|
744
|
-
});
|
|
745
|
-
}
|
|
746
|
-
|
|
747
|
-
export function syncFollowGraph(options: SyncFollowGraphOptions) {
|
|
748
|
-
return runEffectPromise(syncFollowGraphEffect(options));
|
|
749
|
-
}
|
|
750
|
-
|
|
751
|
-
function toGraphProfile(row: Record<string, unknown>): FollowGraphProfile {
|
|
752
|
-
return {
|
|
753
|
-
id: String(row.profile_id ?? row.id),
|
|
754
|
-
externalUserId: String(row.external_user_id),
|
|
755
|
-
handle: String(row.handle),
|
|
756
|
-
displayName: String(row.display_name),
|
|
757
|
-
bio: String(row.bio ?? ""),
|
|
758
|
-
followersCount: Number(row.followers_count ?? 0),
|
|
759
|
-
publicMetrics: parseJsonField<XurlPublicMetrics>(
|
|
760
|
-
row.public_metrics_json,
|
|
761
|
-
{},
|
|
762
|
-
),
|
|
763
|
-
avatarUrl:
|
|
764
|
-
typeof row.avatar_url === "string" && row.avatar_url.length > 0
|
|
765
|
-
? String(row.avatar_url)
|
|
766
|
-
: undefined,
|
|
767
|
-
};
|
|
768
|
-
}
|
|
769
|
-
|
|
770
|
-
export function listTopFollowers({
|
|
771
|
-
account,
|
|
772
|
-
limit = 20,
|
|
773
|
-
}: {
|
|
774
|
-
account?: string;
|
|
775
|
-
limit?: number;
|
|
776
|
-
} = {}) {
|
|
777
|
-
const db = getNativeDb();
|
|
778
|
-
const resolved = resolveAccount(db, account);
|
|
779
|
-
const rows = db
|
|
780
|
-
.prepare(
|
|
781
|
-
`
|
|
782
|
-
select
|
|
783
|
-
e.profile_id,
|
|
784
|
-
e.external_user_id,
|
|
785
|
-
p.handle,
|
|
786
|
-
p.display_name,
|
|
787
|
-
p.bio,
|
|
788
|
-
p.followers_count,
|
|
789
|
-
p.public_metrics_json,
|
|
790
|
-
p.avatar_url
|
|
791
|
-
from follow_edges e
|
|
792
|
-
join profiles p on p.id = e.profile_id
|
|
793
|
-
where e.account_id = ? and e.direction = 'followers' and e.current = 1
|
|
794
|
-
order by p.followers_count desc, lower(p.handle) asc
|
|
795
|
-
limit ?
|
|
796
|
-
`,
|
|
797
|
-
)
|
|
798
|
-
.all(resolved.accountId, limit) as Array<Record<string, unknown>>;
|
|
799
|
-
return {
|
|
800
|
-
accountId: resolved.accountId,
|
|
801
|
-
items: rows.map(toGraphProfile),
|
|
802
|
-
};
|
|
803
|
-
}
|
|
804
|
-
|
|
805
|
-
export function listMutuals({
|
|
806
|
-
account,
|
|
807
|
-
limit = 100,
|
|
808
|
-
}: {
|
|
809
|
-
account?: string;
|
|
810
|
-
limit?: number;
|
|
811
|
-
} = {}) {
|
|
812
|
-
const db = getNativeDb();
|
|
813
|
-
const resolved = resolveAccount(db, account);
|
|
814
|
-
const rows = db
|
|
815
|
-
.prepare(
|
|
816
|
-
`
|
|
817
|
-
select
|
|
818
|
-
following.profile_id,
|
|
819
|
-
following.external_user_id,
|
|
820
|
-
p.handle,
|
|
821
|
-
p.display_name,
|
|
822
|
-
p.bio,
|
|
823
|
-
p.followers_count,
|
|
824
|
-
p.public_metrics_json,
|
|
825
|
-
p.avatar_url
|
|
826
|
-
from follow_edges following
|
|
827
|
-
join follow_edges followers
|
|
828
|
-
on followers.account_id = following.account_id
|
|
829
|
-
and followers.profile_id = following.profile_id
|
|
830
|
-
and followers.direction = 'followers'
|
|
831
|
-
and followers.current = 1
|
|
832
|
-
join profiles p on p.id = following.profile_id
|
|
833
|
-
where
|
|
834
|
-
following.account_id = ?
|
|
835
|
-
and following.direction = 'following'
|
|
836
|
-
and following.current = 1
|
|
837
|
-
order by p.followers_count desc, lower(p.handle) asc
|
|
838
|
-
limit ?
|
|
839
|
-
`,
|
|
840
|
-
)
|
|
841
|
-
.all(resolved.accountId, limit) as Array<Record<string, unknown>>;
|
|
842
|
-
return {
|
|
843
|
-
accountId: resolved.accountId,
|
|
844
|
-
items: rows.map(toGraphProfile),
|
|
845
|
-
};
|
|
846
|
-
}
|
|
847
|
-
|
|
848
|
-
export function listNonMutualFollowing({
|
|
849
|
-
account,
|
|
850
|
-
sort = "followers",
|
|
851
|
-
limit = 100,
|
|
852
|
-
}: {
|
|
853
|
-
account?: string;
|
|
854
|
-
sort?: "followers" | "handle";
|
|
855
|
-
limit?: number;
|
|
856
|
-
} = {}) {
|
|
857
|
-
const db = getNativeDb();
|
|
858
|
-
const resolved = resolveAccount(db, account);
|
|
859
|
-
const orderBy =
|
|
860
|
-
sort === "handle"
|
|
861
|
-
? "lower(p.handle) asc"
|
|
862
|
-
: "p.followers_count desc, lower(p.handle) asc";
|
|
863
|
-
const rows = db
|
|
864
|
-
.prepare(
|
|
865
|
-
`
|
|
866
|
-
select
|
|
867
|
-
following.profile_id,
|
|
868
|
-
following.external_user_id,
|
|
869
|
-
p.handle,
|
|
870
|
-
p.display_name,
|
|
871
|
-
p.bio,
|
|
872
|
-
p.followers_count,
|
|
873
|
-
p.public_metrics_json,
|
|
874
|
-
p.avatar_url
|
|
875
|
-
from follow_edges following
|
|
876
|
-
left join follow_edges followers
|
|
877
|
-
on followers.account_id = following.account_id
|
|
878
|
-
and followers.profile_id = following.profile_id
|
|
879
|
-
and followers.direction = 'followers'
|
|
880
|
-
and followers.current = 1
|
|
881
|
-
join profiles p on p.id = following.profile_id
|
|
882
|
-
where
|
|
883
|
-
following.account_id = ?
|
|
884
|
-
and following.direction = 'following'
|
|
885
|
-
and following.current = 1
|
|
886
|
-
and followers.profile_id is null
|
|
887
|
-
order by ${orderBy}
|
|
888
|
-
limit ?
|
|
889
|
-
`,
|
|
890
|
-
)
|
|
891
|
-
.all(resolved.accountId, limit) as Array<Record<string, unknown>>;
|
|
892
|
-
return {
|
|
893
|
-
accountId: resolved.accountId,
|
|
894
|
-
items: rows.map(toGraphProfile),
|
|
895
|
-
};
|
|
896
|
-
}
|
|
897
|
-
|
|
898
|
-
export function listUnfollowedSince({
|
|
899
|
-
account,
|
|
900
|
-
date,
|
|
901
|
-
direction = "followers",
|
|
902
|
-
limit = 100,
|
|
903
|
-
}: {
|
|
904
|
-
account?: string;
|
|
905
|
-
date: string;
|
|
906
|
-
direction?: FollowDirection;
|
|
907
|
-
limit?: number;
|
|
908
|
-
}) {
|
|
909
|
-
const db = getNativeDb();
|
|
910
|
-
const resolved = resolveAccount(db, account);
|
|
911
|
-
const since = date.includes("T") ? date : `${date}T00:00:00.000Z`;
|
|
912
|
-
const rows = db
|
|
913
|
-
.prepare(
|
|
914
|
-
`
|
|
915
|
-
select
|
|
916
|
-
ev.event_at,
|
|
917
|
-
ev.profile_id,
|
|
918
|
-
ev.external_user_id,
|
|
919
|
-
p.handle,
|
|
920
|
-
p.display_name,
|
|
921
|
-
p.bio,
|
|
922
|
-
p.followers_count,
|
|
923
|
-
p.public_metrics_json,
|
|
924
|
-
p.avatar_url
|
|
925
|
-
from follow_events ev
|
|
926
|
-
join profiles p on p.id = ev.profile_id
|
|
927
|
-
where
|
|
928
|
-
ev.account_id = ?
|
|
929
|
-
and ev.direction = ?
|
|
930
|
-
and ev.kind = 'ended'
|
|
931
|
-
and ev.event_at >= ?
|
|
932
|
-
order by ev.event_at desc, p.followers_count desc
|
|
933
|
-
limit ?
|
|
934
|
-
`,
|
|
935
|
-
)
|
|
936
|
-
.all(resolved.accountId, direction, since, limit) as Array<
|
|
937
|
-
Record<string, unknown>
|
|
938
|
-
>;
|
|
939
|
-
return {
|
|
940
|
-
accountId: resolved.accountId,
|
|
941
|
-
direction,
|
|
942
|
-
since,
|
|
943
|
-
items: rows.map((row) => ({
|
|
944
|
-
eventAt: String(row.event_at),
|
|
945
|
-
profile: toGraphProfile(row),
|
|
946
|
-
})),
|
|
947
|
-
};
|
|
948
|
-
}
|
|
949
|
-
|
|
950
|
-
export function listFollowEvents({
|
|
951
|
-
account,
|
|
952
|
-
direction,
|
|
953
|
-
kind,
|
|
954
|
-
since,
|
|
955
|
-
until,
|
|
956
|
-
limit = 100,
|
|
957
|
-
}: {
|
|
958
|
-
account?: string;
|
|
959
|
-
direction?: FollowDirection;
|
|
960
|
-
kind?: FollowEventKind;
|
|
961
|
-
since?: string;
|
|
962
|
-
until?: string;
|
|
963
|
-
limit?: number;
|
|
964
|
-
} = {}) {
|
|
965
|
-
const db = getNativeDb();
|
|
966
|
-
const resolved = resolveAccount(db, account);
|
|
967
|
-
const where = ["ev.account_id = ?"];
|
|
968
|
-
const params: unknown[] = [resolved.accountId];
|
|
969
|
-
|
|
970
|
-
if (direction) {
|
|
971
|
-
where.push("ev.direction = ?");
|
|
972
|
-
params.push(direction);
|
|
973
|
-
}
|
|
974
|
-
if (kind) {
|
|
975
|
-
where.push("ev.kind = ?");
|
|
976
|
-
params.push(kind);
|
|
977
|
-
}
|
|
978
|
-
if (since) {
|
|
979
|
-
where.push("ev.event_at >= ?");
|
|
980
|
-
params.push(since.includes("T") ? since : `${since}T00:00:00.000Z`);
|
|
981
|
-
}
|
|
982
|
-
if (until) {
|
|
983
|
-
where.push("ev.event_at < ?");
|
|
984
|
-
params.push(until.includes("T") ? until : `${until}T00:00:00.000Z`);
|
|
985
|
-
}
|
|
986
|
-
|
|
987
|
-
params.push(limit);
|
|
988
|
-
const rows = db
|
|
989
|
-
.prepare(
|
|
990
|
-
`
|
|
991
|
-
select
|
|
992
|
-
ev.event_at,
|
|
993
|
-
ev.direction,
|
|
994
|
-
ev.kind,
|
|
995
|
-
ev.snapshot_id,
|
|
996
|
-
ev.profile_id,
|
|
997
|
-
ev.external_user_id,
|
|
998
|
-
p.handle,
|
|
999
|
-
p.display_name,
|
|
1000
|
-
p.bio,
|
|
1001
|
-
p.followers_count,
|
|
1002
|
-
p.public_metrics_json,
|
|
1003
|
-
p.avatar_url
|
|
1004
|
-
from follow_events ev
|
|
1005
|
-
join profiles p on p.id = ev.profile_id
|
|
1006
|
-
where ${where.join(" and ")}
|
|
1007
|
-
order by ev.event_at desc, p.followers_count desc, lower(p.handle) asc
|
|
1008
|
-
limit ?
|
|
1009
|
-
`,
|
|
1010
|
-
)
|
|
1011
|
-
.all(...params) as Array<Record<string, unknown>>;
|
|
1012
|
-
|
|
1013
|
-
return {
|
|
1014
|
-
accountId: resolved.accountId,
|
|
1015
|
-
items: rows.map(
|
|
1016
|
-
(row): FollowGraphEvent => ({
|
|
1017
|
-
eventAt: String(row.event_at),
|
|
1018
|
-
direction: row.direction as FollowDirection,
|
|
1019
|
-
kind: row.kind as FollowEventKind,
|
|
1020
|
-
snapshotId: String(row.snapshot_id),
|
|
1021
|
-
profile: toGraphProfile(row),
|
|
1022
|
-
}),
|
|
1023
|
-
),
|
|
1024
|
-
};
|
|
1025
|
-
}
|
|
1026
|
-
|
|
1027
|
-
export function getFollowGraphSummary({
|
|
1028
|
-
account,
|
|
1029
|
-
}: { account?: string } = {}): FollowGraphSummary {
|
|
1030
|
-
const db = getNativeDb();
|
|
1031
|
-
const resolved = resolveAccount(db, account);
|
|
1032
|
-
const followers = readCurrentCount(db, resolved.accountId, "followers");
|
|
1033
|
-
const following = readCurrentCount(db, resolved.accountId, "following");
|
|
1034
|
-
const mutualsRow = db
|
|
1035
|
-
.prepare(
|
|
1036
|
-
`
|
|
1037
|
-
select count(*) as count
|
|
1038
|
-
from follow_edges following
|
|
1039
|
-
join follow_edges followers
|
|
1040
|
-
on followers.account_id = following.account_id
|
|
1041
|
-
and followers.profile_id = following.profile_id
|
|
1042
|
-
and followers.direction = 'followers'
|
|
1043
|
-
and followers.current = 1
|
|
1044
|
-
where
|
|
1045
|
-
following.account_id = ?
|
|
1046
|
-
and following.direction = 'following'
|
|
1047
|
-
and following.current = 1
|
|
1048
|
-
`,
|
|
1049
|
-
)
|
|
1050
|
-
.get(resolved.accountId) as { count: number };
|
|
1051
|
-
|
|
1052
|
-
return {
|
|
1053
|
-
accountId: resolved.accountId,
|
|
1054
|
-
followers,
|
|
1055
|
-
following,
|
|
1056
|
-
mutuals: Number(mutualsRow.count),
|
|
1057
|
-
nonMutualFollowing: following - Number(mutualsRow.count),
|
|
1058
|
-
lastCompleteSnapshots: {
|
|
1059
|
-
followers: getLastSnapshot(
|
|
1060
|
-
db,
|
|
1061
|
-
resolved.accountId,
|
|
1062
|
-
"followers",
|
|
1063
|
-
"complete",
|
|
1064
|
-
),
|
|
1065
|
-
following: getLastSnapshot(
|
|
1066
|
-
db,
|
|
1067
|
-
resolved.accountId,
|
|
1068
|
-
"following",
|
|
1069
|
-
"complete",
|
|
1070
|
-
),
|
|
1071
|
-
},
|
|
1072
|
-
lastIncompleteSnapshots: {
|
|
1073
|
-
followers: getLastSnapshot(
|
|
1074
|
-
db,
|
|
1075
|
-
resolved.accountId,
|
|
1076
|
-
"followers",
|
|
1077
|
-
"incomplete",
|
|
1078
|
-
),
|
|
1079
|
-
following: getLastSnapshot(
|
|
1080
|
-
db,
|
|
1081
|
-
resolved.accountId,
|
|
1082
|
-
"following",
|
|
1083
|
-
"incomplete",
|
|
1084
|
-
),
|
|
1085
|
-
},
|
|
1086
|
-
};
|
|
1087
|
-
}
|