]> Untitled Git - lemmy.git/blobdiff - crates/api_crud/src/post/update.rs
Embed Peertube videos (#2261)
[lemmy.git] / crates / api_crud / src / post / update.rs
index 0a982d688d989bff69b6f254dd6fa73f953c26cb..3f3e0c2a0537fe97a60ec95c4a65c043bbbf977e 100644 (file)
@@ -1,27 +1,27 @@
 use actix_web::web::Data;
-
 use lemmy_api_common::{
-  blocking,
-  check_community_ban,
-  check_community_deleted_or_removed,
-  get_local_user_view_from_jwt,
-  post::*,
+  post::{EditPost, PostResponse},
+  request::fetch_site_data,
+  utils::{
+    blocking,
+    check_community_ban,
+    check_community_deleted_or_removed,
+    get_local_user_view_from_jwt,
+  },
 };
 use lemmy_apub::protocol::activities::{
   create_or_update::post::CreateOrUpdatePost,
   CreateOrUpdateType,
 };
 use lemmy_db_schema::{
-  naive_now,
   source::post::{Post, PostForm},
   traits::Crud,
+  utils::naive_now,
 };
 use lemmy_utils::{
-  request::fetch_site_data,
-  utils::{check_slurs_opt, clean_url_params, is_valid_post_title},
-  ApiError,
+  error::LemmyError,
+  utils::{check_slurs_opt, clean_optional_text, clean_url_params, is_valid_post_title},
   ConnectionId,
-  LemmyError,
 };
 use lemmy_websocket::{send::send_post_ws_message, LemmyContext, UserOperationCrud};
 
@@ -31,6 +31,7 @@ use crate::PerformCrud;
 impl PerformCrud for EditPost {
   type Response = PostResponse;
 
+  #[tracing::instrument(skip(context, websocket_id))]
   async fn perform(
     &self,
     context: &Data<LemmyContext>,
@@ -46,7 +47,7 @@ impl PerformCrud for EditPost {
 
     if let Some(name) = &data.name {
       if !is_valid_post_title(name) {
-        return Err(ApiError::err_plain("invalid_post_title").into());
+        return Err(LemmyError::from_message("invalid_post_title"));
       }
     }
 
@@ -63,29 +64,29 @@ impl PerformCrud for EditPost {
 
     // Verify that only the creator can edit
     if !Post::is_post_creator(local_user_view.person.id, orig_post.creator_id) {
-      return Err(ApiError::err_plain("no_post_edit_allowed").into());
+      return Err(LemmyError::from_message("no_post_edit_allowed"));
     }
 
     // Fetch post links and Pictrs cached image
     let data_url = data.url.as_ref();
-    let (metadata_res, pictrs_thumbnail) =
+    let (metadata_res, thumbnail_url) =
       fetch_site_data(context.client(), &context.settings(), data_url).await;
-    let (embed_title, embed_description, embed_html) = metadata_res
-      .map(|u| (u.title, u.description, u.html))
-      .unwrap_or((None, None, None));
+    let (embed_title, embed_description, embed_video_url) = metadata_res
+      .map(|u| (u.title, u.description, u.embed_video_url))
+      .unwrap_or_default();
 
     let post_form = PostForm {
       creator_id: orig_post.creator_id.to_owned(),
       community_id: orig_post.community_id,
       name: data.name.to_owned().unwrap_or(orig_post.name),
       url: data_url.map(|u| clean_url_params(u.to_owned()).into()),
-      body: data.body.to_owned(),
+      body: clean_optional_text(&data.body),
       nsfw: data.nsfw,
       updated: Some(naive_now()),
       embed_title,
       embed_description,
-      embed_html,
-      thumbnail_url: pictrs_thumbnail.map(|u| u.into()),
+      embed_video_url,
+      thumbnail_url,
       ..PostForm::default()
     };
 
@@ -103,13 +104,13 @@ impl PerformCrud for EditPost {
           "couldnt_update_post"
         };
 
-        return Err(ApiError::err(err_type, e).into());
+        return Err(LemmyError::from_error_message(e, err_type));
       }
     };
 
     // Send apub update
     CreateOrUpdatePost::send(
-      &updated_post.into(),
+      updated_post.into(),
       &local_user_view.person.clone().into(),
       CreateOrUpdateType::Update,
       context,