xmtp_api_d14n/protocol/extractors/
depends_on.rs

1//! Extractor for an MLS Data field
2//! useful for verifying a message has been read or maybe duplicates.
3use xmtp_proto::ConversionError;
4use xmtp_proto::types::GlobalCursor;
5use xmtp_proto::xmtp::xmtpv4::envelopes::ClientEnvelope;
6
7use crate::protocol::{EnvelopeVisitor, Extractor};
8
9/// Extract DependsOn from Envelopes
10/// If the envelope does not have dependency, or is already
11/// ordered (as is the case for v3), then returns `None`.
12#[derive(Default, Clone, Debug)]
13pub struct DependsOnExtractor {
14    cursor: Option<GlobalCursor>,
15}
16
17impl DependsOnExtractor {
18    pub fn new() -> Self {
19        Default::default()
20    }
21}
22
23impl Extractor for DependsOnExtractor {
24    type Output = Option<GlobalCursor>;
25
26    fn get(self) -> Self::Output {
27        self.cursor
28    }
29}
30
31impl EnvelopeVisitor<'_> for DependsOnExtractor {
32    type Error = ConversionError;
33
34    fn visit_client(&mut self, e: &ClientEnvelope) -> Result<(), Self::Error> {
35        // to avoid clone here & elsewhere
36        // https://github.com/xmtp/libxmtp/issues/2691
37        self.cursor = e
38            .aad
39            .as_ref()
40            .and_then(|a| a.depends_on.clone().map(Into::into));
41        Ok(())
42    }
43}