birdclaw 0.8.1 → 0.8.3

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 (103) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/package.json +2 -4
  3. package/scripts/browser-perf.mjs +27 -0
  4. package/src/cli/command-context.ts +17 -0
  5. package/src/cli/register-analysis.ts +500 -0
  6. package/src/cli/register-compose.ts +40 -0
  7. package/src/cli/register-graph.ts +132 -0
  8. package/src/cli/register-inbox.ts +41 -0
  9. package/src/cli/register-storage.ts +106 -0
  10. package/src/cli.ts +30 -750
  11. package/src/components/AccountSwitcher.tsx +7 -15
  12. package/src/components/AvatarChip.tsx +1 -1
  13. package/src/components/AvatarPreload.ts +149 -0
  14. package/src/components/ConversationThread.tsx +4 -0
  15. package/src/components/EmbeddedTweetCard.tsx +4 -0
  16. package/src/components/FloatingPreview.tsx +382 -0
  17. package/src/components/MarkdownCitations.tsx +680 -0
  18. package/src/components/MarkdownViewer.tsx +7 -715
  19. package/src/components/ProfileAnalysisClient.ts +191 -0
  20. package/src/components/ProfileAnalysisStream.tsx +16 -185
  21. package/src/components/ProfilePreview.tsx +41 -81
  22. package/src/components/TimelineCard.tsx +18 -2
  23. package/src/components/TweetArticleCard.tsx +66 -0
  24. package/src/components/TweetRichText.tsx +33 -2
  25. package/src/components/links-controller.ts +162 -0
  26. package/src/components/links-model.ts +198 -0
  27. package/src/components/network-map-controller.ts +84 -0
  28. package/src/components/network-map-model.ts +255 -0
  29. package/src/components/useDebouncedValue.ts +12 -0
  30. package/src/components/useTimelineRouteData.ts +142 -170
  31. package/src/lib/analysis-runtime.ts +238 -0
  32. package/src/lib/api-client.ts +16 -100
  33. package/src/lib/api-contracts.ts +328 -0
  34. package/src/lib/archive-finder.ts +38 -0
  35. package/src/lib/archive-import-plan.ts +102 -0
  36. package/src/lib/archive-import.ts +170 -239
  37. package/src/lib/authored-live.ts +77 -182
  38. package/src/lib/backup.ts +335 -424
  39. package/src/lib/bird.ts +32 -1
  40. package/src/lib/blocks-write.ts +30 -26
  41. package/src/lib/blocks.ts +18 -20
  42. package/src/lib/database-metrics.ts +88 -0
  43. package/src/lib/database-migrations.ts +34 -0
  44. package/src/lib/database-schema.ts +312 -0
  45. package/src/lib/database-writer.ts +69 -0
  46. package/src/lib/db.ts +141 -334
  47. package/src/lib/dm-read-model.ts +533 -0
  48. package/src/lib/dms-live.ts +34 -97
  49. package/src/lib/follow-graph.ts +17 -27
  50. package/src/lib/import-repository.ts +138 -0
  51. package/src/lib/inbox.ts +2 -1
  52. package/src/lib/live-sync-engine.ts +209 -0
  53. package/src/lib/live-transport-gateway.ts +128 -0
  54. package/src/lib/mention-threads-live.ts +90 -176
  55. package/src/lib/mentions-export.ts +1 -1
  56. package/src/lib/mentions-live.ts +57 -225
  57. package/src/lib/moderation-target.ts +15 -4
  58. package/src/lib/moderation-write.ts +1 -1
  59. package/src/lib/mutes-write.ts +30 -26
  60. package/src/lib/openai-response-runtime.ts +251 -0
  61. package/src/lib/paginated-sync.ts +93 -0
  62. package/src/lib/period-digest.ts +116 -304
  63. package/src/lib/profile-analysis.ts +37 -111
  64. package/src/lib/queries.ts +6 -2380
  65. package/src/lib/query-actions.ts +437 -0
  66. package/src/lib/query-client.tsx +47 -0
  67. package/src/lib/query-read-model-shared.ts +52 -0
  68. package/src/lib/query-read-models.ts +5 -0
  69. package/src/lib/query-resource.ts +41 -0
  70. package/src/lib/query-status.ts +164 -0
  71. package/src/lib/research.ts +1 -1
  72. package/src/lib/runtime-services.ts +20 -0
  73. package/src/lib/search-discussion.ts +75 -279
  74. package/src/lib/server-runtime-services.ts +30 -0
  75. package/src/lib/sqlite.ts +53 -14
  76. package/src/lib/streaming-ingestion.ts +240 -0
  77. package/src/lib/sync-cache.ts +6 -1
  78. package/src/lib/sync-plan.ts +175 -0
  79. package/src/lib/timeline-collections-live.ts +83 -256
  80. package/src/lib/timeline-live.ts +86 -235
  81. package/src/lib/timeline-read-model.ts +1191 -0
  82. package/src/lib/tweet-render.ts +78 -0
  83. package/src/lib/tweet-repository.ts +156 -0
  84. package/src/lib/tweet-search-live.ts +63 -166
  85. package/src/lib/types.ts +8 -0
  86. package/src/lib/ui.ts +1 -1
  87. package/src/lib/web-sync.ts +67 -50
  88. package/src/lib/whois.ts +2 -1
  89. package/src/router.tsx +1 -1
  90. package/src/routes/__root.tsx +11 -21
  91. package/src/routes/api/action.tsx +1 -1
  92. package/src/routes/api/conversation.tsx +1 -1
  93. package/src/routes/api/query.tsx +32 -26
  94. package/src/routes/api/status.tsx +6 -2
  95. package/src/routes/api/sync.tsx +5 -2
  96. package/src/routes/blocks.tsx +97 -131
  97. package/src/routes/data-sources.tsx +17 -25
  98. package/src/routes/dms.tsx +168 -167
  99. package/src/routes/inbox.tsx +63 -57
  100. package/src/routes/links.tsx +31 -329
  101. package/src/routes/network-map.tsx +41 -344
  102. package/src/routes/rate-limits.tsx +17 -21
  103. package/vite.config.ts +0 -2
