@@ -10,6 +10,8 @@ export class FarcasterClient {
10
10
cache : Map < string , any > ;
11
11
lastInteractionTimestamp : Date ;
12
12
farcasterConfig : FarcasterConfig ;
13
+ lastPollTime : Date ;
14
+ lastCleanupTime : Date ;
13
15
14
16
constructor ( opts : {
15
17
runtime : IAgentRuntime ;
@@ -26,6 +28,8 @@ export class FarcasterClient {
26
28
this . signerUuid = opts . signerUuid ;
27
29
this . lastInteractionTimestamp = new Date ( ) ;
28
30
this . farcasterConfig = opts . farcasterConfig ;
31
+ this . lastPollTime = new Date ( ) ;
32
+ this . lastCleanupTime = new Date ( ) ;
29
33
}
30
34
31
35
async loadCastFromNeynarResponse ( neynarResponse : any ) : Promise < Cast > {
@@ -136,14 +140,67 @@ export class FarcasterClient {
136
140
return timeline ;
137
141
}
138
142
143
+ // async getMentions(request: FidRequest): Promise<Cast[]> {
144
+ // const neynarMentionsResponse = await this.neynar.fetchAllNotifications({
145
+ // fid: request.fid,
146
+ // type: ["mentions", "replies"],
147
+ // });
148
+ // const mentions: Cast[] = [];
149
+
150
+ // neynarMentionsResponse.notifications.map((notification) => {
151
+ // const cast = {
152
+ // hash: notification.cast!.hash,
153
+ // authorFid: notification.cast!.author.fid,
154
+ // text: notification.cast!.text,
155
+ // profile: {
156
+ // fid: notification.cast!.author.fid,
157
+ // name: notification.cast!.author.display_name || "anon",
158
+ // username: notification.cast!.author.username,
159
+ // },
160
+ // ...(notification.cast!.parent_hash
161
+ // ? {
162
+ // inReplyTo: {
163
+ // hash: notification.cast!.parent_hash,
164
+ // fid: notification.cast!.parent_author.fid,
165
+ // },
166
+ // }
167
+ // : {}),
168
+ // embeds: notification.cast!.embeds || [],
169
+ // timestamp: new Date(notification.cast!.timestamp),
170
+ // };
171
+ // mentions.push(cast.);
172
+ // this.cache.set(`farcaster/cast/${cast.hash}`, cast);
173
+ // });
174
+
175
+ // // console.log('Notifications Counts:', mentions?.length)
176
+
177
+ // // const neynarMentionMarkedAsSeen = await this.neynar.markNotificationsAsSeen({
178
+ // // signerUuid: this.signerUuid
179
+ // // });
180
+
181
+ // // console.log('Notifications Marked as Seen:', neynarMentionMarkedAsSeen);
182
+
183
+ // return mentions;
184
+ // }
185
+
139
186
async getMentions ( request : FidRequest ) : Promise < Cast [ ] > {
187
+ // Current poll time
188
+ const currentPollTime = new Date ( ) ;
189
+
140
190
const neynarMentionsResponse = await this . neynar . fetchAllNotifications ( {
141
191
fid : request . fid ,
142
192
type : [ "mentions" , "replies" ] ,
143
193
} ) ;
144
194
const mentions : Cast [ ] = [ ] ;
145
-
195
+
146
196
neynarMentionsResponse . notifications . map ( ( notification ) => {
197
+ const notificationTime = new Date ( notification . cast ! . timestamp ) ;
198
+
199
+ // Skip if the notification is older than our last poll time
200
+ if ( notificationTime <= this . lastPollTime ) {
201
+ return ;
202
+ }
203
+
147
204
const cast = {
148
205
hash : notification . cast ! . hash ,
149
206
authorFid : notification . cast ! . author . fid ,
@@ -162,20 +219,18 @@ export class FarcasterClient {
162
219
}
163
220
: { } ) ,
164
221
embeds : notification . cast ! . embeds || [ ] ,
165
- timestamp : new Date ( notification . cast ! . timestamp ) ,
222
+ timestamp : notificationTime ,
166
223
} ;
167
224
mentions . push ( cast ) ;
168
225
this . cache . set ( `farcaster/cast/${ cast . hash } ` , cast ) ;
169
226
} ) ;
170
-
171
- // console.log('Notifications Counts:', mentions?.length)
172
-
173
- // const neynarMentionMarkedAsSeen = await this.neynar.markNotificationsAsSeen({
174
- // signerUuid: this.signerUuid
175
- // });
176
-
177
- // console.log('Notifications Marked as Seen:', neynarMentionMarkedAsSeen);
178
-
227
+
228
+ // Update the last poll time for the next call
229
+ this . lastPollTime = currentPollTime ;
230
+
231
+ // Clear old cache entries periodically (every 30 minutes)
232
+ this . cleanupOldCacheEntries ( ) ;
233
+ console . log ( 'New Notification Casts:' , mentions ?. length ) ;
179
234
return mentions ;
180
235
}
181
236
@@ -241,4 +296,30 @@ export class FarcasterClient {
241
296
//nextPageToken: results.nextPageToken,
242
297
} ;
243
298
}
299
+
300
+ // Helper method to clean up old cache entries
301
+ private cleanupOldCacheEntries ( ) : void {
302
+ // Run cleanup every 30 minutes
303
+ const now = new Date ( ) ;
304
+ if ( now . getTime ( ) - this . lastCleanupTime . getTime ( ) > 1800000 ) {
305
+ elizaLogger . info ( "Cleaning up old cache entries" ) ;
306
+
307
+ // Get all keys that start with farcaster/cast/
308
+ const castKeys = Array . from ( this . cache . keys ( ) )
309
+ . filter ( key => key . startsWith ( 'farcaster/cast/' ) ) ;
310
+
311
+ // Remove entries older than 1 hour
312
+ const cutoffTime = new Date ( ) ;
313
+ cutoffTime . setHours ( cutoffTime . getHours ( ) - 1 ) ;
314
+
315
+ for ( const key of castKeys ) {
316
+ const cast = this . cache . get ( key ) ;
317
+ if ( cast && cast . timestamp && cast . timestamp < cutoffTime ) {
318
+ this . cache . delete ( key ) ;
319
+ }
320
+ }
321
+
322
+ this . lastCleanupTime = now ;
323
+ }
324
+ }
244
325
}
0 commit comments