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
use super::*;

/// Identifies the channel's chat settings (e.g., slow mode duration).
#[derive(Debug, PartialEq, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct RoomState {
    pub(super) tags: Tags,
    pub(super) channel: Channel,
}

impl RoomState {
    /// IRC tags
    pub fn tags(&self) -> &Tags {
        &self.tags
    }
    /// The channel this event came from
    pub fn channel(&self) -> &Channel {
        &self.channel
    }
}

impl RoomState {
    /// Whether this room is in emote-only mode
    pub fn emote_only(&self) -> bool {
        self.get_as_bool("emote-only")
    }
    /// Whether this room is in followers-only mode
    pub fn followers_only(&self) -> FollowersOnly {
        self.get("followers-only")
            .and_then(|k| k.parse().ok())
            .map(|k| match k {
                -1 => FollowersOnly::Disabled,
                0 => FollowersOnly::All,
                d => FollowersOnly::Limit(d),
            })
            .unwrap_or_else(|| FollowersOnly::All)
    }
    /// Whether this room is in r9k mode
    pub fn r9k(&self) -> bool {
        self.get_as_bool("r9k")
    }
    /// Whether this room is in slow mode
    pub fn slow(&self) -> u64 {
        self.get_parsed("slow").unwrap_or_else(|| 0)
    }
    /// Whether this room is in subs-only mode
    pub fn subs_only(&self) -> bool {
        self.get_as_bool("subs-only")
    }
}

impl Tag for RoomState {
    fn get(&self, key: &str) -> Option<&str> {
        self.tags.get(key).map(AsRef::as_ref)
    }
}

/// Followers-only mode
#[derive(Debug, PartialEq, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum FollowersOnly {
    /// `Disabled` signifies that anyone can chat
    Disabled,
    /// `All` signifies that only followers can talk
    All,
    /// `Limit` signifies that followers can only talk if they've been following
    /// for the specified number of minutes
    Limit(i64), // maybe make this a Duration
}