@@ -1,2380 +1,6 @@
1
- import { randomUUID } from "node:crypto";
2
- import { Effect } from "effect";
3
- import type { Database } from "./sqlite";
4
- import { findArchivesEffect } from "./archive-finder";
5
- import { getDb, getNativeDb } from "./db";
6
- import { runEffectPromise, tryPromise } from "./effect-runtime";
7
- import { fetchProfileAffiliations } from "./profile-affiliations";
8
- import { displayUrlForLink, enrichFallbackUrlEntities } from "./tweet-render";
9
- import type {
10
- AccountRecord,
11
- DmConversationItem,
12
- DmMessageItem,
13
- DmQuery,
14
- EmbeddedTweet,
15
- ProfileRecord,
16
- QueryEnvelope,
17
- QueryResponse,
18
- ReplyFilter,
19
- TimelineQualityFilter,
20
- TimelineItem,
21
- TimelineQuery,
22
- TweetEntities,
23
- TweetConversationResponse,
24
- TweetMediaItem,
25
- TweetUrlEntity,
26
- } from "./types";
27
- import {
28
- dmViaXurlEffect,
29
- getTransportStatusEffect,
30
- lookupAuthenticatedUserFresh,
31
- postViaXurlEffect,
32
- replyViaXurlEffect,
33
- } from "./xurl";
34
-
35
- function toError(error: unknown) {
36
- return error instanceof Error ? error : new Error(String(error));
37
- }
38
-
39
- function trySync<T>(try_: () => T) {
40
- return Effect.try({
41
- try: try_,
42
- catch: toError,
43
- });
44
- }
45
-
46
- function e2eFakeLiveWritesEnabled() {
47
- return (
48
- process.env.BIRDCLAW_E2E === "1" &&
49
- process.env.BIRDCLAW_E2E_FAKE_LIVE_WRITES === "1"
50
- );
51
- }
52
-
53
- function liveWritesDisabled() {
54
- return process.env.BIRDCLAW_DISABLE_LIVE_WRITES === "1";
55
- }
56
-
57
- function verifySelectedXurlAccountEffect(accountId: string) {
58
- return Effect.gen(function* () {
59
- if (liveWritesDisabled()) return;
60
- if (e2eFakeLiveWritesEnabled()) return;
61
- const db = yield* trySync(() => getNativeDb());
62
- const account = yield* trySync(
63
- () =>
64
- db
65
- .prepare("select handle, external_user_id from accounts where id = ?")
66
- .get(accountId) as
67
- | { handle: string; external_user_id: string | null }
68
- | undefined,
69
- );
70
- if (!account) {
71
- return yield* Effect.fail(new Error(`Unknown account: ${accountId}`));
72
- }
73
- const authenticated = yield* tryPromise(() =>
74
- lookupAuthenticatedUserFresh(),
75
- );
76
- const authenticatedId =
77
- typeof authenticated?.id === "string" ? authenticated.id : "";
78
- const authenticatedHandle =
79
- typeof authenticated?.username === "string"
80
- ? authenticated.username.replace(/^@/, "")
81
- : "";
82
- const expectedHandle = account.handle.replace(/^@/, "");
83
- if (
84
- (account.external_user_id &&
85
- account.external_user_id !== authenticatedId) ||
86
- (!account.external_user_id &&
87
- (!authenticatedHandle ||
88
- authenticatedHandle.toLowerCase() !== expectedHandle.toLowerCase()))
89
- ) {
90
- return yield* Effect.fail(
91
- new Error(
92
- `xurl is authenticated as @${authenticatedHandle || authenticatedId}, not @${expectedHandle}`,
93
- ),
94
- );
95
- }
96
- });
97
- }
98
-
99
- function getInfluenceScore(followersCount: number) {
100
- return Math.round(Math.log10(followersCount + 10) * 24);
101
- }
102
-
103
- function getMinFollowersForInfluenceScore(score: number) {
104
- if (!Number.isFinite(score)) return undefined;
105
- return Math.max(0, Math.ceil(10 ** ((score - 0.5) / 24) - 10));
106
- }
107
-
108
- function getMaxFollowersForInfluenceScore(score: number) {
109
- if (!Number.isFinite(score)) return undefined;
110
- if (score < getInfluenceScore(0)) return -1;
111
- return Math.max(0, Math.ceil(10 ** ((score + 0.5) / 24) - 10) - 1);
112
- }
113
-
114
- function getInfluenceLabel(score: number) {
115
- if (score >= 150) return "very high";
116
- if (score >= 120) return "high";
117
- if (score >= 90) return "medium";
118
- return "emerging";
119
- }
120
-
121
- function toProfile(row: Record<string, unknown>): ProfileRecord {
122
- const followingCount = Number(row.following_count ?? 0);
123
- const entities = parseJsonField<Record<string, unknown> | undefined>(
124
- row.entities_json,
125
- undefined,
126
- );
127
- return {
128
- id: String(row.id),
129
- handle: String(row.handle),
130
- displayName: String(row.display_name),
131
- bio: String(row.bio),
132
- followersCount: Number(row.followers_count),
133
- ...(Number.isFinite(followingCount) ? { followingCount } : {}),
134
- avatarHue: Number(row.avatar_hue),
135
- avatarUrl:
136
- typeof row.avatar_url === "string" ? String(row.avatar_url) : undefined,
137
- ...(typeof row.location === "string" && row.location.length > 0
138
- ? { location: row.location }
139
- : {}),
140
- ...(typeof row.url === "string" && row.url.length > 0
141
- ? { url: row.url }
142
- : {}),
143
- ...(typeof row.verified_type === "string" && row.verified_type.length > 0
144
- ? { verifiedType: row.verified_type }
145
- : {}),
146
- ...(entities ? { entities } : {}),
147
- createdAt: String(row.created_at),
148
- };
149
- }
150
-
151
- function parseJsonField<T>(value: unknown, fallback: T): T {
152
- if (typeof value !== "string" || value.length === 0) {
153
- return fallback;
154
- }
155
-
156
- try {
157
- return JSON.parse(value) as T;
158
- } catch {
159
- return fallback;
160
- }
161
- }
162
-
163
- function normalizeProfileHandle(handle: string) {
164
- return handle.replace(/^@/, "").toLowerCase();
165
- }
166
-
167
- function avatarHueForHandle(handle: string) {
168
- let hash = 0;
169
- for (const character of handle) {
170
- hash = (hash * 31 + character.charCodeAt(0)) % 360;
171
- }
172
- return hash;
173
- }
174
-
175
- function fallbackProfileForHandle(handle: string): ProfileRecord {
176
- const normalized = normalizeProfileHandle(handle);
177
- return {
178
- id: `profile_handle_${normalized}`,
179
- handle: normalized,
180
- displayName: `@${normalized}`,
181
- bio: "",
182
- followersCount: 0,
183
- avatarHue: avatarHueForHandle(normalized),
184
- createdAt: new Date(0).toISOString(),
185
- };
186
- }
187
-
188
- type ProfileByHandleCache = Map<string, ProfileRecord | null>;
189
-
190
- function getProfileByHandle(
191
- db: Database,
192
- cache: ProfileByHandleCache,
193
- handle: string,
194
- profiles: Record<string, ProfileRecord> = {},
195
- ) {
196
- const normalized = normalizeProfileHandle(handle);
197
- const inlineProfile = Object.values(profiles).find(
198
- (profile) => normalizeProfileHandle(profile.handle) === normalized,
199
- );
200
- if (inlineProfile) {
201
- return inlineProfile;
202
- }
203
-
204
- if (cache.has(normalized)) {
205
- return cache.get(normalized) ?? fallbackProfileForHandle(normalized);
206
- }
207
-
208
- const row = db
209
- .prepare(
210
- `
211
- select *
212
- from profiles
213
- where lower(handle) = lower(?)
214
- limit 1
215
- `,
216
- )
217
- .get(normalized) as Record<string, unknown> | undefined;
218
- const profile = row ? toProfile(row) : null;
219
- cache.set(normalized, profile);
220
- return profile ?? fallbackProfileForHandle(normalized);
221
- }
222
-
223
- function spansOverlap(
224
- leftStart: number,
225
- leftEnd: number,
226
- rightStart: number,
227
- rightEnd: number,
228
- ) {
229
- return leftStart < rightEnd && rightStart < leftEnd;
230
- }
231
-
232
- function enrichFallbackMentionEntities(
233
- text: string,
234
- entities: TweetEntities,
235
- resolveProfileByHandle: (handle: string) => ProfileRecord,
236
- ): TweetEntities {
237
- const existingMentions = entities.mentions ?? [];
238
- const occupied = [
239
- ...existingMentions,
240
- ...(entities.urls ?? []),
241
- ...(entities.hashtags ?? []),
242
- ].map((entry) => ({ start: entry.start, end: entry.end }));
243
- const fallbackMentions = [];
244
- const mentionPattern = /(^|[^\w@])@([A-Za-z0-9_]{1,15})/g;
245
-
246
- for (const match of text.matchAll(mentionPattern)) {
247
- const prefix = match[1] ?? "";
248
- const username = match[2];
249
- if (!username) continue;
250
- const start = (match.index ?? 0) + prefix.length;
251
- const end = start + username.length + 1;
252
- if (
253
- occupied.some((entry) => spansOverlap(start, end, entry.start, entry.end))
254
- ) {
255
- continue;
256
- }
257
-
258
- const profile = resolveProfileByHandle(username);
259
- fallbackMentions.push({
260
- username,
261
- id: profile.id,
262
- start,
263
- end,
264
- profile,
265
- });
266
- occupied.push({ start, end });
267
- }
268
-
269
- if (fallbackMentions.length === 0) {
270
- return entities;
271
- }
272
-
273
- return {
274
- ...entities,
275
- mentions: [...existingMentions, ...fallbackMentions].sort(
276
- (left, right) => left.start - right.start,
277
- ),
278
- };
279
- }
280
-
281
- function toFtsSearchQuery(value: string) {
282
- const terms = value.match(/[\p{L}\p{N}_]+/gu) ?? [];
283
- return terms
284
- .map((term) => term.trim())
285
- .filter((term) => term.length > 0)
286
- .map((term) => `"${term.replaceAll('"', '""')}"`)
287
- .join(" ");
288
- }
289
-
290
- function enrichEntities(
291
- entities: TweetEntities,
292
- profiles: Record<string, ProfileRecord>,
293
- resolveProfileByHandle?: (handle: string) => ProfileRecord,
294
- ): TweetEntities {
295
- const mentions = entities.mentions?.map((mention) => {
296
- const profile =
297
- (mention.id ? profiles[mention.id] : undefined) ??
298
- Object.values(profiles).find(
299
- (candidate) =>
300
- normalizeProfileHandle(candidate.handle) ===
301
- normalizeProfileHandle(mention.username),
302
- ) ??
303
- resolveProfileByHandle?.(mention.username);
304
- return profile ? { ...mention, profile } : mention;
305
- });
306
-
307
- return {
308
- ...entities,
309
- ...(mentions ? { mentions } : {}),
310
- };
311
- }
312
-
313
- type UrlExpansionCache = Map<
314
- string,
315
- | (Pick<TweetUrlEntity, "expandedUrl" | "displayUrl"> &
316
- Partial<
317
- Pick<TweetUrlEntity, "title" | "description" | "imageUrl" | "siteName">
318
- >)
319
- | null
320
- >;
321
-
322
- function getUrlExpansion(
323
- db: Database,
324
- cache: UrlExpansionCache,
325
- rawUrl: string,
326
- ) {
327
- if (cache.has(rawUrl)) {
328
- return cache.get(rawUrl);
329
- }
330
-
331
- const row = db
332
- .prepare(
333
- `
334
- select expanded_url, final_url, title, description, image_url, site_name
335
- from url_expansions
336
- where short_url = ?
337
- and status = 'hit'
338
- `,
339
- )
340
- .get(rawUrl) as
341
- | {
342
- expanded_url: string;
343
- final_url: string;
344
- title: string | null;
345
- description: string | null;
346
- image_url: string | null;
347
- site_name: string | null;
348
- }
349
- | undefined;
350
- if (!row) {
351
- cache.set(rawUrl, null);
352
- return null;
353
- }
354
-
355
- const expandedUrl = row.final_url || row.expanded_url || rawUrl;
356
- const expansion = {
357
- expandedUrl,
358
- displayUrl: displayUrlForLink(expandedUrl),
359
- ...(row.title ? { title: row.title } : {}),
360
- ...(row.description ? { description: row.description } : {}),
361
- ...(row.image_url ? { imageUrl: row.image_url } : {}),
362
- ...(row.site_name ? { siteName: row.site_name } : {}),
363
- };
364
- cache.set(rawUrl, expansion);
365
- return expansion;
366
- }
367
-
368
- function enrichTimelineEntities(
369
- db: Database,
370
- urlExpansionCache: UrlExpansionCache,
371
- text: string,
372
- entities: TweetEntities,
373
- profiles: Record<string, ProfileRecord>,
374
- resolveProfileByHandle?: (handle: string) => ProfileRecord,
375
- ): TweetEntities {
376
- const withUrls = enrichFallbackUrlEntities(
377
- text,
378
- enrichEntities(entities, profiles, resolveProfileByHandle),
379
- (rawUrl) => getUrlExpansion(db, urlExpansionCache, rawUrl),
380
- );
381
- return resolveProfileByHandle
382
- ? enrichFallbackMentionEntities(text, withUrls, resolveProfileByHandle)
383
- : withUrls;
384
- }
385
-
386
- function buildEmbeddedTweet(
387
- db: Database,
388
- urlExpansionCache: UrlExpansionCache,
389
- row: Record<string, unknown>,
390
- prefix: string,
391
- resolveProfileByHandle?: (handle: string) => ProfileRecord,
392
- ): EmbeddedTweet | null {
393
- if (!row[`${prefix}id`]) {
394
- return null;
395
- }
396
-
397
- const author = toProfile({
398
- id: row[`${prefix}profile_id`],
399
- handle: row[`${prefix}handle`],
400
- display_name: row[`${prefix}display_name`],
401
- bio: row[`${prefix}bio`],
402
- followers_count: row[`${prefix}followers_count`],
403
- following_count: row[`${prefix}following_count`],
404
- avatar_hue: row[`${prefix}avatar_hue`],
405
- avatar_url: row[`${prefix}avatar_url`],
406
- created_at: row[`${prefix}profile_created_at`],
407
- });
408
-
409
- const text = String(row[`${prefix}text`] ?? "");
410
- return {
411
- id: String(row[`${prefix}id`]),
412
- text,
413
- createdAt: String(row[`${prefix}created_at`] ?? new Date(0).toISOString()),
414
- replyToId:
415
- typeof row[`${prefix}reply_to_id`] === "string"
416
- ? String(row[`${prefix}reply_to_id`])
417
- : null,
418
- ...(row[`${prefix}is_replied`] === undefined
419
- ? {}
420
- : { isReplied: Boolean(row[`${prefix}is_replied`]) }),
421
- ...(row[`${prefix}like_count`] === undefined
422
- ? {}
423
- : { likeCount: Number(row[`${prefix}like_count`]) }),
424
- ...(row[`${prefix}media_count`] === undefined
425
- ? {}
426
- : { mediaCount: Number(row[`${prefix}media_count`]) }),
427
- ...(row[`${prefix}bookmarked`] === undefined
428
- ? {}
429
- : { bookmarked: Boolean(row[`${prefix}bookmarked`]) }),
430
- ...(row[`${prefix}liked`] === undefined
431
- ? {}
432
- : { liked: Boolean(row[`${prefix}liked`]) }),
433
- author,
434
- entities: enrichTimelineEntities(
435
- db,
436
- urlExpansionCache,
437
- text,
438
- parseJsonField<TweetEntities>(row[`${prefix}entities_json`], {}),
439
- {
440
- [author.id]: author,
441
- },
442
- resolveProfileByHandle,
443
- ),
444
- media: parseJsonField<TweetMediaItem[]>(row[`${prefix}media_json`], []),
445
- };
446
- }
447
-
448
- function getRetweetedTweetIdFromRaw(rawJson: unknown) {
449
- const raw = parseJsonField<Record<string, unknown>>(rawJson, {});
450
- const directCandidates = [
451
- raw.retweeted_tweet_id,
452
- raw.retweetedTweetId,
453
- raw.retweetedStatusId,
454
- raw.retweeted_status_id_str,
455
- ];
456
- for (const candidate of directCandidates) {
457
- if (typeof candidate === "string" && candidate.length > 0) {
458
- return candidate;
459
- }
460
- }
461
-
462
- const nestedCandidates = [raw.retweetedTweet, raw.retweeted_status];
463
- for (const nested of nestedCandidates) {
464
- if (nested && typeof nested === "object") {
465
- const record = nested as Record<string, unknown>;
466
- for (const key of ["id", "id_str"]) {
467
- if (typeof record[key] === "string" && record[key].length > 0) {
468
- return record[key];
469
- }
470
- }
471
- }
472
- }
473
-
474
- const references = [raw.referenced_tweets, raw.referencedTweets].find(
475
- (value): value is unknown[] => Array.isArray(value),
476
- );
477
- for (const reference of references ?? []) {
478
- if (!reference || typeof reference !== "object") continue;
479
- const record = reference as Record<string, unknown>;
480
- if (record.type === "retweeted" && typeof record.id === "string") {
481
- return record.id;
482
- }
483
- }
484
-
485
- return null;
486
- }
487
-
488
- function parseManualRetweet(text: string) {
489
- const match = text.match(/^RT\s+@([A-Za-z0-9_]{1,15}):\s*([\s\S]+)$/);
490
- if (!match?.[1] || !match[2]) {
491
- return null;
492
- }
493
- return {
494
- handle: match[1],
495
- text: match[2].trim(),
496
- };
497
- }
498
-
499
- function buildRetweetedTweet(
500
- db: Database,
501
- urlExpansionCache: UrlExpansionCache,
502
- row: Record<string, unknown>,
503
- resolveProfileByHandle: (handle: string) => ProfileRecord,
504
- ) {
505
- const retweetedId = getRetweetedTweetIdFromRaw(row.edge_raw_json);
506
- const accountId = String(row.account_id);
507
- if (retweetedId) {
508
- const tweet = getTweetById(
509
- db,
510
- urlExpansionCache,
511
- retweetedId,
512
- resolveProfileByHandle,
513
- accountId,
514
- );
515
- if (tweet) {
516
- return tweet;
517
- }
518
- }
519
-
520
- const manualRetweet = parseManualRetweet(String(row.text ?? ""));
521
- if (!manualRetweet) {
522
- return null;
523
- }
524
-
525
- const author = resolveProfileByHandle(manualRetweet.handle);
526
- return {
527
- id: `${String(row.id)}:retweeted`,
528
- text: manualRetweet.text,
529
- createdAt: String(row.created_at ?? new Date(0).toISOString()),
530
- replyToId: null,
531
- isReplied: Boolean(row.is_replied),
532
- likeCount: Number(row.like_count ?? 0),
533
- mediaCount: 0,
534
- bookmarked: Boolean(row.bookmarked),
535
- liked: Boolean(row.liked),
536
- author,
537
- entities: enrichTimelineEntities(
538
- db,
539
- urlExpansionCache,
540
- manualRetweet.text,
541
- {},
542
- { [author.id]: author },
543
- resolveProfileByHandle,
544
- ),
545
- media: [],
546
- };
547
- }
548
-
549
- function buildReplyClause(replyFilter: ReplyFilter) {
550
- if (replyFilter === "replied") {
551
- return " and is_replied = 1";
552
- }
553
- if (replyFilter === "unreplied") {
554
- return " and is_replied = 0";
555
- }
556
- return "";
557
- }
558
-
559
- function normalizeLowQualityThreshold(threshold: number | undefined) {
560
- const value = threshold ?? 50;
561
- if (!Number.isFinite(value) || !Number.isInteger(value) || value < 0) {
562
- throw new Error("lowQualityThreshold must be a non-negative integer");
563
- }
564
- return value;
565
- }
566
-
567
- function buildTimelineQualityClause(
568
- qualityFilter: TimelineQualityFilter,
569
- lowQualityThreshold: number,
570
- ) {
571
- if (qualityFilter === "all") {
572
- return { sql: "", params: [] };
573
- }
574
-
575
- return {
576
- sql: `
577
- and not (
578
- t.text like 'RT @%'
579
- or (
580
- t.like_count < ?
581
- and (
582
- (
583
- length(trim(replace(t.text, 'https://t.co/', ''))) < 16
584
- and t.media_count = 0
585
- )
586
- or (
587
- t.text like '@%'
588
- and length(trim(t.text)) < 60
589
- )
590
- or (
591
- t.text glob '*https://t.co/*'
592
- and t.media_count = 0
593
- and length(trim(replace(t.text, 'https://t.co/', ''))) < 45
594
- )
595
- )
596
- )
597
- )
598
- `,
599
- params: [lowQualityThreshold],
600
- };
601
- }
602
-
603
- function getTimelineQualityReason(
604
- row: Record<string, unknown>,
605
- lowQualityThreshold: number,
606
- ) {
607
- const text = String(row.text);
608
- const trimmed = text.trim();
609
- const strippedShortUrlText = text.replaceAll("https://t.co/", "").trim();
610
- const likeCount = Number(row.like_count);
611
- const mediaCount = Number(row.media_count);
612
-
613
- if (text.startsWith("RT @")) {
614
- return "drop:rt";
615
- }
616
-
617
- if (likeCount < lowQualityThreshold) {
618
- if (text.startsWith("@") && trimmed.length < 60) {
619
- return "drop:short-reply";
620
- }
621
- if (
622
- text.includes("https://t.co/") &&
623
- mediaCount === 0 &&
624
- strippedShortUrlText.length < 45
625
- ) {
626
- return "drop:short-link-only";
627
- }
628
- if (strippedShortUrlText.length < 16 && mediaCount === 0) {
629
- return "drop:short-text";
630
- }
631
- }
632
-
633
- if (mediaCount > 0) {
634
- return "keep:has-media";
635
- }
636
- if (likeCount >= lowQualityThreshold) {
637
- return "keep:high-likes";
638
- }
639
- return "keep:long-text";
640
- }
641
-
642
- function countTimelineEdges(db: Database, kind: "home" | "mention") {
643
- const row = db
644
- .prepare(
645
- `
646
- select count(distinct tweet_id) as count
647
- from (
648
- select edge.tweet_id
649
- from tweet_account_edges edge
650
- where edge.kind = ?
651
- and exists (
652
- select 1
653
- from tweets t
654
- where t.id = edge.tweet_id
655
- )
656
- union all
657
- select legacy.id as tweet_id
658
- from tweets legacy
659
- where legacy.kind = ?
660
- and not exists (
661
- select 1
662
- from tweet_account_edges edge
663
- where edge.account_id = legacy.account_id
664
- and edge.tweet_id = legacy.id
665
- and edge.kind = legacy.kind
666
- )
667
- )
668
- `,
669
- )
670
- .get(kind, kind) as { count: number | bigint } | undefined;
671
- return Number(row?.count ?? 0);
672
- }
673
-
674
- const RECENT_TIMELINE_EDGE_CANDIDATES = 5000;
675
-
676
- function getAccountProfileMeta(
677
- db: Database,
678
- account: { handle: string; external_user_id: string | null },
679
- ) {
680
- const handle = account.handle.replace(/^@/, "");
681
- const externalProfileId = account.external_user_id
682
- ? `profile_user_${account.external_user_id}`
683
- : "";
684
- return db
685
- .prepare(
686
- `
687
- select id, avatar_hue, avatar_url
688
- from profiles
689
- where id = ?
690
- or lower(handle) = lower(?)
691
- order by case
692
- when id = 'profile_me' then 0
693
- when id = ? then 1
694
- else 2
695
- end
696
- limit 1
697
- `,
698
- )
699
- .get(externalProfileId, handle, externalProfileId) as
700
- | { id: string; avatar_hue: number; avatar_url: string | null }
701
- | undefined;
702
- }
703
-
704
- export function getQueryEnvelopeEffect(): Effect.Effect<
705
- QueryEnvelope,
706
- unknown
707
- > {
708
- return Effect.gen(function* () {
709
- const db = yield* trySync(() => getDb());
710
- const nativeDb = yield* trySync(() => getNativeDb());
711
- const homeCount = yield* trySync(() =>
712
- countTimelineEdges(nativeDb, "home"),
713
- );
714
- const mentionCount = yield* trySync(() =>
715
- countTimelineEdges(nativeDb, "mention"),
716
- );
717
- const counts = yield* Effect.all({
718
- dms: tryPromise(() =>
719
- db
720
- .selectFrom("dm_conversations")
721
- .select((eb) => eb.fn.countAll().as("count"))
722
- .executeTakeFirstOrThrow(),
723
- ),
724
- needsReply: tryPromise(() =>
725
- db
726
- .selectFrom("dm_conversations")
727
- .select((eb) => eb.fn.countAll().as("count"))
728
- .where("needs_reply", "=", 1)
729
- .executeTakeFirstOrThrow(),
730
- ),
731
- accounts: tryPromise(() =>
732
- db
733
- .selectFrom("accounts")
734
- .selectAll()
735
- .orderBy("is_default", "desc")
736
- .orderBy("name", "asc")
737
- .execute(),
738
- ),
739
- archives: findArchivesEffect(),
740
- transport: getTransportStatusEffect(),
741
- });
742
-
743
- return {
744
- stats: {
745
- home: homeCount,
746
- mentions: mentionCount,
747
- dms: Number(counts.dms.count),
748
- needsReply: Number(counts.needsReply.count),
749
- inbox: mentionCount + Number(counts.needsReply.count),
750
- },
751
- accounts: counts.accounts.map((row) => {
752
- const profile = getAccountProfileMeta(nativeDb, row);
753
- return {
754
- id: row.id,
755
- name: row.name,
756
- handle: row.handle,
757
- externalUserId: row.external_user_id,
758
- ...(profile
759
- ? {
760
- profileId: profile.id,
761
- avatarHue: Number(profile.avatar_hue),
762
- ...(profile.avatar_url
763
- ? { avatarUrl: profile.avatar_url }
764
- : {}),
765
- }
766
- : {}),
767
- transport: row.transport,
768
- isDefault: row.is_default,
769
- createdAt: row.created_at,
770
- };
771
- }) satisfies AccountRecord[],
772
- archives: counts.archives,
773
- transport: counts.transport,
774
- };
775
- });
776
- }
777
-
778
- export function getQueryEnvelope(): Promise<QueryEnvelope> {
779
- return runEffectPromise(getQueryEnvelopeEffect());
780
- }
781
-
782
- export function listTimelineItems({
783
- resource,
784
- account,
785
- search,
786
- replyFilter = "all",
787
- since,
788
- until,
789
- untilId,
790
- includeReplies = true,
791
- qualityFilter = "all",
792
- lowQualityThreshold,
793
- includeQualityReason = false,
794
- likedOnly = false,
795
- bookmarkedOnly = false,
796
- limit = 18,
797
- }: TimelineQuery): TimelineItem[] {
798
- const db = getNativeDb();
799
- const kind = resource === "mentions" ? "mention" : resource;
800
- const params: Array<string | number> = [];
801
- const normalizedLowQualityThreshold =
802
- normalizeLowQualityThreshold(lowQualityThreshold);
803
- const shouldDedupeAcrossAccounts = !account || account === "all";
804
- let timelineEdgesCte = `
805
- with timeline_edges as (
806
- select account_id, tweet_id, kind, raw_json
807
- from tweet_account_edges
808
- where kind = ?
809
- union all
810
- select legacy.account_id, legacy.id as tweet_id, legacy.kind, '{}' as raw_json
811
- from tweets legacy
812
- where legacy.kind = ?
813
- and not exists (
814
- select 1
815
- from tweet_account_edges edge
816
- where edge.account_id = legacy.account_id
817
- and edge.tweet_id = legacy.id
818
- and edge.kind = legacy.kind
819
- )
820
- )
821
- `;
822
- const unwindowedTimelineEdgesCte = timelineEdgesCte;
823
- let usedRecentEdgeWindow = false;
824
- let join = "";
825
- let where = "where t.kind = ?";
826
- let searchSnippetSelect = "";
827
-
828
- const canUseRecentEdgeWindow =
829
- !likedOnly &&
830
- !bookmarkedOnly &&
831
- !account &&
832
- !search?.trim() &&
833
- replyFilter === "all" &&
834
- !since?.trim() &&
835
- !until?.trim() &&
836
- includeReplies &&
837
- qualityFilter === "all";
838
-
839
- if (likedOnly || bookmarkedOnly) {
840
- if (likedOnly && bookmarkedOnly) {
841
- timelineEdgesCte = `
842
- with timeline_edges as (
843
- select likes.account_id, likes.tweet_id, 'home' as kind, likes.raw_json
844
- from tweet_collections likes
845
- join tweet_collections bookmarks
846
- on bookmarks.account_id = likes.account_id
847
- and bookmarks.tweet_id = likes.tweet_id
848
- and bookmarks.kind = 'bookmarks'
849
- where likes.kind = 'likes'
850
- union all
851
- select legacy.account_id, legacy.id as tweet_id, 'home' as kind, '{}' as raw_json
852
- from tweets legacy
853
- where legacy.liked = 1
854
- and legacy.bookmarked = 1
855
- and not exists (
856
- select 1
857
- from tweet_collections collection
858
- where collection.account_id = legacy.account_id
859
- and collection.tweet_id = legacy.id
860
- and collection.kind in ('likes', 'bookmarks')
861
- )
862
- )
863
- `;
864
- } else {
865
- const collectionKind = likedOnly ? "likes" : "bookmarks";
866
- const legacyColumn = likedOnly ? "liked" : "bookmarked";
867
- timelineEdgesCte = `
868
- with timeline_edges as (
869
- select account_id, tweet_id, 'home' as kind, raw_json
870
- from tweet_collections
871
- where kind = ?
872
- union all
873
- select legacy.account_id, legacy.id as tweet_id, 'home' as kind, '{}' as raw_json
874
- from tweets legacy
875
- where legacy.${legacyColumn} = 1
876
- and not exists (
877
- select 1
878
- from tweet_collections collection
879
- where collection.account_id = legacy.account_id
880
- and collection.tweet_id = legacy.id
881
- and collection.kind = ?
882
- )
883
- )
884
- `;
885
- params.push(collectionKind, collectionKind);
886
- }
887
- where = "where 1 = 1";
888
- } else if (canUseRecentEdgeWindow) {
889
- usedRecentEdgeWindow = true;
890
- timelineEdgesCte = `
891
- with timeline_edges as (
892
- select account_id, tweet_id, kind, raw_json
893
- from tweet_account_edges
894
- where kind = ?
895
- and tweet_id in (
896
- select id
897
- from tweets
898
- order by created_at desc
899
- limit ?
900
- )
901
- union all
902
- select legacy.account_id, legacy.id as tweet_id, legacy.kind, '{}' as raw_json
903
- from tweets legacy
904
- where legacy.kind = ?
905
- and legacy.id in (
906
- select id
907
- from tweets
908
- order by created_at desc
909
- limit ?
910
- )
911
- and not exists (
912
- select 1
913
- from tweet_account_edges edge
914
- where edge.account_id = legacy.account_id
915
- and edge.tweet_id = legacy.id
916
- and edge.kind = legacy.kind
917
- )
918
- )
919
- `;
920
- const candidateLimit = Math.max(
921
- RECENT_TIMELINE_EDGE_CANDIDATES,
922
- limit * 50,
923
- );
924
- params.push(kind, candidateLimit, kind, candidateLimit);
925
- where = "where e.kind = ?";
926
- params.push(kind);
927
- } else {
928
- params.push(kind, kind);
929
- where = "where e.kind = ?";
930
- params.push(kind);
931
- }
932
-
933
- if (account && account !== "all") {
934
- where += " and e.account_id = ?";
935
- params.push(account);
936
- }
937
-
938
- if (shouldDedupeAcrossAccounts) {
939
- where += `
940
- and e.account_id = (
941
- select e2.account_id
942
- from timeline_edges e2
943
- join accounts a2 on a2.id = e2.account_id
944
- where e2.tweet_id = e.tweet_id
945
- and e2.kind = e.kind
946
- order by a2.is_default desc, e2.account_id asc
947
- limit 1
948
- )
949
- `;
950
- }
951
-
952
- where += buildReplyClause(replyFilter).replaceAll(
953
- "is_replied",
954
- "t.is_replied",
955
- );
956
- const qualityClause = buildTimelineQualityClause(
957
- qualityFilter,
958
- normalizedLowQualityThreshold,
959
- );
960
- where += qualityClause.sql;
961
- params.push(...qualityClause.params);
962
-
963
- if (!includeReplies) {
964
- where += " and t.text not like '@%'";
965
- }
966
-
967
- if (since?.trim()) {
968
- where += " and t.created_at >= ?";
969
- params.push(since.trim());
970
- }
971
-
972
- if (until?.trim()) {
973
- // Deterministic keyset cursor: page on (created_at, id) so rows that share
974
- // the boundary timestamp are not skipped. Uses the same text comparison as
975
- // the `order by t.created_at desc, t.id desc` below, which is a total order
976
- // because t.id is unique.
977
- if (untilId?.trim()) {
978
- where += " and (t.created_at < ? or (t.created_at = ? and t.id < ?))";
979
- params.push(until.trim(), until.trim(), untilId.trim());
980
- } else {
981
- where += " and t.created_at < ?";
982
- params.push(until.trim());
983
- }
984
- }
985
-
986
- const ftsSearch = search?.trim() ? toFtsSearchQuery(search) : "";
987
- if (ftsSearch) {
988
- join += " join tweets_fts on tweets_fts.tweet_id = t.id ";
989
- where += " and tweets_fts.text match ?";
990
- searchSnippetSelect =
991
- ", snippet(tweets_fts, 1, '<mark>', '</mark>', '...', 16) as search_snippet";
992
- params.push(ftsSearch);
993
- }
994
-
995
- params.push(limit);
996
-
997
- const buildTimelineSelectSql = (timelineEdgesSql: string) => `
998
- ${timelineEdgesSql}
999
- select
1000
- t.id,
1001
- e.account_id,
1002
- a.handle as account_handle,
1003
- e.kind,
1004
- e.raw_json as edge_raw_json,
1005
- t.text,
1006
- t.created_at,
1007
- t.reply_to_id,
1008
- t.is_replied,
1009
- t.like_count,
1010
- t.media_count,
1011
- case
1012
- when exists (
1013
- select 1 from tweet_collections collection
1014
- where collection.account_id = e.account_id
1015
- and collection.tweet_id = t.id
1016
- and collection.kind = 'bookmarks'
1017
- ) then 1
1018
- when t.account_id = e.account_id and t.bookmarked = 1 then 1
1019
- else 0
1020
- end as bookmarked,
1021
- case
1022
- when exists (
1023
- select 1 from tweet_collections collection
1024
- where collection.account_id = e.account_id
1025
- and collection.tweet_id = t.id
1026
- and collection.kind = 'likes'
1027
- ) then 1
1028
- when t.account_id = e.account_id and t.liked = 1 then 1
1029
- else 0
1030
- end as liked,
1031
- t.entities_json,
1032
- t.media_json,
1033
- t.quoted_tweet_id,
1034
- p.id as profile_id,
1035
- p.handle,
1036
- p.display_name,
1037
- p.bio,
1038
- p.followers_count,
1039
- p.following_count,
1040
- p.avatar_hue,
1041
- p.avatar_url,
1042
- p.location as profile_location,
1043
- p.url as profile_url,
1044
- p.verified_type as profile_verified_type,
1045
- p.entities_json as profile_entities_json,
1046
- p.created_at as profile_created_at,
1047
- rt.id as reply_id,
1048
- rt.text as reply_text,
1049
- rt.created_at as reply_created_at,
1050
- rt.reply_to_id as reply_reply_to_id,
1051
- rt.entities_json as reply_entities_json,
1052
- rt.media_json as reply_media_json,
1053
- rp.id as reply_profile_id,
1054
- rp.handle as reply_handle,
1055
- rp.display_name as reply_display_name,
1056
- rp.bio as reply_bio,
1057
- rp.followers_count as reply_followers_count,
1058
- rp.following_count as reply_following_count,
1059
- rp.avatar_hue as reply_avatar_hue,
1060
- rp.avatar_url as reply_avatar_url,
1061
- rp.created_at as reply_profile_created_at,
1062
- qt.id as quoted_id,
1063
- qt.text as quoted_text,
1064
- qt.created_at as quoted_created_at,
1065
- qt.reply_to_id as quoted_reply_to_id,
1066
- qt.entities_json as quoted_entities_json,
1067
- qt.media_json as quoted_media_json,
1068
- qp.id as quoted_profile_id,
1069
- qp.handle as quoted_handle,
1070
- qp.display_name as quoted_display_name,
1071
- qp.bio as quoted_bio,
1072
- qp.followers_count as quoted_followers_count,
1073
- qp.following_count as quoted_following_count,
1074
- qp.avatar_hue as quoted_avatar_hue,
1075
- qp.avatar_url as quoted_avatar_url,
1076
- qp.created_at as quoted_profile_created_at
1077
- ${searchSnippetSelect}
1078
- from timeline_edges e
1079
- join tweets t on t.id = e.tweet_id
1080
- join accounts a on a.id = e.account_id
1081
- join profiles p on p.id = t.author_profile_id
1082
- left join tweets rt on rt.id = t.reply_to_id
1083
- left join profiles rp on rp.id = rt.author_profile_id
1084
- left join tweets qt on qt.id = t.quoted_tweet_id
1085
- left join profiles qp on qp.id = qt.author_profile_id
1086
- ${join}
1087
- ${where}
1088
- order by t.created_at desc, t.id desc
1089
- limit ?
1090
- `;
1091
-
1092
- let rows = db
1093
- .prepare(buildTimelineSelectSql(timelineEdgesCte))
1094
- .all(...params) as Array<Record<string, unknown>>;
1095
-
1096
- if (usedRecentEdgeWindow && rows.length < limit) {
1097
- rows = db
1098
- .prepare(buildTimelineSelectSql(unwindowedTimelineEdgesCte))
1099
- .all(kind, kind, kind, limit) as Array<Record<string, unknown>>;
1100
- }
1101
-
1102
- const urlExpansionCache: UrlExpansionCache = new Map();
1103
- const profileByHandleCache: ProfileByHandleCache = new Map();
1104
- return rows.map((row) => {
1105
- const author = {
1106
- id: String(row.profile_id),
1107
- handle: String(row.handle),
1108
- displayName: String(row.display_name),
1109
- bio: String(row.bio),
1110
- followersCount: Number(row.followers_count),
1111
- followingCount: Number(row.following_count ?? 0),
1112
- avatarHue: Number(row.avatar_hue),
1113
- avatarUrl:
1114
- typeof row.avatar_url === "string" ? String(row.avatar_url) : undefined,
1115
- createdAt: String(row.profile_created_at),
1116
- };
1117
- const rowProfiles: Record<string, ProfileRecord> = {
1118
- [author.id]: author,
1119
- ...(row.reply_profile_id
1120
- ? {
1121
- [String(row.reply_profile_id)]: toProfile({
1122
- id: row.reply_profile_id,
1123
- handle: row.reply_handle,
1124
- display_name: row.reply_display_name,
1125
- bio: row.reply_bio,
1126
- followers_count: row.reply_followers_count,
1127
- following_count: row.reply_following_count,
1128
- avatar_hue: row.reply_avatar_hue,
1129
- avatar_url: row.reply_avatar_url,
1130
- created_at: row.reply_profile_created_at,
1131
- }),
1132
- }
1133
- : {}),
1134
- ...(row.quoted_profile_id
1135
- ? {
1136
- [String(row.quoted_profile_id)]: toProfile({
1137
- id: row.quoted_profile_id,
1138
- handle: row.quoted_handle,
1139
- display_name: row.quoted_display_name,
1140
- bio: row.quoted_bio,
1141
- followers_count: row.quoted_followers_count,
1142
- following_count: row.quoted_following_count,
1143
- avatar_hue: row.quoted_avatar_hue,
1144
- avatar_url: row.quoted_avatar_url,
1145
- created_at: row.quoted_profile_created_at,
1146
- }),
1147
- }
1148
- : {}),
1149
- };
1150
- const resolveProfileByHandle = (handle: string) =>
1151
- getProfileByHandle(db, profileByHandleCache, handle, rowProfiles);
1152
- const text = String(row.text);
1153
- const entities = enrichTimelineEntities(
1154
- db,
1155
- urlExpansionCache,
1156
- text,
1157
- parseJsonField<TweetEntities>(row.entities_json, {}),
1158
- rowProfiles,
1159
- resolveProfileByHandle,
1160
- );
1161
- const item = {
1162
- id: String(row.id),
1163
- accountId: String(row.account_id),
1164
- accountHandle: String(row.account_handle),
1165
- kind: row.kind as TimelineItem["kind"],
1166
- text,
1167
- ...(typeof row.search_snippet === "string"
1168
- ? { searchSnippet: row.search_snippet }
1169
- : {}),
1170
- createdAt: String(row.created_at),
1171
- replyToId:
1172
- typeof row.reply_to_id === "string" ? String(row.reply_to_id) : null,
1173
- isReplied: Boolean(row.is_replied),
1174
- likeCount: Number(row.like_count),
1175
- mediaCount: Number(row.media_count),
1176
- bookmarked: Boolean(row.bookmarked),
1177
- liked: Boolean(row.liked),
1178
- author,
1179
- entities,
1180
- media: parseJsonField<TweetMediaItem[]>(row.media_json, []),
1181
- replyToTweet: buildEmbeddedTweet(
1182
- db,
1183
- urlExpansionCache,
1184
- row,
1185
- "reply_",
1186
- resolveProfileByHandle,
1187
- ),
1188
- quotedTweet: buildEmbeddedTweet(
1189
- db,
1190
- urlExpansionCache,
1191
- row,
1192
- "quoted_",
1193
- resolveProfileByHandle,
1194
- ),
1195
- retweetedTweet: buildRetweetedTweet(
1196
- db,
1197
- urlExpansionCache,
1198
- row,
1199
- resolveProfileByHandle,
1200
- ),
1201
- };
1202
- return includeQualityReason
1203
- ? {
1204
- ...item,
1205
- qualityReason: getTimelineQualityReason(
1206
- row,
1207
- normalizedLowQualityThreshold,
1208
- ),
1209
- }
1210
- : item;
1211
- });
1212
- }
1213
-
1214
- function conversationTweetSelect(accountId?: string) {
1215
- const collectionStateSelect = accountId
1216
- ? `
1217
- case
1218
- when exists (
1219
- select 1 from tweet_collections collection
1220
- where collection.account_id = ?
1221
- and collection.tweet_id = t.id
1222
- and collection.kind = 'bookmarks'
1223
- ) then 1
1224
- when t.account_id = ? and t.bookmarked = 1 then 1
1225
- else 0
1226
- end as bookmarked,
1227
- case
1228
- when exists (
1229
- select 1 from tweet_collections collection
1230
- where collection.account_id = ?
1231
- and collection.tweet_id = t.id
1232
- and collection.kind = 'likes'
1233
- ) then 1
1234
- when t.account_id = ? and t.liked = 1 then 1
1235
- else 0
1236
- end as liked,`
1237
- : `
1238
- t.bookmarked,
1239
- t.liked,`;
1240
- return `
1241
- select
1242
- t.id,
1243
- t.text,
1244
- t.created_at,
1245
- t.reply_to_id,
1246
- t.is_replied,
1247
- t.like_count,
1248
- t.media_count,
1249
- ${collectionStateSelect}
1250
- t.entities_json,
1251
- t.media_json,
1252
- p.id as profile_id,
1253
- p.handle,
1254
- p.display_name,
1255
- p.bio,
1256
- p.followers_count,
1257
- p.following_count,
1258
- p.avatar_hue,
1259
- p.avatar_url,
1260
- p.created_at as profile_created_at
1261
- from tweets t
1262
- join profiles p on p.id = t.author_profile_id
1263
- `;
1264
- }
1265
-
1266
- function getTweetById(
1267
- db: Database,
1268
- urlExpansionCache: UrlExpansionCache,
1269
- tweetId: string,
1270
- resolveProfileByHandle?: (handle: string) => ProfileRecord,
1271
- accountId?: string,
1272
- ): EmbeddedTweet | null {
1273
- const stateParams = accountId
1274
- ? [accountId, accountId, accountId, accountId]
1275
- : [];
1276
- const row = db
1277
- .prepare(`${conversationTweetSelect(accountId)} where t.id = ?`)
1278
- .get(...stateParams, tweetId) as Record<string, unknown> | undefined;
1279
- if (!row) return null;
1280
- return buildEmbeddedTweet(
1281
- db,
1282
- urlExpansionCache,
1283
- row,
1284
- "",
1285
- resolveProfileByHandle,
1286
- );
1287
- }
1288
-
1289
- export function getTweetsByIds(
1290
- tweetIds: string[],
1291
- accountId?: string,
1292
- ): EmbeddedTweet[] {
1293
- const db = getNativeDb();
1294
- const scopedAccountId =
1295
- accountId && accountId !== "all" ? accountId : undefined;
1296
- const urlExpansionCache: UrlExpansionCache = new Map();
1297
- const profileByHandleCache: ProfileByHandleCache = new Map();
1298
- const resolveProfileByHandle = (handle: string) =>
1299
- getProfileByHandle(db, profileByHandleCache, handle);
1300
- const seen = new Set<string>();
1301
- const tweets: EmbeddedTweet[] = [];
1302
-
1303
- for (const tweetId of tweetIds) {
1304
- const normalized = tweetId.trim().replace(/^tweet_/, "");
1305
- if (!normalized || seen.has(normalized)) continue;
1306
- seen.add(normalized);
1307
- if (
1308
- scopedAccountId &&
1309
- !db
1310
- .prepare(
1311
- `
1312
- select 1
1313
- from tweets tweet
1314
- where tweet.id = ?
1315
- and (
1316
- tweet.account_id = ?
1317
- or exists (
1318
- select 1
1319
- from tweet_account_edges edge
1320
- where edge.account_id = ?
1321
- and edge.tweet_id = tweet.id
1322
- )
1323
- or exists (
1324
- select 1
1325
- from tweet_collections collection
1326
- where collection.account_id = ?
1327
- and collection.tweet_id = tweet.id
1328
- )
1329
- )
1330
- limit 1
1331
- `,
1332
- )
1333
- .get(normalized, scopedAccountId, scopedAccountId, scopedAccountId)
1334
- ) {
1335
- continue;
1336
- }
1337
- const tweet = getTweetById(
1338
- db,
1339
- urlExpansionCache,
1340
- normalized,
1341
- resolveProfileByHandle,
1342
- scopedAccountId,
1343
- );
1344
- if (tweet) tweets.push(tweet);
1345
- }
1346
-
1347
- return tweets;
1348
- }
1349
-
1350
- function listTweetDescendants(
1351
- db: Database,
1352
- urlExpansionCache: UrlExpansionCache,
1353
- rootId: string,
1354
- limit: number,
1355
- resolveProfileByHandle?: (handle: string) => ProfileRecord,
1356
- ) {
1357
- if (limit <= 0) return [];
1358
- const rows = db
1359
- .prepare(
1360
- `
1361
- with recursive branch(id, depth) as (
1362
- select t.id, 0
1363
- from tweets t
1364
- where t.id = ?
1365
- union all
1366
- select child.id, branch.depth + 1
1367
- from tweets child
1368
- join branch on child.reply_to_id = branch.id
1369
- where branch.depth < 8
1370
- )
1371
- ${conversationTweetSelect()}
1372
- join branch on branch.id = t.id
1373
- where t.id != ?
1374
- order by t.created_at asc
1375
- limit ?
1376
- `,
1377
- )
1378
- .all(rootId, rootId, limit) as Array<Record<string, unknown>>;
1379
-
1380
- return rows
1381
- .map((row) =>
1382
- buildEmbeddedTweet(
1383
- db,
1384
- urlExpansionCache,
1385
- row,
1386
- "",
1387
- resolveProfileByHandle,
1388
- ),
1389
- )
1390
- .filter((tweet): tweet is EmbeddedTweet => Boolean(tweet));
1391
- }
1392
-
1393
- function appendConversationTweets(
1394
- target: EmbeddedTweet[],
1395
- seen: Set<string>,
1396
- items: EmbeddedTweet[],
1397
- remaining: number,
1398
- ) {
1399
- for (const tweet of items) {
1400
- if (target.length >= remaining || seen.has(tweet.id)) continue;
1401
- seen.add(tweet.id);
1402
- target.push(tweet);
1403
- }
1404
- }
1405
-
1406
- export function getTweetConversation(
1407
- tweetId: string,
1408
- limit = 80,
1409
- ): TweetConversationResponse | null {
1410
- const db = getNativeDb();
1411
- const urlExpansionCache: UrlExpansionCache = new Map();
1412
- const profileByHandleCache: ProfileByHandleCache = new Map();
1413
- const resolveProfileByHandle = (handle: string) =>
1414
- getProfileByHandle(db, profileByHandleCache, handle);
1415
- const anchor = getTweetById(
1416
- db,
1417
- urlExpansionCache,
1418
- tweetId,
1419
- resolveProfileByHandle,
1420
- );
1421
- if (!anchor) return null;
1422
-
1423
- const ancestors: EmbeddedTweet[] = [];
1424
- let current = anchor;
1425
- for (let depth = 0; depth < 12 && current.replyToId; depth += 1) {
1426
- const parent = getTweetById(
1427
- db,
1428
- urlExpansionCache,
1429
- current.replyToId,
1430
- resolveProfileByHandle,
1431
- );
1432
- if (!parent || ancestors.some((tweet) => tweet.id === parent.id)) break;
1433
- ancestors.push(parent);
1434
- current = parent;
1435
- }
1436
-
1437
- const required = [...ancestors].reverse();
1438
- required.push(anchor);
1439
- const root = required[0] ?? anchor;
1440
- const seen = new Set<string>();
1441
- const items = required.filter((tweet) => {
1442
- if (seen.has(tweet.id)) return false;
1443
- seen.add(tweet.id);
1444
- return true;
1445
- });
1446
- const remainingAfterRequired = Math.max(0, limit - items.length);
1447
- const focusedDescendants = listTweetDescendants(
1448
- db,
1449
- urlExpansionCache,
1450
- anchor.id,
1451
- remainingAfterRequired,
1452
- resolveProfileByHandle,
1453
- );
1454
- appendConversationTweets(items, seen, focusedDescendants, limit);
1455
-
1456
- if (items.length < limit && root.id !== anchor.id) {
1457
- const ambientDescendants = listTweetDescendants(
1458
- db,
1459
- urlExpansionCache,
1460
- root.id,
1461
- limit,
1462
- resolveProfileByHandle,
1463
- );
1464
- appendConversationTweets(items, seen, ambientDescendants, limit);
1465
- }
1466
-
1467
- items.sort((left, right) => left.createdAt.localeCompare(right.createdAt));
1468
-
1469
- return {
1470
- anchorId: anchor.id,
1471
- items,
1472
- };
1473
- }
1474
-
1475
- export function listDmConversations({
1476
- account,
1477
- conversationIds,
1478
- inbox = "all",
1479
- participant,
1480
- search,
1481
- replyFilter = "all",
1482
- since,
1483
- until,
1484
- minFollowers,
1485
- maxFollowers,
1486
- minInfluenceScore,
1487
- maxInfluenceScore,
1488
- sort = "recent",
1489
- context = 0,
1490
- limit = 20,
1491
- }: DmQuery): DmConversationItem[] {
1492
- const db = getNativeDb();
1493
- const params: Array<string | number> = [];
1494
- const joinParams: Array<string | number> = [];
1495
- let searchSnippetCte = "";
1496
- let join = "";
1497
- let where = "where 1 = 1";
1498
- let searchSnippetSelect = "";
1499
- const ftsSearch = search?.trim() ? toFtsSearchQuery(search) : "";
1500
- const influenceMinFollowers =
1501
- typeof minInfluenceScore === "number"
1502
- ? getMinFollowersForInfluenceScore(minInfluenceScore)
1503
- : undefined;
1504
- const influenceMaxFollowers =
1505
- typeof maxInfluenceScore === "number"
1506
- ? getMaxFollowersForInfluenceScore(maxInfluenceScore)
1507
- : undefined;
1508
- const effectiveMinFollowers =
1509
- typeof minFollowers === "number" ||
1510
- typeof influenceMinFollowers === "number"
1511
- ? Math.max(minFollowers ?? 0, influenceMinFollowers ?? 0)
1512
- : undefined;
1513
- const effectiveMaxFollowers =
1514
- typeof maxFollowers === "number" ||
1515
- typeof influenceMaxFollowers === "number"
1516
- ? Math.min(
1517
- maxFollowers ?? Number.MAX_SAFE_INTEGER,
1518
- influenceMaxFollowers ?? Number.MAX_SAFE_INTEGER,
1519
- )
1520
- : undefined;
1521
- const orderBy =
1522
- sort === "followers" || sort === "influence"
1523
- ? "p.followers_count desc, c.last_message_at desc"
1524
- : "c.last_message_at desc";
1525
-
1526
- if (account && account !== "all") {
1527
- where += " and a.id = ?";
1528
- params.push(account);
1529
- }
1530
-
1531
- if (conversationIds && conversationIds.length > 0) {
1532
- where += ` and c.id in (${conversationIds.map(() => "?").join(",")})`;
1533
- params.push(...conversationIds);
1534
- }
1535
-
1536
- if (inbox === "accepted") {
1537
- where += " and c.inbox_kind = 'accepted'";
1538
- } else if (inbox === "requests") {
1539
- where += " and c.inbox_kind = 'request'";
1540
- }
1541
-
1542
- if (participant?.trim()) {
1543
- where += " and (p.handle like ? or p.display_name like ?)";
1544
- params.push(`%${participant.trim()}%`, `%${participant.trim()}%`);
1545
- }
1546
-
1547
- if (replyFilter === "replied") {
1548
- where += " and c.needs_reply = 0";
1549
- } else if (replyFilter === "unreplied") {
1550
- where += " and c.needs_reply = 1";
1551
- }
1552
-
1553
- if (since?.trim()) {
1554
- where += " and c.last_message_at >= ?";
1555
- params.push(since);
1556
- }
1557
- if (until?.trim()) {
1558
- where += " and c.last_message_at < ?";
1559
- params.push(until);
1560
- }
1561
-
1562
- if (typeof effectiveMinFollowers === "number") {
1563
- where += " and p.followers_count >= ?";
1564
- params.push(effectiveMinFollowers);
1565
- }
1566
-
1567
- if (typeof effectiveMaxFollowers === "number") {
1568
- where += " and p.followers_count <= ?";
1569
- params.push(effectiveMaxFollowers);
1570
- }
1571
-
1572
- if (ftsSearch) {
1573
- searchSnippetCte = `
1574
- with ranked_dm_search as materialized (
1575
- select
1576
- latest_search.id,
1577
- latest_search.conversation_id,
1578
- row_number() over (
1579
- partition by latest_search.conversation_id
1580
- order by latest_search.created_at desc, latest_search.id desc
1581
- ) as match_rank
1582
- from dm_messages latest_search
1583
- join dm_fts on dm_fts.message_id = latest_search.id
1584
- where dm_fts.text match ?
1585
- ),
1586
- dm_search as materialized (
1587
- select
1588
- ranked_dm_search.conversation_id,
1589
- snippet(dm_fts, 1, '<mark>', '</mark>', '...', 16) as search_snippet
1590
- from ranked_dm_search
1591
- join dm_fts on dm_fts.message_id = ranked_dm_search.id
1592
- where ranked_dm_search.match_rank = 1
1593
- and dm_fts.text match ?
1594
- )
1595
- `;
1596
- join += " join dm_search on dm_search.conversation_id = c.id ";
1597
- searchSnippetSelect = ", dm_search.search_snippet as search_snippet";
1598
- joinParams.push(ftsSearch, ftsSearch);
1599
- }
1600
-
1601
- params.push(limit);
1602
-
1603
- const rows = db
1604
- .prepare(
1605
- `
1606
- ${searchSnippetCte}
1607
- select
1608
- c.id,
1609
- c.account_id,
1610
- a.handle as account_handle,
1611
- c.title,
1612
- c.inbox_kind,
1613
- c.last_message_at,
1614
- c.unread_count,
1615
- c.needs_reply,
1616
- p.id as profile_id,
1617
- p.handle,
1618
- p.display_name,
1619
- p.bio,
1620
- p.followers_count,
1621
- p.following_count,
1622
- p.avatar_hue,
1623
- p.avatar_url,
1624
- p.location as profile_location,
1625
- p.url as profile_url,
1626
- p.verified_type as profile_verified_type,
1627
- p.entities_json as profile_entities_json,
1628
- p.created_at as profile_created_at,
1629
- (
1630
- select text
1631
- from dm_messages latest_message
1632
- where latest_message.conversation_id = c.id
1633
- order by latest_message.created_at desc
1634
- limit 1
1635
- ) as last_message_preview
1636
- ${searchSnippetSelect}
1637
- from dm_conversations c
1638
- join accounts a on a.id = c.account_id
1639
- join profiles p on p.id = c.participant_profile_id
1640
- ${join}
1641
- ${where}
1642
- group by c.id
1643
- order by ${orderBy}
1644
- limit ?
1645
- `,
1646
- )
1647
- .all(...joinParams, ...params) as Array<Record<string, unknown>>;
1648
-
1649
- const affiliationsByProfile = fetchProfileAffiliations(
1650
- db,
1651
- rows.map((row) => String(row.profile_id)),
1652
- );
1653
- const items: DmConversationItem[] = rows.map((row) => {
1654
- const followersCount = Number(row.followers_count);
1655
- const influenceScore = getInfluenceScore(followersCount);
1656
- const participant = toProfile({
1657
- id: row.profile_id,
1658
- handle: row.handle,
1659
- display_name: row.display_name,
1660
- bio: row.bio,
1661
- followers_count: row.followers_count,
1662
- following_count: row.following_count,
1663
- avatar_hue: row.avatar_hue,
1664
- avatar_url: row.avatar_url,
1665
- location: row.profile_location,
1666
- url: row.profile_url,
1667
- verified_type: row.profile_verified_type,
1668
- entities_json: row.profile_entities_json,
1669
- created_at: row.profile_created_at,
1670
- });
1671
- const affiliations = affiliationsByProfile.get(participant.id) ?? [];
1672
- return {
1673
- id: String(row.id),
1674
- accountId: String(row.account_id),
1675
- accountHandle: String(row.account_handle),
1676
- title: String(row.title),
1677
- ...(typeof row.search_snippet === "string"
1678
- ? { searchSnippet: row.search_snippet }
1679
- : {}),
1680
- inboxKind: row.inbox_kind === "request" ? "request" : "accepted",
1681
- isMessageRequest: row.inbox_kind === "request",
1682
- lastMessageAt: String(row.last_message_at),
1683
- lastMessagePreview: String(row.last_message_preview ?? ""),
1684
- unreadCount: Number(row.unread_count),
1685
- needsReply: Boolean(row.needs_reply),
1686
- influenceScore,
1687
- influenceLabel: getInfluenceLabel(influenceScore),
1688
- participant: {
1689
- ...participant,
1690
- ...(affiliations.length > 0
1691
- ? {
1692
- affiliations,
1693
- primaryAffiliation: affiliations[0],
1694
- }
1695
- : {}),
1696
- },
1697
- };
1698
- });
1699
-
1700
- const filtered = items.filter((item) => {
1701
- if (
1702
- typeof minInfluenceScore === "number" &&
1703
- item.influenceScore < minInfluenceScore
1704
- ) {
1705
- return false;
1706
- }
1707
-
1708
- if (
1709
- typeof maxInfluenceScore === "number" &&
1710
- item.influenceScore > maxInfluenceScore
1711
- ) {
1712
- return false;
1713
- }
1714
-
1715
- return true;
1716
- });
1717
-
1718
- if (sort === "followers" || sort === "influence") {
1719
- filtered.sort((left, right) => {
1720
- if (
1721
- right.participant.followersCount !== left.participant.followersCount
1722
- ) {
1723
- return (
1724
- right.participant.followersCount - left.participant.followersCount
1725
- );
1726
- }
1727
- return (
1728
- new Date(right.lastMessageAt).getTime() -
1729
- new Date(left.lastMessageAt).getTime()
1730
- );
1731
- });
1732
- }
1733
-
1734
- const limited = filtered.slice(0, limit);
1735
- const normalizedContext = normalizeDmContext(context);
1736
- if (ftsSearch && normalizedContext > 0 && limited.length > 0) {
1737
- const matches = getDmSearchMatches({
1738
- search: ftsSearch,
1739
- conversationIds: limited.map((item) => item.id),
1740
- context: normalizedContext,
1741
- });
1742
- for (const item of limited) {
1743
- const itemMatches = matches.get(item.id);
1744
- if (itemMatches && itemMatches.length > 0) {
1745
- item.matches = itemMatches;
1746
- }
1747
- }
1748
- }
1749
-
1750
- return limited;
1751
- }
1752
-
1753
- export function getConversationThread(
1754
- conversationId: string,
1755
- filters: Pick<DmQuery, "account"> = {},
1756
- ): { conversation: DmConversationItem; messages: DmMessageItem[] } | null {
1757
- const conversation = listDmConversations({
1758
- ...filters,
1759
- conversationIds: [conversationId],
1760
- limit: 1,
1761
- }).find((item) => item.id === conversationId);
1762
-
1763
- if (!conversation) {
1764
- return null;
1765
- }
1766
-
1767
- const db = getNativeDb();
1768
- const rows = db
1769
- .prepare(
1770
- `
1771
- select
1772
- m.id,
1773
- m.conversation_id,
1774
- m.text,
1775
- m.created_at,
1776
- m.direction,
1777
- m.is_replied,
1778
- m.media_count,
1779
- p.id as profile_id,
1780
- p.handle,
1781
- p.display_name,
1782
- p.bio,
1783
- p.followers_count,
1784
- p.following_count,
1785
- p.avatar_hue,
1786
- p.avatar_url,
1787
- p.created_at as profile_created_at
1788
- from dm_messages m
1789
- join profiles p on p.id = m.sender_profile_id
1790
- where m.conversation_id = ?
1791
- order by m.created_at asc
1792
- `,
1793
- )
1794
- .all(conversationId) as Array<Record<string, unknown>>;
1795
-
1796
- return {
1797
- conversation,
1798
- messages: rows.map((row) => ({
1799
- id: String(row.id),
1800
- conversationId: String(row.conversation_id),
1801
- text: String(row.text),
1802
- createdAt: String(row.created_at),
1803
- direction: row.direction as DmMessageItem["direction"],
1804
- isReplied: Boolean(row.is_replied),
1805
- mediaCount: Number(row.media_count),
1806
- sender: toProfile({
1807
- id: row.profile_id,
1808
- handle: row.handle,
1809
- display_name: row.display_name,
1810
- bio: row.bio,
1811
- followers_count: row.followers_count,
1812
- following_count: row.following_count,
1813
- avatar_hue: row.avatar_hue,
1814
- avatar_url: row.avatar_url,
1815
- created_at: row.profile_created_at,
1816
- }),
1817
- })),
1818
- };
1819
- }
1820
-
1821
- export type DmRequestMutationAction = "accept" | "reject" | "block";
1822
-
1823
- export function applyDmRequestMutationToLocalStore(
1824
- conversationId: string,
1825
- action: DmRequestMutationAction,
1826
- ) {
1827
- return persistWrite((db) => {
1828
- db.prepare(
1829
- "delete from sync_cache where cache_key like 'dms:bird:%'",
1830
- ).run();
1831
- if (action === "accept") {
1832
- return db
1833
- .prepare(
1834
- `
1835
- update dm_conversations
1836
- set inbox_kind = 'accepted'
1837
- where id = ?
1838
- `,
1839
- )
1840
- .run(conversationId).changes;
1841
- }
1842
-
1843
- db.prepare(
1844
- `
1845
- delete from link_occurrences
1846
- where source_kind = 'dm'
1847
- and source_id in (
1848
- select id from dm_messages where conversation_id = ?
1849
- )
1850
- `,
1851
- ).run(conversationId);
1852
- db.prepare(
1853
- `
1854
- delete from dm_fts
1855
- where message_id in (
1856
- select id from dm_messages where conversation_id = ?
1857
- )
1858
- `,
1859
- ).run(conversationId);
1860
- db.prepare("delete from dm_messages where conversation_id = ?").run(
1861
- conversationId,
1862
- );
1863
- return db
1864
- .prepare("delete from dm_conversations where id = ?")
1865
- .run(conversationId).changes;
1866
- });
1867
- }
1868
-
1869
- function normalizeDmContext(value: number | undefined) {
1870
- if (typeof value !== "number" || !Number.isFinite(value)) {
1871
- return 0;
1872
- }
1873
- return Math.max(0, Math.min(20, Math.trunc(value)));
1874
- }
1875
-
1876
- function mapDmMessageRow(row: Record<string, unknown>): DmMessageItem {
1877
- return {
1878
- id: String(row.id),
1879
- conversationId: String(row.conversation_id),
1880
- text: String(row.text),
1881
- createdAt: String(row.created_at),
1882
- direction: row.direction as DmMessageItem["direction"],
1883
- isReplied: Boolean(row.is_replied),
1884
- mediaCount: Number(row.media_count),
1885
- sender: toProfile({
1886
- id: row.profile_id,
1887
- handle: row.handle,
1888
- display_name: row.display_name,
1889
- bio: row.bio,
1890
- followers_count: row.followers_count,
1891
- following_count: row.following_count,
1892
- avatar_hue: row.avatar_hue,
1893
- avatar_url: row.avatar_url,
1894
- created_at: row.profile_created_at,
1895
- }),
1896
- };
1897
- }
1898
-
1899
- function selectDmMessageSql(where: string, orderBy: string) {
1900
- return `
1901
- select
1902
- m.id,
1903
- m.conversation_id,
1904
- m.text,
1905
- m.created_at,
1906
- m.direction,
1907
- m.is_replied,
1908
- m.media_count,
1909
- p.id as profile_id,
1910
- p.handle,
1911
- p.display_name,
1912
- p.bio,
1913
- p.followers_count,
1914
- p.following_count,
1915
- p.avatar_hue,
1916
- p.avatar_url,
1917
- p.created_at as profile_created_at
1918
- from dm_messages m
1919
- join profiles p on p.id = m.sender_profile_id
1920
- ${where}
1921
- ${orderBy}
1922
- `;
1923
- }
1924
-
1925
- function getDmSearchMatches({
1926
- search,
1927
- conversationIds,
1928
- context,
1929
- }: {
1930
- search: string;
1931
- conversationIds: string[];
1932
- context: number;
1933
- }) {
1934
- const db = getNativeDb();
1935
- if (search.length === 0) {
1936
- return new Map<string, DmConversationItem["matches"]>();
1937
- }
1938
- const conversationPlaceholders = conversationIds.map(() => "?").join(", ");
1939
- const matchRows = db
1940
- .prepare(
1941
- `
1942
- with ranked_matches as (
1943
- select
1944
- m.id,
1945
- m.conversation_id,
1946
- m.text,
1947
- m.created_at,
1948
- m.direction,
1949
- m.is_replied,
1950
- m.media_count,
1951
- p.id as profile_id,
1952
- p.handle,
1953
- p.display_name,
1954
- p.bio,
1955
- p.followers_count,
1956
- p.following_count,
1957
- p.avatar_hue,
1958
- p.avatar_url,
1959
- p.created_at as profile_created_at,
1960
- row_number() over (
1961
- partition by m.conversation_id
1962
- order by m.created_at desc, m.id desc
1963
- ) as match_rank
1964
- from dm_messages m
1965
- join dm_fts on dm_fts.message_id = m.id
1966
- join profiles p on p.id = m.sender_profile_id
1967
- where dm_fts.text match ?
1968
- and m.conversation_id in (${conversationPlaceholders})
1969
- )
1970
- select *
1971
- from ranked_matches
1972
- where match_rank <= 3
1973
- order by created_at desc, id desc
1974
- `,
1975
- )
1976
- .all(search, ...conversationIds) as Array<Record<string, unknown>>;
1977
-
1978
- const beforeStatement = db.prepare(
1979
- selectDmMessageSql(
1980
- `
1981
- where m.conversation_id = ?
1982
- and (m.created_at < ? or (m.created_at = ? and m.id < ?))
1983
- `,
1984
- "order by m.created_at desc, m.id desc limit ?",
1985
- ),
1986
- );
1987
- const afterStatement = db.prepare(
1988
- selectDmMessageSql(
1989
- `
1990
- where m.conversation_id = ?
1991
- and (m.created_at > ? or (m.created_at = ? and m.id > ?))
1992
- `,
1993
- "order by m.created_at asc, m.id asc limit ?",
1994
- ),
1995
- );
1996
- const grouped = new Map<string, DmConversationItem["matches"]>();
1997
-
1998
- for (const row of matchRows) {
1999
- const message = mapDmMessageRow(row);
2000
- const before = (
2001
- beforeStatement.all(
2002
- message.conversationId,
2003
- message.createdAt,
2004
- message.createdAt,
2005
- message.id,
2006
- context,
2007
- ) as Array<Record<string, unknown>>
2008
- )
2009
- .map(mapDmMessageRow)
2010
- .reverse();
2011
- const after = (
2012
- afterStatement.all(
2013
- message.conversationId,
2014
- message.createdAt,
2015
- message.createdAt,
2016
- message.id,
2017
- context,
2018
- ) as Array<Record<string, unknown>>
2019
- ).map(mapDmMessageRow);
2020
- const matches = grouped.get(message.conversationId) ?? [];
2021
- matches.push({ message, before, after });
2022
- grouped.set(message.conversationId, matches);
2023
- }
2024
-
2025
- return grouped;
2026
- }
2027
-
2028
- export function queryResource(
2029
- resource: "home" | "mentions" | "authored" | "search" | "dms",
2030
- filters: (TimelineQuery | DmQuery) & { conversationId?: string },
2031
- ): QueryResponse {
2032
- if (resource === "dms") {
2033
- const dmFilters = filters as DmQuery & { conversationId?: string };
2034
- const items = listDmConversations(dmFilters);
2035
- const requestedConversationId = dmFilters.conversationId;
2036
- const selectedConversationId =
2037
- requestedConversationId &&
2038
- items.some((item) => item.id === requestedConversationId)
2039
- ? requestedConversationId
2040
- : items[0]?.id;
2041
- return {
2042
- resource,
2043
- items,
2044
- selectedConversation: selectedConversationId
2045
- ? getConversationThread(selectedConversationId, {
2046
- account: dmFilters.account,
2047
- })
2048
- : null,
2049
- };
2050
- }
2051
-
2052
- const { resource: _filterResource, ...timelineFilters } =
2053
- filters as TimelineQuery;
2054
-
2055
- return {
2056
- resource,
2057
- items: listTimelineItems({
2058
- resource,
2059
- ...timelineFilters,
2060
- }),
2061
- };
2062
- }
2063
-
2064
- function refreshDmConversationState(
2065
- db: Database,
2066
- conversationId: string,
2067
- lastMessageAt: string,
2068
- observedLastMessageAt = lastMessageAt,
2069
- ) {
2070
- db.prepare(
2071
- `
2072
- update dm_conversations
2073
- set last_message_at = case
2074
- when last_message_at < ? then ?
2075
- else last_message_at
2076
- end,
2077
- unread_count = case
2078
- when last_message_at = ? then 0
2079
- when last_message_at <= ? then 0
2080
- else unread_count
2081
- end,
2082
- needs_reply = case
2083
- when last_message_at = ? then 0
2084
- when last_message_at <= ? then 0
2085
- else needs_reply
2086
- end
2087
- where id = ?
2088
- `,
2089
- ).run(
2090
- lastMessageAt,
2091
- lastMessageAt,
2092
- observedLastMessageAt,
2093
- lastMessageAt,
2094
- observedLastMessageAt,
2095
- lastMessageAt,
2096
- conversationId,
2097
- );
2098
- }
2099
-
2100
- function getLocalAuthorProfileId(accountId: string) {
2101
- const db = getNativeDb();
2102
- const row = db
2103
- .prepare(
2104
- `
2105
- select p.id
2106
- from accounts a
2107
- join profiles p on p.handle = replace(a.handle, '@', '')
2108
- where a.id = ?
2109
- `,
2110
- )
2111
- .get(accountId) as { id: string } | undefined;
2112
-
2113
- return row?.id;
2114
- }
2115
-
2116
- let savepointCounter = 0;
2117
-
2118
- function preflightWrite<T>(write: (db: Database) => T) {
2119
- const db = getNativeDb();
2120
- const savepoint = `__birdclaw_preflight_${++savepointCounter}`;
2121
- db.exec(`savepoint ${savepoint}`);
2122
- try {
2123
- const result = write(db);
2124
- db.exec(`rollback to ${savepoint}`);
2125
- db.exec(`release ${savepoint}`);
2126
- return result;
2127
- } catch (error) {
2128
- try {
2129
- db.exec(`rollback to ${savepoint}`);
2130
- db.exec(`release ${savepoint}`);
2131
- } catch {
2132
- // Preserve the original staging error; cleanup is best effort here.
2133
- }
2134
- throw error;
2135
- }
2136
- }
2137
-
2138
- function persistWrite<T>(write: (db: Database) => T) {
2139
- const db = getNativeDb();
2140
- return db.transaction(() => write(db))();
2141
- }
2142
-
2143
- type PostDraft = {
2144
- actionId: string;
2145
- authorProfileId: string;
2146
- createdAt: string;
2147
- tweetId: string;
2148
- };
2149
-
2150
- function preparePostDraft(accountId: string): PostDraft {
2151
- const authorProfileId = getLocalAuthorProfileId(accountId);
2152
- if (!authorProfileId) {
2153
- throw new Error("No local author profile for account");
2154
- }
2155
-
2156
- return {
2157
- actionId: randomUUID(),
2158
- authorProfileId,
2159
- createdAt: new Date().toISOString(),
2160
- tweetId: `tweet_${randomUUID()}`,
2161
- };
2162
- }
2163
-
2164
- function writePostDraft(
2165
- db: Database,
2166
- accountId: string,
2167
- text: string,
2168
- draft: PostDraft,
2169
- ) {
2170
- db.prepare(
2171
- `
2172
- insert into tweets (
2173
- id, account_id, author_profile_id, kind, text, created_at,
2174
- is_replied, reply_to_id, like_count, media_count, bookmarked, liked
2175
- ) values (?, ?, ?, 'home', ?, ?, 0, null, 0, 0, 0, 0)
2176
- `,
2177
- ).run(draft.tweetId, accountId, draft.authorProfileId, text, draft.createdAt);
2178
-
2179
- db.prepare("insert into tweets_fts (tweet_id, text) values (?, ?)").run(
2180
- draft.tweetId,
2181
- text,
2182
- );
2183
- db.prepare(
2184
- "insert into tweet_actions (id, account_id, tweet_id, kind, body, created_at) values (?, ?, ?, ?, ?, ?)",
2185
- ).run(
2186
- draft.actionId,
2187
- accountId,
2188
- draft.tweetId,
2189
- "post",
2190
- text,
2191
- draft.createdAt,
2192
- );
2193
- }
2194
-
2195
- export function createPostEffect(accountId: string, text: string) {
2196
- return Effect.gen(function* () {
2197
- const draft = yield* trySync(() => {
2198
- const postDraft = preparePostDraft(accountId);
2199
- preflightWrite((db) => writePostDraft(db, accountId, text, postDraft));
2200
- return postDraft;
2201
- });
2202
-
2203
- yield* verifySelectedXurlAccountEffect(accountId);
2204
- const transport = yield* postViaXurlEffect(text);
2205
- if (!transport.ok) {
2206
- return yield* Effect.fail(new Error(transport.output || "post failed"));
2207
- }
2208
- yield* trySync(() =>
2209
- persistWrite((db) => writePostDraft(db, accountId, text, draft)),
2210
- );
2211
-
2212
- return { ok: true, transport, tweetId: draft.tweetId };
2213
- });
2214
- }
2215
-
2216
- export function createPost(accountId: string, text: string) {
2217
- return runEffectPromise(createPostEffect(accountId, text));
2218
- }
2219
-
2220
- export function createTweetReplyEffect(
2221
- accountId: string,
2222
- tweetId: string,
2223
- text: string,
2224
- ) {
2225
- type ReplyDraft = PostDraft & { replyId: string };
2226
-
2227
- function prepareReplyDraft(): ReplyDraft {
2228
- const postDraft = preparePostDraft(accountId);
2229
- return {
2230
- ...postDraft,
2231
- replyId: postDraft.tweetId,
2232
- };
2233
- }
2234
-
2235
- function writeReplyDraft(db: Database, draft: ReplyDraft) {
2236
- db.prepare("update tweets set is_replied = 1 where id = ?").run(tweetId);
2237
-
2238
- db.prepare(
2239
- `
2240
- insert into tweets (
2241
- id, account_id, author_profile_id, kind, text, created_at,
2242
- is_replied, reply_to_id, like_count, media_count, bookmarked, liked
2243
- ) values (?, ?, ?, 'home', ?, ?, 1, ?, 0, 0, 0, 0)
2244
- `,
2245
- ).run(
2246
- draft.replyId,
2247
- accountId,
2248
- draft.authorProfileId,
2249
- text,
2250
- draft.createdAt,
2251
- tweetId,
2252
- );
2253
- db.prepare("insert into tweets_fts (tweet_id, text) values (?, ?)").run(
2254
- draft.replyId,
2255
- text,
2256
- );
2257
-
2258
- db.prepare(
2259
- "insert into tweet_actions (id, account_id, tweet_id, kind, body, created_at) values (?, ?, ?, ?, ?, ?)",
2260
- ).run(draft.actionId, accountId, tweetId, "reply", text, draft.createdAt);
2261
- }
2262
-
2263
- return Effect.gen(function* () {
2264
- const draft = yield* trySync(() => {
2265
- const replyDraft = prepareReplyDraft();
2266
- preflightWrite((db) => writeReplyDraft(db, replyDraft));
2267
- return replyDraft;
2268
- });
2269
-
2270
- yield* verifySelectedXurlAccountEffect(accountId);
2271
- const transport = yield* replyViaXurlEffect(tweetId, text);
2272
- if (!transport.ok) {
2273
- return yield* Effect.fail(new Error(transport.output || "reply failed"));
2274
- }
2275
- yield* trySync(() => persistWrite((db) => writeReplyDraft(db, draft)));
2276
-
2277
- return { ok: true, transport, replyId: draft.replyId };
2278
- });
2279
- }
2280
-
2281
- export function createTweetReply(
2282
- accountId: string,
2283
- tweetId: string,
2284
- text: string,
2285
- ) {
2286
- return runEffectPromise(createTweetReplyEffect(accountId, tweetId, text));
2287
- }
2288
-
2289
- export function createDmReplyEffect(conversationId: string, text: string) {
2290
- return Effect.gen(function* () {
2291
- const draft = yield* trySync(() => {
2292
- const conversation = getConversationThread(conversationId);
2293
- if (!conversation) {
2294
- throw new Error("Conversation not found");
2295
- }
2296
- const authorProfileId = getLocalAuthorProfileId(
2297
- conversation.conversation.accountId,
2298
- );
2299
- if (!authorProfileId) {
2300
- throw new Error("No local author profile for account");
2301
- }
2302
-
2303
- const dmDraft = {
2304
- accountId: conversation.conversation.accountId,
2305
- authorProfileId,
2306
- createdAt: new Date().toISOString(),
2307
- handle: conversation.conversation.participant.handle,
2308
- observedLastMessageAt: conversation.conversation.lastMessageAt,
2309
- outboundId: `msg_${randomUUID()}`,
2310
- };
2311
- preflightWrite((db) => {
2312
- db.prepare(
2313
- `
2314
- insert into dm_messages (
2315
- id, conversation_id, sender_profile_id, text, created_at, direction, is_replied, media_count
2316
- ) values (?, ?, ?, ?, ?, 'outbound', 1, 0)
2317
- `,
2318
- ).run(
2319
- dmDraft.outboundId,
2320
- conversationId,
2321
- dmDraft.authorProfileId,
2322
- text,
2323
- dmDraft.createdAt,
2324
- );
2325
- db.prepare("insert into dm_fts (message_id, text) values (?, ?)").run(
2326
- dmDraft.outboundId,
2327
- text,
2328
- );
2329
-
2330
- refreshDmConversationState(
2331
- db,
2332
- conversationId,
2333
- dmDraft.createdAt,
2334
- dmDraft.observedLastMessageAt,
2335
- );
2336
- });
2337
- return dmDraft;
2338
- });
2339
-
2340
- yield* verifySelectedXurlAccountEffect(draft.accountId);
2341
- const transport = yield* dmViaXurlEffect(draft.handle, text);
2342
- if (!transport.ok) {
2343
- return yield* Effect.fail(new Error(transport.output || "dm failed"));
2344
- }
2345
- yield* trySync(() =>
2346
- persistWrite((db) => {
2347
- db.prepare(
2348
- `
2349
- insert into dm_messages (
2350
- id, conversation_id, sender_profile_id, text, created_at, direction, is_replied, media_count
2351
- ) values (?, ?, ?, ?, ?, 'outbound', 1, 0)
2352
- `,
2353
- ).run(
2354
- draft.outboundId,
2355
- conversationId,
2356
- draft.authorProfileId,
2357
- text,
2358
- draft.createdAt,
2359
- );
2360
- db.prepare("insert into dm_fts (message_id, text) values (?, ?)").run(
2361
- draft.outboundId,
2362
- text,
2363
- );
2364
-
2365
- refreshDmConversationState(
2366
- db,
2367
- conversationId,
2368
- draft.createdAt,
2369
- draft.observedLastMessageAt,
2370
- );
2371
- }),
2372
- );
2373
-
2374
- return { ok: true, transport, messageId: draft.outboundId };
2375
- });
2376
- }
2377
-
2378
- export function createDmReply(conversationId: string, text: string) {
2379
- return runEffectPromise(createDmReplyEffect(conversationId, text));
2380
- }
1
+ // Compatibility facade. New code should import the owned read model or action module.
2
+ export * from "./query-read-models";
3
+ export {
4
+ applyDmRequestMutationToLocalStore,
5
+ type DmRequestMutationAction,
6
+ } from "./query-actions";