Skip to main content

zaun_client/
typing.rs

1//! Who is typing in a channel (`signal.typing`, specification ยง10.5): named, or only how many.
2
3use serde::Serialize;
4use zaun_community_protocol::event::TypingSignal;
5use zaun_community_protocol::ids::{ChannelId, MemberId};
6use zaun_community_protocol::model::TypingMode;
7
8use crate::cache::CommunityData;
9
10/// How long one typing signal shows: the host repeats it at most every 3 s while typing goes on,
11/// so two missed repeats end it.
12pub const TYPING_SHOWN_MS: u64 = 6_000;
13
14/// Who else is typing in a channel, as the host last said. This client is never among them.
15#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
16#[serde(tag = "kind", rename_all = "snake_case")]
17pub enum TypingNow {
18    /// A `named` channel: the members typing.
19    Named {
20        /// The members.
21        member_ids: Vec<MemberId>,
22    },
23    /// An `anonymous` channel: how many, never who. 3 means "three or more".
24    Anonymous {
25        /// 1, 2 or 3.
26        count: u32,
27    },
28}
29
30impl TypingNow {
31    /// The state a `signal.typing` event describes; `None` when it names nobody.
32    #[must_use]
33    pub fn from_signal(s: &TypingSignal) -> Option<Self> {
34        match (s.count, s.member_ids.is_empty()) {
35            (Some(count), _) if count > 0 => Some(Self::Anonymous { count }),
36            (_, false) => Some(Self::Named {
37                member_ids: s.member_ids.clone(),
38            }),
39            _ => None,
40        }
41    }
42
43    /// How many are typing (for an anonymous channel, 3 means "three or more").
44    #[must_use]
45    pub fn count(&self) -> usize {
46        match self {
47            Self::Named { member_ids } => member_ids.len(),
48            Self::Anonymous { count } => usize::try_from(*count).unwrap_or(usize::MAX),
49        }
50    }
51}
52
53/// A typing state and when it stops showing.
54#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
55pub struct TypingSeen {
56    /// Who is typing.
57    pub now: TypingNow,
58    /// When it stops showing, ms since the epoch (this client's clock).
59    pub until_ms: u64,
60}
61
62impl CommunityData {
63    /// Who else is typing in `channel` at `now_ms`, if anyone.
64    #[must_use]
65    pub fn typing_in(&self, channel: &ChannelId, now_ms: u64) -> Option<&TypingNow> {
66        self.typing
67            .get(channel)
68            .filter(|t| t.until_ms > now_ms)
69            .map(|t| &t.now)
70    }
71
72    /// The channel's typing setting (`named` when the cache does not have the channel).
73    #[must_use]
74    pub fn typing_mode(&self, channel: &ChannelId) -> TypingMode {
75        self.channels
76            .get(channel)
77            .map_or(TypingMode::Named, |c| c.typing)
78    }
79
80    /// Records a `signal.typing` event received at `now_ms`.
81    pub(crate) fn saw_typing(&mut self, s: &TypingSignal, now_ms: u64) {
82        match TypingNow::from_signal(s) {
83            Some(now) => {
84                let until_ms = now_ms.saturating_add(TYPING_SHOWN_MS);
85                self.typing
86                    .insert(s.channel_id, TypingSeen { now, until_ms });
87            }
88            None => {
89                self.typing.remove(&s.channel_id);
90            }
91        }
92    }
93}