birdclaw 0.3.0 → 0.4.1
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 +25 -0
- package/README.md +37 -9
- package/package.json +4 -6
- package/src/cli.ts +289 -20
- package/src/lib/archive-import.ts +27 -0
- package/src/lib/backup.ts +271 -33
- package/src/lib/bird-actions.ts +17 -0
- package/src/lib/bird.ts +189 -0
- package/src/lib/blocks.ts +3 -3
- package/src/lib/bookmark-sync-job.ts +3 -3
- package/src/lib/db.ts +404 -14
- package/src/lib/dms-live.ts +3 -3
- package/src/lib/identity-search-index.ts +369 -0
- package/src/lib/link-index.ts +716 -0
- package/src/lib/mention-threads-live.ts +5 -9
- package/src/lib/mentions-live.ts +18 -7
- package/src/lib/moderation-target.ts +4 -4
- 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 +4 -28
- package/src/lib/profile-resolver.ts +428 -0
- package/src/lib/queries.ts +391 -57
- package/src/lib/research.ts +11 -0
- package/src/lib/seed.ts +2 -2
- package/src/lib/sqlite.ts +143 -0
- package/src/lib/timeline-collections-live.ts +7 -7
- package/src/lib/timeline-live.ts +16 -9
- package/src/lib/tweet-account-edges.ts +39 -0
- package/src/lib/types.ts +108 -0
- package/src/lib/url-expansion.ts +155 -0
- package/src/lib/whois.ts +1060 -0
- package/src/lib/x-profile.ts +140 -12
- package/src/lib/xurl.ts +7 -6
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type Database from "
|
|
1
|
+
import type { Database } from "./sqlite";
|
|
2
2
|
import { listThreadViaBird } from "./bird";
|
|
3
3
|
import { getNativeDb } from "./db";
|
|
4
4
|
import type { XurlMentionData, XurlMentionsResponse } from "./types";
|
|
@@ -39,7 +39,7 @@ function getMediaCount(tweet: XurlMentionData) {
|
|
|
39
39
|
).length;
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
-
function replaceTweetFts(db: Database
|
|
42
|
+
function replaceTweetFts(db: Database, tweetId: string, text: string) {
|
|
43
43
|
db.prepare("delete from tweets_fts where tweet_id = ?").run(tweetId);
|
|
44
44
|
db.prepare("insert into tweets_fts (tweet_id, text) values (?, ?)").run(
|
|
45
45
|
tweetId,
|
|
@@ -47,7 +47,7 @@ function replaceTweetFts(db: Database.Database, tweetId: string, text: string) {
|
|
|
47
47
|
);
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
-
function resolveAccount(db: Database
|
|
50
|
+
function resolveAccount(db: Database, accountId?: string) {
|
|
51
51
|
const row = accountId
|
|
52
52
|
? (db
|
|
53
53
|
.prepare("select id, handle from accounts where id = ?")
|
|
@@ -73,11 +73,7 @@ function resolveAccount(db: Database.Database, accountId?: string) {
|
|
|
73
73
|
};
|
|
74
74
|
}
|
|
75
75
|
|
|
76
|
-
function listRecentMentionIds(
|
|
77
|
-
db: Database.Database,
|
|
78
|
-
accountId: string,
|
|
79
|
-
limit: number,
|
|
80
|
-
) {
|
|
76
|
+
function listRecentMentionIds(db: Database, accountId: string, limit: number) {
|
|
81
77
|
return (
|
|
82
78
|
db
|
|
83
79
|
.prepare(
|
|
@@ -105,7 +101,7 @@ function mergeMentionThreadIntoLocalStore({
|
|
|
105
101
|
mentionIds,
|
|
106
102
|
payload,
|
|
107
103
|
}: {
|
|
108
|
-
db: Database
|
|
104
|
+
db: Database;
|
|
109
105
|
accountId: string;
|
|
110
106
|
accountHandle: string;
|
|
111
107
|
mentionIds: Set<string>;
|
package/src/lib/mentions-live.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type Database from "
|
|
1
|
+
import type { Database } from "./sqlite";
|
|
2
2
|
import { listMentionsViaBird } from "./bird";
|
|
3
3
|
import type { MentionsDataSource } from "./config";
|
|
4
4
|
import { getNativeDb } from "./db";
|
|
@@ -12,6 +12,7 @@ import type {
|
|
|
12
12
|
XurlMentionsResponse,
|
|
13
13
|
XurlMentionUser,
|
|
14
14
|
} from "./types";
|
|
15
|
+
import { upsertTweetAccountEdge } from "./tweet-account-edges";
|
|
15
16
|
import { ensureStubProfileForXUser, upsertProfileFromXUser } from "./x-profile";
|
|
16
17
|
import { listMentionsViaXurl, lookupUsersByHandles } from "./xurl";
|
|
17
18
|
|
|
@@ -64,7 +65,7 @@ function parseMaxPages(value?: number) {
|
|
|
64
65
|
return Math.floor(value);
|
|
65
66
|
}
|
|
66
67
|
|
|
67
|
-
function resolveAccount(db: Database
|
|
68
|
+
function resolveAccount(db: Database, accountId?: string) {
|
|
68
69
|
const row = accountId
|
|
69
70
|
? (db
|
|
70
71
|
.prepare("select id, handle from accounts where id = ?")
|
|
@@ -149,7 +150,7 @@ function getMediaCount(tweet: XurlMentionData) {
|
|
|
149
150
|
).length;
|
|
150
151
|
}
|
|
151
152
|
|
|
152
|
-
function replaceTweetFts(db: Database
|
|
153
|
+
function replaceTweetFts(db: Database, tweetId: string, text: string) {
|
|
153
154
|
db.prepare("delete from tweets_fts where tweet_id = ?").run(tweetId);
|
|
154
155
|
db.prepare("insert into tweets_fts (tweet_id, text) values (?, ?)").run(
|
|
155
156
|
tweetId,
|
|
@@ -158,9 +159,10 @@ function replaceTweetFts(db: Database.Database, tweetId: string, text: string) {
|
|
|
158
159
|
}
|
|
159
160
|
|
|
160
161
|
function mergeMentionsIntoLocalStore(
|
|
161
|
-
db: Database
|
|
162
|
+
db: Database,
|
|
162
163
|
accountId: string,
|
|
163
164
|
payload: XurlMentionsResponse,
|
|
165
|
+
source: MentionsDataSource,
|
|
164
166
|
) {
|
|
165
167
|
const usersById = new Map(
|
|
166
168
|
(payload.includes?.users ?? []).map((user) => [user.id, user]),
|
|
@@ -173,9 +175,9 @@ function mergeMentionsIntoLocalStore(
|
|
|
173
175
|
entities_json, media_json, quoted_tweet_id
|
|
174
176
|
) values (?, ?, ?, 'mention', ?, ?, 0, null, ?, ?, 0, 0, ?, '[]', null)
|
|
175
177
|
on conflict(id) do update set
|
|
176
|
-
account_id =
|
|
178
|
+
account_id = tweets.account_id,
|
|
177
179
|
author_profile_id = excluded.author_profile_id,
|
|
178
|
-
kind = excluded.kind,
|
|
180
|
+
kind = case when tweets.kind = 'home' then tweets.kind else excluded.kind end,
|
|
179
181
|
text = excluded.text,
|
|
180
182
|
created_at = excluded.created_at,
|
|
181
183
|
like_count = excluded.like_count,
|
|
@@ -189,6 +191,7 @@ function mergeMentionsIntoLocalStore(
|
|
|
189
191
|
);
|
|
190
192
|
|
|
191
193
|
const writePayload = db.transaction(() => {
|
|
194
|
+
const seenAt = new Date().toISOString();
|
|
192
195
|
for (const tweet of payload.data) {
|
|
193
196
|
const author =
|
|
194
197
|
usersById.get(tweet.author_id) ??
|
|
@@ -210,6 +213,14 @@ function mergeMentionsIntoLocalStore(
|
|
|
210
213
|
getMediaCount(tweet),
|
|
211
214
|
JSON.stringify(toLocalEntities(tweet)),
|
|
212
215
|
);
|
|
216
|
+
upsertTweetAccountEdge(db, {
|
|
217
|
+
accountId,
|
|
218
|
+
tweetId: tweet.id,
|
|
219
|
+
kind: "mention",
|
|
220
|
+
source,
|
|
221
|
+
seenAt,
|
|
222
|
+
rawJson: JSON.stringify(tweet),
|
|
223
|
+
});
|
|
213
224
|
replaceTweetFts(db, tweet.id, tweet.text);
|
|
214
225
|
}
|
|
215
226
|
});
|
|
@@ -408,7 +419,7 @@ async function exportMentionsViaCachedLiveSource({
|
|
|
408
419
|
all: fetchAll,
|
|
409
420
|
parsedMaxPages,
|
|
410
421
|
});
|
|
411
|
-
mergeMentionsIntoLocalStore(db, resolvedAccount.accountId, payload);
|
|
422
|
+
mergeMentionsIntoLocalStore(db, resolvedAccount.accountId, payload, mode);
|
|
412
423
|
writeSyncCache(cacheKey, payload, db);
|
|
413
424
|
|
|
414
425
|
if (
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type Database from "
|
|
1
|
+
import type { Database } from "./sqlite";
|
|
2
2
|
import { lookupProfileViaBird } from "./bird-actions";
|
|
3
3
|
import { getNativeDb } from "./db";
|
|
4
4
|
import type { ProfileRecord, XurlMentionUser } from "./types";
|
|
@@ -41,7 +41,7 @@ export function normalizeProfileQuery(value: string) {
|
|
|
41
41
|
return (urlMatch?.[1] ?? withoutPrefix).replace(/^@/, "").trim();
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
-
export function getDefaultAccountId(db: Database
|
|
44
|
+
export function getDefaultAccountId(db: Database) {
|
|
45
45
|
const row = db
|
|
46
46
|
.prepare(
|
|
47
47
|
`
|
|
@@ -55,7 +55,7 @@ export function getDefaultAccountId(db: Database.Database) {
|
|
|
55
55
|
return row?.id ?? "acct_primary";
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
-
export function getAccountHandle(db: Database
|
|
58
|
+
export function getAccountHandle(db: Database, accountId: string) {
|
|
59
59
|
const row = db
|
|
60
60
|
.prepare("select handle from accounts where id = ?")
|
|
61
61
|
.get(accountId) as { handle: string } | undefined;
|
|
@@ -63,7 +63,7 @@ export function getAccountHandle(db: Database.Database, accountId: string) {
|
|
|
63
63
|
}
|
|
64
64
|
|
|
65
65
|
export function resolveLocalProfile(
|
|
66
|
-
db: Database
|
|
66
|
+
db: Database,
|
|
67
67
|
normalizedQuery: string,
|
|
68
68
|
): ResolvedModerationProfile | null {
|
|
69
69
|
const row = db
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type Database from "
|
|
1
|
+
import type { Database } from "./sqlite";
|
|
2
2
|
import type { ActionsTransport } from "./config";
|
|
3
3
|
import { getNativeDb } from "./db";
|
|
4
4
|
import {
|
|
@@ -43,7 +43,7 @@ export async function resolveModerationTarget({
|
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
export function writeModerationRow(
|
|
46
|
-
db: Database
|
|
46
|
+
db: Database,
|
|
47
47
|
table: "blocks" | "mutes",
|
|
48
48
|
accountId: string,
|
|
49
49
|
profileId: string,
|
|
@@ -61,7 +61,7 @@ export function writeModerationRow(
|
|
|
61
61
|
}
|
|
62
62
|
|
|
63
63
|
export function deleteModerationRow(
|
|
64
|
-
db: Database
|
|
64
|
+
db: Database,
|
|
65
65
|
table: "blocks" | "mutes",
|
|
66
66
|
accountId: string,
|
|
67
67
|
profileId: string,
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import type { Database } from "./sqlite";
|
|
2
|
+
import { lookupProfileViaBird } from "./bird";
|
|
3
|
+
import { syncIdentitySearchIndexForProfileIds } from "./identity-search-index";
|
|
4
|
+
import { syncProfileBioEntitiesForProfileId } from "./profile-bio-entities";
|
|
5
|
+
import { recordProfileSnapshot } from "./profile-history";
|
|
6
|
+
import { upsertProfileFromXUser } from "./x-profile";
|
|
7
|
+
|
|
8
|
+
export interface ProfileAffiliationHydrationResult {
|
|
9
|
+
checked: number;
|
|
10
|
+
hydrated: number;
|
|
11
|
+
skipped: number;
|
|
12
|
+
errors: Array<{ handle: string; error: string }>;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
interface SyntheticAffiliationRow {
|
|
16
|
+
subject_profile_id: string;
|
|
17
|
+
organization_profile_id: string;
|
|
18
|
+
organization_name: string | null;
|
|
19
|
+
organization_handle: string | null;
|
|
20
|
+
badge_url: string | null;
|
|
21
|
+
url: string | null;
|
|
22
|
+
label: string | null;
|
|
23
|
+
source: string;
|
|
24
|
+
raw_json: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function normalizeHandle(value: string | null) {
|
|
28
|
+
const handle = value?.trim().replace(/^@/, "");
|
|
29
|
+
return handle && handle.length > 0 ? handle : null;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function replaceSyntheticAffiliation(
|
|
33
|
+
db: Database,
|
|
34
|
+
row: SyntheticAffiliationRow,
|
|
35
|
+
realProfileId: string,
|
|
36
|
+
) {
|
|
37
|
+
const existing = db
|
|
38
|
+
.prepare(
|
|
39
|
+
`
|
|
40
|
+
select 1
|
|
41
|
+
from profile_affiliations
|
|
42
|
+
where subject_profile_id = ?
|
|
43
|
+
and organization_profile_id = ?
|
|
44
|
+
limit 1
|
|
45
|
+
`,
|
|
46
|
+
)
|
|
47
|
+
.get(row.subject_profile_id, realProfileId);
|
|
48
|
+
|
|
49
|
+
if (existing) {
|
|
50
|
+
db.prepare(
|
|
51
|
+
`
|
|
52
|
+
update profile_affiliations
|
|
53
|
+
set organization_name = coalesce(organization_name, ?),
|
|
54
|
+
organization_handle = coalesce(organization_handle, ?),
|
|
55
|
+
badge_url = coalesce(badge_url, ?),
|
|
56
|
+
url = coalesce(url, ?),
|
|
57
|
+
label = coalesce(label, ?),
|
|
58
|
+
is_active = 1,
|
|
59
|
+
last_seen_at = ?,
|
|
60
|
+
updated_at = ?
|
|
61
|
+
where subject_profile_id = ?
|
|
62
|
+
and organization_profile_id = ?
|
|
63
|
+
`,
|
|
64
|
+
).run(
|
|
65
|
+
row.organization_name,
|
|
66
|
+
row.organization_handle,
|
|
67
|
+
row.badge_url,
|
|
68
|
+
row.url,
|
|
69
|
+
row.label,
|
|
70
|
+
new Date().toISOString(),
|
|
71
|
+
new Date().toISOString(),
|
|
72
|
+
row.subject_profile_id,
|
|
73
|
+
realProfileId,
|
|
74
|
+
);
|
|
75
|
+
db.prepare(
|
|
76
|
+
`
|
|
77
|
+
delete from profile_affiliations
|
|
78
|
+
where subject_profile_id = ?
|
|
79
|
+
and organization_profile_id = ?
|
|
80
|
+
`,
|
|
81
|
+
).run(row.subject_profile_id, row.organization_profile_id);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
db.prepare(
|
|
86
|
+
`
|
|
87
|
+
update profile_affiliations
|
|
88
|
+
set organization_profile_id = ?,
|
|
89
|
+
updated_at = ?
|
|
90
|
+
where subject_profile_id = ?
|
|
91
|
+
and organization_profile_id = ?
|
|
92
|
+
`,
|
|
93
|
+
).run(
|
|
94
|
+
realProfileId,
|
|
95
|
+
new Date().toISOString(),
|
|
96
|
+
row.subject_profile_id,
|
|
97
|
+
row.organization_profile_id,
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function findLocalOrganizationProfileId(db: Database, handle: string) {
|
|
102
|
+
const row = db
|
|
103
|
+
.prepare(
|
|
104
|
+
`
|
|
105
|
+
select id
|
|
106
|
+
from profiles
|
|
107
|
+
where lower(handle) = lower(?)
|
|
108
|
+
limit 1
|
|
109
|
+
`,
|
|
110
|
+
)
|
|
111
|
+
.get(handle) as { id: string } | undefined;
|
|
112
|
+
return row?.id ?? null;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export async function hydrateProfileAffiliationOrganizations(
|
|
116
|
+
db: Database,
|
|
117
|
+
subjectProfileId: string,
|
|
118
|
+
): Promise<ProfileAffiliationHydrationResult> {
|
|
119
|
+
const rows = db
|
|
120
|
+
.prepare(
|
|
121
|
+
`
|
|
122
|
+
select subject_profile_id, organization_profile_id, organization_name,
|
|
123
|
+
organization_handle, badge_url, url, label, source, raw_json
|
|
124
|
+
from profile_affiliations
|
|
125
|
+
where subject_profile_id = ?
|
|
126
|
+
and is_active = 1
|
|
127
|
+
and organization_profile_id like 'profile_affiliation_%'
|
|
128
|
+
and organization_handle is not null
|
|
129
|
+
order by last_seen_at desc
|
|
130
|
+
`,
|
|
131
|
+
)
|
|
132
|
+
.all(subjectProfileId) as SyntheticAffiliationRow[];
|
|
133
|
+
|
|
134
|
+
const result: ProfileAffiliationHydrationResult = {
|
|
135
|
+
checked: rows.length,
|
|
136
|
+
hydrated: 0,
|
|
137
|
+
skipped: 0,
|
|
138
|
+
errors: [],
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
for (const row of rows) {
|
|
142
|
+
const handle = normalizeHandle(row.organization_handle);
|
|
143
|
+
if (!handle) {
|
|
144
|
+
result.skipped += 1;
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
try {
|
|
148
|
+
const localOrganizationProfileId = findLocalOrganizationProfileId(
|
|
149
|
+
db,
|
|
150
|
+
handle,
|
|
151
|
+
);
|
|
152
|
+
if (localOrganizationProfileId) {
|
|
153
|
+
db.transaction(() => {
|
|
154
|
+
replaceSyntheticAffiliation(db, row, localOrganizationProfileId);
|
|
155
|
+
})();
|
|
156
|
+
result.hydrated += 1;
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const user = await lookupProfileViaBird(handle);
|
|
161
|
+
if (!user) {
|
|
162
|
+
result.skipped += 1;
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
const resolved = upsertProfileFromXUser(db, user);
|
|
166
|
+
if (resolved.profile.id === row.organization_profile_id) {
|
|
167
|
+
result.skipped += 1;
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
db.transaction(() => {
|
|
171
|
+
replaceSyntheticAffiliation(db, row, resolved.profile.id);
|
|
172
|
+
})();
|
|
173
|
+
result.hydrated += 1;
|
|
174
|
+
} catch (error) {
|
|
175
|
+
result.errors.push({
|
|
176
|
+
handle,
|
|
177
|
+
error: error instanceof Error ? error.message : String(error),
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
if (result.hydrated > 0) {
|
|
183
|
+
recordProfileSnapshot(db, subjectProfileId, "affiliation_hydration");
|
|
184
|
+
syncProfileBioEntitiesForProfileId(db, subjectProfileId);
|
|
185
|
+
syncIdentitySearchIndexForProfileIds(db, [subjectProfileId]);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
return result;
|
|
189
|
+
}
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import type { Database } from "./sqlite";
|
|
3
|
+
import type { ProfileAffiliation, XurlMentionUser } from "./types";
|
|
4
|
+
|
|
5
|
+
interface NormalizedAffiliationInput {
|
|
6
|
+
organizationProfileId: string;
|
|
7
|
+
organizationName?: string;
|
|
8
|
+
organizationHandle?: string;
|
|
9
|
+
badgeUrl?: string | null;
|
|
10
|
+
url?: string | null;
|
|
11
|
+
label?: string | null;
|
|
12
|
+
source: string;
|
|
13
|
+
raw: Record<string, unknown>;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function getString(value: unknown) {
|
|
17
|
+
return typeof value === "string" && value.trim().length > 0
|
|
18
|
+
? value.trim()
|
|
19
|
+
: undefined;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function getStringArray(value: unknown) {
|
|
23
|
+
if (Array.isArray(value)) {
|
|
24
|
+
return value.filter(
|
|
25
|
+
(item): item is string => typeof item === "string" && item.length > 0,
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
const single = getString(value);
|
|
29
|
+
return single ? [single] : [];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function readFirst(record: Record<string, unknown>, keys: string[]) {
|
|
33
|
+
for (const key of keys) {
|
|
34
|
+
const value = getString(record[key]);
|
|
35
|
+
if (value) {
|
|
36
|
+
return value;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return undefined;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function inferHandleFromUrl(value: string | undefined) {
|
|
43
|
+
if (!value) {
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
46
|
+
try {
|
|
47
|
+
const url = new URL(value);
|
|
48
|
+
if (!/(^|\.)x\.com$|(^|\.)twitter\.com$/.test(url.hostname)) {
|
|
49
|
+
return undefined;
|
|
50
|
+
}
|
|
51
|
+
const [handle] = url.pathname.split("/").filter(Boolean);
|
|
52
|
+
return handle?.replace(/^@/, "");
|
|
53
|
+
} catch {
|
|
54
|
+
return undefined;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function syntheticOrganizationId({
|
|
59
|
+
label,
|
|
60
|
+
url,
|
|
61
|
+
badgeUrl,
|
|
62
|
+
}: {
|
|
63
|
+
label?: string | null;
|
|
64
|
+
url?: string | null;
|
|
65
|
+
badgeUrl?: string | null;
|
|
66
|
+
}) {
|
|
67
|
+
const hash = createHash("sha1")
|
|
68
|
+
.update([label, url, badgeUrl].filter(Boolean).join("|"))
|
|
69
|
+
.digest("hex")
|
|
70
|
+
.slice(0, 16);
|
|
71
|
+
return hash ? `profile_affiliation_${hash}` : undefined;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function normalizeProfileAffiliationsFromUser(
|
|
75
|
+
user: XurlMentionUser,
|
|
76
|
+
): NormalizedAffiliationInput[] {
|
|
77
|
+
const raw = user.affiliation;
|
|
78
|
+
if (!raw || typeof raw !== "object") {
|
|
79
|
+
return [];
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const organizationIds = [
|
|
83
|
+
...getStringArray(raw.organizationIds),
|
|
84
|
+
...getStringArray(raw.organization_ids),
|
|
85
|
+
...getStringArray(raw.userId),
|
|
86
|
+
...getStringArray(raw.user_id),
|
|
87
|
+
];
|
|
88
|
+
const label =
|
|
89
|
+
readFirst(raw, ["label", "description", "organizationName"]) ?? null;
|
|
90
|
+
const url = readFirst(raw, ["url", "expandedUrl", "expanded_url"]) ?? null;
|
|
91
|
+
const badgeUrl = readFirst(raw, ["badgeUrl", "badge_url"]) ?? null;
|
|
92
|
+
if (
|
|
93
|
+
organizationIds.length === 0 &&
|
|
94
|
+
label === null &&
|
|
95
|
+
url === null &&
|
|
96
|
+
badgeUrl === null
|
|
97
|
+
) {
|
|
98
|
+
return [];
|
|
99
|
+
}
|
|
100
|
+
const organizationHandle =
|
|
101
|
+
readFirst(raw, ["organizationHandle", "organization_handle", "username"]) ??
|
|
102
|
+
inferHandleFromUrl(url ?? undefined);
|
|
103
|
+
const ids =
|
|
104
|
+
organizationIds.length > 0
|
|
105
|
+
? Array.from(new Set(organizationIds))
|
|
106
|
+
: [
|
|
107
|
+
syntheticOrganizationId({
|
|
108
|
+
label,
|
|
109
|
+
url,
|
|
110
|
+
badgeUrl,
|
|
111
|
+
}),
|
|
112
|
+
].filter((item): item is string => Boolean(item));
|
|
113
|
+
|
|
114
|
+
return ids.map((organizationProfileId) => ({
|
|
115
|
+
organizationProfileId,
|
|
116
|
+
...(label ? { organizationName: label } : {}),
|
|
117
|
+
...(organizationHandle ? { organizationHandle } : {}),
|
|
118
|
+
badgeUrl,
|
|
119
|
+
url,
|
|
120
|
+
label,
|
|
121
|
+
source: "x_profile",
|
|
122
|
+
raw,
|
|
123
|
+
}));
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export function syncProfileAffiliationsFromUser(
|
|
127
|
+
db: Database,
|
|
128
|
+
subjectProfileId: string,
|
|
129
|
+
user: XurlMentionUser,
|
|
130
|
+
) {
|
|
131
|
+
const affiliations = normalizeProfileAffiliationsFromUser(user);
|
|
132
|
+
if (affiliations.length === 0) {
|
|
133
|
+
return [];
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const now = new Date().toISOString();
|
|
137
|
+
const statement = db.prepare(`
|
|
138
|
+
insert into profile_affiliations (
|
|
139
|
+
subject_profile_id, organization_profile_id, organization_name,
|
|
140
|
+
organization_handle, badge_url, url, label, source, is_active,
|
|
141
|
+
first_seen_at, last_seen_at, raw_json, updated_at
|
|
142
|
+
) values (?, ?, ?, ?, ?, ?, ?, ?, 1, ?, ?, ?, ?)
|
|
143
|
+
on conflict(subject_profile_id, organization_profile_id) do update set
|
|
144
|
+
organization_name = coalesce(excluded.organization_name, profile_affiliations.organization_name),
|
|
145
|
+
organization_handle = coalesce(excluded.organization_handle, profile_affiliations.organization_handle),
|
|
146
|
+
badge_url = coalesce(excluded.badge_url, profile_affiliations.badge_url),
|
|
147
|
+
url = coalesce(excluded.url, profile_affiliations.url),
|
|
148
|
+
label = coalesce(excluded.label, profile_affiliations.label),
|
|
149
|
+
source = excluded.source,
|
|
150
|
+
is_active = 1,
|
|
151
|
+
last_seen_at = excluded.last_seen_at,
|
|
152
|
+
raw_json = excluded.raw_json,
|
|
153
|
+
updated_at = excluded.updated_at
|
|
154
|
+
`);
|
|
155
|
+
|
|
156
|
+
for (const affiliation of affiliations) {
|
|
157
|
+
statement.run(
|
|
158
|
+
subjectProfileId,
|
|
159
|
+
affiliation.organizationProfileId,
|
|
160
|
+
affiliation.organizationName ?? null,
|
|
161
|
+
affiliation.organizationHandle ?? null,
|
|
162
|
+
affiliation.badgeUrl ?? null,
|
|
163
|
+
affiliation.url ?? null,
|
|
164
|
+
affiliation.label ?? null,
|
|
165
|
+
affiliation.source,
|
|
166
|
+
now,
|
|
167
|
+
now,
|
|
168
|
+
JSON.stringify(affiliation.raw),
|
|
169
|
+
now,
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
return affiliations;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export function fetchProfileAffiliations(db: Database, profileIds: string[]) {
|
|
177
|
+
if (profileIds.length === 0) {
|
|
178
|
+
return new Map<string, ProfileAffiliation[]>();
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const placeholders = profileIds.map(() => "?").join(",");
|
|
182
|
+
const rows = db
|
|
183
|
+
.prepare(
|
|
184
|
+
`
|
|
185
|
+
select
|
|
186
|
+
subject_profile_id,
|
|
187
|
+
organization_profile_id,
|
|
188
|
+
organization_name,
|
|
189
|
+
organization_handle,
|
|
190
|
+
badge_url,
|
|
191
|
+
url,
|
|
192
|
+
label,
|
|
193
|
+
source,
|
|
194
|
+
is_active,
|
|
195
|
+
first_seen_at,
|
|
196
|
+
last_seen_at
|
|
197
|
+
from profile_affiliations
|
|
198
|
+
where subject_profile_id in (${placeholders})
|
|
199
|
+
and is_active = 1
|
|
200
|
+
order by subject_profile_id, last_seen_at desc, organization_profile_id
|
|
201
|
+
`,
|
|
202
|
+
)
|
|
203
|
+
.all(...profileIds) as Array<Record<string, unknown>>;
|
|
204
|
+
|
|
205
|
+
const result = new Map<string, ProfileAffiliation[]>();
|
|
206
|
+
for (const row of rows) {
|
|
207
|
+
const subjectProfileId = String(row.subject_profile_id);
|
|
208
|
+
const affiliation: ProfileAffiliation = {
|
|
209
|
+
organizationProfileId: String(row.organization_profile_id),
|
|
210
|
+
...(typeof row.organization_name === "string" &&
|
|
211
|
+
row.organization_name.length > 0
|
|
212
|
+
? { organizationName: row.organization_name }
|
|
213
|
+
: {}),
|
|
214
|
+
...(typeof row.organization_handle === "string" &&
|
|
215
|
+
row.organization_handle.length > 0
|
|
216
|
+
? { organizationHandle: row.organization_handle }
|
|
217
|
+
: {}),
|
|
218
|
+
badgeUrl:
|
|
219
|
+
typeof row.badge_url === "string" ? String(row.badge_url) : null,
|
|
220
|
+
url: typeof row.url === "string" ? String(row.url) : null,
|
|
221
|
+
label: typeof row.label === "string" ? String(row.label) : null,
|
|
222
|
+
source: String(row.source),
|
|
223
|
+
firstSeenAt: String(row.first_seen_at),
|
|
224
|
+
lastSeenAt: String(row.last_seen_at),
|
|
225
|
+
isActive: Boolean(row.is_active),
|
|
226
|
+
};
|
|
227
|
+
const existing = result.get(subjectProfileId);
|
|
228
|
+
if (existing) {
|
|
229
|
+
existing.push(affiliation);
|
|
230
|
+
} else {
|
|
231
|
+
result.set(subjectProfileId, [affiliation]);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
return result;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
export function attachAffiliationsToProfiles<T extends { id: string }>(
|
|
239
|
+
db: Database,
|
|
240
|
+
profiles: T[],
|
|
241
|
+
): Array<
|
|
242
|
+
T & {
|
|
243
|
+
affiliations?: ProfileAffiliation[];
|
|
244
|
+
primaryAffiliation?: ProfileAffiliation;
|
|
245
|
+
}
|
|
246
|
+
> {
|
|
247
|
+
const affiliations = fetchProfileAffiliations(
|
|
248
|
+
db,
|
|
249
|
+
profiles.map((profile) => profile.id),
|
|
250
|
+
);
|
|
251
|
+
return profiles.map((profile) => {
|
|
252
|
+
const profileAffiliations = affiliations.get(profile.id) ?? [];
|
|
253
|
+
if (profileAffiliations.length === 0) {
|
|
254
|
+
return profile;
|
|
255
|
+
}
|
|
256
|
+
return {
|
|
257
|
+
...profile,
|
|
258
|
+
affiliations: profileAffiliations,
|
|
259
|
+
primaryAffiliation: profileAffiliations[0],
|
|
260
|
+
};
|
|
261
|
+
});
|
|
262
|
+
}
|