1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
6 get_local_user_view_from_jwt,
13 post::create_or_update::CreateOrUpdatePost,
14 voting::vote::{Vote, VoteType},
17 fetcher::post_or_comment::PostOrComment,
18 generate_apub_endpoint,
21 use lemmy_db_queries::{source::post::Post_, Crud, Likeable};
22 use lemmy_db_schema::source::post::*;
24 request::fetch_site_data,
25 utils::{check_slurs, check_slurs_opt, clean_url_params, is_valid_post_title},
30 use lemmy_websocket::{send::send_post_ws_message, LemmyContext, UserOperationCrud};
32 use webmention::{Webmention, WebmentionError};
34 #[async_trait::async_trait(?Send)]
35 impl PerformCrud for CreatePost {
36 type Response = PostResponse;
40 context: &Data<LemmyContext>,
41 websocket_id: Option<ConnectionId>,
42 ) -> Result<PostResponse, LemmyError> {
43 let data: &CreatePost = self;
45 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
47 let slur_regex = &context.settings().slur_regex();
48 check_slurs(&data.name, slur_regex)?;
49 check_slurs_opt(&data.body, slur_regex)?;
50 honeypot_check(&data.honeypot)?;
52 if !is_valid_post_title(&data.name) {
53 return Err(ApiError::err_plain("invalid_post_title").into());
56 check_community_ban(local_user_view.person.id, data.community_id, context.pool()).await?;
58 // Fetch post links and pictrs cached image
59 let data_url = data.url.as_ref();
60 let (metadata_res, pictrs_thumbnail) =
61 fetch_site_data(context.client(), &context.settings(), data_url).await;
62 let (embed_title, embed_description, embed_html) = metadata_res
63 .map(|u| (u.title, u.description, u.html))
64 .unwrap_or((None, None, None));
66 let post_form = PostForm {
67 name: data.name.trim().to_owned(),
68 url: data_url.map(|u| clean_url_params(u.to_owned()).into()),
69 body: data.body.to_owned(),
70 community_id: data.community_id,
71 creator_id: local_user_view.person.id,
76 thumbnail_url: pictrs_thumbnail.map(|u| u.into()),
81 match blocking(context.pool(), move |conn| Post::create(conn, &post_form)).await? {
84 let err_type = if e.to_string() == "value too long for type character varying(200)" {
90 return Err(ApiError::err(err_type, e).into());
94 let inserted_post_id = inserted_post.id;
95 let protocol_and_hostname = context.settings().get_protocol_and_hostname();
96 let updated_post = blocking(context.pool(), move |conn| -> Result<Post, LemmyError> {
97 let apub_id = generate_apub_endpoint(
99 &inserted_post_id.to_string(),
100 &protocol_and_hostname,
102 Ok(Post::update_ap_id(conn, inserted_post_id, apub_id)?)
105 .map_err(|e| ApiError::err("couldnt_create_post", e))?;
107 CreateOrUpdatePost::send(
109 &local_user_view.person,
110 CreateOrUpdateType::Create,
115 // They like their own post by default
116 let person_id = local_user_view.person.id;
117 let post_id = inserted_post.id;
118 let like_form = PostLikeForm {
124 let like = move |conn: &'_ _| PostLike::like(conn, &like_form);
125 if blocking(context.pool(), like).await?.is_err() {
126 return Err(ApiError::err_plain("couldnt_like_post").into());
129 // Mark the post as read
130 mark_post_as_read(person_id, post_id, context.pool()).await?;
132 if let Some(url) = &updated_post.url {
133 let mut webmention = Webmention::new(
134 updated_post.ap_id.clone().into_inner(),
135 url.clone().into_inner(),
137 webmention.set_checked(true);
138 match webmention.send().await {
140 Err(WebmentionError::NoEndpointDiscovered(_)) => {}
141 Err(e) => warn!("Failed to send webmention: {}", e),
145 let object = PostOrComment::Post(Box::new(updated_post));
148 &local_user_view.person,
149 inserted_post.community_id,
155 send_post_ws_message(
157 UserOperationCrud::CreatePost,
159 Some(local_user_view.person.id),