|
| 1 | +import { IAgentRuntime } from "@ai16z/eliza"; |
| 2 | +import { |
| 3 | + CastAddMessage, |
| 4 | + CastId, |
| 5 | + FidRequest, |
| 6 | + getInsecureHubRpcClient, |
| 7 | + getSSLHubRpcClient, |
| 8 | + HubRpcClient, |
| 9 | + isCastAddMessage, |
| 10 | + isUserDataAddMessage, |
| 11 | + Message, |
| 12 | + MessagesResponse, |
| 13 | +} from "@farcaster/hub-nodejs"; |
| 14 | +import { Cast, Profile } from "./types"; |
| 15 | +import { toHex } from "viem"; |
| 16 | +import { populateMentions } from "./utils"; |
| 17 | + |
| 18 | +export class FarcasterClient { |
| 19 | + runtime: IAgentRuntime; |
| 20 | + farcaster: HubRpcClient; |
| 21 | + |
| 22 | + cache: Map<string, any>; |
| 23 | + |
| 24 | + constructor(opts: { |
| 25 | + runtime: IAgentRuntime; |
| 26 | + url: string; |
| 27 | + ssl: boolean; |
| 28 | + cache: Map<string, any>; |
| 29 | + }) { |
| 30 | + this.cache = opts.cache; |
| 31 | + this.runtime = opts.runtime; |
| 32 | + this.farcaster = opts.ssl |
| 33 | + ? getSSLHubRpcClient(opts.url) |
| 34 | + : getInsecureHubRpcClient(opts.url); |
| 35 | + } |
| 36 | + |
| 37 | + async submitMessage(cast: Message, retryTimes?: number): Promise<void> { |
| 38 | + const result = await this.farcaster.submitMessage(cast); |
| 39 | + |
| 40 | + if (result.isErr()) { |
| 41 | + throw result.error; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + async loadCastFromMessage(message: CastAddMessage): Promise<Cast> { |
| 46 | + const profileMap = {}; |
| 47 | + |
| 48 | + const profile = await this.getProfile(message.data.fid); |
| 49 | + |
| 50 | + profileMap[message.data.fid] = profile; |
| 51 | + |
| 52 | + for (const mentionId of message.data.castAddBody.mentions) { |
| 53 | + if (profileMap[mentionId]) continue; |
| 54 | + profileMap[mentionId] = await this.getProfile(mentionId); |
| 55 | + } |
| 56 | + |
| 57 | + const text = populateMentions( |
| 58 | + message.data.castAddBody.text, |
| 59 | + message.data.castAddBody.mentions, |
| 60 | + message.data.castAddBody.mentionsPositions, |
| 61 | + profileMap |
| 62 | + ); |
| 63 | + |
| 64 | + return { |
| 65 | + id: toHex(message.hash), |
| 66 | + message, |
| 67 | + text, |
| 68 | + profile, |
| 69 | + }; |
| 70 | + } |
| 71 | + |
| 72 | + async getCast(castId: CastId): Promise<Message> { |
| 73 | + const castHash = toHex(castId.hash); |
| 74 | + |
| 75 | + if (this.cache.has(`farcaster/cast/${castHash}`)) { |
| 76 | + return this.cache.get(`farcaster/cast/${castHash}`); |
| 77 | + } |
| 78 | + |
| 79 | + const cast = await this.farcaster.getCast(castId); |
| 80 | + |
| 81 | + if (cast.isErr()) { |
| 82 | + throw cast.error; |
| 83 | + } |
| 84 | + |
| 85 | + this.cache.set(`farcaster/cast/${castHash}`, cast); |
| 86 | + |
| 87 | + return cast.value; |
| 88 | + } |
| 89 | + |
| 90 | + async getCastsByFid(request: FidRequest): Promise<MessagesResponse> { |
| 91 | + const cast = await this.farcaster.getCastsByFid(request); |
| 92 | + if (cast.isErr()) { |
| 93 | + throw cast.error; |
| 94 | + } |
| 95 | + |
| 96 | + cast.value.messages.map((cast) => { |
| 97 | + this.cache.set(`farcaster/cast/${toHex(cast.hash)}`, cast); |
| 98 | + }); |
| 99 | + |
| 100 | + return cast.value; |
| 101 | + } |
| 102 | + |
| 103 | + async getMentions(request: FidRequest): Promise<MessagesResponse> { |
| 104 | + const cast = await this.farcaster.getCastsByMention(request); |
| 105 | + if (cast.isErr()) { |
| 106 | + throw cast.error; |
| 107 | + } |
| 108 | + |
| 109 | + cast.value.messages.map((cast) => { |
| 110 | + this.cache.set(`farcaster/cast/${toHex(cast.hash)}`, cast); |
| 111 | + }); |
| 112 | + |
| 113 | + return cast.value; |
| 114 | + } |
| 115 | + |
| 116 | + async getProfile(fid: number): Promise<Profile> { |
| 117 | + if (this.cache.has(`farcaster/profile/${fid}`)) { |
| 118 | + return this.cache.get(`farcaster/profile/${fid}`) as Profile; |
| 119 | + } |
| 120 | + |
| 121 | + const result = await this.farcaster.getUserDataByFid({ |
| 122 | + fid: fid, |
| 123 | + reverse: true, |
| 124 | + }); |
| 125 | + |
| 126 | + if (result.isErr()) { |
| 127 | + throw result.error; |
| 128 | + } |
| 129 | + |
| 130 | + const profile: Profile = { |
| 131 | + fid, |
| 132 | + name: "", |
| 133 | + signer: "0x", |
| 134 | + username: "", |
| 135 | + }; |
| 136 | + |
| 137 | + const userDataBodyType = { |
| 138 | + 1: "pfp", |
| 139 | + 2: "name", |
| 140 | + 3: "bio", |
| 141 | + 5: "url", |
| 142 | + 6: "username", |
| 143 | + // 7: "location", |
| 144 | + // 8: "twitter", |
| 145 | + // 9: "github", |
| 146 | + } as const; |
| 147 | + |
| 148 | + for (const message of result.value.messages) { |
| 149 | + if (isUserDataAddMessage(message)) { |
| 150 | + if (message.data.userDataBody.type in userDataBodyType) { |
| 151 | + const prop = |
| 152 | + userDataBodyType[message.data.userDataBody.type]; |
| 153 | + profile[prop] = message.data.userDataBody.value; |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + const [lastMessage] = result.value.messages; |
| 159 | + |
| 160 | + if (lastMessage) { |
| 161 | + profile.signer = toHex(lastMessage.signer); |
| 162 | + } |
| 163 | + |
| 164 | + this.cache.set(`farcaster/profile/${fid}`, profile); |
| 165 | + |
| 166 | + return profile; |
| 167 | + } |
| 168 | + |
| 169 | + async getTimeline(request: FidRequest): Promise<{ |
| 170 | + timeline: Cast[]; |
| 171 | + nextPageToken?: Uint8Array<ArrayBufferLike> | undefined; |
| 172 | + }> { |
| 173 | + const timeline: Cast[] = []; |
| 174 | + |
| 175 | + const results = await this.getCastsByFid(request); |
| 176 | + |
| 177 | + for (const message of results.messages) { |
| 178 | + if (isCastAddMessage(message)) { |
| 179 | + this.cache.set( |
| 180 | + `farcaster/cast/${toHex(message.hash)}`, |
| 181 | + message |
| 182 | + ); |
| 183 | + |
| 184 | + timeline.push(await this.loadCastFromMessage(message)); |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + return { |
| 189 | + timeline, |
| 190 | + nextPageToken: results.nextPageToken, |
| 191 | + }; |
| 192 | + } |
| 193 | +} |
0 commit comments