1use super::*;
4
5pub(super) type ContextParts<Api, S, Db> = Arc<XmtpMlsLocalContext<Api, Db, S>>;
6#[cfg(not(target_arch = "wasm32"))]
8pub(super) type LocationStoreOpener<Db> = Box<
9 dyn FnOnce(
10 crate::storage_location::ResolvedPaths,
11 ) -> xmtp_common::BoxDynFuture<'static, Result<Db, ClientBuilderError>>
12 + Send
13 + Sync,
14>;
15#[cfg(target_arch = "wasm32")]
16pub(super) type LocationStoreOpener<Db> = Box<
17 dyn FnOnce(
18 crate::storage_location::ResolvedPaths,
19 ) -> xmtp_common::BoxDynFuture<'static, Result<Db, ClientBuilderError>>,
20>;
21
22#[cfg(not(target_arch = "wasm32"))]
23fn open_location_store(
24 paths: crate::storage_location::ResolvedPaths,
25 key: xmtp_db::EncryptionKey,
26) -> xmtp_common::BoxDynFuture<'static, Result<xmtp_db::DefaultStore, ClientBuilderError>> {
27 Box::pin(async move {
28 let parent = paths
29 .db_path
30 .parent()
31 .ok_or(crate::storage_location::StorageLocationError::InboxId)?;
32 xmtp_attachments::create_private_directory(parent)
33 .await
34 .map_err(crate::storage_location::StorageLocationError::from)?;
35 let db = xmtp_db::NativeDb::builder()
36 .persistent(paths.db_path.to_string_lossy().into_owned())
37 .key(key)
38 .build()?;
39 Ok(xmtp_db::EncryptedMessageStore::new(db)?)
40 })
41}
42
43impl<ApiClient, S, Db> ClientBuilder<ApiClient, S, Db> {
44 #[cfg(not(target_arch = "wasm32"))]
49 pub async fn data_location(
50 self,
51 location: crate::storage_location::StorageLocation,
52 key: xmtp_db::EncryptionKey,
53 ) -> Result<ClientBuilder<ApiClient, S, xmtp_db::DefaultStore>, ClientBuilderError> {
54 self.data_location_with(location, move |paths| open_location_store(paths, key))
55 }
56
57 pub fn data_location_with<NewDb>(
61 self,
62 location: crate::storage_location::StorageLocation,
63 opener: impl FnOnce(
64 crate::storage_location::ResolvedPaths,
65 )
66 -> xmtp_common::BoxDynFuture<'static, Result<NewDb, ClientBuilderError>>
67 + xmtp_common::MaybeSend
68 + xmtp_common::MaybeSync
69 + 'static,
70 ) -> Result<ClientBuilder<ApiClient, S, NewDb>, ClientBuilderError> {
71 location.validate()?;
72 let conflict = self.storage_location_conflict
73 || self.store.is_some()
74 || self.mls_storage.is_some()
75 || self.mls_storage_factory.is_some()
76 || self.storage_location_selected
77 || self.attachments_dir.is_some();
78 Ok(ClientBuilder {
79 deployment_recorder: None,
80 data_location: Some(location),
81 location_store_opener: Some(Box::new(opener)),
82 mls_storage_factory: None,
83 storage_location_selected: true,
84 storage_location_conflict: conflict,
85 attachments_dir: self.attachments_dir,
86 attachment_options: self.attachment_options,
87 api_client: self.api_client,
88 identity: self.identity,
89 store: None,
90 identity_strategy: self.identity_strategy,
91 scw_verifier: self.scw_verifier,
92 custom_scw_verifier: self.custom_scw_verifier,
93 device_sync_worker_mode: self.device_sync_worker_mode,
94 fork_recovery_opts: self.fork_recovery_opts,
95 change_callbacks: self.change_callbacks,
96 stream_policy: self.stream_policy,
97 incoming_factory: self.incoming_factory,
98 version_info: self.version_info,
99 allow_offline: self.allow_offline,
100 disable_commit_log_worker: self.disable_commit_log_worker,
101 mls_storage: self.mls_storage,
102 disable_workers: self.disable_workers,
103 worker_config: self.worker_config,
104 config_provider: self.config_provider,
105 #[cfg(any(test, feature = "test-utils"))]
106 application_components_for_test: self.application_components_for_test,
107 })
108 }
109
110 pub fn store<NewDb>(self, db: NewDb) -> ClientBuilder<ApiClient, S, NewDb> {
111 ClientBuilder {
112 deployment_recorder: self.deployment_recorder,
113 data_location: self.data_location,
114 location_store_opener: None,
115 mls_storage_factory: None,
116 storage_location_selected: self.storage_location_selected,
117 storage_location_conflict: self.storage_location_conflict
118 || self.storage_location_selected,
119 store: Some(db),
120 api_client: self.api_client,
121 identity: self.identity,
122 identity_strategy: self.identity_strategy,
123 scw_verifier: self.scw_verifier,
124 custom_scw_verifier: self.custom_scw_verifier,
125 device_sync_worker_mode: self.device_sync_worker_mode,
126 fork_recovery_opts: self.fork_recovery_opts,
127 change_callbacks: self.change_callbacks,
128 stream_policy: self.stream_policy,
129 incoming_factory: self.incoming_factory,
130 version_info: self.version_info,
131 allow_offline: self.allow_offline,
132 disable_commit_log_worker: self.disable_commit_log_worker,
133 mls_storage: self.mls_storage,
134 disable_workers: self.disable_workers,
135 worker_config: self.worker_config,
136 config_provider: self.config_provider,
137 #[cfg(any(test, feature = "test-utils"))]
138 application_components_for_test: self.application_components_for_test,
139 attachments_dir: self.attachments_dir,
140 attachment_options: self.attachment_options,
141 }
142 }
143
144 pub fn default_mls_store(
146 self,
147 ) -> Result<
148 ClientBuilder<ApiClient, SqlKeyStore<<Db as XmtpDb>::DbQuery>, Db>,
149 ClientBuilderError,
150 >
151 where
152 Db: XmtpDb,
153 {
154 Ok(ClientBuilder {
155 deployment_recorder: self.deployment_recorder,
156 data_location: self.data_location,
157 location_store_opener: self.location_store_opener,
158 mls_storage_factory: Some(|store: &Db| SqlKeyStore::new(store.db())),
159 storage_location_selected: self.storage_location_selected,
160 storage_location_conflict: self.storage_location_conflict,
161 api_client: self.api_client,
162 identity: self.identity,
163 identity_strategy: self.identity_strategy,
164 scw_verifier: self.scw_verifier,
165 custom_scw_verifier: self.custom_scw_verifier,
166 device_sync_worker_mode: self.device_sync_worker_mode,
167 fork_recovery_opts: self.fork_recovery_opts,
168 change_callbacks: self.change_callbacks,
169 stream_policy: self.stream_policy,
170 incoming_factory: self.incoming_factory,
171 version_info: self.version_info,
172 allow_offline: self.allow_offline,
173 disable_commit_log_worker: self.disable_commit_log_worker,
174 mls_storage: self
175 .store
176 .as_ref()
177 .map(|store| SqlKeyStore::new(store.db())),
178 store: self.store,
179 disable_workers: self.disable_workers,
180 worker_config: self.worker_config,
181 config_provider: self.config_provider,
182 #[cfg(any(test, feature = "test-utils"))]
183 application_components_for_test: self.application_components_for_test,
184 attachments_dir: self.attachments_dir,
185 attachment_options: self.attachment_options,
186 })
187 }
188
189 pub fn mls_storage<NewS>(self, mls_storage: NewS) -> ClientBuilder<ApiClient, NewS, Db> {
190 let storage_location_conflict =
191 self.storage_location_conflict || self.storage_location_selected;
192 ClientBuilder {
193 deployment_recorder: self.deployment_recorder,
194 data_location: self.data_location,
195 location_store_opener: self.location_store_opener,
196 mls_storage_factory: None,
197 storage_location_selected: self.storage_location_selected,
198 storage_location_conflict,
199 store: self.store,
200 api_client: self.api_client,
201 identity: self.identity,
202 identity_strategy: self.identity_strategy,
203 scw_verifier: self.scw_verifier,
204 custom_scw_verifier: self.custom_scw_verifier,
205 device_sync_worker_mode: self.device_sync_worker_mode,
206 fork_recovery_opts: self.fork_recovery_opts,
207 change_callbacks: self.change_callbacks,
208 stream_policy: self.stream_policy,
209 incoming_factory: self.incoming_factory,
210 version_info: self.version_info,
211 allow_offline: self.allow_offline,
212 disable_commit_log_worker: self.disable_commit_log_worker,
213 mls_storage: Some(mls_storage),
214 disable_workers: self.disable_workers,
215 worker_config: self.worker_config,
216 config_provider: self.config_provider,
217 #[cfg(any(test, feature = "test-utils"))]
218 application_components_for_test: self.application_components_for_test,
219 attachments_dir: self.attachments_dir,
220 attachment_options: self.attachment_options,
221 }
222 }
223}