Skip to content
This repository has been archived by the owner on Jan 18, 2024. It is now read-only.

Commit

Permalink
fix: use correct cast type
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-grover committed Oct 23, 2023
1 parent 9366675 commit 234c33a
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 7 deletions.
10 changes: 7 additions & 3 deletions src/server/NeynarClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import { Hash } from 'viem'
import { mnemonicToAccount } from 'viem/accounts'
import {
Cast,
CastV1,
CastV1WithViewerContext,
CastWithViewerContext,
convertCasts,
GeneratedSigner,
Notification,
NotificationWithViewerContext,
Expand Down Expand Up @@ -141,12 +144,13 @@ export default class NeynarClient {
threadHash: string,
viewer: number,
): Promise<{ result: { casts: CastWithViewerContext[] } }>
getCastsInThread(threadHash: string, viewer?: number | null) {
async getCastsInThread(threadHash: string, viewer?: number | null) {
const params = new URLSearchParams({ threadHash })
if (viewer) params.set('viewerFid', viewer.toString())
return this.get<{
result: { casts: Cast[] | CastWithViewerContext[] }
const response = await this.get<{
result: { casts: CastV1[] | CastV1WithViewerContext[] }
}>('all-casts-in-thread', params, 1)
return { result: { casts: convertCasts(response.result.casts) } }
}

getMentionsAndReplies(
Expand Down
114 changes: 110 additions & 4 deletions src/server/types/Cast.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Hash } from 'viem'
import { Address, Hash } from 'viem'

export type Cast = {
hash: Hash
Expand All @@ -20,8 +20,8 @@ export type Cast = {
}
follower_count: number
following_count: number
verifications: Hash[]
active_status: 'active'
verifications: Address[]
active_status: 'active' | 'inactive'
}
text: string
timestamp: string
Expand All @@ -36,7 +36,7 @@ export type Cast = {
}

export type CastWithViewerContext = Cast & {
viewerContext: {
viewer_context: {
liked: boolean
recasted: boolean
}
Expand All @@ -54,3 +54,109 @@ type Recast = {
fid: number
fname: string
}

export type CastV1 = {
hash: Hash
threadHash: Hash | null
parentHash: Hash | null
parentUrl: string | null
parentAuthor: {
fid: number | null
}
author: {
fid: number
custodyAddress: Address
username: string
displayName: string
pfp: {
url: string
}
profile: {
bio: {
text: string
mentions: [] // Known bug - this is always empty
}
}
followerCount: number
followingCount: number
verifications: Address[]
activeStatus: 'active' | 'inactive'
}
text: string
timestamp: string
embeds: Embed[]
reactions: {
count: number
fids: number[]
}
recasts: {
count: number
fids: number[]
}
recasters: string[]
replies: {
count: number
}
}

export type CastV1WithViewerContext = CastV1 & {
viewerContext: {
liked: boolean
recasted: boolean
}
}

export function convertCasts(casts: CastV1[]): Cast[]
export function convertCasts(
casts: CastV1WithViewerContext[],
): CastWithViewerContext[]
export function convertCasts(
casts: (CastV1 | CastV1WithViewerContext)[],
): (Cast | CastWithViewerContext)[] {
return casts.map((castV1) => {
const cast: Cast = {
hash: castV1.hash,
thread_hash: castV1.threadHash,
parent_hash: castV1.parentHash,
parent_url: castV1.parentUrl,
parent_author: castV1.parentAuthor,
author: {
fid: castV1.author.fid,
username: castV1.author.username,
display_name: castV1.author.displayName,
pfp_url: castV1.author.pfp.url,
profile: {
bio: {
text: castV1.author.profile.bio.text,
},
},
follower_count: castV1.author.followerCount,
following_count: castV1.author.followingCount,
verifications: castV1.author.verifications,
active_status: castV1.author.activeStatus,
},
text: castV1.text,
timestamp: castV1.timestamp,
embeds: castV1.embeds,
reactions: {
likes: castV1.reactions.fids.map((fid) => ({ fid })),
recasts: castV1.recasts.fids.map((fid, index) => ({
fid,
fname: castV1.recasters[index],
})),
},
replies: castV1.replies,
}

if ('viewerContext' in castV1) {
return {
...cast,
viewer_context: {
...castV1.viewerContext,
},
}
}

return cast
})
}

0 comments on commit 234c33a

Please sign in to comment.