improve match logic

This commit is contained in:
kawamataryo 2023-08-20 15:22:55 +09:00
parent ea28f45e7f
commit 1e24268375
3 changed files with 40 additions and 27 deletions

View File

@ -1,7 +1,13 @@
import type { ProfileView } from "@atproto/api/dist/client/types/app/bsky/actor/defs"
import { BSKY_USER_MATCH_TYPE } from "./constants"
export const isSimilarUser = (terms: string[], bskyProfile: ProfileView | undefined): {
type Names = {
accountName: string,
accountNameRemoveUnderscore: 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],
} => {
@ -12,27 +18,34 @@ export const isSimilarUser = (terms: string[], bskyProfile: ProfileView | undefi
}
}
for (const term of terms) {
const lowerCaseName = term.toLocaleLowerCase()
if (lowerCaseName === bskyProfile?.handle.toLocaleLowerCase().replace("@", "").split('.')[0]) {
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) {
return {
isSimilar: true,
type: BSKY_USER_MATCH_TYPE.HANDLE,
}
}
if (lowerCaseName === bskyProfile.displayName?.toLocaleLowerCase()) {
if (lowerCaseNames.displayName === bskyProfile.displayName?.toLocaleLowerCase()) {
return {
isSimilar: true,
type: BSKY_USER_MATCH_TYPE.DISPLAY_NAME,
}
}
if (bskyProfile.description?.toLocaleLowerCase().includes(lowerCaseName)) {
if (bskyProfile.description?.toLocaleLowerCase().includes(lowerCaseNames.accountName)) {
return {
isSimilar: true,
type: BSKY_USER_MATCH_TYPE.DESCRIPTION,
}
}
}
return {
isSimilar: false,
type: BSKY_USER_MATCH_TYPE.NONE,

View File

@ -86,7 +86,7 @@ const MatchTypeLabel = ({ matchType }: { matchType: typeof BSKY_USER_MATCH_TYPE[
const [text, labelClass] = match(matchType)
.with(
BSKY_USER_MATCH_TYPE.HANDLE,
() => ["Same handle", "match-type__handle"]
() => ["Same handle name", "match-type__handle"]
)
.with(
BSKY_USER_MATCH_TYPE.DISPLAY_NAME,
@ -94,7 +94,7 @@ const MatchTypeLabel = ({ matchType }: { matchType: typeof BSKY_USER_MATCH_TYPE[
)
.with(
BSKY_USER_MATCH_TYPE.DESCRIPTION,
() => ["Included handle or display name in description", "match-type__description"]
() => ["Included handle name in description", "match-type__description"]
)
.run()

View File

@ -64,11 +64,11 @@ export const searchAndInsertBskyUsers = async (
limit: 1,
})
const { isSimilar: isUserFound, type } = isSimilarUser([
twAccountName,
twAccountNameRemoveUnderscore,
twDisplayName,
], searchResult)
const { isSimilar: isUserFound, type } = isSimilarUser({
accountName: twAccountName,
accountNameRemoveUnderscore: twAccountNameRemoveUnderscore,
displayName: twDisplayName,
}, searchResult)
if (isUserFound) {
targetAccount = searchResult