Skip to content

Commit

Permalink
Fix NSFW Checks (#418)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lakelezz committed Oct 20, 2018
1 parent 6a68f68 commit 75fb5c0
Show file tree
Hide file tree
Showing 6 changed files with 7 additions and 22 deletions.
3 changes: 1 addition & 2 deletions src/model/channel/channel_category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,9 @@ impl ChannelCategory {
})
}

#[cfg(feature = "utils")]
#[inline]
pub fn is_nsfw(&self) -> bool {
self.kind == ChannelType::Text && (self.nsfw || serenity_utils::is_nsfw(&self.name))
self.kind == ChannelType::Text && self.nsfw
}

/// Returns the name of the category.
Expand Down
4 changes: 0 additions & 4 deletions src/model/channel/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,8 @@ impl Group {

/// Determines if the channel is NSFW.
///
/// Refer to [`utils::is_nsfw`] for more details.
///
/// **Note**: This method is for consistency. This will always return
/// `false`, due to groups not being considered NSFW.
///
/// [`utils::is_nsfw`]: ../../utils/fn.is_nsfw.html
#[inline]
pub fn is_nsfw(&self) -> bool { false }

Expand Down
6 changes: 1 addition & 5 deletions src/model/channel/guild_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,18 +386,14 @@ impl GuildChannel {

/// Determines if the channel is NSFW.
///
/// Refer to [`utils::is_nsfw`] for more details.
///
/// Only [text channels][`ChannelType::Text`] are taken into consideration
/// as being NSFW. [voice channels][`ChannelType::Voice`] are never NSFW.
///
/// [`ChannelType::Text`]: enum.ChannelType.html#variant.Text
/// [`ChannelType::Voice`]: enum.ChannelType.html#variant.Voice
/// [`utils::is_nsfw`]: ../../utils/fn.is_nsfw.html
#[cfg(feature = "utils")]
#[inline]
pub fn is_nsfw(&self) -> bool {
self.kind == ChannelType::Text && (self.nsfw || serenity_utils::is_nsfw(&self.name))
self.kind == ChannelType::Text && self.nsfw
}

/// Gets a message from the channel.
Expand Down
10 changes: 3 additions & 7 deletions src/model/channel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,11 +334,7 @@ impl Channel {
}

/// Determines if the channel is NSFW.
///
/// Refer to [`utils::is_nsfw`] for more details.
///
/// [`utils::is_nsfw`]: ../../utils/fn.is_nsfw.html
#[cfg(all(feature = "model", feature = "utils"))]
#[cfg(feature = "model")]
#[inline]
pub fn is_nsfw(&self) -> bool {
match *self {
Expand Down Expand Up @@ -769,7 +765,7 @@ mod test {
#[test]
fn nsfw_checks() {
let mut channel = guild_channel();
assert!(channel.is_nsfw());
assert!(!channel.is_nsfw());
channel.kind = ChannelType::Voice;
assert!(!channel.is_nsfw());

Expand All @@ -778,7 +774,7 @@ mod test {
assert!(!channel.is_nsfw());

channel.name = "nsfw".to_string();
assert!(channel.is_nsfw());
assert!(!channel.is_nsfw());
channel.kind = ChannelType::Voice;
assert!(!channel.is_nsfw());
channel.kind = ChannelType::Text;
Expand Down
4 changes: 0 additions & 4 deletions src/model/channel/private_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,8 @@ impl PrivateChannel {

/// Determines if the channel is NSFW.
///
/// Refer to [`utils::is_nsfw`] for more details.
///
/// **Note**: This method is for consistency. This will always return
/// `false`, due to DMs not being considered NSFW.
///
/// [`utils::is_nsfw`]: ../../utils/fn.is_nsfw.html
#[inline]
pub fn is_nsfw(&self) -> bool { false }

Expand Down
2 changes: 2 additions & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ pub fn vecmap_to_json_map<K: PartialEq + ToString>(map: VecMap<K, Value>) -> Map
///
/// assert!(!utils::is_nsfw("nsfwstuff"));
/// ```
#[deprecated(since = "0.5.10", note = "Discord no longer turns a channel NSFW based on its name.")]
pub fn is_nsfw(name: &str) -> bool {
name == "nsfw" || name.chars().count() > 5 && name.starts_with("nsfw-")

This comment has been minimized.

Copy link
@meithecatte

meithecatte Oct 20, 2018

Contributor

Is there a reason for the length check? This will iterate over the string twice for no reason, AFAICS.

This comment has been minimized.

Copy link
@meithecatte

meithecatte Oct 20, 2018

Contributor

Ah, of course. Classic off-by-one.

}
Expand Down Expand Up @@ -566,6 +567,7 @@ mod test {
assert_eq!(parsed, ["a", "b c", "d", "e f", "g"]);
}

#[allow(deprecated)]
#[test]
fn test_is_nsfw() {
assert!(!is_nsfw("general"));
Expand Down

0 comments on commit 75fb5c0

Please sign in to comment.