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 type { ProfileView } from "@atproto/api/dist/client/types/app/bsky/actor/defs"
import { BSKY_USER_MATCH_TYPE } from "./constants" 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, isSimilar: boolean,
type: typeof BSKY_USER_MATCH_TYPE[keyof typeof BSKY_USER_MATCH_TYPE], 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 lowerCaseNames = Object.entries(names).reduce<Names>((acc, [key, value]) => {
const lowerCaseName = term.toLocaleLowerCase() acc[key] = value.toLowerCase();
if (lowerCaseName === bskyProfile?.handle.toLocaleLowerCase().replace("@", "").split('.')[0]) { return acc;
return { }, {} as Names);
isSimilar: true,
type: BSKY_USER_MATCH_TYPE.HANDLE, const bskyHandle = bskyProfile.handle.toLocaleLowerCase().replace("@", "").split('.')[0];
}
} if (lowerCaseNames.accountName === bskyHandle || lowerCaseNames.accountNameRemoveUnderscore === bskyHandle) {
if (lowerCaseName === bskyProfile.displayName?.toLocaleLowerCase()) { return {
return { isSimilar: true,
isSimilar: true, type: BSKY_USER_MATCH_TYPE.HANDLE,
type: BSKY_USER_MATCH_TYPE.DISPLAY_NAME,
}
}
if (bskyProfile.description?.toLocaleLowerCase().includes(lowerCaseName)) {
return {
isSimilar: true,
type: BSKY_USER_MATCH_TYPE.DESCRIPTION,
}
} }
} }
if (lowerCaseNames.displayName === bskyProfile.displayName?.toLocaleLowerCase()) {
return {
isSimilar: true,
type: BSKY_USER_MATCH_TYPE.DISPLAY_NAME,
}
}
if (bskyProfile.description?.toLocaleLowerCase().includes(lowerCaseNames.accountName)) {
return {
isSimilar: true,
type: BSKY_USER_MATCH_TYPE.DESCRIPTION,
}
}
return { return {
isSimilar: false, isSimilar: false,
type: BSKY_USER_MATCH_TYPE.NONE, 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) const [text, labelClass] = match(matchType)
.with( .with(
BSKY_USER_MATCH_TYPE.HANDLE, BSKY_USER_MATCH_TYPE.HANDLE,
() => ["Same handle", "match-type__handle"] () => ["Same handle name", "match-type__handle"]
) )
.with( .with(
BSKY_USER_MATCH_TYPE.DISPLAY_NAME, BSKY_USER_MATCH_TYPE.DISPLAY_NAME,
@ -94,7 +94,7 @@ const MatchTypeLabel = ({ matchType }: { matchType: typeof BSKY_USER_MATCH_TYPE[
) )
.with( .with(
BSKY_USER_MATCH_TYPE.DESCRIPTION, BSKY_USER_MATCH_TYPE.DESCRIPTION,
() => ["Included handle or display name in description", "match-type__description"] () => ["Included handle name in description", "match-type__description"]
) )
.run() .run()

View File

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