]> Untitled Git - lemmy.git/blobdiff - crates/api/src/lib.rs
Sanitize html (#3708)
[lemmy.git] / crates / api / src / lib.rs
index 44e7a76b59c458a25af7e4facc8ac4192b5a7b82..b297f503f6f9d4abc2115255a55e8ca567bd5fff 100644 (file)
@@ -3,7 +3,10 @@ use base64::{engine::general_purpose::STANDARD_NO_PAD as base64, Engine};
 use captcha::Captcha;
 use lemmy_api_common::{context::LemmyContext, utils::local_site_to_slur_regex};
 use lemmy_db_schema::source::local_site::LocalSite;
-use lemmy_utils::{error::LemmyError, utils::slurs::check_slurs};
+use lemmy_utils::{
+  error::{LemmyError, LemmyErrorExt, LemmyErrorType},
+  utils::slurs::check_slurs,
+};
 use std::io::Cursor;
 
 mod comment;
@@ -37,7 +40,7 @@ pub(crate) fn captcha_as_wav_base64(captcha: &Captcha) -> Result<String, LemmyEr
     if let Some(samples16) = samples.as_sixteen() {
       concat_samples.extend(samples16);
     } else {
-      return Err(LemmyError::from_message("couldnt_create_audio_captcha"));
+      return Err(LemmyErrorType::CouldntCreateAudioCaptcha)?;
     }
   }
 
@@ -45,39 +48,37 @@ pub(crate) fn captcha_as_wav_base64(captcha: &Captcha) -> Result<String, LemmyEr
   let mut output_buffer = Cursor::new(vec![]);
   let header = match any_header {
     Some(header) => header,
-    None => return Err(LemmyError::from_message("couldnt_create_audio_captcha")),
+    None => return Err(LemmyErrorType::CouldntCreateAudioCaptcha)?,
   };
-  let wav_write_result = wav::write(
+  wav::write(
     header,
     &wav::BitDepth::Sixteen(concat_samples),
     &mut output_buffer,
-  );
-  if let Err(e) = wav_write_result {
-    return Err(LemmyError::from_error_message(
-      e,
-      "couldnt_create_audio_captcha",
-    ));
-  }
+  )
+  .with_lemmy_type(LemmyErrorType::CouldntCreateAudioCaptcha)?;
 
   Ok(base64.encode(output_buffer.into_inner()))
 }
 
-/// Check size of report and remove whitespace
+/// Check size of report
 pub(crate) fn check_report_reason(reason: &str, local_site: &LocalSite) -> Result<(), LemmyError> {
   let slur_regex = &local_site_to_slur_regex(local_site);
 
   check_slurs(reason, slur_regex)?;
   if reason.is_empty() {
-    return Err(LemmyError::from_message("report_reason_required"));
+    return Err(LemmyErrorType::ReportReasonRequired)?;
   }
   if reason.chars().count() > 1000 {
-    return Err(LemmyError::from_message("report_too_long"));
+    return Err(LemmyErrorType::ReportTooLong)?;
   }
   Ok(())
 }
 
 #[cfg(test)]
 mod tests {
+  #![allow(clippy::unwrap_used)]
+  #![allow(clippy::indexing_slicing)]
+
   use lemmy_api_common::utils::check_validator_time;
   use lemmy_db_schema::{
     source::{
@@ -96,6 +97,7 @@ mod tests {
   #[serial]
   async fn test_should_not_validate_user_token_after_password_change() {
     let pool = &build_db_pool_for_tests().await;
+    let pool = &mut pool.into();
     let secret = Secret::init(pool).await.unwrap();
     let settings = &SETTINGS.to_owned();