twilight_cache_any_backend/model/
presence.rs

1use twilight_model::{
2    gateway::presence::{Activity, ActivityFlags, ActivityType, Presence, Status},
3    id::{
4        marker::{ApplicationMarker, GuildMarker, UserMarker},
5        Id,
6    },
7};
8
9/// A cached activity
10///
11/// It is the same as [`twilight_model::gateway::presence::Activity`] except:
12///
13/// - `user_id` field is added, making it possible to return a user's activities
14///
15/// - `buttons` field is removed, as caching it is likely unnecessary, if you
16///   need this field, please create an issue
17///
18/// - `assets`, `emoji`, `party` and `party` fields are flattened, making this
19///   struct easier to cache
20///
21/// - `secrets` field is removed, as it's not sent to bots
22#[derive(Clone, Debug)]
23pub struct CachedActivity {
24    pub user_id: Id<UserMarker>,
25    pub application_id: Option<Id<ApplicationMarker>>,
26    pub asset_large_image: Option<String>,
27    pub asset_large_text: Option<String>,
28    pub asset_small_image: Option<String>,
29    pub asset_small_text: Option<String>,
30    pub created_at: Option<u64>,
31    pub details: Option<String>,
32    pub emoji_animated: Option<bool>,
33    pub emoji_name: Option<String>,
34    pub emoji_id: Option<String>,
35    pub flags: Option<ActivityFlags>,
36    pub id: Option<String>,
37    pub instance: Option<bool>,
38    pub kind: ActivityType,
39    pub name: String,
40    pub party_id: Option<String>,
41    pub party_size: Option<[u64; 2]>,
42    pub state: Option<String>,
43    pub timestamp_end: Option<u64>,
44    pub timestamp_start: Option<u64>,
45    pub url: Option<String>,
46}
47
48impl CachedActivity {
49    /// Create a cached activity from a given activity and user ID
50    #[must_use]
51    pub fn from_activity(activity: &Activity, user_id: Id<UserMarker>) -> Self {
52        Self {
53            user_id,
54            application_id: activity.application_id,
55            asset_large_image: activity
56                .assets
57                .as_ref()
58                .and_then(|asset| asset.large_image.clone()),
59            asset_large_text: activity
60                .assets
61                .as_ref()
62                .and_then(|asset| asset.large_text.clone()),
63            asset_small_image: activity
64                .assets
65                .as_ref()
66                .and_then(|asset| asset.small_image.clone()),
67            asset_small_text: activity
68                .assets
69                .as_ref()
70                .and_then(|asset| asset.small_text.clone()),
71            created_at: activity.created_at,
72            details: activity.details.clone(),
73            emoji_animated: activity.emoji.as_ref().and_then(|emoji| emoji.animated),
74            emoji_name: activity.emoji.as_ref().map(|emoji| emoji.name.clone()),
75            emoji_id: activity.emoji.as_ref().and_then(|emoji| emoji.id.clone()),
76            flags: activity.flags,
77            id: activity.id.clone(),
78            instance: activity.instance,
79            kind: activity.kind,
80            name: activity.name.clone(),
81            party_id: activity.party.as_ref().and_then(|party| party.id.clone()),
82            party_size: activity.party.as_ref().and_then(|party| party.size),
83            state: activity.state.clone(),
84            timestamp_end: activity
85                .timestamps
86                .as_ref()
87                .and_then(|timestamp| timestamp.end),
88            timestamp_start: activity
89                .timestamps
90                .as_ref()
91                .and_then(|timestamp| timestamp.start),
92            url: activity.url.clone(),
93        }
94    }
95}
96
97/// A cached presence
98///
99/// It's the same as [`twilight_model::gateway::presence::Presence`] except:
100///
101/// - `user` field is changed to a user ID, since users are cached separately
102///
103/// - `client_status` field is removed, as caching it is likely unnecessary, if
104///   you need this field, please create an issue
105///
106/// - `activities` field is removed, since they're cached separately
107#[derive(Clone, Copy, Debug)]
108pub struct CachedPresence {
109    pub guild_id: Id<GuildMarker>,
110    pub status: Status,
111    pub user: Id<UserMarker>,
112}
113
114impl From<&Presence> for CachedPresence {
115    fn from(presence: &Presence) -> Self {
116        Self {
117            guild_id: presence.guild_id,
118            status: presence.status,
119            user: presence.user.id(),
120        }
121    }
122}