1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
6 check_community_deleted_or_removed,
7 get_local_user_view_from_jwt,
13 fetcher::post_or_comment::PostOrComment,
14 generate_local_apub_endpoint,
15 objects::post::ApubPost,
16 protocol::activities::{
17 create_or_update::post::CreateOrUpdatePost,
18 voting::vote::{Vote, VoteType},
23 use lemmy_db_schema::{
24 source::post::{Post, PostForm, PostLike, PostLikeForm},
25 traits::{Crud, Likeable},
28 request::fetch_site_data,
39 use lemmy_websocket::{send::send_post_ws_message, LemmyContext, UserOperationCrud};
40 use tracing::{warn, Instrument};
42 use webmention::{Webmention, WebmentionError};
44 #[async_trait::async_trait(?Send)]
45 impl PerformCrud for CreatePost {
46 type Response = PostResponse;
48 #[tracing::instrument(skip(context, websocket_id))]
51 context: &Data<LemmyContext>,
52 websocket_id: Option<ConnectionId>,
53 ) -> Result<PostResponse, LemmyError> {
54 let data: &CreatePost = self;
56 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
58 let slur_regex = &context.settings().slur_regex();
59 check_slurs(&data.name, slur_regex)?;
60 check_slurs_opt(&data.body, slur_regex)?;
61 honeypot_check(&data.honeypot)?;
63 if !is_valid_post_title(&data.name) {
64 return Err(LemmyError::from_message("invalid_post_title"));
67 check_community_ban(local_user_view.person.id, data.community_id, context.pool()).await?;
68 check_community_deleted_or_removed(data.community_id, context.pool()).await?;
70 // Fetch post links and pictrs cached image
71 let data_url = data.url.as_ref();
72 let (metadata_res, pictrs_thumbnail) =
73 fetch_site_data(context.client(), &context.settings(), data_url).await;
74 let (embed_title, embed_description, embed_html) = metadata_res
75 .map(|u| (u.title, u.description, u.html))
76 .unwrap_or((None, None, None));
78 let post_form = PostForm {
79 name: data.name.trim().to_owned(),
80 url: data_url.map(|u| clean_url_params(u.to_owned()).into()),
81 body: clean_optional_text(&data.body),
82 community_id: data.community_id,
83 creator_id: local_user_view.person.id,
88 thumbnail_url: pictrs_thumbnail.map(|u| u.into()),
93 match blocking(context.pool(), move |conn| Post::create(conn, &post_form)).await? {
96 let err_type = if e.to_string() == "value too long for type character varying(200)" {
102 return Err(LemmyError::from(e).with_message(err_type));
106 let inserted_post_id = inserted_post.id;
107 let protocol_and_hostname = context.settings().get_protocol_and_hostname();
108 let updated_post = blocking(context.pool(), move |conn| -> Result<Post, LemmyError> {
109 let apub_id = generate_local_apub_endpoint(
111 &inserted_post_id.to_string(),
112 &protocol_and_hostname,
114 Ok(Post::update_ap_id(conn, inserted_post_id, apub_id)?)
117 .map_err(LemmyError::from)
118 .map_err(|e| e.with_message("couldnt_create_post"))?;
120 // They like their own post by default
121 let person_id = local_user_view.person.id;
122 let post_id = inserted_post.id;
123 let like_form = PostLikeForm {
129 let like = move |conn: &'_ _| PostLike::like(conn, &like_form);
130 if blocking(context.pool(), like).await?.is_err() {
131 return Err(LemmyError::from_message("couldnt_like_post"));
134 // Mark the post as read
135 mark_post_as_read(person_id, post_id, context.pool()).await?;
137 if let Some(url) = &updated_post.url {
139 Webmention::new::<Url>(updated_post.ap_id.clone().into(), url.clone().into())?;
140 webmention.set_checked(true);
143 .instrument(tracing::info_span!("Sending webmention"))
147 Err(WebmentionError::NoEndpointDiscovered(_)) => {}
148 Err(e) => warn!("Failed to send webmention: {}", e),
152 let apub_post: ApubPost = updated_post.into();
153 CreateOrUpdatePost::send(
155 &local_user_view.person.clone().into(),
156 CreateOrUpdateType::Create,
160 let object = PostOrComment::Post(Box::new(apub_post));
163 &local_user_view.person.clone().into(),
164 inserted_post.community_id,
170 send_post_ws_message(
172 UserOperationCrud::CreatePost,
174 Some(local_user_view.person.id),