1pub use zaun_community_protocol::command::object::{Activity, ActivityType};
7pub use zaun_community_protocol::command::signal::PresenceStatus as Status;
8use zaun_community_protocol::event::PresenceSignal;
9use zaun_community_protocol::ids::MemberId;
10
11use crate::cache::CommunityData;
12
13#[derive(Debug, Clone, PartialEq, Eq)]
15pub struct Presence {
16 pub member: MemberId,
18 pub status: Status,
20 pub activity: Option<Activity>,
22}
23
24impl From<&PresenceSignal> for Presence {
25 fn from(p: &PresenceSignal) -> Self {
26 Self {
27 member: p.member_id,
28 status: p.status,
29 activity: p.activity.clone(),
30 }
31 }
32}
33
34impl Presence {
35 #[must_use]
38 pub fn activity_line(&self) -> Option<String> {
39 self.activity.as_ref().map(line)
40 }
41}
42
43#[must_use]
46pub fn line(a: &Activity) -> String {
47 match a.kind {
48 ActivityType::Playing => format!("Playing {}", a.name),
49 ActivityType::Streaming => format!("Streaming {}", a.name),
50 ActivityType::Listening => format!("Listening to {}", a.name),
51 ActivityType::Watching => format!("Watching {}", a.name),
52 ActivityType::Competing => format!("Competing in {}", a.name),
53 ActivityType::Custom => match &a.emoji {
54 Some(e) => format!("{e} {}", a.name),
55 None => a.name.clone(),
56 },
57 }
58}
59
60#[must_use]
63pub fn activity(kind: ActivityType, name: impl Into<String>) -> Activity {
64 Activity {
65 kind,
66 name: name.into(),
67 details: None,
68 state: None,
69 url: None,
70 started_at: None,
71 emoji: None,
72 }
73}
74
75impl CommunityData {
76 pub(crate) fn saw_presence(&mut self, p: &PresenceSignal) {
78 if p.status.public() == Status::Offline {
79 self.presences.remove(&p.member_id);
80 } else {
81 self.presences.insert(p.member_id, p.clone());
82 }
83 }
84
85 pub(crate) fn set_presences(&mut self, list: Vec<PresenceSignal>) {
87 self.presences = list.into_iter().map(|p| (p.member_id, p)).collect();
88 }
89
90 #[must_use]
92 pub fn presence_of(&self, member: MemberId) -> Presence {
93 self.presences.get(&member).map_or(
94 Presence {
95 member,
96 status: Status::Offline,
97 activity: None,
98 },
99 Presence::from,
100 )
101 }
102}