Skip to main content

xmtp_mls/client/
sync.rs

1//! Client sync: welcome, group and device-sync group sync.
2
3use super::*;
4
5impl<Context> Client<Context>
6where
7    Context: XmtpSharedContext,
8{
9    /// Download all unread welcome messages and converts to a group struct, ignoring malformed messages.
10    /// Returns any new groups created in the operation
11    #[tracing::instrument(skip_all)]
12    pub async fn sync_welcomes(&self) -> Result<Vec<MlsGroup<Context>>, GroupError> {
13        self.ensure_identity_ready::<GroupError>()?;
14        WelcomeService::new(self.context.clone())
15            .sync_welcomes()
16            .await
17            .map_err(|error| self.context.server_configuration().blocked_or(error))
18    }
19
20    /// Sync all groups for the current installation and return the number of groups that were synced.
21    /// Only active groups will be synced.
22    #[tracing::instrument(err, skip_all, fields(operation = "sync_all_groups"))]
23    pub async fn sync_all_groups(
24        &self,
25        groups: Vec<MlsGroup<Context>>,
26    ) -> Result<GroupSyncSummary, GroupError> {
27        self.ensure_identity_ready::<GroupError>()?;
28        WelcomeService::new(self.context.clone())
29            .sync_all_groups(groups)
30            .await
31            .map_err(|error| self.context.server_configuration().blocked_or(error))
32    }
33
34    /// Sync all unread welcome messages and then sync all groups.
35    /// Returns the total number of active groups synced.
36    #[xmtp_common::mls_span]
37    pub async fn sync_all_welcomes_and_groups(
38        &self,
39        consent_states: Option<Vec<ConsentState>>,
40    ) -> Result<GroupSyncSummary, GroupError> {
41        self.ensure_identity_ready::<GroupError>()?;
42        WelcomeService::new(self.context.clone())
43            .sync_all_welcomes_and_groups(consent_states)
44            .await
45            .map_err(|error| self.context.server_configuration().blocked_or(error))
46    }
47
48    /// Sweep every group flagged `paused_for_version` and clear the
49    /// pause flag for any whose floor is now satisfied by this
50    /// client's `pkg_version`. Pure local-state operation — no
51    /// network calls. Returns the count of groups unstuck.
52    ///
53    /// `sync_all_welcomes_and_groups` already runs this sweep as a
54    /// preamble; the standalone entry point is for SDKs that want a
55    /// cheap "post-upgrade recovery" hook independent of the normal
56    /// sync flow.
57    pub async fn unstick_paused_groups(&self) -> Result<usize, GroupError> {
58        self.ensure_identity_ready::<GroupError>()?;
59        WelcomeService::new(self.context.clone())
60            .unstick_paused_groups()
61            .await
62    }
63
64    pub async fn sync_all_welcomes_and_device_sync_groups(
65        &self,
66    ) -> Result<GroupSyncSummary, ClientError> {
67        self.sync_welcomes().await?;
68        self.sync_all_device_sync_groups().await
69    }
70
71    pub async fn sync_all_device_sync_groups(&self) -> Result<GroupSyncSummary, ClientError> {
72        let groups = self
73            .context
74            .db()
75            .all_sync_groups()?
76            .into_iter()
77            .map(|g| {
78                MlsGroup::new(
79                    self.context.clone(),
80                    g.id,
81                    g.dm_id,
82                    g.conversation_type,
83                    g.created_at_ns,
84                )
85            })
86            .collect();
87
88        Ok(self.sync_all_groups(groups).await?)
89    }
90}