birdclaw 0.8.2 → 0.8.4

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 (247) hide show
  1. package/CHANGELOG.md +33 -0
  2. package/README.md +10 -8
  3. package/bin/birdclaw.mjs +4 -66
  4. package/dist/cli/birdclaw.js +28165 -0
  5. package/dist/client/assets/AvatarChip-DLoqM5xD.js +1 -0
  6. package/dist/client/assets/ProfilePreview-BejAuYsJ.js +1 -0
  7. package/dist/client/assets/SavedTimelineView-BIS7HKpp.js +1 -0
  8. package/dist/client/assets/SmartTimestamp-BR0-9se2.js +1 -0
  9. package/dist/client/assets/TimelineRouteFrame-CyGPj0eM.js +1 -0
  10. package/dist/client/assets/blocks-7UDKQp8H.js +1 -0
  11. package/dist/client/assets/bookmarks-CbBkMvXk.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-ys3XhWG4.js +5 -0
  15. package/dist/client/assets/createLucideIcon-JV8isFFR.js +1 -0
  16. package/dist/client/assets/data-sources-Cm2bpNvg.js +1 -0
  17. package/dist/client/assets/discuss-DTjaOugw.js +1 -0
  18. package/dist/client/assets/dms-CGd6Eogq.js +1 -0
  19. package/dist/client/assets/inbox-BHlGuOy2.js +1 -0
  20. package/dist/client/assets/index-CEIG7S6s.js +31 -0
  21. package/dist/client/assets/index-bpFrQ5yG.css +1 -0
  22. package/dist/client/assets/likes-cLrcAQem.js +1 -0
  23. package/dist/client/assets/links-CM9QO1LC.js +1 -0
  24. package/dist/client/assets/mapbox-BxTlqXt9.js +2 -0
  25. package/dist/client/assets/mapbox-gl-dgf_hX3a.js +3215 -0
  26. package/dist/client/assets/mentions-CxKCgzIf.js +1 -0
  27. package/dist/client/assets/network-map-B5a3whqj.js +2 -0
  28. package/dist/client/assets/profile-analyze-CRtNEwlb.js +1 -0
  29. package/dist/client/assets/profile-hydration-client-YEtJsNTv.js +1 -0
  30. package/dist/client/assets/profiles._handle-C82_HoKX.js +1 -0
  31. package/dist/client/assets/query-client-fYu2rzRC.js +17 -0
  32. package/dist/client/assets/rate-limits-ScaEwdVK.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-q7ZaW4En.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-CeG1O7Py.js +1 -0
  42. package/dist/client/assets/triangle-alert-CBL7s2pf.js +1 -0
  43. package/dist/client/assets/useTimelineRouteData-1HsqgvUi.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-BFvhVdhn.js +198 -0
  48. package/dist/server/assets/ProfilePreview-C6gAor_j.js +752 -0
  49. package/dist/server/assets/SavedTimelineView-BG1jnBp1.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-CU7ilM7U.js +85 -0
  53. package/dist/server/assets/TweetRichText-DhCVv1-6.js +74 -0
  54. package/dist/server/assets/_tanstack-start-manifest_v-C-ZRnG_n.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-BJN8tykY.js +14 -0
  59. package/dist/server/assets/client-stream-contracts-C2xbWVjm.js +769 -0
  60. package/dist/server/assets/data-sources-BnA1Q94p.js +180 -0
  61. package/dist/server/assets/discuss-CJ1dau1j.js +15 -0
  62. package/dist/server/assets/discuss-QTo08NCN.js +338 -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-DBOAtsJU.js +14 -0
  69. package/dist/server/assets/links-DAhSjynu.js +240 -0
  70. package/dist/server/assets/links-DmH5lxHO.js +468 -0
  71. package/dist/server/assets/mentions-BH2MzKtB.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-BP65PED0.js +90 -0
  75. package/dist/server/assets/profile-analyze-C1NoM0tV.js +9 -0
  76. package/dist/server/assets/profile-hydration-client-GJ4kwJS8.js +133 -0
  77. package/dist/server/assets/profiles._handle-DjwlN_ac.js +184 -0
  78. package/dist/server/assets/profiles._handle-DsLNj2Ca.js +12 -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-DqkwSPiM.js +17026 -0
  83. package/dist/server/assets/routes-A9c9ZhwN.js +26 -0
  84. package/dist/server/assets/start-FaDOTmto.js +4 -0
  85. package/dist/server/assets/today-B5hVM57Z.js +17 -0
  86. package/dist/server/assets/today-fvGiZ7ud.js +303 -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-27cxynxa.js +1101 -0
  90. package/dist/server/server.js +5520 -0
  91. package/package.json +27 -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-moderation.ts +0 -210
  100. package/src/cli.ts +0 -2336
  101. package/src/components/AccountSwitcher.tsx +0 -186
  102. package/src/components/AppNav.tsx +0 -118
  103. package/src/components/AvatarChip.tsx +0 -58
  104. package/src/components/BrandMark.tsx +0 -67
  105. package/src/components/ConversationThread.tsx +0 -131
  106. package/src/components/DmWorkspace.tsx +0 -286
  107. package/src/components/EmbeddedTweetCard.tsx +0 -55
  108. package/src/components/FeedState.tsx +0 -147
  109. package/src/components/FloatingPreview.tsx +0 -382
  110. package/src/components/InboxCard.tsx +0 -152
  111. package/src/components/LinkPreviewCard.tsx +0 -292
  112. package/src/components/MarkdownViewer.tsx +0 -777
  113. package/src/components/ProfileAnalysisStream.tsx +0 -428
  114. package/src/components/ProfilePreview.tsx +0 -129
  115. package/src/components/SavedTimelineView.tsx +0 -155
  116. package/src/components/SmartTimestamp.tsx +0 -72
  117. package/src/components/SyncNowButton.tsx +0 -140
  118. package/src/components/ThemeSlider.tsx +0 -80
  119. package/src/components/TimelineCard.tsx +0 -533
  120. package/src/components/TimelineRouteFrame.tsx +0 -186
  121. package/src/components/TweetArticleCard.tsx +0 -66
  122. package/src/components/TweetMediaGrid.tsx +0 -185
  123. package/src/components/TweetRichText.tsx +0 -159
  124. package/src/components/account-selection.ts +0 -64
  125. package/src/components/useTimelineRouteData.ts +0 -323
  126. package/src/lib/account-sync-job.ts +0 -666
  127. package/src/lib/actions-transport.ts +0 -300
  128. package/src/lib/api-client.ts +0 -419
  129. package/src/lib/archive-finder.ts +0 -185
  130. package/src/lib/archive-import.ts +0 -2764
  131. package/src/lib/authored-live.ts +0 -1072
  132. package/src/lib/avatar-cache.ts +0 -349
  133. package/src/lib/backup.ts +0 -2741
  134. package/src/lib/bird-actions.ts +0 -294
  135. package/src/lib/bird-command.ts +0 -130
  136. package/src/lib/bird.ts +0 -1140
  137. package/src/lib/blocklist.ts +0 -136
  138. package/src/lib/blocks-write.ts +0 -167
  139. package/src/lib/blocks.ts +0 -389
  140. package/src/lib/bookmark-sync-job.ts +0 -536
  141. package/src/lib/client-cache.ts +0 -109
  142. package/src/lib/config.ts +0 -224
  143. package/src/lib/conversation-surface.ts +0 -205
  144. package/src/lib/data-sources.ts +0 -219
  145. package/src/lib/db.ts +0 -1135
  146. package/src/lib/dms-live.ts +0 -933
  147. package/src/lib/effect-runtime.ts +0 -45
  148. package/src/lib/follow-graph.ts +0 -1097
  149. package/src/lib/geocoding.ts +0 -296
  150. package/src/lib/http-effect.ts +0 -222
  151. package/src/lib/identity-search-index.ts +0 -369
  152. package/src/lib/inbox.ts +0 -241
  153. package/src/lib/link-index.ts +0 -641
  154. package/src/lib/link-insights.ts +0 -860
  155. package/src/lib/link-preview-metadata.ts +0 -754
  156. package/src/lib/location.ts +0 -137
  157. package/src/lib/media-fetch.ts +0 -860
  158. package/src/lib/media-includes.ts +0 -165
  159. package/src/lib/mention-threads-live.ts +0 -917
  160. package/src/lib/mentions-export.ts +0 -147
  161. package/src/lib/mentions-live.ts +0 -1238
  162. package/src/lib/moderation-target.ts +0 -216
  163. package/src/lib/moderation-write.ts +0 -131
  164. package/src/lib/mutes-write.ts +0 -167
  165. package/src/lib/mutes.ts +0 -84
  166. package/src/lib/ndjson-stream.ts +0 -106
  167. package/src/lib/network-map.ts +0 -382
  168. package/src/lib/openai.ts +0 -117
  169. package/src/lib/period-digest.ts +0 -1552
  170. package/src/lib/present.ts +0 -118
  171. package/src/lib/profile-affiliation-hydration.ts +0 -228
  172. package/src/lib/profile-affiliations.ts +0 -262
  173. package/src/lib/profile-analysis.ts +0 -1272
  174. package/src/lib/profile-bio-entities.ts +0 -318
  175. package/src/lib/profile-history.ts +0 -164
  176. package/src/lib/profile-hydration.ts +0 -180
  177. package/src/lib/profile-replies.ts +0 -77
  178. package/src/lib/profile-resolver.ts +0 -650
  179. package/src/lib/queries.ts +0 -2381
  180. package/src/lib/research.ts +0 -652
  181. package/src/lib/search-discussion.ts +0 -1016
  182. package/src/lib/seed.ts +0 -613
  183. package/src/lib/sqlite.ts +0 -147
  184. package/src/lib/sync-cache.ts +0 -65
  185. package/src/lib/theme-transition.ts +0 -113
  186. package/src/lib/theme.tsx +0 -165
  187. package/src/lib/timeline-collections-live.ts +0 -581
  188. package/src/lib/timeline-live.ts +0 -452
  189. package/src/lib/tweet-account-edges.ts +0 -48
  190. package/src/lib/tweet-lookup.ts +0 -46
  191. package/src/lib/tweet-render.ts +0 -430
  192. package/src/lib/tweet-search-live.ts +0 -566
  193. package/src/lib/types.ts +0 -723
  194. package/src/lib/ui.ts +0 -529
  195. package/src/lib/url-expansion-store.ts +0 -110
  196. package/src/lib/url-expansion.ts +0 -343
  197. package/src/lib/url-safety.ts +0 -220
  198. package/src/lib/web-sync.ts +0 -516
  199. package/src/lib/whois.ts +0 -1079
  200. package/src/lib/x-profile.ts +0 -365
  201. package/src/lib/x-web.ts +0 -199
  202. package/src/lib/xurl-rate-limits.ts +0 -267
  203. package/src/lib/xurl.ts +0 -1677
  204. package/src/routeTree.gen.ts +0 -723
  205. package/src/router.tsx +0 -20
  206. package/src/routes/__root.tsx +0 -79
  207. package/src/routes/api/action.tsx +0 -147
  208. package/src/routes/api/avatar.tsx +0 -50
  209. package/src/routes/api/blocks.tsx +0 -35
  210. package/src/routes/api/conversation.tsx +0 -45
  211. package/src/routes/api/data-sources.tsx +0 -24
  212. package/src/routes/api/inbox.tsx +0 -43
  213. package/src/routes/api/link-insights.tsx +0 -85
  214. package/src/routes/api/link-preview.tsx +0 -47
  215. package/src/routes/api/network-map.tsx +0 -55
  216. package/src/routes/api/period-digest.tsx +0 -101
  217. package/src/routes/api/profile-analysis.tsx +0 -97
  218. package/src/routes/api/profile-hydrate.tsx +0 -54
  219. package/src/routes/api/query.tsx +0 -115
  220. package/src/routes/api/search-discussion.tsx +0 -113
  221. package/src/routes/api/status.tsx +0 -28
  222. package/src/routes/api/sync.tsx +0 -105
  223. package/src/routes/api/xurl-rate-limits.tsx +0 -24
  224. package/src/routes/blocks.tsx +0 -377
  225. package/src/routes/bookmarks.tsx +0 -18
  226. package/src/routes/data-sources.tsx +0 -257
  227. package/src/routes/discuss.tsx +0 -419
  228. package/src/routes/dms.tsx +0 -432
  229. package/src/routes/inbox.tsx +0 -226
  230. package/src/routes/index.tsx +0 -32
  231. package/src/routes/likes.tsx +0 -18
  232. package/src/routes/links.tsx +0 -994
  233. package/src/routes/mentions.tsx +0 -32
  234. package/src/routes/network-map.tsx +0 -1035
  235. package/src/routes/profile-analyze.tsx +0 -112
  236. package/src/routes/profiles.$handle.tsx +0 -228
  237. package/src/routes/rate-limits.tsx +0 -309
  238. package/src/routes/today.tsx +0 -511
  239. package/src/styles.css +0 -257
  240. package/tsconfig.json +0 -29
  241. package/vite.config.ts +0 -29
  242. /package/{public → dist/client}/birdclaw-mark.png +0 -0
  243. /package/{public → dist/client}/favicon.ico +0 -0
  244. /package/{public → dist/client}/logo192.png +0 -0
  245. /package/{public → dist/client}/logo512.png +0 -0
  246. /package/{public → dist/client}/manifest.json +0 -0
  247. /package/{public → dist/client}/robots.txt +0 -0
