waypoint 2025.12.1

Waypoint is a Farcaster synchronization tool built in Rust, optimized for memory efficiency.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
//! Data access abstractions and context
use crate::core::types::{Fid, Message, MessageId, MessageType};
use async_trait::async_trait;
use thiserror::Error;

/// Error type for data access operations
#[derive(Error, Debug)]
pub enum DataAccessError {
    #[error("Database error: {0}")]
    Database(#[from] sqlx::Error),

    #[error("Redis error: {0}")]
    Redis(String),

    #[error("Message not found: {0}")]
    NotFound(String),

    #[error("Search engine error: {0}")]
    Search(String),

    #[error("Serialization error: {0}")]
    Serialization(String),

    #[error("Hub client error: {0}")]
    HubClient(String),

    #[error("Other error: {0}")]
    Other(String),
}

/// Result type for data access operations
pub type Result<T> = std::result::Result<T, DataAccessError>;

/// Generic trait for database operations
#[async_trait]
pub trait Database: Send + Sync {
    /// Get a message by ID and type
    async fn get_message(&self, id: &MessageId, message_type: MessageType) -> Result<Message>;

    /// Get messages by FID
    async fn get_messages_by_fid(
        &self,
        fid: Fid,
        message_type: MessageType,
        limit: usize,
        cursor: Option<MessageId>,
    ) -> Result<Vec<Message>>;

    /// Store a message
    async fn store_message(&self, message: Message) -> Result<()>;

    /// Delete a message
    async fn delete_message(&self, id: &MessageId, message_type: MessageType) -> Result<()>;
}

/// Generic trait for hub operations
#[async_trait]
pub trait HubClient: Send + Sync {
    /// Get user data by FID
    async fn get_user_data_by_fid(&self, fid: Fid, limit: usize) -> Result<Vec<Message>>;

    /// Get specific user data type
    async fn get_user_data(&self, fid: Fid, data_type: &str) -> Result<Option<Message>>;

    /// Get username proofs
    async fn get_username_proofs_by_fid(&self, fid: Fid) -> Result<Vec<Message>>;

    /// Get username proof by name
    async fn get_username_proof_by_name(
        &self,
        name: &str,
    ) -> Result<Option<crate::proto::UserNameProof>>;

    /// Get verifications
    async fn get_verifications_by_fid(&self, fid: Fid, limit: usize) -> Result<Vec<Message>>;

    /// Get casts by FID
    async fn get_casts_by_fid(&self, fid: Fid, limit: usize) -> Result<Vec<Message>>;

    /// Get a specific cast by ID
    async fn get_cast(&self, fid: Fid, hash: &[u8]) -> Result<Option<Message>>;

    /// Get casts mentioning an FID
    async fn get_casts_by_mention(&self, fid: Fid, limit: usize) -> Result<Vec<Message>>;

    /// Get casts by parent
    async fn get_casts_by_parent(
        &self,
        parent_fid: Fid,
        parent_hash: &[u8],
        limit: usize,
    ) -> Result<Vec<Message>>;

    /// Get casts by parent URL
    async fn get_casts_by_parent_url(&self, parent_url: &str, limit: usize)
    -> Result<Vec<Message>>;

    /// Get all casts by FID with timestamp filtering
    async fn get_all_casts_by_fid(
        &self,
        fid: Fid,
        limit: usize,
        start_time: Option<u64>,
        end_time: Option<u64>,
    ) -> Result<Vec<Message>>;

    /// Get a specific reaction by params
    async fn get_reaction(
        &self,
        fid: Fid,
        reaction_type: u8,
        target_cast_fid: Option<Fid>,
        target_cast_hash: Option<&[u8]>,
        target_url: Option<&str>,
    ) -> Result<Option<Message>>;

    /// Get reactions by FID
    async fn get_reactions_by_fid(
        &self,
        fid: Fid,
        reaction_type: Option<u8>,
        limit: usize,
    ) -> Result<Vec<Message>>;

