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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
use serde::Serialize;
use twilight_model::{
    id::{
        marker::{ChannelMarker, GuildMarker, UserMarker},
        Id,
    },
    util::Timestamp,
    voice::VoiceState,
};

/// Represents a cached [`VoiceState`].
///
/// [`VoiceState`]: twilight_model::voice::VoiceState
#[allow(clippy::struct_excessive_bools)]
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct CachedVoiceState {
    channel_id: Id<ChannelMarker>,
    deaf: bool,
    guild_id: Id<GuildMarker>,
    mute: bool,
    request_to_speak_timestamp: Option<Timestamp>,
    self_deaf: bool,
    self_mute: bool,
    self_stream: bool,
    self_video: bool,
    session_id: String,
    suppress: bool,
    user_id: Id<UserMarker>,
}

impl CachedVoiceState {
    /// ID of the channel that this user is connected to.
    pub const fn channel_id(&self) -> Id<ChannelMarker> {
        self.channel_id
    }

    /// Whether the user is deafened.
    pub const fn deaf(&self) -> bool {
        self.deaf
    }

    /// ID of the guild that this user is connected in, if there is one.
    pub const fn guild_id(&self) -> Id<GuildMarker> {
        self.guild_id
    }

    /// Whether the user is muted.
    pub const fn mute(&self) -> bool {
        self.mute
    }

    /// Timestamp of when the user requested to speak.
    pub const fn request_to_speak_timestamp(&self) -> Option<Timestamp> {
        self.request_to_speak_timestamp
    }

    /// Whether the user has deafened themself.
    pub const fn self_deaf(&self) -> bool {
        self.self_deaf
    }

    /// Whether the user has muted themself.
    pub const fn self_mute(&self) -> bool {
        self.self_mute
    }

    /// Whether the user is streaming via "Go Live".
    pub const fn self_stream(&self) -> bool {
        self.self_stream
    }

    /// Whether the user's camera is enabled.
    pub const fn self_video(&self) -> bool {
        self.self_video
    }

    /// Session ID.
    pub fn session_id(&self) -> &str {
        &self.session_id
    }

    /// Whether this user is muted by the current user.
    pub const fn suppress(&self) -> bool {
        self.suppress
    }

    /// ID of the user.
    pub const fn user_id(&self) -> Id<UserMarker> {
        self.user_id
    }

    /// Construct a cached voice state from its [`twilight_model`] form.
    #[allow(clippy::missing_const_for_fn)]
    pub(crate) fn from_model(
        channel_id: Id<ChannelMarker>,
        guild_id: Id<GuildMarker>,
        voice_state: VoiceState,
    ) -> Self {
        // Reasons for dropping fields:
        //
        // - `channel_id`: provided as a function parameter
        // - `guild_id`: provided as a function parameter
        // - `member`: we have the user's ID from the `user_id` field
        let VoiceState {
            channel_id: _,
            deaf,
            guild_id: _,
            member: _,
            mute,
            self_deaf,
            self_mute,
            self_stream,
            self_video,
            session_id,
            suppress,
            user_id,
            request_to_speak_timestamp,
        } = voice_state;

        Self {
            channel_id,
            deaf,
            guild_id,
            mute,
            request_to_speak_timestamp,
            self_deaf,
            self_mute,
            self_stream,
            self_video,
            session_id,
            suppress,
            user_id,
        }
    }
}

impl PartialEq<VoiceState> for CachedVoiceState {
    fn eq(&self, other: &VoiceState) -> bool {
        Some(self.channel_id) == other.channel_id
            && self.deaf == other.deaf
            && Some(self.guild_id) == other.guild_id
            && self.mute == other.mute
            && self.request_to_speak_timestamp == other.request_to_speak_timestamp
            && self.self_deaf == other.self_deaf
            && self.self_mute == other.self_mute
            && self.self_stream == other.self_stream
            && self.self_video == other.self_video
            && self.session_id == other.session_id
            && self.suppress == other.suppress
            && self.user_id == other.user_id
    }
}

#[cfg(test)]
mod tests {
    use super::CachedVoiceState;
    use crate::test;
    use serde::Serialize;
    use static_assertions::{assert_fields, assert_impl_all};
    use std::fmt::Debug;
    use twilight_model::{
        id::{
            marker::{ChannelMarker, GuildMarker, UserMarker},
            Id,
        },
        voice::VoiceState,
    };

    assert_fields!(
        CachedVoiceState: channel_id,
        deaf,
        guild_id,
        mute,
        request_to_speak_timestamp,
        self_deaf,
        self_mute,
        self_stream,
        self_video,
        session_id,
        suppress,
        user_id
    );
    assert_impl_all!(
        CachedVoiceState: Clone,
        Debug,
        Eq,
        PartialEq,
        PartialEq<VoiceState>,
        Serialize,
    );

    const CHANNEL_ID: Id<ChannelMarker> = Id::new(1);
    const GUILD_ID: Id<GuildMarker> = Id::new(2);
    const USER_ID: Id<UserMarker> = Id::new(3);

    #[test]
    fn eq() {
        let voice_state = test::voice_state(GUILD_ID, Some(CHANNEL_ID), USER_ID);
        let cached = CachedVoiceState::from_model(CHANNEL_ID, GUILD_ID, voice_state.clone());

        assert_eq!(cached, voice_state);
    }

    #[test]
    fn getters() {
        let voice_state = test::voice_state(GUILD_ID, Some(CHANNEL_ID), USER_ID);
        let cached = CachedVoiceState::from_model(CHANNEL_ID, GUILD_ID, voice_state.clone());

        assert_eq!(Some(cached.channel_id()), voice_state.channel_id);
        assert_eq!(cached.deaf(), voice_state.deaf);
        assert_eq!(Some(cached.guild_id()), voice_state.guild_id);
        assert_eq!(cached.mute(), voice_state.mute);
        assert_eq!(
            cached.request_to_speak_timestamp(),
            voice_state.request_to_speak_timestamp
        );
        assert_eq!(cached.self_deaf(), voice_state.self_deaf);
        assert_eq!(cached.self_mute(), voice_state.self_mute);
        assert_eq!(cached.self_stream(), voice_state.self_stream);
        assert_eq!(cached.self_video(), voice_state.self_video);
        assert_eq!(cached.session_id(), voice_state.session_id);
        assert_eq!(cached.suppress(), voice_state.suppress);
        assert_eq!(cached.user_id(), voice_state.user_id);
    }
}