From 1c7a9c1b0a64d99fd944f1fb445e2e611fae4e2a Mon Sep 17 00:00:00 2001 From: DM Date: Mon, 30 Oct 2023 18:44:42 +0800 Subject: [PATCH] feat: get farcaster following address --- packages/site/src/pages/monitor/list.tsx | 4 +- packages/site/src/utils/snap.ts | 2 + packages/snap/snap.manifest.json | 2 +- packages/snap/src/farcaster.ts | 56 ++++++++++++++++++++++-- packages/snap/src/index.ts | 21 +-------- 5 files changed, 60 insertions(+), 25 deletions(-) diff --git a/packages/site/src/pages/monitor/list.tsx b/packages/site/src/pages/monitor/list.tsx index 7ece635..42464bc 100644 --- a/packages/site/src/pages/monitor/list.tsx +++ b/packages/site/src/pages/monitor/list.tsx @@ -70,7 +70,9 @@ const MonitorList = () => { key={monitor.search} className="flex flex-col items-start justify-center gap-4 border border-solid border-gray-100 w-full p-6 rounded-lg" > -

{monitor.search}

+

+ Monitor {monitor.search}'s following +

Last Updated:{' '} diff --git a/packages/site/src/utils/snap.ts b/packages/site/src/utils/snap.ts index cf2a330..9d8ec1b 100644 --- a/packages/site/src/utils/snap.ts +++ b/packages/site/src/utils/snap.ts @@ -62,6 +62,8 @@ export type TProfile = { handle: string; address?: string; avatar?: string; + activities?: SocialActivity[]; + lastActivities?: SocialActivity[]; }; export enum Platform { diff --git a/packages/snap/snap.manifest.json b/packages/snap/snap.manifest.json index 6194415..9f20341 100644 --- a/packages/snap/snap.manifest.json +++ b/packages/snap/snap.manifest.json @@ -7,7 +7,7 @@ "url": "https://github.com/NaturalSelectionLabs/RSS3-MetaMask-Snap.git" }, "source": { - "shasum": "UJyFr462IqzFG5RCIRT3NjyOdGLTJsI/5Kr/BMx8b6U=", + "shasum": "4j3pb8CbYuEUdHa7PUICtBRVvCEjyPan3AwxoUujXj8=", "location": { "npm": { "filePath": "dist/bundle.js", diff --git a/packages/snap/src/farcaster.ts b/packages/snap/src/farcaster.ts index 886df2d..607a615 100644 --- a/packages/snap/src/farcaster.ts +++ b/packages/snap/src/farcaster.ts @@ -71,7 +71,15 @@ export async function getOwnerProfileByUsername(username: string) { /** * If there are no errors, return the owner profile. */ - const { result } = json as { result: { user: TFarcasterUser } }; + const { result } = json as { + result: { + user: TFarcasterUser; + extras: { + fid: number; + custodyAddress: string; + }; + }; + }; return { handle: result.user.username, avatar: result.user.pfp.url, @@ -79,6 +87,7 @@ export async function getOwnerProfileByUsername(username: string) { followerCount: result.user.followerCount, followingCount: result.user.followingCount, bio: result.user.profile.bio.text, + address: result.extras.custodyAddress, }; } @@ -88,13 +97,50 @@ export async function getOwnerProfileByUsername(username: string) { * @param data - The response data from FarCaster API. * @returns An array of TProfile objects. */ -export function format(data: TFarcasterUser[]): TProfile[] { - return data.map((item) => { +export async function format(data: TFarcasterUser[]): Promise { + const promises = data.map(async (item) => { + const resp = await fetch(userByUsernameApi(item.username)); + const json = (await resp.json()) as + | { result: { user: TFarcasterUser } } + | TFarcasterError; + + /** + * If there are errors, return the handle. + */ + if ((json as TFarcasterError)?.errors) { + return { + handle: item.username, + avatar: item.pfp.url, + }; + } + + /** + * If there are no errors, return the owner profile. + */ + const { result } = json as { + result: { + user: TFarcasterUser; + extras: { + fid: number; + custodyAddress: string; + }; + }; + }; + return { handle: item.username, avatar: item.pfp.url, + address: result.extras.custodyAddress, }; }); + + return await Promise.all(promises); + // return data.map((item) => { + // return { + // handle: item.username, + // avatar: item.pfp.url, + // }; + // }); } /** @@ -114,7 +160,7 @@ export async function getFollowingByFid(fid: number) { : `${getFollowingByFidApi(fid)}&cursor=${cursor}`; const resp = await fetch(url); const data = (await resp.json()) as TFarcasterFollowingResponse; - following.push(...format(data.result.users)); + following.push(...(await format(data.result.users))); if (data.next === undefined) { hasNextPage = false; } else { @@ -137,6 +183,7 @@ export async function handler(handle: string): Promise { return { owner: { handle, + address: owner.address, }, status: false, message: `${handle} not found`, @@ -149,6 +196,7 @@ export async function handler(handle: string): Promise { owner: { handle: owner.handle, avatar: owner.avatar, + address: owner.address, }, status: true, message: 'success', diff --git a/packages/snap/src/index.ts b/packages/snap/src/index.ts index b75635d..2687f52 100644 --- a/packages/snap/src/index.ts +++ b/packages/snap/src/index.ts @@ -19,11 +19,6 @@ import { } from './state'; import { diff, getSocialActivities } from './fetch'; import { getProfilesBySearch } from './profiles'; -// import { -// executeCrossbell, -// executeFarcaster, -// executeLens, -// } from './relation-chain'; import { handler as CrossbellHandler } from './crossbel'; import { handler as LensHandler } from './lens'; import { handler as FarcasterHandler } from './farcaster'; @@ -47,6 +42,8 @@ export type TProfile = { handle: string; address?: string; avatar?: string; + activities?: SocialActivity[]; + lastActivities?: SocialActivity[]; }; export type FetchSocialCountParams = { @@ -430,19 +427,6 @@ export const onCronjob: OnCronjobHandler = async ({ request }) => { const state = await getState(); const monitorPromises = state.monitor.map(async (item) => { item.latestUpdateTime = moment().format('YYYY/MM/DD hh:mm:ss'); - - // const executeArray = [ - // { platform: Platform.Lens, handler: executeLens }, - // { - // platform: Platform.Crossbell, - // handler: executeCrossbell, - // }, - // { - // platform: Platform.Farcaster, - // handler: executeFarcaster, - // }, - // ]; - const handles = item.profiles .filter((profile) => profile.handle !== undefined) .map((profile) => { @@ -482,7 +466,6 @@ export const onCronjob: OnCronjobHandler = async ({ request }) => { } } }); - await Promise.all(promises); return { ...item,