1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{blocking, check_community_ban, get_local_user_view_from_jwt, post::*};
4 use lemmy_apub::activities::{post::create_or_update::CreateOrUpdatePost, CreateOrUpdateType};
5 use lemmy_db_queries::{source::post::Post_, Crud};
6 use lemmy_db_schema::{naive_now, source::post::*};
8 request::fetch_iframely_and_pictrs_data,
9 utils::{check_slurs_opt, clean_url_params, is_valid_post_title},
14 use lemmy_websocket::{send::send_post_ws_message, LemmyContext, UserOperationCrud};
16 #[async_trait::async_trait(?Send)]
17 impl PerformCrud for EditPost {
18 type Response = PostResponse;
22 context: &Data<LemmyContext>,
23 websocket_id: Option<ConnectionId>,
24 ) -> Result<PostResponse, LemmyError> {
25 let data: &EditPost = self;
26 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
28 check_slurs_opt(&data.name)?;
29 check_slurs_opt(&data.body)?;
31 if let Some(name) = &data.name {
32 if !is_valid_post_title(name) {
33 return Err(ApiError::err("invalid_post_title").into());
37 let post_id = data.post_id;
38 let orig_post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
41 local_user_view.person.id,
42 orig_post.community_id,
47 // Verify that only the creator can edit
48 if !Post::is_post_creator(local_user_view.person.id, orig_post.creator_id) {
49 return Err(ApiError::err("no_post_edit_allowed").into());
52 // Fetch Iframely and Pictrs cached image
53 let data_url = data.url.as_ref();
54 let (iframely_response, pictrs_thumbnail) =
55 fetch_iframely_and_pictrs_data(context.client(), data_url).await?;
56 let (embed_title, embed_description, embed_html) = iframely_response
57 .map(|u| (u.title, u.description, u.html))
58 .unwrap_or((None, None, None));
60 let post_form = PostForm {
61 creator_id: orig_post.creator_id.to_owned(),
62 community_id: orig_post.community_id,
63 name: data.name.to_owned().unwrap_or(orig_post.name),
64 url: data_url.map(|u| clean_url_params(u.to_owned()).into()),
65 body: data.body.to_owned(),
67 updated: Some(naive_now()),
71 thumbnail_url: pictrs_thumbnail.map(|u| u.into()),
75 let post_id = data.post_id;
76 let res = blocking(context.pool(), move |conn| {
77 Post::update(conn, post_id, &post_form)
80 let updated_post: Post = match res {
83 let err_type = if e.to_string() == "value too long for type character varying(200)" {
89 return Err(ApiError::err(err_type).into());
94 CreateOrUpdatePost::send(
96 &local_user_view.person,
97 CreateOrUpdateType::Update,
102 send_post_ws_message(
104 UserOperationCrud::EditPost,
106 Some(local_user_view.person.id),