sdk+ffi: in notification settings, replace members_count by is_one_to_one for more clarity

This commit is contained in:
Nicolas Mauri
2023-08-10 15:08:44 +02:00
committed by Benjamin Bouvier
parent f64a46a2be
commit ac42d07de7
3 changed files with 40 additions and 33 deletions

View File

@@ -137,13 +137,13 @@ impl NotificationSettings {
///
/// * `room_id` - the room ID
/// * `is_encrypted` - whether the room is encrypted
/// * `active_members_count` - the room's active members count (joined +
/// invited)
/// * `is_one_to_one` - whether the room is a direct chat involving two
/// people
pub async fn get_room_notification_settings(
&self,
room_id: String,
is_encrypted: bool,
active_members_count: u64,
is_one_to_one: bool,
) -> Result<RoomNotificationSettings, NotificationSettingsError> {
let notification_settings = self.sdk_notification_settings.read().await;
let parsed_room_id = RoomId::parse(&room_id)
@@ -158,7 +158,7 @@ impl NotificationSettings {
// If the user has not defined a notification mode, return the default one for
// this room
let mode = notification_settings
.get_default_room_notification_mode(is_encrypted.into(), active_members_count)
.get_default_room_notification_mode(is_encrypted.into(), is_one_to_one.into())
.await;
Ok(RoomNotificationSettings::new(mode.into(), true))
}
@@ -202,16 +202,16 @@ impl NotificationSettings {
/// # Arguments
///
/// * `is_encrypted` - whether the room is encrypted
/// * `active_members_count` - the room's active members count (joined +
/// invited)
/// * `is_one_to_one` - whether the room is a direct chats involving two
/// people
pub async fn get_default_room_notification_mode(
&self,
is_encrypted: bool,
active_members_count: u64,
is_one_to_one: bool,
) -> RoomNotificationMode {
let notification_settings = self.sdk_notification_settings.read().await;
let mode = notification_settings
.get_default_room_notification_mode(is_encrypted.into(), active_members_count)
.get_default_room_notification_mode(is_encrypted.into(), is_one_to_one.into())
.await;
mode.into()
}
@@ -344,17 +344,24 @@ impl NotificationSettings {
}
/// Unmute a room.
///
/// # Arguments
///
/// * `room_id` - the room to unmute
/// * `is_encrypted` - whether the room is encrypted
/// * `is_one_to_one` - whether the room is a direct chat involving two
/// people
pub async fn unmute_room(
&self,
room_id: String,
is_encrypted: bool,
members_count: u64,
is_one_to_one: bool,
) -> Result<(), NotificationSettingsError> {
let notification_settings = self.sdk_notification_settings.read().await;
let parsed_room_id = RoomId::parse(&room_id)
.map_err(|_e| NotificationSettingsError::InvalidRoomId(room_id))?;
notification_settings
.unmute_room(&parsed_room_id, is_encrypted.into(), members_count)
.unmute_room(&parsed_room_id, is_encrypted.into(), is_one_to_one.into())
.await?;
Ok(())
}

View File

@@ -121,13 +121,14 @@ impl NotificationSettings {
/// # Arguments
///
/// * `is_encrypted` - `Yes` if the room is encrypted
/// * `members_count` - the room members count
/// * `is_one_to_one` - `Yes` if the room is a direct chat involving two
/// people
pub async fn get_default_room_notification_mode(
&self,
is_encrypted: IsEncrypted,
members_count: u64,
is_one_to_one: IsOneToOne,
) -> RoomNotificationMode {
self.rules.read().await.get_default_room_notification_mode(is_encrypted, members_count)
self.rules.read().await.get_default_room_notification_mode(is_encrypted, is_one_to_one)
}
/// Get all room IDs for which a user-defined rule exists.
@@ -289,7 +290,7 @@ impl NotificationSettings {
&self,
room_id: &RoomId,
is_encrypted: IsEncrypted,
members_count: u64,
is_one_to_one: IsOneToOne,
) -> Result<(), NotificationSettingsError> {
let rules = self.rules.read().await.clone();
@@ -302,7 +303,7 @@ impl NotificationSettings {
// Get default mode for this room
let default_mode =
rules.get_default_room_notification_mode(is_encrypted, members_count);
rules.get_default_room_notification_mode(is_encrypted, is_one_to_one);
// If the default mode is `Mute`, set it to `AllMessages`
if default_mode == RoomNotificationMode::Mute {
@@ -518,7 +519,7 @@ mod tests {
let settings = NotificationSettings::new(client, ruleset);
assert_eq!(
settings.get_default_room_notification_mode(IsEncrypted::No, 2).await,
settings.get_default_room_notification_mode(IsEncrypted::No, IsOneToOne::Yes).await,
RoomNotificationMode::AllMessages
);
}
@@ -537,7 +538,7 @@ mod tests {
let settings = NotificationSettings::new(client.to_owned(), ruleset.to_owned());
assert_eq!(
settings.get_default_room_notification_mode(IsEncrypted::No, 2).await,
settings.get_default_room_notification_mode(IsEncrypted::No, IsOneToOne::Yes).await,
RoomNotificationMode::MentionsAndKeywordsOnly
);
@@ -549,7 +550,7 @@ mod tests {
let settings = NotificationSettings::new(client, ruleset);
assert_eq!(
settings.get_default_room_notification_mode(IsEncrypted::No, 2).await,
settings.get_default_room_notification_mode(IsEncrypted::No, IsOneToOne::Yes).await,
RoomNotificationMode::MentionsAndKeywordsOnly
);
}
@@ -849,7 +850,7 @@ mod tests {
);
// Unmute the room
settings.unmute_room(&room_id, IsEncrypted::Yes, 2).await.unwrap();
settings.unmute_room(&room_id, IsEncrypted::Yes, IsOneToOne::Yes).await.unwrap();
// The ruleset must not be modified
assert_eq!(
@@ -883,7 +884,7 @@ mod tests {
);
// Unmute the room
settings.unmute_room(&room_id, IsEncrypted::No, 2).await.unwrap();
settings.unmute_room(&room_id, IsEncrypted::No, IsOneToOne::Yes).await.unwrap();
// The user defined mode must have been removed
assert!(settings.get_user_defined_room_notification_mode(&room_id).await.is_none());
@@ -898,7 +899,7 @@ mod tests {
let settings = client.notification_settings().await;
// Unmute the room
settings.unmute_room(&room_id, IsEncrypted::No, 2).await.unwrap();
settings.unmute_room(&room_id, IsEncrypted::No, IsOneToOne::Yes).await.unwrap();
// The new mode must be `AllMessages`
assert_eq!(
@@ -934,7 +935,7 @@ mod tests {
let settings = NotificationSettings::new(client, ruleset);
assert_eq!(
settings.get_default_room_notification_mode(IsEncrypted::No, 2).await,
settings.get_default_room_notification_mode(IsEncrypted::No, IsOneToOne::Yes).await,
RoomNotificationMode::AllMessages
);
@@ -958,7 +959,7 @@ mod tests {
// and the new mode returned by `get_default_room_notification_mode()` should
// reflect the change.
assert_matches!(
settings.get_default_room_notification_mode(IsEncrypted::No, 2).await,
settings.get_default_room_notification_mode(IsEncrypted::No, IsOneToOne::Yes).await,
RoomNotificationMode::MentionsAndKeywordsOnly
);
}

View File

@@ -99,17 +99,16 @@ impl Rules {
///
/// # Arguments
///
/// * `is_encrypted` - `true` if the room is encrypted
/// * `members_count` - the room members count
/// * `is_encrypted` - `Yes` if the room is encrypted
/// * `is_one_to_one` - `Yes` if the room is a direct chat involving two
/// people
pub(crate) fn get_default_room_notification_mode(
&self,
is_encrypted: IsEncrypted,
members_count: u64,
is_one_to_one: IsOneToOne,
) -> RoomNotificationMode {
// get the correct default rule ID based on `is_encrypted` and `members_count`
let is_one_to_one = members_count == 2;
let predefined_rule_id =
get_predefined_underride_room_rule_id(is_encrypted, is_one_to_one.into());
// get the correct default rule ID based on `is_encrypted` and `is_one_to_one`
let predefined_rule_id = get_predefined_underride_room_rule_id(is_encrypted, is_one_to_one);
let rule_id = predefined_rule_id.as_str();
// If there is an `Underride` rule that should trigger a notification, the mode
@@ -264,7 +263,7 @@ impl Rules {
/// # Arguments
///
/// * `is_encrypted` - `Yes` if the room is encrypted
/// * `is_one_to_one` - `Yes` if the room is a direct chat
/// * `is_one_to_one` - `Yes` if the room is a direct chat involving two people
pub(crate) fn get_predefined_underride_room_rule_id(
is_encrypted: IsEncrypted,
is_one_to_one: IsOneToOne,
@@ -419,7 +418,7 @@ pub(crate) mod tests {
.unwrap();
let rules = Rules::new(ruleset);
let mode = rules.get_default_room_notification_mode(IsEncrypted::No, 2);
let mode = rules.get_default_room_notification_mode(IsEncrypted::No, IsOneToOne::Yes);
// Then the mode should be `MentionsAndKeywordsOnly`
assert_eq!(mode, RoomNotificationMode::MentionsAndKeywordsOnly);
}
@@ -433,7 +432,7 @@ pub(crate) mod tests {
.unwrap();
let rules = Rules::new(ruleset);
let mode = rules.get_default_room_notification_mode(IsEncrypted::No, 2);
let mode = rules.get_default_room_notification_mode(IsEncrypted::No, IsOneToOne::Yes);
// Then the mode should be `AllMessages`
assert_eq!(mode, RoomNotificationMode::AllMessages);
}