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::ApubObjectType;
5 use lemmy_db_queries::{source::post::Post_, Crud};
6 use lemmy_db_schema::{naive_now, source::post::*};
7 use lemmy_db_views::post_view::PostView;
9 request::fetch_iframely_and_pictrs_data,
10 utils::{check_slurs, check_slurs_opt, is_valid_post_title},
15 use lemmy_websocket::{messages::SendPost, LemmyContext, UserOperation};
17 #[async_trait::async_trait(?Send)]
18 impl PerformCrud for EditPost {
19 type Response = PostResponse;
23 context: &Data<LemmyContext>,
24 websocket_id: Option<ConnectionId>,
25 ) -> Result<PostResponse, LemmyError> {
26 let data: &EditPost = &self;
27 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
29 check_slurs(&data.name)?;
30 check_slurs_opt(&data.body)?;
32 if !is_valid_post_title(&data.name) {
33 return Err(ApiError::err("invalid_post_title").into());
36 let post_id = data.post_id;
37 let orig_post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
40 local_user_view.person.id,
41 orig_post.community_id,
46 // Verify that only the creator can edit
47 if !Post::is_post_creator(local_user_view.person.id, orig_post.creator_id) {
48 return Err(ApiError::err("no_post_edit_allowed").into());
51 // Fetch Iframely and Pictrs cached image
52 let data_url = data.url.as_ref();
53 let (iframely_title, iframely_description, iframely_html, pictrs_thumbnail) =
54 fetch_iframely_and_pictrs_data(context.client(), data_url).await;
56 let post_form = PostForm {
57 name: data.name.trim().to_owned(),
58 url: data_url.map(|u| u.to_owned().into()),
59 body: data.body.to_owned(),
61 creator_id: orig_post.creator_id.to_owned(),
62 community_id: orig_post.community_id,
63 removed: Some(orig_post.removed),
64 deleted: Some(orig_post.deleted),
65 locked: Some(orig_post.locked),
66 stickied: Some(orig_post.stickied),
67 updated: Some(naive_now()),
68 embed_title: iframely_title,
69 embed_description: iframely_description,
70 embed_html: iframely_html,
71 thumbnail_url: pictrs_thumbnail.map(|u| u.into()),
72 ap_id: Some(orig_post.ap_id),
73 local: orig_post.local,
77 let post_id = data.post_id;
78 let res = blocking(context.pool(), move |conn| {
79 Post::update(conn, post_id, &post_form)
82 let updated_post: Post = match res {
85 let err_type = if e.to_string() == "value too long for type character varying(200)" {
91 return Err(ApiError::err(err_type).into());
97 .send_update(&local_user_view.person, context)
100 let post_id = data.post_id;
101 let post_view = blocking(context.pool(), move |conn| {
102 PostView::read(conn, post_id, Some(local_user_view.person.id))
106 let res = PostResponse { post_view };
108 context.chat_server().do_send(SendPost {
109 op: UserOperation::EditPost,