matrix_sdk/config/
sync.rs

1// Copyright 2021 The Matrix.org Foundation C.I.C.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::{fmt, time::Duration};
16
17use matrix_sdk_common::debug::DebugStructExt;
18use ruma::{api::client::sync::sync_events, presence::PresenceState};
19
20const DEFAULT_SYNC_TIMEOUT: Duration = Duration::from_secs(30);
21
22/// Settings for a sync call.
23#[derive(Clone)]
24pub struct SyncSettings {
25    // Filter is pretty big at 1000 bytes, box it to reduce stack size
26    pub(crate) filter: Option<Box<sync_events::v3::Filter>>,
27    pub(crate) timeout: Option<Duration>,
28    pub(crate) token: Option<String>,
29    pub(crate) full_state: bool,
30    pub(crate) set_presence: PresenceState,
31}
32
33impl Default for SyncSettings {
34    fn default() -> Self {
35        Self::new()
36    }
37}
38
39#[cfg(not(tarpaulin_include))]
40impl fmt::Debug for SyncSettings {
41    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42        let Self { filter, timeout, token: _, full_state, set_presence } = self;
43        f.debug_struct("SyncSettings")
44            .maybe_field("filter", filter)
45            .maybe_field("timeout", timeout)
46            .field("full_state", full_state)
47            .field("set_presence", set_presence)
48            .finish()
49    }
50}
51
52impl SyncSettings {
53    /// Create new default sync settings.
54    #[must_use]
55    pub fn new() -> Self {
56        Self {
57            filter: None,
58            timeout: Some(DEFAULT_SYNC_TIMEOUT),
59            token: None,
60            full_state: false,
61            set_presence: PresenceState::Online,
62        }
63    }
64
65    /// Set the sync token.
66    ///
67    /// # Arguments
68    ///
69    /// * `token` - The sync token that should be used for the sync call.
70    #[must_use]
71    pub fn token(mut self, token: impl Into<String>) -> Self {
72        self.token = Some(token.into());
73        self
74    }
75
76    /// Set the maximum time the server can wait, in milliseconds, before
77    /// responding to the sync request.
78    ///
79    /// # Arguments
80    ///
81    /// * `timeout` - The time the server is allowed to wait.
82    #[must_use]
83    pub fn timeout(mut self, timeout: Duration) -> Self {
84        self.timeout = Some(timeout);
85        self
86    }
87
88    /// Set the sync filter.
89    /// It can be either the filter ID, or the definition for the filter.
90    ///
91    /// # Arguments
92    ///
93    /// * `filter` - The filter configuration that should be used for the sync
94    ///   call.
95    #[must_use]
96    pub fn filter(mut self, filter: sync_events::v3::Filter) -> Self {
97        self.filter = Some(Box::new(filter));
98        self
99    }
100
101    /// Should the server return the full state from the start of the timeline.
102    ///
103    /// This does nothing if no sync token is set.
104    ///
105    /// # Arguments
106    /// * `full_state` - A boolean deciding if the server should return the full
107    ///   state or not.
108    #[must_use]
109    pub fn full_state(mut self, full_state: bool) -> Self {
110        self.full_state = full_state;
111        self
112    }
113
114    /// Set the presence state
115    ///
116    /// `PresenceState::Online` - The client is marked as being online. This is
117    /// the default preset.
118    ///
119    /// `PresenceState::Offline` - The client is not marked as being online.
120    ///
121    /// `PresenceState::Unavailable` - The client is marked as being idle.
122    ///
123    /// # Arguments
124    /// * `set_presence` - The `PresenceState` that the server should set for
125    ///   the client.
126    #[must_use]
127    pub fn set_presence(mut self, presence: PresenceState) -> Self {
128        self.set_presence = presence;
129        self
130    }
131}