birdclaw 0.2.1 → 0.4.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 +44 -1
- package/README.md +54 -15
- package/package.json +14 -15
- package/playwright.config.ts +5 -2
- package/src/cli.ts +289 -20
- package/src/lib/actions-transport.ts +58 -1
- package/src/lib/archive-import.ts +34 -2
- package/src/lib/backup.ts +271 -33
- package/src/lib/bird-actions.ts +21 -0
- package/src/lib/bird.ts +332 -17
- package/src/lib/blocks.ts +3 -3
- package/src/lib/bookmark-sync-job.ts +3 -3
- package/src/lib/config.ts +34 -1
- package/src/lib/db.ts +321 -14
- package/src/lib/dms-live.ts +3 -3
- package/src/lib/identity-search-index.ts +369 -0
- package/src/lib/mention-threads-live.ts +267 -0
- package/src/lib/mentions-live.ts +18 -7
- package/src/lib/moderation-target.ts +7 -5
- package/src/lib/moderation-write.ts +3 -3
- package/src/lib/profile-affiliation-hydration.ts +189 -0
- package/src/lib/profile-affiliations.ts +262 -0
- package/src/lib/profile-bio-entities.ts +318 -0
- package/src/lib/profile-history.ts +164 -0
- package/src/lib/profile-hydration.ts +8 -24
- package/src/lib/profile-resolver.ts +428 -0
- package/src/lib/queries.ts +522 -68
- package/src/lib/research.ts +607 -0
- package/src/lib/seed.ts +10 -4
- package/src/lib/sqlite.ts +143 -0
- package/src/lib/timeline-collections-live.ts +22 -8
- package/src/lib/timeline-live.ts +182 -0
- package/src/lib/tweet-account-edges.ts +39 -0
- package/src/lib/tweet-lookup.ts +35 -0
- package/src/lib/types.ts +103 -1
- package/src/lib/url-expansion.ts +147 -0
- package/src/lib/whois.ts +1060 -0
- package/src/lib/x-profile.ts +174 -17
- package/src/lib/xurl.ts +41 -6
|
@@ -0,0 +1,318 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,164 @@
|
|
|
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
|
+
}
|
|
@@ -5,6 +5,7 @@ import {
|
|
|
5
5
|
lookupAuthenticatedUser,
|
|
6
6
|
lookupUsersByIds,
|
|
7
7
|
} from "./xurl";
|
|
8
|
+
import { upsertProfileFromXUser } from "./x-profile";
|
|
8
9
|
|
|
9
10
|
function asRecord(value: unknown): Record<string, unknown> | null {
|
|
10
11
|
return value && typeof value === "object" && !Array.isArray(value)
|
|
@@ -45,16 +46,6 @@ export async function hydrateProfilesFromX() {
|
|
|
45
46
|
.map((row) => row.id.replace(/^profile_user_/, ""))
|
|
46
47
|
.filter((id) => id.length > 0);
|
|
47
48
|
|
|
48
|
-
const updateProfile = db.prepare(`
|
|
49
|
-
update profiles
|
|
50
|
-
set handle = ?,
|
|
51
|
-
display_name = ?,
|
|
52
|
-
bio = ?,
|
|
53
|
-
followers_count = ?,
|
|
54
|
-
avatar_url = coalesce(?, avatar_url),
|
|
55
|
-
created_at = coalesce(?, created_at)
|
|
56
|
-
where id = ?
|
|
57
|
-
`);
|
|
58
49
|
const updateConversationTitle = db.prepare(`
|
|
59
50
|
update dm_conversations
|
|
60
51
|
set title = ?
|
|
@@ -66,6 +57,7 @@ export async function hydrateProfilesFromX() {
|
|
|
66
57
|
display_name = ?,
|
|
67
58
|
bio = ?,
|
|
68
59
|
followers_count = ?,
|
|
60
|
+
following_count = coalesce(?, following_count),
|
|
69
61
|
avatar_url = coalesce(?, avatar_url),
|
|
70
62
|
created_at = coalesce(?, created_at)
|
|
71
63
|
where id = 'profile_me'
|
|
@@ -86,24 +78,13 @@ export async function hydrateProfilesFromX() {
|
|
|
86
78
|
|
|
87
79
|
db.transaction(() => {
|
|
88
80
|
for (const user of users) {
|
|
89
|
-
const metrics = asRecord(user.public_metrics);
|
|
90
81
|
const profileId = `profile_user_${String(user.id ?? "")}`;
|
|
91
82
|
if (profileId === "profile_user_") continue;
|
|
92
83
|
|
|
93
|
-
const
|
|
94
|
-
const displayName = String(user.name ?? username);
|
|
95
|
-
updateProfile.run(
|
|
96
|
-
username || profileId,
|
|
97
|
-
displayName || username || profileId,
|
|
98
|
-
String(user.description ?? ""),
|
|
99
|
-
toInt(metrics?.followers_count),
|
|
100
|
-
normalizeAvatarUrl(user.profile_image_url),
|
|
101
|
-
typeof user.created_at === "string" ? user.created_at : null,
|
|
102
|
-
profileId,
|
|
103
|
-
);
|
|
84
|
+
const resolved = upsertProfileFromXUser(db, user);
|
|
104
85
|
updateConversationTitle.run(
|
|
105
|
-
displayName ||
|
|
106
|
-
|
|
86
|
+
resolved.profile.displayName || resolved.profile.handle,
|
|
87
|
+
resolved.profile.id,
|
|
107
88
|
);
|
|
108
89
|
hydratedProfiles += 1;
|
|
109
90
|
}
|
|
@@ -120,6 +101,9 @@ export async function hydrateProfilesFromX() {
|
|
|
120
101
|
String(me.name ?? "Peter Steinberger"),
|
|
121
102
|
String(me.description ?? ""),
|
|
122
103
|
toInt(metrics?.followers_count),
|
|
104
|
+
metrics && "following_count" in metrics
|
|
105
|
+
? toInt(metrics.following_count)
|
|
106
|
+
: null,
|
|
123
107
|
normalizeAvatarUrl(me.profile_image_url),
|
|
124
108
|
typeof me.created_at === "string" ? me.created_at : null,
|
|
125
109
|
);
|