sliding sync: always fall back to the auto-discovered proxy URL, if any

Signed-off-by: Benjamin Bouvier <public@benj.me>
This commit is contained in:
Benjamin Bouvier
2023-06-13 12:30:02 +02:00
parent 47a67d1d67
commit a79d519ff9
4 changed files with 51 additions and 15 deletions

View File

@@ -865,12 +865,7 @@ impl Client {
///
/// Note: the identifier must be less than 16 chars long.
pub fn sliding_sync(&self, id: String) -> Result<Arc<SlidingSyncBuilder>, ClientError> {
let mut inner = self.inner.sliding_sync(id)?;
if let Some(sliding_sync_proxy) = self.inner.sliding_sync_proxy() {
inner = inner.sliding_sync_proxy(sliding_sync_proxy);
}
let inner = self.inner.sliding_sync(id)?;
Ok(Arc::new(SlidingSyncBuilder { inner, client: self.clone() }))
}
}

View File

@@ -96,14 +96,9 @@ impl RoomList {
/// A [`matrix_sdk::SlidingSync`] client will be created, with a cached list
/// already pre-configured.
pub async fn new(client: Client) -> Result<Self, Error> {
let mut sliding_sync_builder =
client.sliding_sync("room-list").map_err(Error::SlidingSync)?;
if let Some(sliding_sync_proxy_url) = client.sliding_sync_proxy() {
sliding_sync_builder = sliding_sync_builder.sliding_sync_proxy(sliding_sync_proxy_url);
}
let sliding_sync = sliding_sync_builder
let sliding_sync = client
.sliding_sync("room-list")
.map_err(Error::SlidingSync)?
// Enable the account data extension.
.with_account_data_extension(
assign! { AccountDataConfig::default(), { enabled: Some(true) }},

View File

@@ -262,6 +262,10 @@ impl SlidingSyncBuilder {
let rooms = AsyncRwLock::new(self.rooms);
let lists = AsyncRwLock::new(lists);
// Use the configured sliding sync proxy, or if not set, try to use the one
// auto-discovered by the client, if any.
let sliding_sync_proxy = self.sliding_sync_proxy.or_else(|| client.sliding_sync_proxy());
// Always enable to-device events and the e2ee-extension on the initial request,
// no matter what the caller wants.
let mut extensions = self.extensions.unwrap_or_default();
@@ -270,7 +274,7 @@ impl SlidingSyncBuilder {
Ok(SlidingSync::new(SlidingSyncInner {
_id: Some(self.id),
sliding_sync_proxy: self.sliding_sync_proxy,
sliding_sync_proxy,
client,
storage_key: self.storage_key,

View File

@@ -1188,4 +1188,46 @@ mod tests {
Ok(())
}
#[tokio::test]
async fn test_sliding_sync_proxy_url() -> Result<()> {
let server = MockServer::start().await;
let client = logged_in_client(Some(server.uri())).await;
{
// A server that doesn't expose a sliding sync proxy gets and transmits none, by
// default.
let sync = client.sliding_sync("no-proxy")?.build().await?;
assert!(sync.sliding_sync_proxy().is_none());
}
{
// The sliding sync builder can be used to customize a proxy, though.
let url = Url::parse("https://bar.matrix/").unwrap();
let sync =
client.sliding_sync("own-proxy")?.sliding_sync_proxy(url.clone()).build().await?;
assert_eq!(sync.sliding_sync_proxy(), Some(url));
}
// Set the client's proxy, that will be inherited by sliding sync.
let url = Url::parse("https://foo.matrix/").unwrap();
client.set_sliding_sync_proxy(Some(url.clone()));
{
// The sliding sync inherits the client's sliding sync proxy URL.
let sync = client.sliding_sync("client-proxy")?.build().await?;
assert_eq!(sync.sliding_sync_proxy(), Some(url));
}
{
// …unless we override it.
let url = Url::parse("https://bar.matrix/").unwrap();
let sync =
client.sliding_sync("own-proxy")?.sliding_sync_proxy(url.clone()).build().await?;
assert_eq!(sync.sliding_sync_proxy(), Some(url));
}
Ok(())
}
}