-
Notifications
You must be signed in to change notification settings - Fork 28
/
searchBskyUsers.ts
64 lines (58 loc) · 1.72 KB
/
searchBskyUsers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { isSimilarUser } from "~lib/bskyHelpers";
import { isOneSymbol } from "~lib/utils";
import type { CrawledUserInfo } from "~types";
import { isImpersonationUser } from "./bskyHelpers";
import type { BskyServiceWorkerClient } from "./bskyServiceWorkerClient";
export const searchBskyUser = async ({
client,
userData,
}: {
client: BskyServiceWorkerClient;
userData: CrawledUserInfo;
}) => {
const searchTerms = [
...(userData.bskyHandle ? [userData.bskyHandle] : []),
userData.accountNameRemoveUnderscore,
userData.accountNameReplaceUnderscore,
userData.displayName,
];
const uniqueSearchTerms = new Set(searchTerms);
for (const term of uniqueSearchTerms) {
// one symbol is not a valid search term for bsky
if (!term || isOneSymbol(term)) {
continue;
}
try {
const searchResults = await client.searchUser({
term,
limit: 3,
});
for (const searchResult of searchResults) {
// skip impersonation users
if (isImpersonationUser(searchResult)) {
continue;
}
const { isSimilar: isUserFound, type } = isSimilarUser(
// TODO: simplify
{
bskyHandleInDescription: userData.bskyHandle,
accountName: userData.accountName,
accountNameRemoveUnderscore: userData.accountNameRemoveUnderscore,
accountNameReplaceUnderscore: userData.accountNameReplaceUnderscore,
displayName: userData.displayName,
},
searchResult,
);
if (isUserFound) {
return {
bskyProfile: searchResult,
matchType: type,
};
}
}
} catch (e) {
console.error(e);
}
}
return null;
};