1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
use derive_builder::Builder;
use matrix_sdk_base::ruma::events::macros::EventContent;
use matrix_sdk_base::ruma::events::EmptyStateKey;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Deserialize, Serialize, Builder)]
pub struct SimpleSettingWithTurnOff {
    active: bool,
}

impl Default for SimpleSettingWithTurnOff {
    fn default() -> Self {
        SimpleSettingWithTurnOff { active: true }
    }
}

impl SimpleSettingWithTurnOff {
    pub fn off() -> Option<Self> {
        Some(SimpleSettingWithTurnOff { active: false })
    }
    pub fn active(&self) -> bool {
        self.active
    }
    pub fn updater(&self) -> SimpleSettingWithTurnOffBuilder {
        SimpleSettingWithTurnOffBuilder::default()
            .active(self.active)
            .to_owned()
    }
}

#[derive(Clone, Debug, Deserialize, Serialize, Builder, Default)]
pub struct SimpleOnOffSetting {
    // default: off
    active: bool,
}
impl SimpleOnOffSetting {
    pub fn off() -> Option<Self> {
        // no need, we are off by default
        None
    }

    pub fn active(&self) -> bool {
        self.active
    }
    pub fn updater(&self) -> SimpleOnOffSettingBuilder {
        SimpleOnOffSettingBuilder::default()
            .active(self.active)
            .to_owned()
    }
}

pub type TasksSettings = SimpleOnOffSetting;
pub type NewsSettings = SimpleSettingWithTurnOff;
pub type PinsSettings = SimpleSettingWithTurnOff;
pub type EventsSettings = SimpleSettingWithTurnOff;

/// Backwards compatibility note:
///
/// In an earlier version, we agreed that if pins, news and events hadn't changed,
/// we'd assume they are activated. Even switching the default today means, we'd
/// change that behavior for all where at least _some_ had been changed. Thus, we
/// are keeping that behavior but _recommend_ using `off` to explicitly set
/// the right behavior up for all future cases.
#[derive(Clone, Debug, Deserialize, Serialize, EventContent, Builder, Default)]
#[ruma_event(type = "global.acter.app_settings", kind = State, state_key_type = EmptyStateKey)]
pub struct ActerAppSettingsContent {
    news: Option<NewsSettings>,
    pins: Option<PinsSettings>,
    events: Option<EventsSettings>,
    tasks: Option<TasksSettings>,
}

impl ActerAppSettingsContent {
    pub fn news(&self) -> NewsSettings {
        self.news.clone().unwrap_or_default()
    }
    pub fn pins(&self) -> PinsSettings {
        self.pins.clone().unwrap_or_default()
    }
    pub fn events(&self) -> EventsSettings {
        self.events.clone().unwrap_or_default()
    }
    pub fn tasks(&self) -> TasksSettings {
        self.tasks.clone().unwrap_or_default()
    }

    pub fn off() -> ActerAppSettingsContent {
        ActerAppSettingsContent {
            news: NewsSettings::off(),
            pins: PinsSettings::off(),
            events: EventsSettings::off(),
            tasks: TasksSettings::off(),
        }
    }

    pub fn updater(&self) -> ActerAppSettingsContentBuilder {
        ActerAppSettingsContentBuilder::default()
            .news(self.news.clone())
            .pins(self.pins.clone())
            .events(self.events.clone())
            .tasks(self.tasks.clone())
            .to_owned()
    }
}