-
Notifications
You must be signed in to change notification settings - Fork 28
/
bskyHelpers.ts
78 lines (70 loc) · 1.76 KB
/
bskyHelpers.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import type { ProfileView } from "@atproto/api/dist/client/types/app/bsky/actor/defs";
import { BSKY_USER_MATCH_TYPE } from "./constants";
type Names = {
accountName: string;
accountNameRemoveUnderscore: string;
accountNameReplaceUnderscore: string;
displayName: string;
};
export const isSimilarUser = (
names: Names,
bskyProfile: ProfileView | undefined,
): {
isSimilar: boolean;
type: (typeof BSKY_USER_MATCH_TYPE)[keyof typeof BSKY_USER_MATCH_TYPE];
} => {
if (!bskyProfile) {
return {
isSimilar: false,
type: BSKY_USER_MATCH_TYPE.NONE,
};
}
const lowerCaseNames = Object.entries(names).reduce<Names>(
(acc, [key, value]) => {
acc[key] = value.toLowerCase();
return acc;
},
{} as Names,
);
const bskyHandle = bskyProfile.handle
.toLocaleLowerCase()
.replace("@", "")
.split(".")[0];
if (
lowerCaseNames.accountName === bskyHandle ||
lowerCaseNames.accountNameRemoveUnderscore === bskyHandle ||
lowerCaseNames.accountNameReplaceUnderscore === bskyHandle
) {
return {
isSimilar: true,
type: BSKY_USER_MATCH_TYPE.HANDLE,
};
}
if (
lowerCaseNames.displayName === bskyProfile.displayName?.toLocaleLowerCase()
) {
return {
isSimilar: true,
type: BSKY_USER_MATCH_TYPE.DISPLAY_NAME,
};
}
if (
bskyProfile.description
?.toLocaleLowerCase()
.includes(`@${lowerCaseNames.accountName}`) &&
!["pfp ", "pfp: ", "pfp by "].some((t) =>
bskyProfile.description
.toLocaleLowerCase()
.includes(`${t}@${lowerCaseNames.accountName}`),
)
) {
return {
isSimilar: true,
type: BSKY_USER_MATCH_TYPE.DESCRIPTION,
};
}
return {
isSimilar: false,
type: BSKY_USER_MATCH_TYPE.NONE,
};
};