Skip to content

Commit

Permalink
Merge pull request #461 from twitch-rs/fix/automod-thing
Browse files Browse the repository at this point in the history
fix(eventsub): flatten automod v2 held reason
  • Loading branch information
Nerixyz authored Dec 5, 2024
2 parents 0c7edfc + f3668dd commit ecf5cd2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/eventsub/automod/message/hold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ fn parse_payload_v2_automod() {
assert_eq!(notif.broadcaster_user_id.as_str(), "129546453");
assert_eq!(notif.message.fragments.len(), 2);

let AutomodHeldReason::Automod { automod } = &notif.reason else {
let AutomodHeldReason::Automod(automod) = &notif.reason else {
panic!("invalid held reason");
};
assert_eq!(automod.category, AutomodCategory::Swearing);
Expand Down Expand Up @@ -419,7 +419,7 @@ fn parse_payload_v2_blocked_term() {
assert_eq!(notif.broadcaster_user_id.as_str(), "129546453");
assert_eq!(notif.message.fragments.len(), 7);

let AutomodHeldReason::BlockedTerm { blocked_term } = &notif.reason else {
let AutomodHeldReason::BlockedTerm(blocked_term) = &notif.reason else {
panic!("invalid held reason");
};
assert_eq!(blocked_term.terms_found.len(), 2);
Expand Down
20 changes: 12 additions & 8 deletions src/eventsub/automod/message/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,11 @@ pub struct AutomodMessageEmote {
#[non_exhaustive]
pub enum AutomodHeldReason {
/// The message was caught by automod's rules
Automod {
/// Information on why a message was caught by automod
automod: AutomodMessageInfo,
},
#[serde(with = "crate::eventsub::enum_field_as_inner")]
Automod(AutomodMessageInfo),
/// The message was caught because of one or more blocked terms
BlockedTerm {
/// Information on which blocked terms were matched in a message
blocked_term: AutomodBlockedTermInfo,
},
#[serde(with = "crate::eventsub::enum_field_as_inner")]
BlockedTerm(AutomodBlockedTermInfo),
}

/// Information on why a message was caught by automod
Expand All @@ -132,6 +128,10 @@ pub struct AutomodMessageInfo {
pub boundaries: Vec<AutomodMessageBoundary>,
}

impl crate::eventsub::NamedField for AutomodMessageInfo {
const NAME: &'static str = "automod";
}

/// Information on which blocked terms were matched in a message
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
Expand All @@ -141,6 +141,10 @@ pub struct AutomodBlockedTermInfo {
pub terms_found: Vec<AutomodBlockedTerm>,
}

impl crate::eventsub::NamedField for AutomodBlockedTermInfo {
const NAME: &'static str = "blocked_term";
}

/// The bounds of the text that caused the message to be caught.
///
/// These bounds are given in Unicode code points not in bytes.
Expand Down
4 changes: 2 additions & 2 deletions src/eventsub/automod/message/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ fn parse_payload_v2_automod() {
assert_eq!(notif.message.fragments.len(), 3);
assert_eq!(notif.status, AutomodMessageStatus::Denied);

let AutomodHeldReason::Automod { automod } = &notif.reason else {
let AutomodHeldReason::Automod(automod) = &notif.reason else {
panic!("invalid held reason");
};
assert_eq!(automod.category, AutomodCategory::Swearing);
Expand Down Expand Up @@ -416,7 +416,7 @@ fn parse_payload_v2_blocked_term() {
assert_eq!(notif.message.fragments.len(), 1);
assert_eq!(notif.status, AutomodMessageStatus::Approved);

let AutomodHeldReason::BlockedTerm { blocked_term } = &notif.reason else {
let AutomodHeldReason::BlockedTerm(blocked_term) = &notif.reason else {
panic!("invalid held reason");
};
assert_eq!(blocked_term.terms_found.len(), 1);
Expand Down

0 comments on commit ecf5cd2

Please sign in to comment.