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
|
@@ -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";
|
|
@@ -15,12 +15,14 @@ export interface ResolvedModerationProfile {
|
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
export function toProfile(row: Record<string, unknown>): ProfileRecord {
|
|
18
|
+
const followingCount = Number(row.following_count ?? 0);
|
|
18
19
|
return {
|
|
19
20
|
id: String(row.id),
|
|
20
21
|
handle: String(row.handle),
|
|
21
22
|
displayName: String(row.display_name),
|
|
22
23
|
bio: String(row.bio),
|
|
23
24
|
followersCount: Number(row.followers_count),
|
|
25
|
+
...(Number.isFinite(followingCount) ? { followingCount } : {}),
|
|
24
26
|
avatarHue: Number(row.avatar_hue),
|
|
25
27
|
avatarUrl:
|
|
26
28
|
typeof row.avatar_url === "string" ? String(row.avatar_url) : undefined,
|
|
@@ -39,7 +41,7 @@ export function normalizeProfileQuery(value: string) {
|
|
|
39
41
|
return (urlMatch?.[1] ?? withoutPrefix).replace(/^@/, "").trim();
|
|
40
42
|
}
|
|
41
43
|
|
|
42
|
-
export function getDefaultAccountId(db: Database
|
|
44
|
+
export function getDefaultAccountId(db: Database) {
|
|
43
45
|
const row = db
|
|
44
46
|
.prepare(
|
|
45
47
|
`
|
|
@@ -53,7 +55,7 @@ export function getDefaultAccountId(db: Database.Database) {
|
|
|
53
55
|
return row?.id ?? "acct_primary";
|
|
54
56
|
}
|
|
55
57
|
|
|
56
|
-
export function getAccountHandle(db: Database
|
|
58
|
+
export function getAccountHandle(db: Database, accountId: string) {
|
|
57
59
|
const row = db
|
|
58
60
|
.prepare("select handle from accounts where id = ?")
|
|
59
61
|
.get(accountId) as { handle: string } | undefined;
|
|
@@ -61,13 +63,13 @@ export function getAccountHandle(db: Database.Database, accountId: string) {
|
|
|
61
63
|
}
|
|
62
64
|
|
|
63
65
|
export function resolveLocalProfile(
|
|
64
|
-
db: Database
|
|
66
|
+
db: Database,
|
|
65
67
|
normalizedQuery: string,
|
|
66
68
|
): ResolvedModerationProfile | null {
|
|
67
69
|
const row = db
|
|
68
70
|
.prepare(
|
|
69
71
|
`
|
|
70
|
-
select id, handle, display_name, bio, followers_count, avatar_hue, avatar_url, created_at
|
|
72
|
+
select id, handle, display_name, bio, followers_count, following_count, avatar_hue, avatar_url, created_at
|
|
71
73
|
from profiles
|
|
72
74
|
where id = ? or handle = ?
|
|
73
75
|
limit 1
|
|
@@ -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
|
+
}
|