Skip to content

Commit

Permalink
For You Feed backend + api
Browse files Browse the repository at this point in the history
  • Loading branch information
artlu99 committed Jun 25, 2024
1 parent bcb83f6 commit f3e2383
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 0 deletions.
27 changes: 27 additions & 0 deletions functions/getForYouFeed/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Env } from '../common';
import { FeedObject } from '../shared/feed-types';

export const onRequestPost: PagesFunction<Env> = async (context) => {
const { request } = context;
const js = (await request.json()) as { fid: number; limit: number; cursor?: string };
const { fid, limit, cursor } = js;

const neynarApiKey = context.env.NEYNAR_API_KEY;
const provider = 'karma';

const paginationParam = cursor ? `&cursor=${cursor}` : '';
const endpoint = `https://api.neynar.com/v2/farcaster/feed/for_you?fid=${fid}&viewer_fid=${fid}&provider=${provider}&limit=${limit}${paginationParam}`;

const res = await fetch(endpoint, {
method: 'GET',
headers: {
accept: 'application/json',
api_key: `${neynarApiKey}`,
},
});

if (!res.ok) throw new Error('Failed to fetch data');

const forYouFeedResponse = (await res.json()) as FeedObject;
return new Response(JSON.stringify(forYouFeedResponse));
};
19 changes: 19 additions & 0 deletions functions/shared/feed-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface CastObject {
mentioned_profiles?: UserProfile[];
};
};
viewer_context?: AuthorViewerContext;
};
text: string;
mentioned_profiles: UserProfile[];
Expand All @@ -39,6 +40,8 @@ export interface CastObject {
parent_url?: string;
embeds?: EmbedObject[];
frames?: FrameObject[];
channel?: ChannelObject;
viewer_context?: CastViewerContext;
reactions: {
likes: UserDetails[];
likes_count: number;
Expand All @@ -48,6 +51,22 @@ export interface CastObject {
replies: { count: number };
}

interface AuthorViewerContext {
following: boolean;
followed_by: boolean;
}

interface CastViewerContext {
liked: false;
recasted: false;
}
interface ChannelObject {
object: 'channel_dehydrated';
id: string;
name: string;
image_url: string;
}

export interface EmbedObject {
url?: string;
cast_id?: {
Expand Down
19 changes: 19 additions & 0 deletions src/api/feed-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface CastObject {
mentioned_profiles?: UserProfile[];
};
};
viewer_context?: AuthorViewerContext;
};
text: string;
mentioned_profiles: UserProfile[];
Expand All @@ -39,6 +40,8 @@ export interface CastObject {
parent_url?: string;
embeds?: EmbedObject[];
frames?: FrameObject[];
channel?: ChannelObject;
viewer_context?: CastViewerContext;
reactions: {
likes: UserDetails[];
likes_count: number;
Expand All @@ -48,6 +51,22 @@ export interface CastObject {
replies: { count: number };
}

interface AuthorViewerContext {
following: boolean;
followed_by: boolean;
}

interface CastViewerContext {
liked: false;
recasted: false;
}
interface ChannelObject {
object: 'channel_dehydrated';
id: string;
name: string;
image_url: string;
}

export interface EmbedObject {
url?: string;
cast_id?: {
Expand Down
17 changes: 17 additions & 0 deletions src/api/forYouFeed.api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { httpApi } from '@app/api/http.api';
import './mocks/mockornot';

interface ForYouFeedRequest {
fid: number;
limit: number;
cursor?: string;
}

interface ForYouFeedResponse {
casts: {
object: 'cast';
}[];
cursor?: string;
}
export const getNeynarOpenrankForYouFeed = (forYouFeedRequestPayload: ForYouFeedRequest): Promise<ForYouFeedResponse> =>
httpApi.post<ForYouFeedResponse>('getForYouFeed', { ...forYouFeedRequestPayload }).then(({ data }) => data);
96 changes: 96 additions & 0 deletions src/api/mocks/forYouFeed.api.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { FeedObject } from '@app/api/feed-types';
import { RawAPIResponse, transformRawAPIResponse } from '@app/api/mocks/cronFeed.api.mock';
import { httpApiMock } from '@app/api/mocks/http.api.mock';

const rawForYouFeed: RawAPIResponse = {
casts: [
{
object: 'cast',
hash: '0x14be3ad800e885d545671acb647123d70bd8b182',
thread_hash: '0x14be3ad800e885d545671acb647123d70bd8b182',
parent_hash: null,
parent_url: 'https://warpcast.com/~/channel/six',
root_parent_url: 'https://warpcast.com/~/channel/six',
parent_author: {
fid: null,
},
author: {
object: 'user',
fid: 680,
custody_address: '0x0b28a373fc8d92669aefb420499e74ce2dd5d356',
username: 'woj.eth',
display_name: '​woj',
pfp_url:
'https://peach-changing-limpet-80.mypinata.cloud/ipfs/QmZenqRfBERmodRVUHhLaYvYVsRasihVcMY2gqUP2tX7t2?filename=profile_picture.png',
profile: {
bio: {
text: 'customer support @supercast\n\ntrying to sound smart /doingnumbers',
},
},
follower_count: 155270,
following_count: 982,
verifications: ['0xf417ace7b13c0ef4fcb5548390a450a4b75d3eb3'],
verified_addresses: {
eth_addresses: ['0xf417ace7b13c0ef4fcb5548390a450a4b75d3eb3'],
sol_addresses: ['9hrZunMWouN6AtFp4Hoi3e4yWkVT32ZyfsQVqkmqLAft'],
},
active_status: 'inactive',
power_badge: true,
viewer_context: {
following: true,
followed_by: false,
},
},
text: 'threads algo absolutely cooked',
timestamp: '2024-06-25T12:45:13.000Z',
embeds: [
{
url: 'https://supercast.mypinata.cloud/ipfs/QmVXYY4ekyvGKi4Kem6iTUEc1xhpiVoArHeXx7XUnEvMhe?filename=IMG_2551.png',
},
],
reactions: {
likes_count: 22,
recasts_count: 0,
likes: [
{
fid: 4167,
fname: 'nounishprof',
},
{
fid: 648638,
fname: 'fac12344321666',
},
{
fid: 530854,
fname: 'javahed',
},
{
fid: 380246,
fname: 'filimonovaver',
},
{
fid: 697932,
fname: 'illeks',
},
],
recasts: [],
},
replies: {
count: 7,
},
channel: {
object: 'channel_dehydrated',
id: 'six',
name: 'six',
image_url: 'https://i.imgur.com/eLsRTEP.jpg',
},
mentioned_profiles: [],
viewer_context: {
liked: false,
recasted: false,
},
},
],
};
const mockForYouFeed: FeedObject = transformRawAPIResponse(rawForYouFeed);
httpApiMock.onPost('getForYouFeed').reply(200, mockForYouFeed);
1 change: 1 addition & 0 deletions src/api/mocks/mockornot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// import './fcan.api.mock';
// import './followingByFid.api.mock';
// import './followingFeed.api.mock';
// import './forYouFeed.api.mock';
// import './hubReactionsByFid.api.mock';
// import './metadata.api.mock';
// import './neynarReactionsByFid.api.mock';
Expand Down

0 comments on commit f3e2383

Please sign in to comment.