Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(http, validate)!: channel user limits #2077

Merged
merged 3 commits into from
Jan 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions twilight-http/src/request/channel/update_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use twilight_model::{
use twilight_validate::{
channel::{
bitrate as validate_bitrate, forum_topic as validate_forum_topic, name as validate_name,
topic as validate_topic, ChannelValidationError,
topic as validate_topic, user_limit as validate_user_limit, ChannelValidationError,
},
request::{audit_reason as validate_audit_reason, ValidationError},
};
Expand Down Expand Up @@ -334,11 +334,18 @@ impl<'a> UpdateChannel<'a> {
/// Set to 0 for no limit. Limit can otherwise be between 1 and 99
/// inclusive. See [Discord Docs/Modify Channel].
///
/// # Errors
///
/// Returns an error of type [`UserLimitInvalid`] if the bitrate is invalid.
///
/// [Discord Docs/Modify Channel]: https://discord.com/developers/docs/resources/channel#modify-channel-json-params-guild-channel
pub const fn user_limit(mut self, user_limit: u16) -> Self {
/// [`UserLimitInvalid`]: twilight_validate::channel::ChannelValidationErrorType::UserLimitInvalid
pub fn user_limit(mut self, user_limit: u16) -> Result<Self, ChannelValidationError> {
validate_user_limit(user_limit)?;

self.fields.user_limit = Some(user_limit);

self
Ok(self)
}

/// Set the [`VideoQualityMode`] for the voice channel.
Expand Down
39 changes: 39 additions & 0 deletions twilight-validate/src/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ pub const CHANNEL_RATE_LIMIT_PER_USER_MAX: u16 = 21_600;
/// Maximum length of a channel's topic.
pub const CHANNEL_TOPIC_LENGTH_MAX: usize = 1024;

/// Maximum user limit of an audio channel.
pub const CHANNEL_USER_LIMIT_MAX: u16 = 99;

/// Returned when the channel can not be updated as configured.
#[derive(Debug)]
pub struct ChannelValidationError {
Expand Down Expand Up @@ -79,6 +82,11 @@ impl Display for ChannelValidationError {

f.write_str(" is not a thread")
}
ChannelValidationErrorType::UserLimitInvalid => {
f.write_str("user limit is greater than ")?;

Display::fmt(&CHANNEL_USER_LIMIT_MAX, f)
}
}
}
}
Expand Down Expand Up @@ -108,6 +116,8 @@ pub enum ChannelValidationErrorType {
/// Provided type.
kind: ChannelType,
},
/// User limit is greater than 99.
UserLimitInvalid,
}

/// Ensure a channel's bitrate is collect.
Expand Down Expand Up @@ -236,6 +246,25 @@ pub fn topic(value: impl AsRef<str>) -> Result<(), ChannelValidationError> {
}
}

/// Ensure a channel's user limit is correct.
///
/// Must be at most 99.
///
/// # Errors
///
/// Returns an error of type [`UserLimitInvalid`] if the user limit is invalid.
///
/// [`UserLimitInvalid`]: ChannelValidationErrorType::BitrateInvalid
pub const fn user_limit(value: u16) -> Result<(), ChannelValidationError> {
if value <= CHANNEL_USER_LIMIT_MAX {
Ok(())
} else {
Err(ChannelValidationError {
kind: ChannelValidationErrorType::UserLimitInvalid,
})
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -281,4 +310,14 @@ mod tests {

assert!(topic("a".repeat(1_025)).is_err());
}

#[test]
fn user_limit() {
assert!(super::user_limit(0).is_ok());
assert!(super::user_limit(99).is_ok());
assert!(matches!(
super::user_limit(100).unwrap_err().kind(),
ChannelValidationErrorType::UserLimitInvalid
));
}
}