Skip to main content

zaun_community_protocol/command/
scope.rs

1//! Facts about a command the envelope layer and the engine need besides its body.
2
3use super::{BodyError, Command, check};
4use crate::ids::ChannelId;
5use crate::limits::{SUSPENSION_MAX_MS, TIMEOUT_MAX_MS};
6
7/// Channel-scoped commands: the channel comes from the envelope's `channel_id` (§7.1) or,
8/// where §8 lists it, from the body.
9const CHANNEL_SCOPED: &[&str] = &[
10    "channel.update",
11    "channel.delete",
12    "channel.permissions.update",
13    "message.send",
14    "message.edit",
15    "message.delete",
16    "message.history",
17    "reaction.add",
18    "reaction.remove",
19    "attachment.upload",
20    "attachment.download",
21    "voice.join",
22    "signal.typing",
23];
24
25impl Command {
26    /// Whether the command needs a channel: the envelope's `channel_id` or the body's
27    /// ([`Command::channel_id`]). `invite.create` and `sync.events` name an optional or
28    /// stream-dependent channel in the body only and are not listed.
29    #[must_use]
30    pub fn is_channel_scoped(&self) -> bool {
31        CHANNEL_SCOPED.contains(&self.name())
32    }
33
34    /// The `channel_id` carried in the body, for commands whose §8/§10.5 body lists one.
35    /// When both the envelope and the body carry one they must be equal (envelope layer).
36    #[must_use]
37    pub const fn channel_id(&self) -> Option<ChannelId> {
38        match self {
39            Self::ChannelUpdate(b) => b.channel_id,
40            Self::ChannelDelete(b) => b.channel_id,
41            Self::ChannelPermissionsUpdate(b) => b.channel_id,
42            Self::SignalTyping(b) => b.channel_id,
43            Self::VoiceJoin(b) => b.channel_id,
44            Self::InviteCreate(b) => b.channel_id,
45            Self::SyncEvents(b) => b.channel_id,
46            _ => None,
47        }
48    }
49
50    /// Whether the command is mutating, hence idempotency-recorded (§7.3); the same rule as
51    /// [`crate::envelope::is_mutating`] on the command's wire name.
52    #[must_use]
53    pub fn is_mutating(&self) -> bool {
54        crate::envelope::is_mutating(self.name())
55    }
56
57    /// [`Command::validate`] plus the bounds relative to the host's clock: `member.timeout`
58    /// `until` ≤ now + 28 days and `membership.suspend` `until` ≤ now + 90 days.
59    ///
60    /// SPEC: whether an `until` in the past is an error is not said; it is accepted here
61    /// (the engine treats it as already elapsed).
62    ///
63    /// # Errors
64    /// As [`Command::validate`], or `BAD_REQUEST{field: "until"}`.
65    pub fn validate_at(&self, now_ms: u64) -> Result<(), BodyError> {
66        self.validate()?;
67        let (until, max) = match self {
68            Self::MemberTimeout(b) => (b.until, TIMEOUT_MAX_MS),
69            Self::MembershipSuspend(b) => (b.until, SUSPENSION_MAX_MS),
70            _ => return Ok(()),
71        };
72        until.map_or(Ok(()), |u| {
73            check::range("until", u, 0, now_ms.saturating_add(max))
74        })
75    }
76}