fix: change search logic

This commit is contained in:
kawamataryo 2024-11-13 07:35:52 +09:00
parent ed98361201
commit 9141ef69d8
3 changed files with 32 additions and 4 deletions

View File

@ -1,7 +1,8 @@
import type { ProfileView } from "@atproto/api/dist/client/types/app/bsky/actor/defs";
import { BSKY_USER_MATCH_TYPE } from "./constants";
type Names = {
type xUserInfo = {
bskyHandleInDescription: string;
accountName: string;
accountNameRemoveUnderscore: string;
accountNameReplaceUnderscore: string;
@ -9,7 +10,7 @@ type Names = {
};
export const isSimilarUser = (
names: Names,
xUserInfo: xUserInfo,
bskyProfile: ProfileView | undefined,
): {
isSimilar: boolean;
@ -22,12 +23,31 @@ export const isSimilarUser = (
};
}
const lowerCaseNames = Object.entries(names).reduce<Names>(
// 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.HANDLE,
};
}
}
const lowerCaseNames = Object.entries(xUserInfo).reduce<xUserInfo>(
(acc, [key, value]) => {
if (!value) {
return acc;
}
acc[key] = value.toLowerCase();
return acc;
},
{} as Names,
{} as xUserInfo,
);
const bskyHandle = bskyProfile.handle

View File

@ -25,10 +25,16 @@ export const getAccountNameAndDisplayName = (userCell: Element) => {
const twAccountNameRemoveUnderscore = twAccountName.replaceAll("_", ""); // bsky does not allow underscores in handle, so remove them.
const twAccountNameReplaceUnderscore = twAccountName.replaceAll("_", "-");
const twDisplayName = displayNameEl?.textContent;
const bskyHandle =
userCell.textContent?.match(/([^/\s]+\.bsky\.social)/)?.[1] ??
userCell.textContent?.match(/bsky\.app\/profile\/([^/\s]+)…/)?.[1] ??
null;
return {
twAccountName,
twDisplayName,
twAccountNameRemoveUnderscore,
twAccountNameReplaceUnderscore,
bskyHandle,
};
};

View File

@ -11,6 +11,7 @@ export const searchBskyUser = async ({
userData: ReturnType<typeof getAccountNameAndDisplayName>;
}) => {
const searchTerms = [
...(userData.bskyHandle ? [userData.bskyHandle] : []),
userData.twAccountNameRemoveUnderscore,
userData.twAccountNameReplaceUnderscore,
userData.twDisplayName,
@ -31,6 +32,7 @@ export const searchBskyUser = async ({
const { isSimilar: isUserFound, type } = isSimilarUser(
// TODO: simplify
{
bskyHandleInDescription: userData.bskyHandle,
accountName: userData.twAccountName,
accountNameRemoveUnderscore: userData.twAccountNameRemoveUnderscore,
accountNameReplaceUnderscore: