Skip to content

Commit

Permalink
feat: get farcaster following address
Browse files Browse the repository at this point in the history
  • Loading branch information
dmoosocool committed Oct 30, 2023
1 parent 4485841 commit 1c7a9c1
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 25 deletions.
4 changes: 3 additions & 1 deletion packages/site/src/pages/monitor/list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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"
>
<h3 className="text-lg font-bold">{monitor.search}</h3>
<h3 className="text-lg font-bold">
Monitor {monitor.search}'s following
</h3>
<p>
Last Updated:{' '}
<span className="text-base text-muted-foreground">
Expand Down
2 changes: 2 additions & 0 deletions packages/site/src/utils/snap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ export type TProfile = {
handle: string;
address?: string;
avatar?: string;
activities?: SocialActivity[];
lastActivities?: SocialActivity[];
};

export enum Platform {
Expand Down
2 changes: 1 addition & 1 deletion packages/snap/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
56 changes: 52 additions & 4 deletions packages/snap/src/farcaster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,23 @@ 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,
fid: result.user.fid,
followerCount: result.user.followerCount,
followingCount: result.user.followingCount,
bio: result.user.profile.bio.text,
address: result.extras.custodyAddress,
};
}

Expand All @@ -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<TProfile[]> {
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,
// };
// });
}

/**
Expand All @@ -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 {
Expand All @@ -137,6 +183,7 @@ export async function handler(handle: string): Promise<TRelationChainResult> {
return {
owner: {
handle,
address: owner.address,
},
status: false,
message: `${handle} not found`,
Expand All @@ -149,6 +196,7 @@ export async function handler(handle: string): Promise<TRelationChainResult> {
owner: {
handle: owner.handle,
avatar: owner.avatar,
address: owner.address,
},
status: true,
message: 'success',
Expand Down
21 changes: 2 additions & 19 deletions packages/snap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -47,6 +42,8 @@ export type TProfile = {
handle: string;
address?: string;
avatar?: string;
activities?: SocialActivity[];
lastActivities?: SocialActivity[];
};

export type FetchSocialCountParams = {
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -482,7 +466,6 @@ export const onCronjob: OnCronjobHandler = async ({ request }) => {
}
}
});

await Promise.all(promises);
return {
...item,
Expand Down

0 comments on commit 1c7a9c1

Please sign in to comment.