]> Untitled Git - lemmy.git/blob - crates/api_crud/src/post/create.rs
231a891d48e86ebe6481b6552d698ef6d279f004
[lemmy.git] / crates / api_crud / src / post / create.rs
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::{generate_apub_endpoint, ApubLikeableType, ApubObjectType, EndpointType};
5 use lemmy_db_queries::{source::post::Post_, Crud, Likeable};
6 use lemmy_db_schema::source::post::*;
7 use lemmy_db_views::post_view::PostView;
8 use lemmy_utils::{
9   request::fetch_iframely_and_pictrs_data,
10   utils::{check_slurs, check_slurs_opt, is_valid_post_title},
11   ApiError,
12   ConnectionId,
13   LemmyError,
14 };
15 use lemmy_websocket::{messages::SendPost, LemmyContext, UserOperation};
16
17 #[async_trait::async_trait(?Send)]
18 impl PerformCrud for CreatePost {
19   type Response = PostResponse;
20
21   async fn perform(
22     &self,
23     context: &Data<LemmyContext>,
24     websocket_id: Option<ConnectionId>,
25   ) -> Result<PostResponse, LemmyError> {
26     let data: &CreatePost = &self;
27     let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
28
29     check_slurs(&data.name)?;
30     check_slurs_opt(&data.body)?;
31
32     if !is_valid_post_title(&data.name) {
33       return Err(ApiError::err("invalid_post_title").into());
34     }
35
36     check_community_ban(local_user_view.person.id, data.community_id, context.pool()).await?;
37
38     // Fetch Iframely and pictrs cached image
39     let data_url = data.url.as_ref();
40     let (iframely_title, iframely_description, iframely_html, pictrs_thumbnail) =
41       fetch_iframely_and_pictrs_data(context.client(), data_url).await;
42
43     let post_form = PostForm {
44       name: data.name.trim().to_owned(),
45       url: data_url.map(|u| u.to_owned().into()),
46       body: data.body.to_owned(),
47       community_id: data.community_id,
48       creator_id: local_user_view.person.id,
49       removed: None,
50       deleted: None,
51       nsfw: data.nsfw,
52       locked: None,
53       stickied: None,
54       updated: None,
55       embed_title: iframely_title,
56       embed_description: iframely_description,
57       embed_html: iframely_html,
58       thumbnail_url: pictrs_thumbnail.map(|u| u.into()),
59       ap_id: None,
60       local: true,
61       published: None,
62     };
63
64     let inserted_post =
65       match blocking(context.pool(), move |conn| Post::create(conn, &post_form)).await? {
66         Ok(post) => post,
67         Err(e) => {
68           let err_type = if e.to_string() == "value too long for type character varying(200)" {
69             "post_title_too_long"
70           } else {
71             "couldnt_create_post"
72           };
73
74           return Err(ApiError::err(err_type).into());
75         }
76       };
77
78     let inserted_post_id = inserted_post.id;
79     let updated_post = match blocking(context.pool(), move |conn| -> Result<Post, LemmyError> {
80       let apub_id = generate_apub_endpoint(EndpointType::Post, &inserted_post_id.to_string())?;
81       Ok(Post::update_ap_id(conn, inserted_post_id, apub_id)?)
82     })
83     .await?
84     {
85       Ok(post) => post,
86       Err(_e) => return Err(ApiError::err("couldnt_create_post").into()),
87     };
88
89     updated_post
90       .send_create(&local_user_view.person, context)
91       .await?;
92
93     // They like their own post by default
94     let like_form = PostLikeForm {
95       post_id: inserted_post.id,
96       person_id: local_user_view.person.id,
97       score: 1,
98     };
99
100     let like = move |conn: &'_ _| PostLike::like(conn, &like_form);
101     if blocking(context.pool(), like).await?.is_err() {
102       return Err(ApiError::err("couldnt_like_post").into());
103     }
104
105     updated_post
106       .send_like(&local_user_view.person, context)
107       .await?;
108
109     // Refetch the view
110     let inserted_post_id = inserted_post.id;
111     let post_view = match blocking(context.pool(), move |conn| {
112       PostView::read(conn, inserted_post_id, Some(local_user_view.person.id))
113     })
114     .await?
115     {
116       Ok(post) => post,
117       Err(_e) => return Err(ApiError::err("couldnt_find_post").into()),
118     };
119
120     let res = PostResponse { post_view };
121
122     context.chat_server().do_send(SendPost {
123       op: UserOperation::CreatePost,
124       post: res.clone(),
125       websocket_id,
126     });
127
128     Ok(res)
129   }
130 }