twilight_cache_inmemory/model/
presence.rs

1use serde::Serialize;
2use twilight_model::{
3    gateway::presence::{Activity, ClientStatus, Presence, Status},
4    id::{
5        marker::{GuildMarker, UserMarker},
6        Id,
7    },
8};
9
10use crate::CacheablePresence;
11
12/// Represents a cached [`Presence`].
13///
14/// [`Presence`]: twilight_model::gateway::presence::Presence
15#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
16pub struct CachedPresence {
17    pub(crate) activities: Vec<Activity>,
18    pub(crate) client_status: ClientStatus,
19    pub(crate) guild_id: Id<GuildMarker>,
20    pub(crate) status: Status,
21    pub(crate) user_id: Id<UserMarker>,
22}
23
24impl CachedPresence {
25    /// Current activities.
26    pub fn activities(&self) -> &[Activity] {
27        &self.activities
28    }
29
30    /// Platform-dependent status.
31    pub const fn client_status(&self) -> &ClientStatus {
32        &self.client_status
33    }
34
35    /// ID of the guild.
36    pub const fn guild_id(&self) -> Id<GuildMarker> {
37        self.guild_id
38    }
39
40    /// Status of the user.
41    pub const fn status(&self) -> Status {
42        self.status
43    }
44
45    /// ID of the user.
46    pub const fn user_id(&self) -> Id<UserMarker> {
47        self.user_id
48    }
49}
50
51impl From<Presence> for CachedPresence {
52    fn from(presence: Presence) -> Self {
53        let Presence {
54            activities,
55            client_status,
56            guild_id,
57            status,
58            user,
59        } = presence;
60
61        Self {
62            activities,
63            client_status,
64            guild_id,
65            status,
66            user_id: user.id(),
67        }
68    }
69}
70
71impl PartialEq<Presence> for CachedPresence {
72    fn eq(&self, other: &Presence) -> bool {
73        self.activities == other.activities
74            && self.client_status == other.client_status
75            && self.guild_id == other.guild_id
76            && self.status == other.status
77            && self.user_id == other.user.id()
78    }
79}
80
81impl CacheablePresence for CachedPresence {}
82
83#[cfg(test)]
84mod tests {
85    use super::CachedPresence;
86    use serde::Serialize;
87    use static_assertions::{assert_fields, assert_impl_all};
88    use std::fmt::Debug;
89    use twilight_model::gateway::presence::Presence;
90
91    assert_fields!(
92        CachedPresence: activities,
93        client_status,
94        guild_id,
95        status,
96        user_id
97    );
98    assert_impl_all!(
99        CachedPresence: Clone,
100        Debug,
101        Eq,
102        From<Presence>,
103        PartialEq,
104        PartialEq<Presence>,
105        Send,
106        Serialize,
107        Sync,
108    );
109}