Allow *all* m.room.power_levels fields to be absent

This commit is contained in:
Jonas Platte 2019-12-16 21:10:55 +01:00
parent 1a3f6ed0b6
commit dd16d0f2f0
No known key found for this signature in database
GPG Key ID: 7D261D771D915378

View File

@ -48,6 +48,7 @@ pub struct PowerLevelsEventContent {
/// The level required to send specific event types.
///
/// This is a mapping from event type to power level required.
#[serde(default, skip_serializing_if = "HashMap::is_empty")]
pub events: HashMap<EventType, Int>,
/// The default level required to send message events.
@ -73,6 +74,7 @@ pub struct PowerLevelsEventContent {
/// The power levels for specific users.
///
/// This is a mapping from `user_id` to power level for that user.
#[serde(default, skip_serializing_if = "HashMap::is_empty")]
pub users: HashMap<UserId, Int>,
/// The default power level for every user in the room.
@ -82,6 +84,7 @@ pub struct PowerLevelsEventContent {
/// The power level requirements for specific notification types.
///
/// This is a mapping from `key` to power level for that notifications key.
#[serde(default, skip_serializing_if = "NotificationPowerLevels::is_default")]
pub notifications: NotificationPowerLevels,
}
@ -214,6 +217,7 @@ pub(crate) mod raw {
/// The level required to send specific event types.
///
/// This is a mapping from event type to power level required.
#[serde(default, skip_serializing_if = "HashMap::is_empty")]
pub events: HashMap<EventType, Int>,
/// The default level required to send message events.
@ -239,6 +243,7 @@ pub(crate) mod raw {
/// The power levels for specific users.
///
/// This is a mapping from `user_id` to power level for that user.
#[serde(default, skip_serializing_if = "HashMap::is_empty")]
pub users: HashMap<UserId, Int>,
/// The default power level for every user in the room.
@ -248,6 +253,7 @@ pub(crate) mod raw {
/// The power level requirements for specific notification types.
///
/// This is a mapping from `key` to power level for that notifications key.
#[serde(default, skip_serializing_if = "NotificationPowerLevels::is_default")]
pub notifications: NotificationPowerLevels,
}
}
@ -260,6 +266,23 @@ pub struct NotificationPowerLevels {
pub room: Int,
}
impl NotificationPowerLevels {
// TODO: Make public under this name?
// pass-by-ref required for #[serde(skip_serializing_if)]
#[allow(clippy::trivially_copy_pass_by_ref)]
fn is_default(&self) -> bool {
*self == Self::default()
}
}
impl Default for NotificationPowerLevels {
fn default() -> Self {
Self {
room: default_power_level(),
}
}
}
/// Used to default power levels to 50 during deserialization.
fn default_power_level() -> Int {
Int::from(50)