birdclaw 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +39 -1
- package/README.md +108 -7
- package/package.json +24 -23
- package/playwright.config.ts +1 -0
- package/public/favicon.ico +0 -0
- package/public/logo192.png +0 -0
- package/public/logo512.png +0 -0
- package/public/manifest.json +2 -2
- package/src/cli.ts +563 -18
- package/src/components/AppNav.tsx +66 -29
- package/src/components/AvatarChip.tsx +10 -5
- package/src/components/ConversationThread.tsx +125 -0
- package/src/components/DmWorkspace.tsx +96 -98
- package/src/components/EmbeddedTweetCard.tsx +20 -14
- package/src/components/InboxCard.tsx +92 -90
- package/src/components/LinkPreviewCard.tsx +270 -0
- package/src/components/ProfilePreview.tsx +8 -3
- package/src/components/SavedTimelineView.tsx +48 -38
- package/src/components/TimelineCard.tsx +228 -84
- package/src/components/TweetRichText.tsx +6 -2
- package/src/lib/archive-import.ts +1567 -65
- package/src/lib/authored-live.ts +1074 -0
- package/src/lib/backup.ts +318 -14
- package/src/lib/bird-actions.ts +1 -10
- package/src/lib/bird-command.ts +57 -0
- package/src/lib/bird.ts +89 -5
- package/src/lib/config.ts +1 -1
- package/src/lib/db.ts +282 -4
- package/src/lib/follow-graph.ts +1053 -0
- package/src/lib/link-index.ts +629 -0
- package/src/lib/link-insights.ts +834 -0
- package/src/lib/link-preview-metadata.ts +334 -0
- package/src/lib/media-fetch.ts +787 -0
- package/src/lib/media-includes.ts +165 -0
- package/src/lib/mention-threads-live.ts +535 -43
- package/src/lib/mentions-live.ts +623 -19
- package/src/lib/moderation-target.ts +6 -0
- package/src/lib/profile-hydration.ts +1 -1
- package/src/lib/profile-resolver.ts +115 -1
- package/src/lib/queries.ts +233 -7
- package/src/lib/seed.ts +127 -8
- package/src/lib/timeline-collections-live.ts +145 -22
- package/src/lib/timeline-live.ts +10 -15
- package/src/lib/tweet-account-edges.ts +9 -2
- package/src/lib/tweet-render.ts +97 -0
- package/src/lib/types.ts +219 -2
- package/src/lib/ui.ts +375 -147
- package/src/lib/url-expansion-store.ts +110 -0
- package/src/lib/url-expansion.ts +33 -8
- package/src/lib/x-profile.ts +7 -2
- package/src/lib/xurl.ts +296 -12
- package/src/routeTree.gen.ts +105 -0
- package/src/routes/__root.tsx +7 -3
- package/src/routes/api/conversation.tsx +34 -0
- package/src/routes/api/link-insights.tsx +79 -0
- package/src/routes/api/link-preview.tsx +43 -0
- package/src/routes/api/profile-hydrate.tsx +51 -0
- package/src/routes/blocks.tsx +111 -86
- package/src/routes/dms.tsx +90 -78
- package/src/routes/inbox.tsx +98 -86
- package/src/routes/index.tsx +63 -50
- package/src/routes/links.tsx +883 -0
- package/src/routes/mentions.tsx +63 -50
- package/src/styles.css +106 -43
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import type { TweetMediaItem, XurlMediaItem } from "./types";
|
|
2
|
+
|
|
3
|
+
interface TweetWithMediaAttachments {
|
|
4
|
+
attachments?: { media_keys?: string[] };
|
|
5
|
+
entities?: Record<string, unknown>;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
function mediaKeys(tweet: TweetWithMediaAttachments) {
|
|
9
|
+
return Array.isArray(tweet.attachments?.media_keys)
|
|
10
|
+
? tweet.attachments.media_keys.filter((key) => typeof key === "string")
|
|
11
|
+
: [];
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function countTweetMedia(tweet: TweetWithMediaAttachments) {
|
|
15
|
+
const keys = mediaKeys(tweet);
|
|
16
|
+
if (keys.length > 0) {
|
|
17
|
+
return keys.length;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const urls = Array.isArray(tweet.entities?.urls) ? tweet.entities.urls : [];
|
|
21
|
+
return urls.filter(
|
|
22
|
+
(url) =>
|
|
23
|
+
url &&
|
|
24
|
+
typeof url === "object" &&
|
|
25
|
+
typeof (url as Record<string, unknown>).media_key === "string",
|
|
26
|
+
).length;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function localType(media: XurlMediaItem): TweetMediaItem["type"] {
|
|
30
|
+
if (media.type === "photo") {
|
|
31
|
+
return "image";
|
|
32
|
+
}
|
|
33
|
+
if (media.type === "animated_gif") {
|
|
34
|
+
return "gif";
|
|
35
|
+
}
|
|
36
|
+
if (media.type === "video") {
|
|
37
|
+
return "video";
|
|
38
|
+
}
|
|
39
|
+
return "unknown";
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function mp4Variants(
|
|
43
|
+
media: XurlMediaItem,
|
|
44
|
+
): NonNullable<TweetMediaItem["variants"]> {
|
|
45
|
+
return (media.variants ?? [])
|
|
46
|
+
.filter(
|
|
47
|
+
(variant) =>
|
|
48
|
+
variant.content_type === "video/mp4" && typeof variant.url === "string",
|
|
49
|
+
)
|
|
50
|
+
.map((variant) => ({
|
|
51
|
+
url: variant.url,
|
|
52
|
+
contentType: variant.content_type,
|
|
53
|
+
...(Number.isFinite(Number(variant.bit_rate))
|
|
54
|
+
? { bitRate: Number(variant.bit_rate) }
|
|
55
|
+
: {}),
|
|
56
|
+
}))
|
|
57
|
+
.sort(
|
|
58
|
+
(left, right) => Number(right.bitRate ?? 0) - Number(left.bitRate ?? 0),
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function record(value: unknown) {
|
|
63
|
+
return value && typeof value === "object" && !Array.isArray(value)
|
|
64
|
+
? (value as Record<string, unknown>)
|
|
65
|
+
: null;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function parsedMediaUrl(value: unknown) {
|
|
69
|
+
if (typeof value !== "string" || value.length === 0) return null;
|
|
70
|
+
try {
|
|
71
|
+
const url = new URL(value);
|
|
72
|
+
return url.protocol === "https:" ? url : null;
|
|
73
|
+
} catch {
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function isKnownMediaCdn(url: URL) {
|
|
79
|
+
return url.hostname === "pbs.twimg.com" || url.hostname === "video.twimg.com";
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function directMediaUrl(value: unknown, trustedField = false) {
|
|
83
|
+
const url = parsedMediaUrl(value);
|
|
84
|
+
if (!url || url.hostname === "t.co") return null;
|
|
85
|
+
return trustedField || isKnownMediaCdn(url) ? url.toString() : null;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function entityMediaUrl(value: Record<string, unknown>) {
|
|
89
|
+
for (const key of ["media_url_https", "media_url"]) {
|
|
90
|
+
const url = directMediaUrl(value[key], true);
|
|
91
|
+
if (url) return url;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
for (const key of ["expanded_url", "url"]) {
|
|
95
|
+
const url = directMediaUrl(value[key]);
|
|
96
|
+
if (url) return url;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const images = Array.isArray(value.images) ? value.images : [];
|
|
100
|
+
for (const image of images) {
|
|
101
|
+
const item = record(image);
|
|
102
|
+
const url = directMediaUrl(item?.url);
|
|
103
|
+
if (url) return url;
|
|
104
|
+
}
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function birdEntityMedia(tweet: TweetWithMediaAttachments) {
|
|
109
|
+
const urls = Array.isArray(tweet.entities?.urls) ? tweet.entities.urls : [];
|
|
110
|
+
const seen = new Set<string>();
|
|
111
|
+
const items: TweetMediaItem[] = [];
|
|
112
|
+
for (const url of urls) {
|
|
113
|
+
const item = record(url);
|
|
114
|
+
if (!item || typeof item.media_key !== "string") continue;
|
|
115
|
+
const mediaUrl = entityMediaUrl(item);
|
|
116
|
+
if (!mediaUrl || seen.has(mediaUrl)) continue;
|
|
117
|
+
seen.add(mediaUrl);
|
|
118
|
+
items.push({ url: mediaUrl, type: "image" });
|
|
119
|
+
}
|
|
120
|
+
return items;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export function buildMediaJsonFromIncludes(
|
|
124
|
+
tweet: TweetWithMediaAttachments,
|
|
125
|
+
media: XurlMediaItem[] = [],
|
|
126
|
+
) {
|
|
127
|
+
const byKey = new Map(media.map((item) => [item.media_key, item]));
|
|
128
|
+
const keys = mediaKeys(tweet);
|
|
129
|
+
const items = keys
|
|
130
|
+
.map((key) => byKey.get(key))
|
|
131
|
+
.filter((item): item is XurlMediaItem => item !== undefined)
|
|
132
|
+
.map((item) => {
|
|
133
|
+
const variants = mp4Variants(item);
|
|
134
|
+
const type = localType(item);
|
|
135
|
+
const url =
|
|
136
|
+
type === "image"
|
|
137
|
+
? (item.url ?? item.preview_image_url ?? "")
|
|
138
|
+
: (item.preview_image_url ?? variants[0]?.url ?? item.url ?? "");
|
|
139
|
+
if (!url) {
|
|
140
|
+
return null;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return {
|
|
144
|
+
url,
|
|
145
|
+
type,
|
|
146
|
+
...(item.alt_text ? { altText: item.alt_text } : {}),
|
|
147
|
+
...(Number.isFinite(Number(item.width))
|
|
148
|
+
? { width: Number(item.width) }
|
|
149
|
+
: {}),
|
|
150
|
+
...(Number.isFinite(Number(item.height))
|
|
151
|
+
? { height: Number(item.height) }
|
|
152
|
+
: {}),
|
|
153
|
+
...(item.preview_image_url
|
|
154
|
+
? { thumbnailUrl: item.preview_image_url }
|
|
155
|
+
: {}),
|
|
156
|
+
...(Number.isFinite(Number(item.duration_ms))
|
|
157
|
+
? { durationMs: Number(item.duration_ms) }
|
|
158
|
+
: {}),
|
|
159
|
+
...(variants.length > 0 ? { variants } : {}),
|
|
160
|
+
} satisfies TweetMediaItem;
|
|
161
|
+
})
|
|
162
|
+
.filter((item): item is TweetMediaItem => item !== null);
|
|
163
|
+
|
|
164
|
+
return JSON.stringify(keys.length > 0 ? items : birdEntityMedia(tweet));
|
|
165
|
+
}
|