1 use actix_web::web::Data;
2 use lemmy_api_common::{
3 post::{EditPost, PostResponse},
4 request::fetch_site_data,
8 check_community_deleted_or_removed,
9 get_local_user_view_from_jwt,
12 use lemmy_apub::protocol::activities::{
13 create_or_update::post::CreateOrUpdatePost,
16 use lemmy_db_schema::{
19 post::{Post, PostForm},
22 utils::{diesel_option_overwrite, naive_now},
26 utils::{check_slurs_opt, clean_url_params, is_valid_post_title},
29 use lemmy_websocket::{send::send_post_ws_message, LemmyContext, UserOperationCrud};
31 use crate::PerformCrud;
33 #[async_trait::async_trait(?Send)]
34 impl PerformCrud for EditPost {
35 type Response = PostResponse;
37 #[tracing::instrument(skip(context, websocket_id))]
40 context: &Data<LemmyContext>,
41 websocket_id: Option<ConnectionId>,
42 ) -> Result<PostResponse, LemmyError> {
43 let data: &EditPost = self;
45 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
47 let data_url = data.url.as_ref();
49 // TODO No good way to handle a clear.
50 // Issue link: https://github.com/LemmyNet/lemmy/issues/2287
51 let url = Some(data_url.map(clean_url_params).map(Into::into));
52 let body = diesel_option_overwrite(&data.body);
54 let slur_regex = &context.settings().slur_regex();
55 check_slurs_opt(&data.name, slur_regex)?;
56 check_slurs_opt(&data.body, slur_regex)?;
58 if let Some(name) = &data.name {
59 if !is_valid_post_title(name) {
60 return Err(LemmyError::from_message("invalid_post_title"));
64 let post_id = data.post_id;
65 let orig_post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
68 local_user_view.person.id,
69 orig_post.community_id,
73 check_community_deleted_or_removed(orig_post.community_id, context.pool()).await?;
75 // Verify that only the creator can edit
76 if !Post::is_post_creator(local_user_view.person.id, orig_post.creator_id) {
77 return Err(LemmyError::from_message("no_post_edit_allowed"));
80 // Fetch post links and Pictrs cached image
81 let data_url = data.url.as_ref();
82 let (metadata_res, thumbnail_url) =
83 fetch_site_data(context.client(), context.settings(), data_url).await;
84 let (embed_title, embed_description, embed_video_url) = metadata_res
85 .map(|u| (Some(u.title), Some(u.description), Some(u.embed_video_url)))
88 let language_id = Some(
89 data.language_id.unwrap_or(
90 blocking(context.pool(), move |conn| {
91 Language::read_undetermined(conn)
97 let post_form = PostForm {
98 creator_id: orig_post.creator_id.to_owned(),
99 community_id: orig_post.community_id,
100 name: data.name.to_owned().unwrap_or(orig_post.name),
104 updated: Some(naive_now()),
109 thumbnail_url: Some(thumbnail_url),
110 ..PostForm::default()
113 let post_id = data.post_id;
114 let res = blocking(context.pool(), move |conn| {
115 Post::update(conn, post_id, &post_form)
118 let updated_post: Post = match res {
121 let err_type = if e.to_string() == "value too long for type character varying(200)" {
122 "post_title_too_long"
124 "couldnt_update_post"
127 return Err(LemmyError::from_error_message(e, err_type));
132 CreateOrUpdatePost::send(
134 &local_user_view.person.clone().into(),
135 CreateOrUpdateType::Update,
140 send_post_ws_message(
142 UserOperationCrud::EditPost,
144 Some(local_user_view.person.id),