Skip to main content

xmtp_mls/client/
streams.rs

1//! Client streams: streamed welcome processing, conversation, message, consent and
2//! preference streams, and catch-up.
3
4use super::*;
5
6impl<Context> Client<Context>
7where
8    Context: XmtpSharedContext + 'static,
9{
10    /// Use push metadata as a target. Fetch its complete prefix before processing.
11    pub async fn process_streamed_welcome_message(
12        &self,
13        envelope_bytes: Vec<u8>,
14    ) -> Result<Vec<MlsGroup<Context>>, SubscribeError> {
15        subscriptions::process_streamed_welcome_message(&self.context, envelope_bytes).await
16    }
17
18    pub async fn stream_conversations(
19        &self,
20        conversation_type: Option<ConversationType>,
21        include_duplicate_dms: bool,
22    ) -> Result<
23        impl Stream<Item = Result<MlsGroup<Context>, SubscribeError>> + use<'_, Context>,
24        SubscribeError,
25    >
26    where
27        Context::ApiClient: XmtpMlsStreams,
28    {
29        subscriptions::stream_conversations(&self.context, conversation_type, include_duplicate_dms)
30            .await
31    }
32
33    /// Stream conversations but decouple the lifetime of 'self' from the stream.
34    pub async fn stream_conversations_owned(
35        &self,
36        conversation_type: Option<ConversationType>,
37        include_duplicate_dms: bool,
38    ) -> Result<
39        impl Stream<Item = Result<MlsGroup<Context>, SubscribeError>> + 'static + use<Context>,
40        SubscribeError,
41    >
42    where
43        Context::ApiClient: XmtpMlsStreams,
44    {
45        subscriptions::stream_conversations_owned(
46            self.context.clone(),
47            conversation_type,
48            include_duplicate_dms,
49        )
50        .await
51    }
52}
53
54impl<Context> Client<Context>
55where
56    Context: XmtpSharedContext + 'static,
57    Context::ApiClient: XmtpMlsStreams + 'static,
58    Context::MlsStorage: 'static,
59{
60    pub fn stream_conversations_with_callback(
61        client: Arc<Client<Context>>,
62        conversation_type: Option<ConversationType>,
63        convo_callback: impl FnMut(Result<MlsGroup<Context>, SubscribeError>) + MaybeSend + 'static,
64        on_close: impl FnOnce() + MaybeSend + 'static,
65        include_duplicate_dms: bool,
66    ) -> impl StreamHandle<StreamOutput = Result<(), SubscribeError>> {
67        subscriptions::stream_conversations_with_callback(
68            client.context.clone(),
69            client,
70            conversation_type,
71            convo_callback,
72            on_close,
73            include_duplicate_dms,
74        )
75    }
76
77    pub async fn stream_all_messages(
78        &self,
79        conversation_type: Option<ConversationType>,
80        consent_state: Option<Vec<ConsentState>>,
81    ) -> Result<impl Stream<Item = Result<StoredGroupMessage, SubscribeError>> + '_, SubscribeError>
82    {
83        subscriptions::stream_all_messages(&self.context, conversation_type, consent_state).await
84    }
85
86    pub async fn stream_all_messages_owned(
87        &self,
88        conversation_type: Option<ConversationType>,
89        consent_state: Option<Vec<ConsentState>>,
90    ) -> Result<
91        impl Stream<Item = Result<StoredGroupMessage, SubscribeError>> + 'static + use<Context>,
92        SubscribeError,
93    > {
94        subscriptions::stream_all_messages_owned(
95            self.context.clone(),
96            conversation_type,
97            consent_state,
98        )
99        .await
100    }
101
102    pub fn stream_consent_with_callback(
103        client: Arc<Client<Context>>,
104        callback: impl FnMut(Result<Vec<StoredConsentRecord>, SubscribeError>) + MaybeSend + 'static,
105        on_close: impl FnOnce() + MaybeSend + 'static,
106    ) -> impl StreamHandle<StreamOutput = Result<(), SubscribeError>> {
107        subscriptions::stream_consent_with_callback(
108            client.context.clone(),
109            client,
110            callback,
111            on_close,
112        )
113    }
114
115    pub fn stream_preferences_with_callback(
116        client: Arc<Client<Context>>,
117        callback: impl FnMut(Result<Vec<PreferenceUpdate>, SubscribeError>) + MaybeSend + 'static,
118        on_close: impl FnOnce() + MaybeSend + 'static,
119    ) -> impl StreamHandle<StreamOutput = Result<(), SubscribeError>> {
120        subscriptions::stream_preferences_with_callback(
121            client.context.clone(),
122            client,
123            callback,
124            on_close,
125        )
126    }
127
128    pub fn stream_message_deletions_with_callback(
129        client: Arc<Client<Context>>,
130        callback: impl FnMut(Result<DecodedMessage, SubscribeError>) + MaybeSend + 'static,
131        on_close: impl FnOnce() + MaybeSend + 'static,
132    ) -> impl StreamHandle<StreamOutput = Result<(), SubscribeError>> {
133        subscriptions::stream_message_deletions_with_callback(
134            client.context.clone(),
135            client,
136            callback,
137            on_close,
138        )
139    }
140}
141
142impl<Context> Client<Context>
143where
144    Context: XmtpSharedContext + 'static,
145    Context::ApiClient: XmtpMlsStreams + 'static,
146    Context::MlsStorage: 'static,
147    <Context::ApiClient as XmtpMlsStreams>::GroupMessageStream: Unpin,
148    <Context::ApiClient as XmtpMlsStreams>::WelcomeMessageStream: Unpin,
149{
150    #[cfg(any(test, feature = "test-utils"))]
151    pub async fn stream_all_messages_owned_with_stats(
152        &self,
153        conversation_type: Option<ConversationType>,
154        consent_state: Option<Vec<ConsentState>>,
155    ) -> Result<
156        impl StreamWithStats<Item = Result<StoredGroupMessage, SubscribeError>> + 'static,
157        SubscribeError,
158    > {
159        subscriptions::stream_all_messages_owned_with_stats(
160            self.context.clone(),
161            conversation_type,
162            consent_state,
163        )
164        .await
165    }
166}
167
168impl<C: XmtpSharedContext + 'static> Client<C> {
169    /// Process fixed starting targets and only the discoveries from the enrolled Welcomes.
170    pub async fn catch_up_to_live(
171        &self,
172        timeout: Option<xmtp_common::time::Duration>,
173    ) -> Result<CatchUpSummary, CatchUpError> {
174        subscriptions::catch_up::catch_up_to_live(&self.context, timeout).await
175    }
176}
177
178#[cfg(not(target_arch = "wasm32"))]
179impl<C> Client<C>
180where
181    C: XmtpSharedContext + 'static,
182    C::ApiClient:
183        XmtpMlsBidiStreams + XmtpMlsStreams + ApiClientIdentity + Clone + Send + Sync + 'static,
184    <C::ApiClient as XmtpMlsBidiStreams>::SubscribeStream: 'static,
185{
186    pub fn stream_all_messages_with_callback_dispatch(
187        client: Arc<Client<C>>,
188        conversation_type: Option<ConversationType>,
189        consent_states: Option<Vec<ConsentState>>,
190        callback: impl FnMut(Result<StoredGroupMessage, SubscribeError>) + MaybeSend + 'static,
191        on_close: impl FnOnce() + MaybeSend + 'static,
192    ) -> impl StreamHandle<StreamOutput = Result<(), SubscribeError>> {
193        router_callbacks::stream_all_messages_with_callback_dispatch(
194            client.context.clone(),
195            client,
196            conversation_type,
197            consent_states,
198            callback,
199            on_close,
200        )
201    }
202
203    pub fn stream_conversations_with_callback_dispatch(
204        client: Arc<Client<C>>,
205        conversation_type: Option<ConversationType>,
206        include_duplicate_dms: bool,
207        callback: impl FnMut(Result<MlsGroup<C>, SubscribeError>) + MaybeSend + 'static,
208        on_close: impl FnOnce() + MaybeSend + 'static,
209    ) -> impl StreamHandle<StreamOutput = Result<(), SubscribeError>> {
210        router_callbacks::stream_conversations_with_callback_dispatch(
211            client.context.clone(),
212            client,
213            conversation_type,
214            include_duplicate_dms,
215            callback,
216            on_close,
217        )
218    }
219}