1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
6 get_local_user_view_from_jwt,
12 post::create_or_update::CreateOrUpdatePost,
13 voting::vote::{Vote, VoteType},
16 generate_apub_endpoint,
20 use lemmy_db_queries::{source::post::Post_, Crud, Likeable};
21 use lemmy_db_schema::source::post::*;
22 use lemmy_db_views::post_view::PostView;
24 request::fetch_iframely_and_pictrs_data,
25 utils::{check_slurs, check_slurs_opt, clean_url_params, is_valid_post_title},
30 use lemmy_websocket::{messages::SendPost, LemmyContext, UserOperationCrud};
32 #[async_trait::async_trait(?Send)]
33 impl PerformCrud for CreatePost {
34 type Response = PostResponse;
38 context: &Data<LemmyContext>,
39 websocket_id: Option<ConnectionId>,
40 ) -> Result<PostResponse, LemmyError> {
41 let data: &CreatePost = self;
42 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
44 check_slurs(&data.name)?;
45 check_slurs_opt(&data.body)?;
47 if !is_valid_post_title(&data.name) {
48 return Err(ApiError::err("invalid_post_title").into());
51 check_community_ban(local_user_view.person.id, data.community_id, context.pool()).await?;
53 // Fetch Iframely and pictrs cached image
54 let data_url = data.url.as_ref();
55 let (iframely_title, iframely_description, iframely_html, pictrs_thumbnail) =
56 fetch_iframely_and_pictrs_data(context.client(), data_url).await;
58 let post_form = PostForm {
59 name: data.name.trim().to_owned(),
60 url: data_url.map(|u| clean_url_params(u.to_owned()).into()),
61 body: data.body.to_owned(),
62 community_id: data.community_id,
63 creator_id: local_user_view.person.id,
65 embed_title: iframely_title,
66 embed_description: iframely_description,
67 embed_html: iframely_html,
68 thumbnail_url: pictrs_thumbnail.map(|u| u.into()),
73 match blocking(context.pool(), move |conn| Post::create(conn, &post_form)).await? {
76 let err_type = if e.to_string() == "value too long for type character varying(200)" {
82 return Err(ApiError::err(err_type).into());
86 let inserted_post_id = inserted_post.id;
87 let updated_post = blocking(context.pool(), move |conn| -> Result<Post, LemmyError> {
88 let apub_id = generate_apub_endpoint(EndpointType::Post, &inserted_post_id.to_string())?;
89 Ok(Post::update_ap_id(conn, inserted_post_id, apub_id)?)
92 .map_err(|_| ApiError::err("couldnt_create_post"))?;
94 CreateOrUpdatePost::send(
96 &local_user_view.person,
97 CreateOrUpdateType::Create,
102 // They like their own post by default
103 let person_id = local_user_view.person.id;
104 let post_id = inserted_post.id;
105 let like_form = PostLikeForm {
111 let like = move |conn: &'_ _| PostLike::like(conn, &like_form);
112 if blocking(context.pool(), like).await?.is_err() {
113 return Err(ApiError::err("couldnt_like_post").into());
116 // Mark the post as read
117 mark_post_as_read(person_id, post_id, context.pool()).await?;
119 let object = PostOrComment::Post(Box::new(updated_post));
122 &local_user_view.person,
123 inserted_post.community_id,
130 let inserted_post_id = inserted_post.id;
131 let post_view = blocking(context.pool(), move |conn| {
132 PostView::read(conn, inserted_post_id, Some(local_user_view.person.id))
135 .map_err(|_| ApiError::err("couldnt_find_post"))?;
137 let res = PostResponse { post_view };
139 context.chat_server().do_send(SendPost {
140 op: UserOperationCrud::CreatePost,