Skip to content

Commit d8652d2

Browse files
committed
feat(sdk): forbid ignoring the current user
1 parent 0f4afb3 commit d8652d2

2 files changed

Lines changed: 40 additions & 8 deletions

File tree

crates/matrix-sdk/src/account.rs

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -890,25 +890,34 @@ impl Account {
890890

891891
/// Adds the given user ID to the account's ignore list.
892892
pub async fn ignore_user(&self, user_id: &UserId) -> Result<()> {
893+
let own_user_id = self.client.user_id().ok_or(Error::AuthenticationRequired)?;
894+
if user_id == own_user_id {
895+
return Err(Error::CantIgnoreLoggedInUser);
896+
}
897+
893898
let mut ignored_user_list = self.get_ignored_user_list_event_content().await?;
894899
ignored_user_list.ignored_users.insert(user_id.to_owned(), IgnoredUser::new());
895900

896-
// Updating the account data
897901
self.set_account_data(ignored_user_list).await?;
898-
// TODO: I think I should reset all the storage and perform a new local sync
899-
// here but I don't know how
902+
903+
// In theory, we should also clear some caches here, because they may include
904+
// events sent by the ignored user. In practice, we expect callers to
905+
// take care of this, or subsystems to listen to user list changes and
906+
// clear caches accordingly.
907+
900908
Ok(())
901909
}
902910

903911
/// Removes the given user ID from the account's ignore list.
904912
pub async fn unignore_user(&self, user_id: &UserId) -> Result<()> {
905913
let mut ignored_user_list = self.get_ignored_user_list_event_content().await?;
906-
ignored_user_list.ignored_users.remove(user_id);
907914

908-
// Updating the account data
909-
self.set_account_data(ignored_user_list).await?;
910-
// TODO: I think I should reset all the storage and perform a new local sync
911-
// here but I don't know how
915+
// Only update account data if the user was ignored in the first place.
916+
if ignored_user_list.ignored_users.remove(user_id).is_some() {
917+
self.set_account_data(ignored_user_list).await?;
918+
}
919+
920+
// See comment in `ignore_user`.
912921
Ok(())
913922
}
914923

@@ -1158,3 +1167,22 @@ fn get_raw_content<Ev, C>(raw: Option<Raw<Ev>>) -> Result<Option<Raw<C>>> {
11581167
.transpose()?
11591168
.map(|get_raw| get_raw.content))
11601169
}
1170+
1171+
#[cfg(test)]
1172+
mod tests {
1173+
use assert_matches::assert_matches;
1174+
use matrix_sdk_test::async_test;
1175+
1176+
use crate::{test_utils::client::MockClientBuilder, Error};
1177+
1178+
#[async_test]
1179+
async fn test_dont_ignore_oneself() {
1180+
let client = MockClientBuilder::new("https://example.org".to_owned()).build().await;
1181+
1182+
// It's forbidden to ignore the logged-in user.
1183+
assert_matches!(
1184+
client.account().ignore_user(client.user_id().unwrap()).await,
1185+
Err(Error::CantIgnoreLoggedInUser)
1186+
);
1187+
}
1188+
}

crates/matrix-sdk/src/error.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,10 @@ pub enum Error {
404404
#[error("backups are not enabled")]
405405
BackupNotEnabled,
406406

407+
/// It's forbidden to ignore your own user.
408+
#[error("can't ignore the logged-in user")]
409+
CantIgnoreLoggedInUser,
410+
407411
/// An error happened during handling of a media subrequest.
408412
#[error(transparent)]
409413
Media(#[from] MediaError),

0 commit comments

Comments
 (0)