Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
artlu99 committed Jul 1, 2024
1 parent c576460 commit a43ec7d
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 12 deletions.
1 change: 1 addition & 0 deletions functions/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface Env {
AIRSTACK_API_KEY: string;
UPSTASH_REDIS_REST_URL: string;
UPSTASH_REDIS_REST_TOKEN: string;
REACT_APP_DEFAULT_FID: number;
REACT_APP_PUBLIC_POSTHOG_HOST: string;
REACT_APP_PUBLIC_POSTHOG_KEY: string;
D1: D1Database;
Expand Down
4 changes: 2 additions & 2 deletions functions/getCastsByFollowing/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Env } from '../common';
import { FeedObject } from '../shared/feed-types';
import sendPosthogEvent from '../shared/posthog';
import { sendPosthogFid } from '../shared/posthog';

export const onRequestPost: PagesFunction<Env> = async (context) => {
const { request } = context;
Expand All @@ -20,7 +20,7 @@ export const onRequestPost: PagesFunction<Env> = async (context) => {
});
if (!res.ok) throw new Error('Failed to fetch data');

await sendPosthogEvent(context.env, 'getCastsByFollowing', 'not_tracking_by_fid');
await sendPosthogFid(context.env, 'getCastsByFollowing', fid);

const followingCronFeedResponse = (await res.json()) as FeedObject;
return new Response(JSON.stringify(followingCronFeedResponse));
Expand Down
4 changes: 2 additions & 2 deletions functions/getCronFeed/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Env } from '../common';
import { FeedObject } from '../shared/feed-types';
import sendPosthogEvent from '../shared/posthog';
import { sendPosthogChannelId } from '../shared/posthog';

export const onRequestPost: PagesFunction<Env> = async (context) => {
const { request } = context;
Expand All @@ -20,7 +20,7 @@ export const onRequestPost: PagesFunction<Env> = async (context) => {
});
if (!res.ok) throw new Error('Failed to fetch data');

await sendPosthogEvent(context.env, 'getCronFeed', channelId);
await sendPosthogChannelId(context.env, 'getCronFeed', channelId);

const cronFeedResponse = (await res.json()) as FeedObject;
return new Response(JSON.stringify(cronFeedResponse));
Expand Down
4 changes: 2 additions & 2 deletions functions/getFollowingByFid/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Env } from '../common';
import { FollowingByFidResponseSchema } from '../shared/farquest-types';
import sendPosthogEvent from '../shared/posthog';
import { sendPosthogFid } from '../shared/posthog';

export const onRequestPost: PagesFunction<Env> = async (context) => {
const { request } = context;
Expand All @@ -20,7 +20,7 @@ export const onRequestPost: PagesFunction<Env> = async (context) => {
});
if (!res.ok) throw new Error('Failed to fetch data');

await sendPosthogEvent(context.env, 'getFollowingByFid', 'not_tracking_by_fid');
await sendPosthogFid(context.env, 'getFollowingByFid', fid);

const followingResponse = (await res.json()) as FollowingByFidResponseSchema;
return new Response(JSON.stringify(followingResponse));
Expand Down
4 changes: 2 additions & 2 deletions functions/getForYouFeed/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Env } from '../common';
import { FeedObject } from '../shared/feed-types';
import sendPosthogEvent from '../shared/posthog';
import { sendPosthogFid } from '../shared/posthog';

export const onRequestPost: PagesFunction<Env> = async (context) => {
const { request } = context;
Expand All @@ -26,7 +26,7 @@ export const onRequestPost: PagesFunction<Env> = async (context) => {
throw new Error('Failed to fetch data');
}

await sendPosthogEvent(context.env, 'getForYouFeed', 'not_tracking_by_fid');
await sendPosthogFid(context.env, 'getForYouFeed', fid);

const forYouFeedResponse = (await res.json()) as FeedObject;
return new Response(JSON.stringify(forYouFeedResponse));
Expand Down
4 changes: 2 additions & 2 deletions functions/getNeynarReactionsByFid/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ReactionType, ReactionsCastResponse } from '@neynar/nodejs-sdk/build/neynar-api/v2';
import { Env } from '../common';
import sendPosthogEvent from '../shared/posthog';
import { sendPosthogFid } from '../shared/posthog';

export const onRequestPost: PagesFunction<Env> = async (context) => {
const { request } = context;
Expand All @@ -27,7 +27,7 @@ export const onRequestPost: PagesFunction<Env> = async (context) => {

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

await sendPosthogEvent(context.env, 'getNeynarReactionsByFid', 'not_tracking_by_fid');
await sendPosthogFid(context.env, 'getNeynarReactionsByFid', fid);

const reactionsResponse = (await res.json()) as ReactionsCastResponse;
return new Response(JSON.stringify(reactionsResponse));
Expand Down
11 changes: 9 additions & 2 deletions functions/shared/posthog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const sendPosthogEvent = async (env: Env, event: string, distinct_id: string) =>
api_key,
distinct_id,
event,
properties: { none: undefined },
timestamp,
};

Expand All @@ -27,4 +26,12 @@ const sendPosthogEvent = async (env: Env, event: string, distinct_id: string) =>
}
};

export default sendPosthogEvent;
export const sendPosthogFid = async (env: Env, event: string, fid: number) => {
const distinct_id = fid === env.REACT_APP_DEFAULT_FID ? 'not signed in' : 'SIWN';
await sendPosthogEvent(env, event, distinct_id);
};

export const sendPosthogChannelId = async (env: Env, event: string, channelId: string) => {
const distinct_id = channelId;
await sendPosthogEvent(env, event, distinct_id);
};

0 comments on commit a43ec7d

Please sign in to comment.