xmtp_api_d14n/protocol/traits/
xmtp_query.rs

1//! XmtpQuery allows accessing the network while bypassing any local cursor cache.
2use xmtp_common::{MaybeSend, MaybeSync};
3use xmtp_proto::types::Cursor;
4
5use super::*;
6
7// XMTP Query queries the network for any envelopes
8/// matching the cursor criteria given.
9#[xmtp_common::async_trait]
10pub trait XmtpQuery: MaybeSend + MaybeSync {
11    type Error: RetryableError + 'static;
12    /// Query every [`Topic`] at [`GlobalCursor`]
13    async fn query_at(
14        &self,
15        topic: Topic,
16        at: Option<GlobalCursor>,
17    ) -> Result<XmtpEnvelope, Self::Error>;
18}
19
20// hides implementation detail of XmtpEnvelope/traits
21/// Envelopes from the XMTP Network received from a general [`XmtpQuery`]
22pub struct XmtpEnvelope {
23    inner: Box<dyn EnvelopeCollection<'static>>,
24}
25
26impl XmtpEnvelope {
27    pub fn new(envelope: impl EnvelopeCollection<'static> + 'static) -> Self {
28        Self {
29            inner: Box::new(envelope) as Box<_>,
30        }
31    }
32
33    pub fn len(&self) -> usize {
34        self.inner.len()
35    }
36
37    pub fn cursors(&self) -> Result<Vec<Cursor>, EnvelopeError> {
38        self.inner.cursors()
39    }
40
41    pub fn is_empty(&self) -> bool {
42        self.inner.is_empty()
43    }
44
45    pub fn group_messages(&self) -> Result<Vec<GroupMessage>, EnvelopeError> {
46        Ok(self.inner.group_messages()?.into_iter().flatten().collect())
47    }
48
49    pub fn welcome_messages(&self) -> Result<Vec<WelcomeMessage>, EnvelopeError> {
50        Ok(self
51            .inner
52            .welcome_messages()?
53            .into_iter()
54            .flatten()
55            .collect())
56    }
57
58    pub fn client_envelopes(&self) -> Result<Vec<ClientEnvelope>, EnvelopeError> {
59        self.inner.client_envelopes()
60    }
61}