@@ -1,318 +0,0 @@
1
- import type { Database } from "./sqlite";
2
- import { fetchProfileAffiliations } from "./profile-affiliations";
3
- import type { ProfileBioEntity, ProfileRecord } from "./types";
4
-
5
- interface ExtractedBioEntity {
6
- kind: ProfileBioEntity["kind"];
7
- value: string;
8
- source: string;
9
- raw: Record<string, unknown>;
10
- }
11
-
12
- const HANDLE_RE = /(^|[^\w@./])@([A-Za-z0-9_]{1,15})\b/g;
13
- const DOMAIN_RE =
14
- /\b(?:https?:\/\/)?(?:www\.)?([a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)+)\b/gi;
15
- const COMPANY_RE =
16
- /\b(?:at|with|for|building|founder at|cofounder at|co-founder at|working on)\s+(@[A-Za-z0-9_]{1,15}|[A-Z][A-Za-z0-9&.+ -]{2,48})/g;
17
-
18
- function normalizeDomain(value: string) {
19
- try {
20
- const url = value.startsWith("http")
21
- ? new URL(value)
22
- : new URL(`https://${value}`);
23
- return url.hostname.replace(/^www\./, "").toLowerCase();
24
- } catch {
25
- return value.replace(/^www\./, "").toLowerCase();
26
- }
27
- }
28
-
29
- function addEntity(
30
- entities: Map<string, ExtractedBioEntity>,
31
- entity: ExtractedBioEntity,
32
- ) {
33
- const key = `${entity.kind}:${entity.value.toLowerCase()}`;
34
- if (!entities.has(key)) {
35
- entities.set(key, entity);
36
- }
37
- }
38
-
39
- function addHandleDerivedCompany(
40
- entities: Map<string, ExtractedBioEntity>,
41
- handle: string,
42
- source: string,
43
- ) {
44
- const cleaned = handle.replace(/^@/, "").trim();
45
- if (cleaned.length < 3) {
46
- return;
47
- }
48
- addEntity(entities, {
49
- kind: "company_phrase",
50
- value: cleaned,
51
- source,
52
- raw: { handle },
53
- });
54
- }
55
-
56
- function getUrlEntityExpandedUrls(profile: ProfileRecord) {
57
- const urls: string[] = [];
58
- for (const key of ["url", "description"] as const) {
59
- const block = profile.entities?.[key];
60
- if (!block || typeof block !== "object") {
61
- continue;
62
- }
63
- const entries = (block as { urls?: unknown }).urls;
64
- if (!Array.isArray(entries)) {
65
- continue;
66
- }
67
- for (const entry of entries) {
68
- if (!entry || typeof entry !== "object") {
69
- continue;
70
- }
71
- const record = entry as Record<string, unknown>;
72
- const expanded = record.expandedUrl ?? record.expanded_url ?? record.url;
73
- if (typeof expanded === "string" && expanded.length > 0) {
74
- urls.push(expanded);
75
- }
76
- }
77
- }
78
- return urls;
79
- }
80
-
81
- export function extractProfileBioEntities(profile: ProfileRecord) {
82
- const entities = new Map<string, ExtractedBioEntity>();
83
- const bio = profile.bio ?? "";
84
-
85
- for (const match of bio.matchAll(HANDLE_RE)) {
86
- const handle = `@${match[2]}`;
87
- addEntity(entities, {
88
- kind: "handle",
89
- value: handle,
90
- source: "bio",
91
- raw: { match: match[0].trim() },
92
- });
93
- addHandleDerivedCompany(entities, handle, "bio_handle");
94
- }
95
-
96
- for (const match of bio.matchAll(DOMAIN_RE)) {
97
- addEntity(entities, {
98
- kind: "domain",
99
- value: normalizeDomain(match[1] ?? match[0]),
100
- source: "bio",
101
- raw: { match: match[0] },
102
- });
103
- }
104
-
105
- for (const match of bio.matchAll(COMPANY_RE)) {
106
- const phrase = match[1]?.trim();
107
- if (!phrase) {
108
- continue;
109
- }
110
- addEntity(entities, {
111
- kind: phrase.startsWith("@") ? "handle" : "company_phrase",
112
- value: phrase,
113
- source: "bio_phrase",
114
- raw: { match: match[0] },
115
- });
116
- if (phrase.startsWith("@")) {
117
- addHandleDerivedCompany(entities, phrase, "bio_phrase");
118
- }
119
- }
120
-
121
- for (const url of [profile.url, ...getUrlEntityExpandedUrls(profile)]) {
122
- if (!url) {
123
- continue;
124
- }
125
- addEntity(entities, {
126
- kind: "domain",
127
- value: normalizeDomain(url),
128
- source: "profile_url",
129
- raw: { url },
130
- });
131
- }
132
-
133
- for (const affiliation of profile.affiliations ?? []) {
134
- if (affiliation.organizationName) {
135
- addEntity(entities, {
136
- kind: "company_phrase",
137
- value: affiliation.organizationName,
138
- source: "affiliation",
139
- raw: { organizationProfileId: affiliation.organizationProfileId },
140
- });
141
- }
142
- if (affiliation.organizationHandle) {
143
- const handle = `@${affiliation.organizationHandle.replace(/^@/, "")}`;
144
- addEntity(entities, {
145
- kind: "handle",
146
- value: handle,
147
- source: "affiliation",
148
- raw: { organizationProfileId: affiliation.organizationProfileId },
149
- });
150
- addHandleDerivedCompany(entities, handle, "affiliation");
151
- }
152
- if (affiliation.url) {
153
- addEntity(entities, {
154
- kind: "domain",
155
- value: normalizeDomain(affiliation.url),
156
- source: "affiliation",
157
- raw: { organizationProfileId: affiliation.organizationProfileId },
158
- });
159
- }
160
- }
161
-
162
- return [...entities.values()];
163
- }
164
-
165
- function getProfileRecord(
166
- db: Database,
167
- profileId: string,
168
- ): ProfileRecord | null {
169
- const row = db
170
- .prepare(
171
- `
172
- select id, handle, display_name, bio, followers_count, following_count,
173
- avatar_hue, avatar_url, location, url, verified_type, entities_json, created_at
174
- from profiles
175
- where id = ?
176
- `,
177
- )
178
- .get(profileId) as Record<string, unknown> | undefined;
179
- if (!row) {
180
- return null;
181
- }
182
- let parsedEntities: Record<string, unknown> | undefined;
183
- if (typeof row.entities_json === "string" && row.entities_json.length > 0) {
184
- try {
185
- const parsed = JSON.parse(row.entities_json) as unknown;
186
- if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
187
- parsedEntities = parsed as Record<string, unknown>;
188
- }
189
- } catch {
190
- parsedEntities = undefined;
191
- }
192
- }
193
- const profile: ProfileRecord = {
194
- id: String(row.id),
195
- handle: String(row.handle),
196
- displayName: String(row.display_name),
197
- bio: String(row.bio),
198
- followersCount: Number(row.followers_count ?? 0),
199
- followingCount: Number(row.following_count ?? 0),
200
- avatarHue: Number(row.avatar_hue ?? 0),
201
- ...(typeof row.avatar_url === "string"
202
- ? { avatarUrl: String(row.avatar_url) }
203
- : {}),
204
- ...(typeof row.location === "string" && row.location.length > 0
205
- ? { location: row.location }
206
- : {}),
207
- ...(typeof row.url === "string" && row.url.length > 0
208
- ? { url: row.url }
209
- : {}),
210
- ...(typeof row.verified_type === "string" && row.verified_type.length > 0
211
- ? { verifiedType: row.verified_type }
212
- : {}),
213
- ...(parsedEntities ? { entities: parsedEntities } : {}),
214
- createdAt: String(row.created_at),
215
- };
216
- const affiliations = fetchProfileAffiliations(db, [profileId]).get(profileId);
217
- return affiliations && affiliations.length > 0
218
- ? { ...profile, affiliations, primaryAffiliation: affiliations[0] }
219
- : profile;
220
- }
221
-
222
- export function syncProfileBioEntitiesForProfileId(
223
- db: Database,
224
- profileId: string,
225
- ) {
226
- const profile = getProfileRecord(db, profileId);
227
- if (!profile) {
228
- return [];
229
- }
230
- const entities = extractProfileBioEntities(profile);
231
- const now = new Date().toISOString();
232
- const seen = new Set(
233
- entities.map((entity) => `${entity.kind}:${entity.value}`),
234
- );
235
- const insert = db.prepare(
236
- `
237
- insert into profile_bio_entities (
238
- profile_id, kind, value, source, is_active, first_seen_at, last_seen_at, raw_json
239
- ) values (?, ?, ?, ?, 1, ?, ?, ?)
240
- on conflict(profile_id, kind, value) do update set
241
- source = excluded.source,
242
- is_active = 1,
243
- last_seen_at = excluded.last_seen_at,
244
- raw_json = excluded.raw_json
245
- `,
246
- );
247
-
248
- for (const entity of entities) {
249
- insert.run(
250
- profileId,
251
- entity.kind,
252
- entity.value,
253
- entity.source,
254
- now,
255
- now,
256
- JSON.stringify(entity.raw),
257
- );
258
- }
259
-
260
- const existingRows = db
261
- .prepare(
262
- `
263
- select kind, value
264
- from profile_bio_entities
265
- where profile_id = ?
266
- `,
267
- )
268
- .all(profileId) as Array<{ kind: string; value: string }>;
269
- const deactivate = db.prepare(
270
- `
271
- update profile_bio_entities
272
- set is_active = 0, last_seen_at = ?
273
- where profile_id = ? and kind = ? and value = ?
274
- `,
275
- );
276
- for (const row of existingRows) {
277
- if (!seen.has(`${row.kind}:${row.value}`)) {
278
- deactivate.run(now, profileId, row.kind, row.value);
279
- }
280
- }
281
-
282
- return entities;
283
- }
284
-
285
- export function fetchProfileBioEntities(db: Database, profileIds: string[]) {
286
- if (profileIds.length === 0) {
287
- return new Map<string, ProfileBioEntity[]>();
288
- }
289
- const placeholders = profileIds.map(() => "?").join(",");
290
- const rows = db
291
- .prepare(
292
- `
293
- select profile_id, kind, value, source, is_active, first_seen_at, last_seen_at
294
- from profile_bio_entities
295
- where profile_id in (${placeholders})
296
- and is_active = 1
297
- order by profile_id, kind, value
298
- `,
299
- )
300
- .all(...profileIds) as Array<Record<string, unknown>>;
301
- const result = new Map<string, ProfileBioEntity[]>();
302
- for (const row of rows) {
303
- const profileId = String(row.profile_id);
304
- const entity: ProfileBioEntity = {
305
- profileId,
306
- kind: row.kind as ProfileBioEntity["kind"],
307
- value: String(row.value),
308
- source: String(row.source),
309
- firstSeenAt: String(row.first_seen_at),
310
- lastSeenAt: String(row.last_seen_at),
311
- isActive: Boolean(row.is_active),
312
- };
313
- const existing = result.get(profileId) ?? [];
314
- existing.push(entity);
315
- result.set(profileId, existing);
316
- }
317
- return result;
318
- }
@@ -1,164 +0,0 @@
1
- import { createHash } from "node:crypto";
2
- import type { Database } from "./sqlite";
3
- import { fetchProfileAffiliations } from "./profile-affiliations";
4
- import type { ProfileSnapshot } from "./types";
5
-
6
- function stableJson(value: unknown): string {
7
- if (Array.isArray(value)) {
8
- return `[${value.map(stableJson).join(",")}]`;
9
- }
10
- if (value && typeof value === "object") {
11
- const record = value as Record<string, unknown>;
12
- return `{${Object.keys(record)
13
- .sort()
14
- .map((key) => `${JSON.stringify(key)}:${stableJson(record[key])}`)
15
- .join(",")}}`;
16
- }
17
- return JSON.stringify(value);
18
- }
19
-
20
- function snapshotHash(value: unknown) {
21
- return createHash("sha1").update(stableJson(value)).digest("hex");
22
- }
23
-
24
- function parseJsonArray(value: unknown) {
25
- if (typeof value !== "string" || value.length === 0) {
26
- return [];
27
- }
28
- try {
29
- const parsed = JSON.parse(value) as unknown;
30
- return Array.isArray(parsed) ? parsed : [];
31
- } catch {
32
- return [];
33
- }
34
- }
35
-
36
- function toSnapshot(row: Record<string, unknown>): ProfileSnapshot {
37
- return {
38
- profileId: String(row.profile_id),
39
- snapshotHash: String(row.snapshot_hash),
40
- observedAt: String(row.observed_at),
41
- lastSeenAt: String(row.last_seen_at),
42
- source: String(row.source),
43
- handle: String(row.handle),
44
- displayName: String(row.display_name),
45
- bio: String(row.bio),
46
- location:
47
- typeof row.location === "string" && row.location.length > 0
48
- ? row.location
49
- : null,
50
- url: typeof row.url === "string" && row.url.length > 0 ? row.url : null,
51
- verifiedType:
52
- typeof row.verified_type === "string" && row.verified_type.length > 0
53
- ? row.verified_type
54
- : null,
55
- followersCount: Number(row.followers_count ?? 0),
56
- followingCount: Number(row.following_count ?? 0),
57
- affiliations: parseJsonArray(row.affiliations_json),
58
- };
59
- }
60
-
61
- export function recordProfileSnapshot(
62
- db: Database,
63
- profileId: string,
64
- source = "x_profile",
65
- ) {
66
- const profile = db
67
- .prepare(
68
- `
69
- select id, handle, display_name, bio, location, url, verified_type,
70
- followers_count, following_count, raw_json
71
- from profiles
72
- where id = ?
73
- `,
74
- )
75
- .get(profileId) as Record<string, unknown> | undefined;
76
- if (!profile) {
77
- return null;
78
- }
79
-
80
- const affiliations =
81
- fetchProfileAffiliations(db, [profileId]).get(profileId) ?? [];
82
- const snapshot = {
83
- handle: String(profile.handle),
84
- displayName: String(profile.display_name),
85
- bio: String(profile.bio),
86
- location: profile.location ?? null,
87
- url: profile.url ?? null,
88
- verifiedType: profile.verified_type ?? null,
89
- followersCount: Number(profile.followers_count ?? 0),
90
- followingCount: Number(profile.following_count ?? 0),
91
- affiliations: affiliations.map((affiliation) => ({
92
- organizationProfileId: affiliation.organizationProfileId,
93
- organizationName: affiliation.organizationName ?? null,
94
- organizationHandle: affiliation.organizationHandle ?? null,
95
- url: affiliation.url ?? null,
96
- label: affiliation.label ?? null,
97
- })),
98
- };
99
- const hash = snapshotHash(snapshot);
100
- const now = new Date().toISOString();
101
-
102
- db.prepare(
103
- `
104
- insert into profile_snapshots (
105
- profile_id, snapshot_hash, observed_at, last_seen_at, source, handle,
106
- display_name, bio, location, url, verified_type, followers_count,
107
- following_count, affiliations_json, raw_json
108
- ) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
109
- on conflict(profile_id, snapshot_hash) do update set
110
- last_seen_at = excluded.last_seen_at,
111
- source = excluded.source,
112
- raw_json = excluded.raw_json
113
- `,
114
- ).run(
115
- profileId,
116
- hash,
117
- now,
118
- now,
119
- source,
120
- snapshot.handle,
121
- snapshot.displayName,
122
- snapshot.bio,
123
- snapshot.location,
124
- snapshot.url,
125
- snapshot.verifiedType,
126
- snapshot.followersCount,
127
- snapshot.followingCount,
128
- JSON.stringify(snapshot.affiliations),
129
- typeof profile.raw_json === "string" ? profile.raw_json : "{}",
130
- );
131
-
132
- return hash;
133
- }
134
-
135
- export function fetchProfileSnapshots(
136
- db: Database,
137
- profileIds: string[],
138
- limitPerProfile = 5,
139
- ) {
140
- if (profileIds.length === 0) {
141
- return new Map<string, ProfileSnapshot[]>();
142
- }
143
- const placeholders = profileIds.map(() => "?").join(",");
144
- const rows = db
145
- .prepare(
146
- `
147
- select *
148
- from profile_snapshots
149
- where profile_id in (${placeholders})
150
- order by profile_id, last_seen_at desc
151
- `,
152
- )
153
- .all(...profileIds) as Array<Record<string, unknown>>;
154
- const result = new Map<string, ProfileSnapshot[]>();
155
- for (const row of rows) {
156
- const snapshot = toSnapshot(row);
157
- const existing = result.get(snapshot.profileId) ?? [];
158
- if (existing.length < limitPerProfile) {
159
- existing.push(snapshot);
160
- result.set(snapshot.profileId, existing);
161
- }
162
- }
163
- return result;
164
- }
@@ -1,180 +0,0 @@
1
- import { Effect } from "effect";
2
-
3
- import { normalizeAvatarUrl } from "./avatar-cache";
4
- import { getNativeDb } from "./db";
5
- import { runEffectPromise, tryPromise } from "./effect-runtime";
6
- import {
7
- getTransportStatus,
8
- lookupAuthenticatedUser,
9
- lookupUsersByIds,
10
- } from "./xurl";
11
- import { upsertProfileFromXUser } from "./x-profile";
12
-
13
- export type HydrateProfilesResult = {
14
- ok: true;
15
- hydratedProfiles: number;
16
- hydratedAccount: boolean;
17
- reason?: string;
18
- };
19
-
20
- function asRecord(value: unknown): Record<string, unknown> | null {
21
- return value && typeof value === "object" && !Array.isArray(value)
22
- ? (value as Record<string, unknown>)
23
- : null;
24
- }
25
-
26
- function toInt(value: unknown) {
27
- const parsed = Number(value);
28
- return Number.isFinite(parsed) ? Math.trunc(parsed) : 0;
29
- }
30
-
31
- function toError(error: unknown) {
32
- return error instanceof Error ? error : new Error(String(error));
33
- }
34
-
35
- function trySync<T>(try_: () => T) {
36
- return Effect.try({
37
- try: try_,
38
- catch: toError,
39
- });
40
- }
41
-
42
- export function hydrateProfilesFromXEffect(): Effect.Effect<
43
- HydrateProfilesResult,
44
- unknown
45
- > {
46
- return Effect.gen(function* () {
47
- const transport = yield* tryPromise(() => getTransportStatus());
48
- if (transport.availableTransport !== "xurl") {
49
- return {
50
- ok: true,
51
- hydratedProfiles: 0,
52
- hydratedAccount: false,
53
- reason: transport.statusText,
54
- };
55
- }
56
-
57
- const {
58
- candidateIds,
59
- db,
60
- updateAccount,
61
- updateConversationTitle,
62
- updateLocalProfile,
63
- } = yield* trySync(() => {
64
- const db = getNativeDb();
65
- const candidateRows = db
66
- .prepare(
67
- `
68
- select id
69
- from profiles
70
- where id like 'profile_user_%'
71
- and (followers_count = 0 or bio like 'Imported from archive user %' or handle like 'id%')
72
- order by id asc
73
- `,
74
- )
75
- .all() as Array<{ id: string }>;
76
-
77
- const candidateIds = candidateRows
78
- .map((row) => row.id.replace(/^profile_user_/, ""))
79
- .filter((id) => /^\d+$/.test(id));
80
-
81
- const updateConversationTitle = db.prepare(`
82
- update dm_conversations
83
- set title = ?
84
- where participant_profile_id = ?
85
- `);
86
- const updateLocalProfile = db.prepare(`
87
- update profiles
88
- set handle = ?,
89
- display_name = ?,
90
- bio = ?,
91
- followers_count = ?,
92
- following_count = coalesce(?, following_count),
93
- avatar_url = coalesce(?, avatar_url),
94
- created_at = coalesce(?, created_at)
95
- where id = 'profile_me'
96
- `);
97
- const updateAccount = db.prepare(`
98
- update accounts
99
- set name = ?,
100
- handle = ?,
101
- transport = 'xurl'
102
- where id = 'acct_primary'
103
- `);
104
-
105
- return {
106
- candidateIds,
107
- db,
108
- updateAccount,
109
- updateConversationTitle,
110
- updateLocalProfile,
111
- };
112
- });
113
-
114
- let hydratedProfiles = 0;
115
-
116
- for (let index = 0; index < candidateIds.length; index += 100) {
117
- const batch = candidateIds.slice(index, index + 100);
118
- const users = yield* tryPromise(() => lookupUsersByIds(batch));
119
-
120
- yield* trySync(() => {
121
- db.transaction(() => {
122
- for (const user of users) {
123
- const profileId = `profile_user_${String(user.id ?? "")}`;
124
- if (profileId === "profile_user_") continue;
125
-
126
- const resolved = upsertProfileFromXUser(db, user);
127
- updateConversationTitle.run(
128
- resolved.profile.displayName || resolved.profile.handle,
129
- resolved.profile.id,
130
- );
131
- hydratedProfiles += 1;
132
- }
133
- })();
134
- });
135
- }
136
-
137
- let hydratedAccount = false;
138
- const me = yield* tryPromise(() => lookupAuthenticatedUser()).pipe(
139
- Effect.catchAll(() => Effect.succeed(null)),
140
- );
141
- if (me) {
142
- const metrics = asRecord(me.public_metrics);
143
- yield* trySync(() => {
144
- db.transaction(() => {
145
- updateLocalProfile.run(
146
- String(me.username ?? "steipete").replace(/^@/, ""),
147
- String(me.name ?? "Peter Steinberger"),
148
- String(me.description ?? ""),
149
- toInt(metrics?.followers_count),
150
- metrics && "following_count" in metrics
151
- ? toInt(metrics.following_count)
152
- : null,
153
- normalizeAvatarUrl(me.profile_image_url),
154
- typeof me.created_at === "string" ? me.created_at : null,
155
- );
156
- updateAccount.run(
157
- String(me.name ?? "Peter Steinberger"),
158
- `@${String(me.username ?? "steipete").replace(/^@/, "")}`,
159
- );
160
- })();
161
- });
162
- hydratedAccount = true;
163
- }
164
-
165
- return {
166
- ok: true,
167
- hydratedProfiles,
168
- hydratedAccount,
169
- };
170
- });
171
- }
172
-
173
- export function hydrateProfilesFromX(): Promise<HydrateProfilesResult> {
174
- return runEffectPromise(hydrateProfilesFromXEffect());
175
- }
176
-
177
- export const __test__ = {
178
- asRecord,
179
- toInt,
180
- };