-
Notifications
You must be signed in to change notification settings - Fork 28
/
bskyHelpers.ts
88 lines (79 loc) · 2.21 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
79
80
81
82
83
84
85
86
87
88
import type { ProfileView } from "@atproto/api/dist/client/types/app/bsky/actor/defs";
import { BSKY_PROFILE_LABEL, BSKY_USER_MATCH_TYPE } from "./constants";
type xUserInfo = {
bskyHandleInDescription: string;
accountName: string;
accountNameRemoveUnderscore: string;
accountNameReplaceUnderscore: string;
displayName: string;
};
export const isSimilarUser = (
xUserInfo: xUserInfo,
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,
};
}
// this is to handle the case where the user has a bsky handle in their description
if (xUserInfo.bskyHandleInDescription) {
const formattedBskyHandle = bskyProfile.handle.replace("@", "");
const formattedBskyHandleInDescription =
xUserInfo.bskyHandleInDescription.replace("@", "");
if (
formattedBskyHandle === formattedBskyHandleInDescription ||
formattedBskyHandle.includes(formattedBskyHandleInDescription)
) {
return {
isSimilar: true,
type: BSKY_USER_MATCH_TYPE.DESCRIPTION,
};
}
}
const lowerCaseNames = Object.entries(xUserInfo).reduce<xUserInfo>(
(acc, [key, value]) => {
if (!value) {
return acc;
}
acc[key] = value.toLowerCase();
return acc;
},
{} as xUserInfo,
);
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,
};
}
return {
isSimilar: false,
type: BSKY_USER_MATCH_TYPE.NONE,
};
};
export const isImpersonationUser = (user: ProfileView) => {
return user.labels.some(
(label) => label.val === BSKY_PROFILE_LABEL.IMPERSONATION,
);
};