1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 post::{EditPost, PostResponse},
5 request::fetch_site_data,
8 check_community_deleted_or_removed,
9 get_local_user_view_from_jwt,
10 local_site_to_slur_regex,
13 use lemmy_apub::protocol::activities::{
14 create_or_update::page::CreateOrUpdatePage,
17 use lemmy_db_schema::{
19 actor_language::CommunityLanguage,
20 local_site::LocalSite,
21 post::{Post, PostUpdateForm},
24 utils::diesel_option_overwrite,
28 utils::{check_slurs_opt, clean_url_params, is_valid_post_title},
31 use lemmy_websocket::{send::send_post_ws_message, LemmyContext, UserOperationCrud};
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?;
46 let local_site = LocalSite::read(context.pool()).await?;
48 let data_url = data.url.as_ref();
50 // TODO No good way to handle a clear.
51 // Issue link: https://github.com/LemmyNet/lemmy/issues/2287
52 let url = Some(data_url.map(clean_url_params).map(Into::into));
53 let body = diesel_option_overwrite(&data.body);
55 let slur_regex = local_site_to_slur_regex(&local_site);
56 check_slurs_opt(&data.name, &slur_regex)?;
57 check_slurs_opt(&data.body, &slur_regex)?;
59 if let Some(name) = &data.name {
60 if !is_valid_post_title(name) {
61 return Err(LemmyError::from_message("invalid_post_title"));
65 let post_id = data.post_id;
66 let orig_post = Post::read(context.pool(), post_id).await?;
69 local_user_view.person.id,
70 orig_post.community_id,
74 check_community_deleted_or_removed(orig_post.community_id, context.pool()).await?;
76 // Verify that only the creator can edit
77 if !Post::is_post_creator(local_user_view.person.id, orig_post.creator_id) {
78 return Err(LemmyError::from_message("no_post_edit_allowed"));
81 // Fetch post links and Pictrs cached image
82 let data_url = data.url.as_ref();
83 let (metadata_res, thumbnail_url) =
84 fetch_site_data(context.client(), context.settings(), data_url).await;
85 let (embed_title, embed_description, embed_video_url) = metadata_res
86 .map(|u| (Some(u.title), Some(u.description), Some(u.embed_video_url)))
89 let language_id = self.language_id;
90 CommunityLanguage::is_allowed_community_language(
93 orig_post.community_id,
97 let post_form = PostUpdateForm::builder()
98 .name(data.name.clone())
102 .embed_title(embed_title)
103 .embed_description(embed_description)
104 .embed_video_url(embed_video_url)
105 .language_id(data.language_id)
106 .thumbnail_url(Some(thumbnail_url))
109 let post_id = data.post_id;
110 let res = Post::update(context.pool(), post_id, &post_form).await;
111 let updated_post: Post = match res {
114 let err_type = if e.to_string() == "value too long for type character varying(200)" {
115 "post_title_too_long"
117 "couldnt_update_post"
120 return Err(LemmyError::from_error_message(e, err_type));
125 CreateOrUpdatePage::send(
127 &local_user_view.person.clone().into(),
128 CreateOrUpdateType::Update,
133 send_post_ws_message(
135 UserOperationCrud::EditPost,
137 Some(local_user_view.person.id),