1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
6 get_local_user_view_from_jwt,
11 activities::{post::create_or_update::CreateOrUpdatePost, CreateOrUpdateType},
12 generate_apub_endpoint,
16 use lemmy_db_queries::{source::post::Post_, Crud, Likeable};
17 use lemmy_db_schema::source::post::*;
18 use lemmy_db_views::post_view::PostView;
20 request::fetch_iframely_and_pictrs_data,
21 utils::{check_slurs, check_slurs_opt, clean_url_params, is_valid_post_title},
26 use lemmy_websocket::{messages::SendPost, LemmyContext, UserOperationCrud};
28 #[async_trait::async_trait(?Send)]
29 impl PerformCrud for CreatePost {
30 type Response = PostResponse;
34 context: &Data<LemmyContext>,
35 websocket_id: Option<ConnectionId>,
36 ) -> Result<PostResponse, LemmyError> {
37 let data: &CreatePost = self;
38 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
40 check_slurs(&data.name)?;
41 check_slurs_opt(&data.body)?;
43 if !is_valid_post_title(&data.name) {
44 return Err(ApiError::err("invalid_post_title").into());
47 check_community_ban(local_user_view.person.id, data.community_id, context.pool()).await?;
49 // Fetch Iframely and pictrs cached image
50 let data_url = data.url.as_ref();
51 let (iframely_title, iframely_description, iframely_html, pictrs_thumbnail) =
52 fetch_iframely_and_pictrs_data(context.client(), data_url).await;
54 let post_form = PostForm {
55 name: data.name.trim().to_owned(),
56 url: data_url.map(|u| clean_url_params(u.to_owned()).into()),
57 body: data.body.to_owned(),
58 community_id: data.community_id,
59 creator_id: local_user_view.person.id,
61 embed_title: iframely_title,
62 embed_description: iframely_description,
63 embed_html: iframely_html,
64 thumbnail_url: pictrs_thumbnail.map(|u| u.into()),
69 match blocking(context.pool(), move |conn| Post::create(conn, &post_form)).await? {
72 let err_type = if e.to_string() == "value too long for type character varying(200)" {
78 return Err(ApiError::err(err_type).into());
82 let inserted_post_id = inserted_post.id;
83 let updated_post = blocking(context.pool(), move |conn| -> Result<Post, LemmyError> {
84 let apub_id = generate_apub_endpoint(EndpointType::Post, &inserted_post_id.to_string())?;
85 Ok(Post::update_ap_id(conn, inserted_post_id, apub_id)?)
88 .map_err(|_| ApiError::err("couldnt_create_post"))?;
90 CreateOrUpdatePost::send(
92 &local_user_view.person,
93 CreateOrUpdateType::Create,
98 // They like their own post by default
99 let person_id = local_user_view.person.id;
100 let post_id = inserted_post.id;
101 let like_form = PostLikeForm {
107 let like = move |conn: &'_ _| PostLike::like(conn, &like_form);
108 if blocking(context.pool(), like).await?.is_err() {
109 return Err(ApiError::err("couldnt_like_post").into());
112 // Mark the post as read
113 mark_post_as_read(person_id, post_id, context.pool()).await?;
116 .send_like(&local_user_view.person, context)
120 let inserted_post_id = inserted_post.id;
121 let post_view = blocking(context.pool(), move |conn| {
122 PostView::read(conn, inserted_post_id, Some(local_user_view.person.id))
125 .map_err(|_| ApiError::err("couldnt_find_post"))?;
127 let res = PostResponse { post_view };
129 context.chat_server().do_send(SendPost {
130 op: UserOperationCrud::CreatePost,