Skip to content

Commit

Permalink
refactor: rename
Browse files Browse the repository at this point in the history
  • Loading branch information
kawamataryo committed Jan 3, 2025
1 parent 16ae674 commit a758b3a
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 27 deletions.
35 changes: 15 additions & 20 deletions src/lib/bskyHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import type { ProfileView } from "@atproto/api/dist/client/types/app/bsky/actor/defs";
import type { CrawledUserInfo } from "~types";
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,
crawledUserInfo: Omit<
CrawledUserInfo,
"originalAvatar" | "originalProfileLink"
>,
bskyProfile: ProfileView | undefined,
): {
isSimilar: boolean;
Expand All @@ -24,10 +20,10 @@ export const isSimilarUser = (
}

// this is to handle the case where the user has a bsky handle in their description
if (xUserInfo.bskyHandleInDescription) {
if (crawledUserInfo.bskyHandleInDescription) {
const formattedBskyHandle = bskyProfile.handle.replace("@", "");
const formattedBskyHandleInDescription =
xUserInfo.bskyHandleInDescription.replace("@", "");
crawledUserInfo.bskyHandleInDescription.replace("@", "");
if (
formattedBskyHandle === formattedBskyHandleInDescription ||
formattedBskyHandle.includes(formattedBskyHandleInDescription)
Expand All @@ -39,16 +35,15 @@ export const isSimilarUser = (
}
}

const lowerCaseNames = Object.entries(xUserInfo).reduce<xUserInfo>(
(acc, [key, value]) => {
if (!value) {
return acc;
}
acc[key] = value.toLowerCase();
const lowerCaseNames = Object.entries(
crawledUserInfo,
).reduce<CrawledUserInfo>((acc, [key, value]) => {
if (!value) {
return acc;
},
{} as xUserInfo,
);
}
acc[key] = value.toLowerCase();
return acc;
}, {} as CrawledUserInfo);

const bskyHandle = bskyProfile.handle
.toLocaleLowerCase()
Expand Down
6 changes: 4 additions & 2 deletions src/lib/searchBskyUsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ export const searchBskyUser = async ({
userData: CrawledUserInfo;
}) => {
const searchTerms = [
...(userData.bskyHandle ? [userData.bskyHandle] : []),
...(userData.bskyHandleInDescription
? [userData.bskyHandleInDescription]
: []),
userData.accountNameRemoveUnderscore,
userData.accountNameReplaceUnderscore,
userData.displayName,
Expand All @@ -39,7 +41,7 @@ export const searchBskyUser = async ({
const { isSimilar: isUserFound, type } = isSimilarUser(
// TODO: simplify
{
bskyHandleInDescription: userData.bskyHandle,
bskyHandleInDescription: userData.bskyHandleInDescription,
accountName: userData.accountName,
accountNameRemoveUnderscore: userData.accountNameRemoveUnderscore,
accountNameReplaceUnderscore: userData.accountNameReplaceUnderscore,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/services/instagramService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export class InstagramService implements IService {
displayName,
accountNameRemoveUnderscore,
accountNameReplaceUnderscore,
bskyHandle: "",
bskyHandleInDescription: "",
originalAvatar: avatarSrc,
originalProfileLink: `https://www.instagram.com/${_accountName}`,
};
Expand Down
2 changes: 1 addition & 1 deletion src/lib/services/threadsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class ThreadsService implements IService {
displayName,
accountNameRemoveUnderscore,
accountNameReplaceUnderscore,
bskyHandle: "",
bskyHandleInDescription: "",
originalAvatar: avatarSrc,
originalProfileLink: `https://www.threads.net/@${_accountName}`,
};
Expand Down
4 changes: 2 additions & 2 deletions src/lib/services/xService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class XService implements IService {
const accountNameRemoveUnderscore = accountName.replaceAll("_", ""); // bsky does not allow underscores in handle, so remove them.
const accountNameReplaceUnderscore = accountName.replaceAll("_", "-");
const displayName = displayNameEl?.textContent;
const bskyHandle =
const bskyHandleInDescription =
userCell.textContent?.match(
new RegExp(`([^/\\s]+\\.${BSKY_DOMAIN})`),
)?.[1] ??
Expand All @@ -64,7 +64,7 @@ export class XService implements IService {
displayName,
accountNameRemoveUnderscore,
accountNameReplaceUnderscore,
bskyHandle,
bskyHandleInDescription,
originalAvatar,
originalProfileLink: `https://x.com/${accountName}`,
};
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export type CrawledUserInfo = {
displayName: string;
accountNameRemoveUnderscore: string;
accountNameReplaceUnderscore: string;
bskyHandle: string;
bskyHandleInDescription: string;
originalAvatar: string;
originalProfileLink: string;
};
Expand Down

0 comments on commit a758b3a

Please sign in to comment.