Skip to content

Commit

Permalink
refactor(key_backups): Rename exists_on_server to fetch_exists_on_server
Browse files Browse the repository at this point in the history
  • Loading branch information
andybalaam committed Dec 9, 2024
1 parent a1cdef8 commit 1ca31c4
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 28 deletions.
2 changes: 1 addition & 1 deletion bindings/matrix-sdk-ffi/src/encryption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ impl Encryption {
/// Therefore it is necessary to poll the server for an answer every time
/// you want to differentiate between those two states.
pub async fn backup_exists_on_server(&self) -> Result<bool, ClientError> {
Ok(self.inner.backups().exists_on_server().await?)
Ok(self.inner.backups().fetch_exists_on_server().await?)
}

pub fn recovery_state(&self) -> RecoveryState {
Expand Down
44 changes: 22 additions & 22 deletions crates/matrix-sdk/src/encryption/backups/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,32 +386,32 @@ impl Backups {
/// Does a backup exist on the server?
///
/// This method will request info about the current backup from the
/// homeserver and if a backup exits return `true`, otherwise `false`.
pub async fn exists_on_server(&self) -> Result<bool, Error> {
/// homeserver and if a backup exists return `true`, otherwise `false`.
pub async fn fetch_exists_on_server(&self) -> Result<bool, Error> {
let exists_on_server = self.get_current_version().await?.is_some();
self.client.inner.e2ee.backup_state.set_backup_exists_on_server(exists_on_server);
Ok(exists_on_server)
}

/// Does a backup exist on the server?
///
/// This method is identical to [`Self::exists_on_server`] except that we
/// cache the latest answer in memory and only empty the cache if the local
/// device adds or deletes a backup itself.
/// This method is identical to [`Self::fetch_exists_on_server`] except that
/// we cache the latest answer in memory and only empty the cache if the
/// local device adds or deletes a backup itself.
///
/// Do not use this method if you need an accurate answer about whether a
/// backup exists - instead use [`Self::exists_on_server`]. This method is
/// useful when performance is more important than guaranteed accuracy,
/// such as when classifying UTDs.
/// backup exists - instead use [`Self::fetch_exists_on_server`]. This
/// method is useful when performance is more important than guaranteed
/// accuracy, such as when classifying UTDs.
pub async fn fast_exists_on_server(&self) -> Result<bool, Error> {
// If we have an answer cached, return it immediately
if let Some(cached_value) = self.client.inner.e2ee.backup_state.backup_exists_on_server() {
return Ok(cached_value);
}

// Otherwise, delegate to exists_on_server. (It will update the cached value for
// us.)
self.exists_on_server().await
// Otherwise, delegate to fetch_exists_on_server. (It will update the cached
// value for us.)
self.fetch_exists_on_server().await
}

/// Subscribe to a stream that notifies when a room key for the specified
Expand Down Expand Up @@ -1147,7 +1147,7 @@ mod test {
}

#[async_test]
async fn test_when_a_backup_exists_then_exists_on_server_returns_true() {
async fn test_when_a_backup_exists_then_fetch_exists_on_server_returns_true() {
let server = MatrixMockServer::new().await;
let client = server.client_builder().build().await;

Expand All @@ -1156,15 +1156,15 @@ mod test {
let exists = client
.encryption()
.backups()
.exists_on_server()
.fetch_exists_on_server()
.await
.expect("We should be able to check if backups exist on the server");

assert!(exists, "We should deduce that a backup exists on the server");
}

#[async_test]
async fn test_repeated_calls_to_exists_on_server_makes_repeated_requests() {
async fn test_repeated_calls_to_fetch_exists_on_server_makes_repeated_requests() {
let server = MatrixMockServer::new().await;
let client = server.client_builder().build().await;

Expand All @@ -1173,15 +1173,15 @@ mod test {

let backups = client.encryption().backups();

// Call exists_on_server twice
backups.exists_on_server().await.unwrap();
let exists = backups.exists_on_server().await.unwrap();
// Call fetch_exists_on_server twice
backups.fetch_exists_on_server().await.unwrap();
let exists = backups.fetch_exists_on_server().await.unwrap();

assert!(exists, "We should deduce that a backup exists on the server");
}

#[async_test]
async fn test_when_no_backup_exists_then_exists_on_server_returns_false() {
async fn test_when_no_backup_exists_then_fetch_exists_on_server_returns_false() {
let server = MatrixMockServer::new().await;
let client = server.client_builder().build().await;

Expand All @@ -1190,23 +1190,23 @@ mod test {
let exists = client
.encryption()
.backups()
.exists_on_server()
.fetch_exists_on_server()
.await
.expect("We should be able to check if backups exist on the server");

assert!(!exists, "We should deduce that no backup exists on the server");
}

#[async_test]
async fn test_when_server_returns_an_error_then_exists_on_server_returns_an_error() {
async fn test_when_server_returns_an_error_then_fetch_exists_on_server_returns_an_error() {
let server = MatrixMockServer::new().await;
let client = server.client_builder().build().await;

{
let _scope =
server.mock_room_keys_version().error429().expect(1).mount_as_scoped().await;

client.encryption().backups().exists_on_server().await.expect_err(
client.encryption().backups().fetch_exists_on_server().await.expect_err(
"If the /version endpoint returns a non 404 error we should throw an error",
);
}
Expand All @@ -1215,7 +1215,7 @@ mod test {
let _scope =
server.mock_room_keys_version().error404().expect(1).mount_as_scoped().await;

client.encryption().backups().exists_on_server().await.expect_err(
client.encryption().backups().fetch_exists_on_server().await.expect_err(
"If the /version endpoint returns a non-Matrix 404 error we should throw an error",
);
}
Expand Down
3 changes: 2 additions & 1 deletion crates/matrix-sdk/src/encryption/backups/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ pub enum BackupState {
/// The reason we don't know whether a server-side backup exists is that we
/// don't get notified by the server about the creation and deletion of
/// backups. If we want to know the current state, we need to poll the
/// server, which is done using the [`Backups::exists_on_server()`] method.
/// server, which is done using the [`Backups::fetch_exists_on_server()`]
/// method.
#[default]
Unknown,
/// A new backup is being created by this [`Client`]. This state will be
Expand Down
2 changes: 1 addition & 1 deletion crates/matrix-sdk/src/encryption/recovery/futures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl<'a> IntoFuture for Enable<'a> {

let future = async move {
if !recovery.client.encryption().backups().are_enabled().await {
if recovery.client.encryption().backups().exists_on_server().await? {
if recovery.client.encryption().backups().fetch_exists_on_server().await? {
return Err(RecoveryError::BackupExistsOnServer);
} else {
progress.set(EnableProgress::CreatingBackup);
Expand Down
4 changes: 2 additions & 2 deletions crates/matrix-sdk/src/encryption/recovery/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ impl Recovery {
/// ```
#[instrument(skip_all)]
pub async fn enable_backup(&self) -> Result<()> {
if !self.client.encryption().backups().exists_on_server().await? {
if !self.client.encryption().backups().fetch_exists_on_server().await? {
self.mark_backup_as_enabled().await?;

self.client.encryption().backups().create().await?;
Expand Down Expand Up @@ -503,7 +503,7 @@ impl Recovery {
// disabled, then we can automatically enable them.
Ok(self.client.inner.e2ee.encryption_settings.auto_enable_backups
&& !self.client.encryption().backups().are_enabled().await
&& !self.client.encryption().backups().exists_on_server().await?
&& !self.client.encryption().backups().fetch_exists_on_server().await?
&& !self.are_backups_marked_as_disabled().await?)
}

Expand Down
2 changes: 1 addition & 1 deletion crates/matrix-sdk/src/test_utils/mocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ impl MatrixMockServer {
/// mock_server.mock_room_keys_version().exists().expect(1).mount().await;
///
/// let exists =
/// client.encryption().backups().exists_on_server().await.unwrap();
/// client.encryption().backups().fetch_exists_on_server().await.unwrap();
///
/// assert!(exists);
/// # });
Expand Down

0 comments on commit 1ca31c4

Please sign in to comment.