twitch_api/helix/endpoints/moderation/
update_automod_settings.rs1use super::*;
57use helix::RequestPut;
58
59pub use super::AutoModSettings;
60
61#[derive(PartialEq, Eq, Deserialize, Serialize, Clone, Debug)]
65#[cfg_attr(feature = "typed-builder", derive(typed_builder::TypedBuilder))]
66#[non_exhaustive]
67pub struct UpdateAutoModSettingsRequest<'a> {
68 #[cfg_attr(feature = "typed-builder", builder(setter(into)))]
70 #[cfg_attr(feature = "deser_borrow", serde(borrow = "'a"))]
71 #[cfg_attr(not(feature = "deser_borrow"), serde(bound(deserialize = "'de: 'a")))]
72 pub broadcaster_id: Cow<'a, types::UserIdRef>,
73 #[cfg_attr(feature = "typed-builder", builder(setter(into)))]
75 #[cfg_attr(feature = "deser_borrow", serde(borrow = "'a"))]
76 #[cfg_attr(not(feature = "deser_borrow"), serde(bound(deserialize = "'de: 'a")))]
77 pub moderator_id: Cow<'a, types::UserIdRef>,
78}
79
80impl<'a> UpdateAutoModSettingsRequest<'a> {
81 pub fn new(
83 broadcaster_id: impl types::IntoCow<'a, types::UserIdRef> + 'a,
84 moderator_id: impl types::IntoCow<'a, types::UserIdRef> + 'a,
85 ) -> Self {
86 Self {
87 broadcaster_id: broadcaster_id.into_cow(),
88 moderator_id: moderator_id.into_cow(),
89 }
90 }
91}
92
93#[derive(PartialEq, Eq, Deserialize, Serialize, Clone, Debug)]
102#[serde(untagged)]
103#[non_exhaustive]
104pub enum UpdateAutoModSettingsBody {
105 #[non_exhaustive]
111 Overall {
112 overall_level: u8,
114 },
115 Individual(UpdateAutoModSettingsIndividual),
117}
118
119#[derive(PartialEq, Eq, Deserialize, Serialize, Clone, Debug, Default)]
124#[cfg_attr(feature = "typed-builder", derive(typed_builder::TypedBuilder))]
125#[non_exhaustive]
126pub struct UpdateAutoModSettingsIndividual {
127 #[cfg_attr(feature = "typed-builder", builder(default))]
129 #[serde(skip_serializing_if = "Option::is_none")]
130 pub aggression: Option<u8>,
131 #[cfg_attr(feature = "typed-builder", builder(default))]
133 #[serde(skip_serializing_if = "Option::is_none")]
134 pub bullying: Option<u8>,
135 #[cfg_attr(feature = "typed-builder", builder(default))]
137 #[serde(skip_serializing_if = "Option::is_none")]
138 pub disability: Option<u8>,
139 #[cfg_attr(feature = "typed-builder", builder(default))]
141 #[serde(skip_serializing_if = "Option::is_none")]
142 pub misogyny: Option<u8>,
143 #[cfg_attr(feature = "typed-builder", builder(default))]
145 #[serde(skip_serializing_if = "Option::is_none")]
146 pub race_ethnicity_or_religion: Option<u8>,
147 #[cfg_attr(feature = "typed-builder", builder(default))]
149 #[serde(skip_serializing_if = "Option::is_none")]
150 pub sex_based_terms: Option<u8>,
151 #[cfg_attr(feature = "typed-builder", builder(default))]
153 #[serde(skip_serializing_if = "Option::is_none")]
154 pub sexuality_sex_or_gender: Option<u8>,
155 #[cfg_attr(feature = "typed-builder", builder(default))]
157 #[serde(skip_serializing_if = "Option::is_none")]
158 pub swearing: Option<u8>,
159}
160
161impl helix::private::SealedSerialize for UpdateAutoModSettingsBody {}
162
163impl UpdateAutoModSettingsBody {
164 pub const fn overall(overall_level: u8) -> Self { Self::Overall { overall_level } }
166
167 pub const fn from_settings(settings: &AutoModSettings) -> Self {
169 Self::Individual(UpdateAutoModSettingsIndividual::from_settings(settings))
170 }
171}
172
173impl UpdateAutoModSettingsIndividual {
174 pub const fn from_settings(settings: &AutoModSettings) -> Self {
176 Self {
177 aggression: Some(settings.aggression),
178 bullying: Some(settings.bullying),
179 disability: Some(settings.disability),
180 misogyny: Some(settings.misogyny),
181 race_ethnicity_or_religion: Some(settings.race_ethnicity_or_religion),
182 sex_based_terms: Some(settings.sex_based_terms),
183 sexuality_sex_or_gender: Some(settings.sexuality_sex_or_gender),
184 swearing: Some(settings.swearing),
185 }
186 }
187}
188
189impl Request for UpdateAutoModSettingsRequest<'_> {
190 type Response = super::AutoModSettings;
191
192 const PATH: &'static str = "moderation/automod/settings";
193 #[cfg(feature = "twitch_oauth2")]
194 const SCOPE: twitch_oauth2::Validator =
195 twitch_oauth2::validator![twitch_oauth2::Scope::ModeratorManageAutomodSettings];
196}
197
198impl RequestPut for UpdateAutoModSettingsRequest<'_> {
199 type Body = UpdateAutoModSettingsBody;
200
201 fn parse_inner_response(
202 request: Option<Self>,
203 uri: &http::Uri,
204 response: &str,
205 status: http::StatusCode,
206 ) -> Result<helix::Response<Self, <Self as Request>::Response>, helix::HelixRequestPutError>
207 where
208 Self: Sized,
209 {
210 helix::parse_single_return(request, uri, response, status)
211 }
212}
213
214#[cfg(test)]
215#[test]
216fn test_request_overall() {
217 use helix::*;
218 let req = UpdateAutoModSettingsRequest::new("1234", "5678");
219 let body = UpdateAutoModSettingsBody::overall(3);
220
221 assert_eq!(
222 std::str::from_utf8(&body.try_to_body().unwrap()).unwrap(),
223 r#"{"overall_level":3}"#
224 );
225
226 req.create_request(body, "token", "clientid").unwrap();
227
228 let data = br#"
230 {
231 "data": [
232 {
233 "broadcaster_id": "1234",
234 "moderator_id": "5678",
235 "overall_level": 3,
236 "disability": 3,
237 "aggression": 3,
238 "sexuality_sex_or_gender": 3,
239 "misogyny": 3,
240 "bullying": 2,
241 "swearing": 0,
242 "race_ethnicity_or_religion": 3,
243 "sex_based_terms": 3
244 }
245 ]
246 }
247 "#
248 .to_vec();
249
250 let http_response = http::Response::builder().status(200).body(data).unwrap();
251
252 let uri = req.get_uri().unwrap();
253 assert_eq!(
254 uri.to_string(),
255 "https://api.twitch.tv/helix/moderation/automod/settings?broadcaster_id=1234&moderator_id=5678"
256 );
257
258 let res = UpdateAutoModSettingsRequest::parse_response(Some(req), &uri, http_response)
259 .unwrap()
260 .data;
261 assert_eq!(res.overall_level, Some(3));
262 assert_eq!(res.disability, 3);
263}
264
265#[cfg(test)]
266#[test]
267fn test_request_individual() {
268 use helix::*;
269 let req = UpdateAutoModSettingsRequest::new("1234", "5678");
270 let body = UpdateAutoModSettingsBody::Individual(UpdateAutoModSettingsIndividual {
271 aggression: Some(0),
272 bullying: Some(1),
273 disability: None,
274 misogyny: None,
275 race_ethnicity_or_religion: None,
276 sex_based_terms: None,
277 sexuality_sex_or_gender: None,
278 swearing: Some(2),
279 });
280
281 assert_eq!(
282 std::str::from_utf8(&body.try_to_body().unwrap()).unwrap(),
283 r#"{"aggression":0,"bullying":1,"swearing":2}"#
284 );
285
286 req.create_request(body, "token", "clientid").unwrap();
287
288 let data = br#"
289 {
290 "data": [
291 {
292 "aggression": 0,
293 "broadcaster_id": "1234",
294 "bullying": 1,
295 "disability": 0,
296 "misogyny": 0,
297 "moderator_id": "5678",
298 "overall_level": null,
299 "race_ethnicity_or_religion": 0,
300 "sex_based_terms": 0,
301 "sexuality_sex_or_gender": 0,
302 "swearing": 2
303 }
304 ]
305 }
306 "#
307 .to_vec();
308
309 let http_response = http::Response::builder().status(200).body(data).unwrap();
310
311 let uri = req.get_uri().unwrap();
312 assert_eq!(
313 uri.to_string(),
314 "https://api.twitch.tv/helix/moderation/automod/settings?broadcaster_id=1234&moderator_id=5678"
315 );
316
317 let res = UpdateAutoModSettingsRequest::parse_response(Some(req), &uri, http_response)
318 .unwrap()
319 .data;
320 assert_eq!(res.overall_level, None);
321 assert_eq!(res.swearing, 2);
322}