-
Notifications
You must be signed in to change notification settings - Fork 246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(sdk): Welcome sliding_sync::Version
and sliding_sync::VersionBuilder
#3889
Merged
bnjbvr
merged 11 commits into
matrix-org:main
from
Hywan:feat-sdk-sliding-sync-version-selector
Aug 27, 2024
Merged
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f0100f5
feat(sdk): Add `sliding_sync::Version`.
Hywan b917304
feat: Use `sliding_sync::Version` everywhere.
Hywan a5e6bd0
feat(sdk): `ClientBuilder` extracts `get_supported_versions`.
Hywan 3793c06
feat(sdk): Add `sliding_sync::VersionBuilder`.
Hywan ea8e885
feat: Use the new `sliding_sync::VersionBuilder`.
Hywan 8990789
test(sdk): Restore disabled tests.
Hywan 4b2464b
test(sdk): Add 2 more tests for auto-discovery of sliding sync.
Hywan fa4ddde
test: Disable Complement Crypto for a short period.
Hywan c4a6ee2
feat(sdk): Add `Client::available_sliding_sync_versions`.
Hywan 54b066e
feat(ffi): Add `Client::available_sliding_sync_versions`.
Hywan 47f2c6f
sliding sync version(chore): address review comments
bnjbvr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
use std::collections::BTreeMap; | ||
|
||
use as_variant::as_variant; | ||
use imbl::Vector; | ||
use matrix_sdk_base::{sliding_sync::http, sync::SyncResponse, PreviousEventsProvider}; | ||
use ruma::{ | ||
|
@@ -14,13 +15,13 @@ use ruma::{ | |
use tracing::error; | ||
use url::Url; | ||
|
||
use super::{Error, SlidingSync, SlidingSyncBuilder}; | ||
use super::{SlidingSync, SlidingSyncBuilder}; | ||
use crate::{config::RequestConfig, Client, Result, SlidingSyncRoom}; | ||
|
||
/// A sliding sync version. | ||
#[derive(Clone, Debug)] | ||
pub enum Version { | ||
/// No version. Useful to represent that sliding sync is disable for | ||
/// No version. Useful to represent that sliding sync is disabled for | ||
/// example, and that the version is unknown. | ||
None, | ||
|
||
|
@@ -41,13 +42,35 @@ impl Version { | |
} | ||
|
||
pub(crate) fn overriding_url(&self) -> Option<&Url> { | ||
match self { | ||
Self::Proxy { url } => Some(url), | ||
_ => None, | ||
} | ||
as_variant!(self, Self::Proxy { url } => url) | ||
} | ||
} | ||
|
||
/// An error when building a version. | ||
#[derive(thiserror::Error, Debug)] | ||
pub enum VersionBuilderError { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎵 This is the story of a lazy man 🎵 with an awesome non-lazy co-worker 🎶 |
||
/// The `.well-known` response is not set. | ||
#[error("`.well-known` is not set")] | ||
WellKnownNotSet, | ||
|
||
/// `.well-known` does not contain a `sliding_sync_proxy` entry. | ||
#[error("`.well-known` does not contain a `sliding_sync_proxy` entry")] | ||
NoSlidingSyncInWellKnown, | ||
|
||
/// The `sliding_sync_proxy` URL in .well-known` is not valid ({0}). | ||
#[error("the `sliding_sync_proxy` URL in .well-known` is not valid ({0})")] | ||
UnparsableSlidingSyncUrl(url::ParseError), | ||
|
||
/// The `/versions` response is not set. | ||
#[error("The `/versions` response is not set")] | ||
MissingVersionsResponse, | ||
|
||
/// `/versions` does not contain `org.matrix.simplified_msc3575` in its | ||
/// `unstable_features`, or it's not set to true. | ||
#[error("`/versions` does not contain `org.matrix.simplified_msc3575` in its `unstable_features`, or it's not set to true.")] | ||
NativeVersionIsUnset, | ||
} | ||
|
||
/// A builder for [`Version`]. | ||
#[derive(Clone, Debug)] | ||
pub enum VersionBuilder { | ||
|
@@ -87,7 +110,7 @@ impl VersionBuilder { | |
self, | ||
well_known: Option<&discover_homeserver::Response>, | ||
versions: Option<&get_supported_versions::Response>, | ||
) -> Result<Version, Error> { | ||
) -> Result<Version, VersionBuilderError> { | ||
Ok(match self { | ||
Self::None => Version::None, | ||
|
||
|
@@ -97,42 +120,27 @@ impl VersionBuilder { | |
|
||
Self::DiscoverProxy => { | ||
let Some(well_known) = well_known else { | ||
return Err(Error::VersionCannotBeDiscovered( | ||
"`.well-known` is `None`".to_owned(), | ||
)); | ||
return Err(VersionBuilderError::WellKnownNotSet); | ||
}; | ||
|
||
let Some(info) = &well_known.sliding_sync_proxy else { | ||
return Err(Error::VersionCannotBeDiscovered( | ||
"`.well-known` does not contain a `sliding_sync_proxy` entry".to_owned(), | ||
)); | ||
return Err(VersionBuilderError::NoSlidingSyncInWellKnown); | ||
}; | ||
|
||
let url = Url::parse(&info.url).map_err(|e| { | ||
Error::VersionCannotBeDiscovered(format!( | ||
"`.well-known` contains an invalid `sliding_sync_proxy` entry ({e})" | ||
)) | ||
})?; | ||
let url = | ||
Url::parse(&info.url).map_err(VersionBuilderError::UnparsableSlidingSyncUrl)?; | ||
|
||
Version::Proxy { url } | ||
} | ||
|
||
Self::DiscoverNative => { | ||
let Some(versions) = versions else { | ||
return Err(Error::VersionCannotBeDiscovered( | ||
"`/versions` is `None`".to_owned(), | ||
)); | ||
return Err(VersionBuilderError::MissingVersionsResponse); | ||
}; | ||
|
||
match versions.unstable_features.get("org.matrix.simplified_msc3575") { | ||
Some(value) if *value => Version::Native, | ||
_ => { | ||
return Err(Error::VersionCannotBeDiscovered( | ||
"`/versions` does not contain `org.matrix.simplified_msc3575` in its \ | ||
`unstable_features`" | ||
.to_owned(), | ||
)) | ||
} | ||
_ => return Err(VersionBuilderError::NativeVersionIsUnset), | ||
} | ||
} | ||
}) | ||
|
@@ -324,9 +332,11 @@ mod tests { | |
Mock, ResponseTemplate, | ||
}; | ||
|
||
use super::{discover_homeserver, get_supported_versions, Error, Version, VersionBuilder}; | ||
use super::{discover_homeserver, get_supported_versions, Version, VersionBuilder}; | ||
use crate::{ | ||
error::Result, sliding_sync::http, test_utils::logged_in_client_with_server, | ||
error::Result, | ||
sliding_sync::{http, VersionBuilderError}, | ||
test_utils::logged_in_client_with_server, | ||
SlidingSyncList, SlidingSyncMode, | ||
}; | ||
|
||
|
@@ -373,9 +383,7 @@ mod tests { | |
fn test_version_builder_discover_proxy_no_well_known() { | ||
assert_matches!( | ||
VersionBuilder::DiscoverProxy.build(None, None), | ||
Err(Error::VersionCannotBeDiscovered(msg)) => { | ||
assert_eq!(msg, "`.well-known` is `None`"); | ||
} | ||
Err(VersionBuilderError::WellKnownNotSet) | ||
); | ||
} | ||
|
||
|
@@ -388,9 +396,7 @@ mod tests { | |
|
||
assert_matches!( | ||
VersionBuilder::DiscoverProxy.build(Some(&response), None), | ||
Err(Error::VersionCannotBeDiscovered(msg)) => { | ||
assert_eq!(msg, "`.well-known` does not contain a `sliding_sync_proxy` entry"); | ||
} | ||
Err(VersionBuilderError::NoSlidingSyncInWellKnown) | ||
); | ||
} | ||
|
||
|
@@ -404,8 +410,8 @@ mod tests { | |
|
||
assert_matches!( | ||
VersionBuilder::DiscoverProxy.build(Some(&response), None), | ||
Err(Error::VersionCannotBeDiscovered(msg)) => { | ||
assert_eq!(msg, "`.well-known` contains an invalid `sliding_sync_proxy` entry (relative URL without a base)"); | ||
Err(VersionBuilderError::UnparsableSlidingSyncUrl(err)) => { | ||
assert_eq!(err.to_string(), "relative URL without a base"); | ||
} | ||
); | ||
} | ||
|
@@ -425,9 +431,7 @@ mod tests { | |
fn test_version_builder_discover_native_no_supported_versions() { | ||
assert_matches!( | ||
VersionBuilder::DiscoverNative.build(None, None), | ||
Err(Error::VersionCannotBeDiscovered(msg)) => { | ||
assert_eq!(msg, "`/versions` is `None`".to_owned()); | ||
} | ||
Err(VersionBuilderError::MissingVersionsResponse) | ||
); | ||
} | ||
|
||
|
@@ -438,12 +442,7 @@ mod tests { | |
|
||
assert_matches!( | ||
VersionBuilder::DiscoverNative.build(None, Some(&response)), | ||
Err(Error::VersionCannotBeDiscovered(msg)) => { | ||
assert_eq!( | ||
msg, | ||
"`/versions` does not contain `org.matrix.simplified_msc3575` in its `unstable_features`".to_owned() | ||
); | ||
} | ||
Err(VersionBuilderError::NativeVersionIsUnset) | ||
); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤦 of course! Thanks for the fix.