use super::*;
use helix::RequestPut;
pub use super::AutoModSettings;
#[derive(PartialEq, Eq, Deserialize, Serialize, Clone, Debug)]
#[cfg_attr(feature = "typed-builder", derive(typed_builder::TypedBuilder))]
#[non_exhaustive]
pub struct UpdateAutoModSettingsRequest<'a> {
#[cfg_attr(feature = "typed-builder", builder(setter(into)))]
#[cfg_attr(feature = "deser_borrow", serde(borrow = "'a"))]
#[cfg_attr(not(feature = "deser_borrow"), serde(bound(deserialize = "'de: 'a")))]
pub broadcaster_id: Cow<'a, types::UserIdRef>,
#[cfg_attr(feature = "typed-builder", builder(setter(into)))]
#[cfg_attr(feature = "deser_borrow", serde(borrow = "'a"))]
#[cfg_attr(not(feature = "deser_borrow"), serde(bound(deserialize = "'de: 'a")))]
pub moderator_id: Cow<'a, types::UserIdRef>,
}
impl<'a> UpdateAutoModSettingsRequest<'a> {
pub fn new(
broadcaster_id: impl types::IntoCow<'a, types::UserIdRef> + 'a,
moderator_id: impl types::IntoCow<'a, types::UserIdRef> + 'a,
) -> Self {
Self {
broadcaster_id: broadcaster_id.into_cow(),
moderator_id: moderator_id.into_cow(),
}
}
}
#[derive(PartialEq, Eq, Deserialize, Serialize, Clone, Debug)]
#[serde(untagged)]
#[non_exhaustive]
pub enum UpdateAutoModSettingsBody {
#[non_exhaustive]
Overall {
overall_level: u8,
},
Individual(UpdateAutoModSettingsIndividual),
}
#[derive(PartialEq, Eq, Deserialize, Serialize, Clone, Debug, Default)]
#[cfg_attr(feature = "typed-builder", derive(typed_builder::TypedBuilder))]
#[non_exhaustive]
pub struct UpdateAutoModSettingsIndividual {
#[cfg_attr(feature = "typed-builder", builder(default))]
#[serde(skip_serializing_if = "Option::is_none")]
pub aggression: Option<u8>,
#[cfg_attr(feature = "typed-builder", builder(default))]
#[serde(skip_serializing_if = "Option::is_none")]
pub bullying: Option<u8>,
#[cfg_attr(feature = "typed-builder", builder(default))]
#[serde(skip_serializing_if = "Option::is_none")]
pub disability: Option<u8>,
#[cfg_attr(feature = "typed-builder", builder(default))]
#[serde(skip_serializing_if = "Option::is_none")]
pub misogyny: Option<u8>,
#[cfg_attr(feature = "typed-builder", builder(default))]
#[serde(skip_serializing_if = "Option::is_none")]
pub race_ethnicity_or_religion: Option<u8>,
#[cfg_attr(feature = "typed-builder", builder(default))]
#[serde(skip_serializing_if = "Option::is_none")]
pub sex_based_terms: Option<u8>,
#[cfg_attr(feature = "typed-builder", builder(default))]
#[serde(skip_serializing_if = "Option::is_none")]
pub sexuality_sex_or_gender: Option<u8>,
#[cfg_attr(feature = "typed-builder", builder(default))]
#[serde(skip_serializing_if = "Option::is_none")]
pub swearing: Option<u8>,
}
impl helix::private::SealedSerialize for UpdateAutoModSettingsBody {}
impl UpdateAutoModSettingsBody {
pub const fn overall(overall_level: u8) -> Self { Self::Overall { overall_level } }
pub const fn from_settings(settings: &AutoModSettings) -> Self {
Self::Individual(UpdateAutoModSettingsIndividual::from_settings(settings))
}
}
impl UpdateAutoModSettingsIndividual {
pub const fn from_settings(settings: &AutoModSettings) -> Self {
Self {
aggression: Some(settings.aggression),
bullying: Some(settings.bullying),
disability: Some(settings.disability),
misogyny: Some(settings.misogyny),
race_ethnicity_or_religion: Some(settings.race_ethnicity_or_religion),
sex_based_terms: Some(settings.sex_based_terms),
sexuality_sex_or_gender: Some(settings.sexuality_sex_or_gender),
swearing: Some(settings.swearing),
}
}
}
impl Request for UpdateAutoModSettingsRequest<'_> {
type Response = super::AutoModSettings;
const PATH: &'static str = "moderation/automod/settings";
#[cfg(feature = "twitch_oauth2")]
const SCOPE: twitch_oauth2::Validator =
twitch_oauth2::validator![twitch_oauth2::Scope::ModeratorManageAutomodSettings];
}
impl RequestPut for UpdateAutoModSettingsRequest<'_> {
type Body = UpdateAutoModSettingsBody;
fn parse_inner_response(
request: Option<Self>,
uri: &http::Uri,
response: &str,
status: http::StatusCode,
) -> Result<helix::Response<Self, <Self as Request>::Response>, helix::HelixRequestPutError>
where
Self: Sized,
{
helix::parse_single_return(request, uri, response, status)
}
}
#[cfg(test)]
#[test]
fn test_request_overall() {
use helix::*;
let req = UpdateAutoModSettingsRequest::new("1234", "5678");
let body = UpdateAutoModSettingsBody::overall(3);
assert_eq!(
std::str::from_utf8(&body.try_to_body().unwrap()).unwrap(),
r#"{"overall_level":3}"#
);
req.create_request(body, "token", "clientid").unwrap();
let data = br#"
{
"data": [
{
"broadcaster_id": "1234",
"moderator_id": "5678",
"overall_level": 3,
"disability": 3,
"aggression": 3,
"sexuality_sex_or_gender": 3,
"misogyny": 3,
"bullying": 2,
"swearing": 0,
"race_ethnicity_or_religion": 3,
"sex_based_terms": 3
}
]
}
"#
.to_vec();
let http_response = http::Response::builder().status(200).body(data).unwrap();
let uri = req.get_uri().unwrap();
assert_eq!(
uri.to_string(),
"https://api.twitch.tv/helix/moderation/automod/settings?broadcaster_id=1234&moderator_id=5678"
);
let res = UpdateAutoModSettingsRequest::parse_response(Some(req), &uri, http_response)
.unwrap()
.data;
assert_eq!(res.overall_level, Some(3));
assert_eq!(res.disability, 3);
}
#[cfg(test)]
#[test]
fn test_request_individual() {
use helix::*;
let req = UpdateAutoModSettingsRequest::new("1234", "5678");
let body = UpdateAutoModSettingsBody::Individual(UpdateAutoModSettingsIndividual {
aggression: Some(0),
bullying: Some(1),
disability: None,
misogyny: None,
race_ethnicity_or_religion: None,
sex_based_terms: None,
sexuality_sex_or_gender: None,
swearing: Some(2),
});
assert_eq!(
std::str::from_utf8(&body.try_to_body().unwrap()).unwrap(),
r#"{"aggression":0,"bullying":1,"swearing":2}"#
);
req.create_request(body, "token", "clientid").unwrap();
let data = br#"
{
"data": [
{
"aggression": 0,
"broadcaster_id": "1234",
"bullying": 1,
"disability": 0,
"misogyny": 0,
"moderator_id": "5678",
"overall_level": null,
"race_ethnicity_or_religion": 0,
"sex_based_terms": 0,
"sexuality_sex_or_gender": 0,
"swearing": 2
}
]
}
"#
.to_vec();
let http_response = http::Response::builder().status(200).body(data).unwrap();
let uri = req.get_uri().unwrap();
assert_eq!(
uri.to_string(),
"https://api.twitch.tv/helix/moderation/automod/settings?broadcaster_id=1234&moderator_id=5678"
);
let res = UpdateAutoModSettingsRequest::parse_response(Some(req), &uri, http_response)
.unwrap()
.data;
assert_eq!(res.overall_level, None);
assert_eq!(res.swearing, 2);
}