Skip to main content

cosmic_applet_avestruz/
config.rs

1// SPDX-License-Identifier: MIT
2
3//! Persisted configuration schema for Avestruz.
4//!
5//! Settings are stored via COSMIC's config system and survive restarts.
6//! Secrets are not settings: an account's secret key is in the system keyring
7//! (see [`crate::nostr::account`]), and only its public half is named here.
8
9use std::collections::BTreeMap;
10
11use cosmic::cosmic_config::{self, CosmicConfigEntry, cosmic_config_derive::CosmicConfigEntry};
12use serde::{Deserialize, Serialize};
13
14use crate::emoji::Used;
15use crate::nostr::feed::FeedMode;
16
17/// The relays a new installation reads from: three large public relays.
18pub const DEFAULT_RELAYS: [&str; 3] = ["wss://relay.damus.io", "wss://nos.lol", "wss://relay.primal.net"];
19
20/// How much is logged, to the journal or the terminal.
21#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
22pub enum LogLevel {
23    /// Only errors.
24    Error,
25    /// Warnings and errors.
26    Warn,
27    /// Informational messages and above (the default).
28    #[default]
29    Info,
30    /// Debug messages and above.
31    Debug,
32    /// Everything.
33    Trace,
34}
35
36impl LogLevel {
37    /// Every level, from the least said to the most.
38    pub const ALL: [Self; 5] = [Self::Error, Self::Warn, Self::Info, Self::Debug, Self::Trace];
39
40    /// The `EnvFilter` base directive for this level.
41    pub fn as_filter_str(&self) -> &'static str {
42        match self {
43            Self::Error => "error",
44            Self::Warn => "warn",
45            Self::Info => "info",
46            Self::Debug => "debug",
47            Self::Trace => "trace",
48        }
49    }
50
51    /// The label for the settings dropdown. The names of the levels are
52    /// technical, and the same in every language.
53    pub fn display_name(&self) -> &'static str {
54        match self {
55            Self::Error => "Error",
56            Self::Warn => "Warning",
57            Self::Info => "Info (default)",
58            Self::Debug => "Debug",
59            Self::Trace => "Trace",
60        }
61    }
62}
63
64impl AsRef<str> for LogLevel {
65    fn as_ref(&self) -> &str {
66        self.display_name()
67    }
68}
69
70/// How large the text of notes is set: unless the settings say otherwise,
71/// and at the least and the most they can say.
72pub const NOTE_TEXT_SIZE_DEFAULT: u16 = 13;
73pub const NOTE_TEXT_SIZE_MIN: u16 = 10;
74pub const NOTE_TEXT_SIZE_MAX: u16 = 24;
75
76/// For how many days the event store keeps what it can ask the relays for
77/// again, unless the settings say otherwise; and the numbers of days the
78/// settings offer, of which `0` is for good.
79pub const KEEP_DAYS_DEFAULT: u32 = 30;
80pub const KEEP_DAYS_CHOICES: [u32; 5] = [7, 30, 90, 365, 0];
81
82/// Configuration for Avestruz
83#[derive(Debug, Clone, CosmicConfigEntry, PartialEq)]
84#[version = 1]
85pub struct Config {
86    /// The signed-in account, as an `npub`. `None` when signed out.
87    pub account: Option<String>,
88    /// The relays to read from, as `wss://` URLs.
89    pub relays: Vec<String>,
90    /// How much is logged.
91    pub log_level: LogLevel,
92    /// How large the text of notes is set. Read through
93    /// [`Config::note_text_size`]: the file is anyone's to edit.
94    pub note_text_size: u16,
95    /// Which notes the feed and the profiles show: with replies, or not.
96    pub feed_mode: FeedMode,
97    /// For how many days the event store keeps other people's notes and
98    /// what was made of them (see [`crate::nostr::sweep`]). `0` keeps them
99    /// for good.
100    pub keep_days: u32,
101    /// The emoji reacted with, how often and when last: what is offered
102    /// first to react with (see [`crate::emoji`]). Kept on this machine
103    /// only.
104    pub reaction_emojis: Vec<Used>,
105    /// When each account last looked at its notifications, in seconds
106    /// since the Unix epoch, by `npub`: what was written after is new to
107    /// it. Kept on this machine only.
108    pub notifications_seen: BTreeMap<String, u64>,
109}
110
111impl Config {
112    /// How large the text of notes is set, within what can be read.
113    pub fn note_text_size(&self) -> u16 {
114        self.note_text_size.clamp(NOTE_TEXT_SIZE_MIN, NOTE_TEXT_SIZE_MAX)
115    }
116}
117
118impl Default for Config {
119    fn default() -> Self {
120        Self {
121            account: None,
122            relays: DEFAULT_RELAYS.map(String::from).to_vec(),
123            log_level: LogLevel::default(),
124            note_text_size: NOTE_TEXT_SIZE_DEFAULT,
125            feed_mode: FeedMode::default(),
126            keep_days: KEEP_DAYS_DEFAULT,
127            reaction_emojis: Vec::new(),
128            notifications_seen: BTreeMap::new(),
129        }
130    }
131}