    /// Get reactions by target (cast or URL)
    async fn get_reactions_by_target(
        &self,
        target_cast_fid: Option<Fid>,
        target_cast_hash: Option<&[u8]>,
        target_url: Option<&str>,
        reaction_type: Option<u8>,
        limit: usize,
    ) -> Result<Vec<Message>>;

    /// Get all reactions by FID with timestamp filtering
    async fn get_all_reactions_by_fid(
        &self,
        fid: Fid,
        limit: usize,
        start_time: Option<u64>,
        end_time: Option<u64>,
    ) -> Result<Vec<Message>>;

    /// Get a specific link by params
    async fn get_link(&self, fid: Fid, link_type: &str, target_fid: Fid)
    -> Result<Option<Message>>;

    /// Get links by FID
    async fn get_links_by_fid(
        &self,
        fid: Fid,
        link_type: Option<&str>,
        limit: usize,
    ) -> Result<Vec<Message>>;

    /// Get links by target
    async fn get_links_by_target(
        &self,
        target_fid: Fid,
        link_type: Option<&str>,
        limit: usize,
    ) -> Result<Vec<Message>>;

    /// Get link compact state messages by FID
    async fn get_link_compact_state_by_fid(&self, fid: Fid) -> Result<Vec<Message>>;

    /// Get all links by FID with timestamp filtering
    async fn get_all_links_by_fid(
        &self,
        fid: Fid,
        limit: usize,
        start_time: Option<u64>,
        end_time: Option<u64>,
    ) -> Result<Vec<Message>>;
}

/// Generic data access context
#[derive(Debug, Clone)]
pub struct DataContext<DB, HC> {
    database: Option<DB>,
    hub_client: Option<HC>,
}

impl<DB, HC> DataContext<DB, HC>
where
    DB: Database,
    HC: HubClient,
{
    /// Get user data by FID, using Hub if available, falling back to database
    pub async fn get_user_data_by_fid(&self, fid: Fid, limit: usize) -> Result<Vec<Message>> {
        // Try hub client first if available
        if let Some(hub) = &self.hub_client {
            match hub.get_user_data_by_fid(fid, limit).await {
                Ok(data) if !data.is_empty() => return Ok(data),
                _ => {},
            }
        }

        Err(DataAccessError::Other("No data source available".to_string()))
    }

    /// Get specific user data, with Hub priority
    pub async fn get_user_data(&self, fid: Fid, data_type: &str) -> Result<Option<Message>> {
        if let Some(hub) = &self.hub_client {
            // Fall through to database on error
            if let Ok(data) = hub.get_user_data(fid, data_type).await {
                return Ok(data);
            }
        }

        Err(DataAccessError::Other("No data source available".to_string()))
    }

    /// Get username proofs
    pub async fn get_username_proofs_by_fid(&self, fid: Fid) -> Result<Vec<Message>> {
        if let Some(hub) = &self.hub_client {
            return hub.get_username_proofs_by_fid(fid).await;
        }

        Err(DataAccessError::Other("Hub client not available".to_string()))
    }

    /// Get FID by username
    pub async fn get_fid_by_username(&self, username: &str) -> Result<Option<Fid>> {
        if let Some(hub) = &self.hub_client {
            let proof = hub.get_username_proof_by_name(username).await?;
            return Ok(proof.map(|p| Fid::new(p.fid)));
        }

        Err(DataAccessError::Other("Hub client not available".to_string()))
    }

    /// Get verifications
    pub async fn get_verifications_by_fid(&self, fid: Fid, limit: usize) -> Result<Vec<Message>> {
        if let Some(hub) = &self.hub_client {
            return hub.get_verifications_by_fid(fid, limit).await;
        }

        if let Some(db) = &self.database {
            return db.get_messages_by_fid(fid, MessageType::Verification, limit, None).await;
        }

        Err(DataAccessError::Other("No data source available".to_string()))
    }

    /// Get casts by FID
    pub async fn get_casts_by_fid(&self, fid: Fid, limit: usize) -> Result<Vec<Message>> {
        if let Some(hub) = &self.hub_client {
            return hub.get_casts_by_fid(fid, limit).await;
        }

        if let Some(db) = &self.database {
            return db.get_messages_by_fid(fid, MessageType::Cast, limit, None).await;
        }

        Err(DataAccessError::Other("No data source available".to_string()))
    }

    /// Get a specific cast by ID
    pub async fn get_cast(&self, fid: Fid, hash: &[u8]) -> Result<Option<Message>> {
        if let Some(hub) = &self.hub_client {
            return hub.get_cast(fid, hash).await;
        }

        Err(DataAccessError::Other("Hub client not available".to_string()))
    }

    /// Get casts mentioning an FID
    pub async fn get_casts_by_mention(&self, fid: Fid, limit: usize) -> Result<Vec<Message>> {
        if let Some(hub) = &self.hub_client {
            return hub.get_casts_by_mention(fid, limit).await;
        }

        Err(DataAccessError::Other("Hub client not available".to_string()))
    }

    /// Get casts by parent
    pub async fn get_casts_by_parent(
        &self,
        parent_fid: Fid,
        parent_hash: &[u8],
        limit: usize,
    ) -> Result<Vec<Message>> {
        if let Some(hub) = &self.hub_client {
            return hub.get_casts_by_parent(parent_fid, parent_hash, limit).await;
        }

        Err(DataAccessError::Other("Hub client not available".to_string()))
    }

    /// Get casts by parent URL
    pub async fn get_casts_by_parent_url(
        &self,
        parent_url: &str,
        limit: usize,
    ) -> Result<Vec<Message>> {
        if let Some(hub) = &self.hub_client {
            return hub.get_casts_by_parent_url(parent_url, limit).await;
        }

        Err(DataAccessError::Other("Hub client not available".to_string()))
    }

    /// Get all casts by FID with timestamp filtering
    pub async fn get_all_casts_by_fid(
        &self,
        fid: Fid,
        limit: usize,
        start_time: Option<u64>,
        end_time: Option<u64>,
    ) -> Result<Vec<Message>> {
        if let Some(hub) = &self.hub_client {
            return hub.get_all_casts_by_fid(fid, limit, start_time, end_time).await;
        }

        Err(DataAccessError::Other("Hub client not available".to_string()))
    }

    /// Get a specific reaction
    pub async fn get_reaction(
        &self,
        fid: Fid,
        reaction_type: u8,
        target_cast_fid: Option<Fid>,
        target_cast_hash: Option<&[u8]>,
        target_url: Option<&str>,
    ) -> Result<Option<Message>> {
        if let Some(hub) = &self.hub_client {
            return hub
                .get_reaction(fid, reaction_type, target_cast_fid, target_cast_hash, target_url)
                .await;
        }

        Err(DataAccessError::Other("Hub client not available".to_string()))
    }

    /// Get reactions by FID
    pub async fn get_reactions_by_fid(
        &self,
        fid: Fid,
        reaction_type: Option<u8>,
        limit: usize,
    ) -> Result<Vec<Message>> {
        if let Some(hub) = &self.hub_client {
            return hub.get_reactions_by_fid(fid, reaction_type, limit).await;
        }

        if let Some(db) = &self.database {
            return db.get_messages_by_fid(fid, MessageType::Reaction, limit, None).await;
        }

        Err(DataAccessError::Other("No data source available".to_string()))
    }

    /// Get reactions by target
    pub async fn get_reactions_by_target(
        &self,
        target_cast_fid: Option<Fid>,
        target_cast_hash: Option<&[u8]>,
        target_url: Option<&str>,
        reaction_type: Option<u8>,
        limit: usize,
    ) -> Result<Vec<Message>> {
        if let Some(hub) = &self.hub_client {
            return hub
                .get_reactions_by_target(
                    target_cast_fid,
                    target_cast_hash,
                    target_url,
                    reaction_type,
                    limit,
                )
                .await;
        }

        Err(DataAccessError::Other("Hub client not available".to_string()))
    }

    /// Get all reactions by FID with timestamp filtering
    pub async fn get_all_reactions_by_fid(
        &self,
        fid: Fid,
        limit: usize,
        start_time: Option<u64>,
        end_time: Option<u64>,
    ) -> Result<Vec<Message>> {
        if let Some(hub) = &self.hub_client {
            return hub.get_all_reactions_by_fid(fid, limit, start_time, end_time).await;
        }

        Err(DataAccessError::Other("Hub client not available".to_string()))
    }

    /// Get a specific link
    pub async fn get_link(
        &self,
        fid: Fid,
        link_type: &str,
        target_fid: Fid,
    ) -> Result<Option<Message>> {
        if let Some(hub) = &self.hub_client {
            return hub.get_link(fid, link_type, target_fid).await;
        }

        Err(DataAccessError::Other("Hub client not available".to_string()))
    }

    /// Get links by FID
    pub async fn get_links_by_fid(
        &self,
        fid: Fid,
        link_type: Option<&str>,
        limit: usize,
    ) -> Result<Vec<Message>> {
        if let Some(hub) = &self.hub_client {
            return hub.get_links_by_fid(fid, link_type, limit).await;
        }

        if let Some(db) = &self.database {
            return db.get_messages_by_fid(fid, MessageType::Link, limit, None).await;
        }

        Err(DataAccessError::Other("No data source available".to_string()))
    }

    /// Get links by target
    pub async fn get_links_by_target(
        &self,
        target_fid: Fid,
        link_type: Option<&str>,
        limit: usize,
    ) -> Result<Vec<Message>> {
        if let Some(hub) = &self.hub_client {
            return hub.get_links_by_target(target_fid, link_type, limit).await;
        }

        Err(DataAccessError::Other("Hub client not available".to_string()))
    }

    /// Get link compact state messages by FID
    pub async fn get_link_compact_state_by_fid(&self, fid: Fid) -> Result<Vec<Message>> {
        if let Some(hub) = &self.hub_client {
            return hub.get_link_compact_state_by_fid(fid).await;
        }

        Err(DataAccessError::Other("Hub client not available".to_string()))
    }

    /// Get all links by FID with timestamp filtering
    pub async fn get_all_links_by_fid(
        &self,
        fid: Fid,
        limit: usize,
        start_time: Option<u64>,
        end_time: Option<u64>,
    ) -> Result<Vec<Message>> {
        if let Some(hub) = &self.hub_client {
            return hub.get_all_links_by_fid(fid, limit, start_time, end_time).await;
        }

        Err(DataAccessError::Other("Hub client not available".to_string()))
    }

    /// Generic database operation
    pub fn database(&self) -> Result<&DB> {
        self.database
            .as_ref()
            .ok_or_else(|| DataAccessError::Other("Database not configured".to_string()))
    }

    /// Generic hub operation
    pub fn hub(&self) -> Result<&HC> {
        self.hub_client
            .as_ref()
            .ok_or_else(|| DataAccessError::Other("Hub client not configured".to_string()))
    }
}

/// Builder for the data context
pub struct DataContextBuilder<DB, HC> {
    database: Option<DB>,
    hub_client: Option<HC>,
}

impl<DB, HC> Default for DataContextBuilder<DB, HC> {
    fn default() -> Self {
        Self { database: None, hub_client: None }
    }
}

impl<DB, HC> DataContextBuilder<DB, HC> {
    /// Create a new builder
    pub fn new() -> Self {
        Self::default()
    }

    /// Add a database
    pub fn with_database(mut self, database: DB) -> Self {
        self.database = Some(database);
        self
    }

    /// Add a Hub client
    pub fn with_hub_client(mut self, hub_client: HC) -> Self {
        self.hub_client = Some(hub_client);
        self
    }

    /// Build the context
    pub fn build(self) -> DataContext<DB, HC> {
        DataContext { database: self.database, hub_client: self.hub_client }
    }
}