Skip to main content

palpo_core/
power_levels.rs

1//! Common types for the [`m.room.power_levels` event][power_levels].
2//!
3//! [power_levels]: https://spec.matrix.org/latest/client-server-api/#mroompower_levels
4use salvo::prelude::*;
5use serde::{Deserialize, Serialize};
6
7/// The power level requirements for specific notification types.
8#[derive(ToSchema, Deserialize, Serialize, Clone, Debug)]
9pub struct NotificationPowerLevels {
10    /// The level required to trigger an `@room` notification.
11    #[serde(
12        default = "default_power_level",
13        deserialize_with = "crate::serde::deserialize_v1_powerlevel"
14    )]
15    pub room: i64,
16}
17
18impl NotificationPowerLevels {
19    /// Create a new `NotificationPowerLevels` with all-default values.
20    pub fn new() -> Self {
21        Self {
22            room: default_power_level(),
23        }
24    }
25
26    /// Value associated with the given `key`.
27    pub fn get(&self, key: &str) -> Option<&i64> {
28        match key {
29            "room" => Some(&self.room),
30            _ => None,
31        }
32    }
33
34    /// Whether all fields have their default values.
35    pub fn is_default(&self) -> bool {
36        self.room == default_power_level()
37    }
38}
39
40impl Default for NotificationPowerLevels {
41    fn default() -> Self {
42        Self::new()
43    }
44}
45
46/// Used to default power levels to 50 during deserialization.
47pub fn default_power_level() -> i64 {
48    50
49}