1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 build_response::build_post_response,
6 post::{CreatePost, PostResponse},
7 request::fetch_site_data,
10 check_community_deleted_or_removed,
11 generate_local_apub_endpoint,
13 local_site_to_slur_regex,
14 local_user_view_from_jwt,
19 use lemmy_db_schema::{
20 impls::actor_language::default_post_language,
22 actor_language::CommunityLanguage,
24 local_site::LocalSite,
25 post::{Post, PostInsertForm, PostLike, PostLikeForm, PostUpdateForm},
27 traits::{Crud, Likeable},
29 use lemmy_db_views_actor::structs::CommunityView;
31 error::{LemmyError, LemmyErrorExt, LemmyErrorType},
34 slurs::{check_slurs, check_slurs_opt},
35 validation::{check_url_scheme, clean_url_params, is_valid_body_field, is_valid_post_title},
37 SYNCHRONOUS_FEDERATION,
39 use tracing::Instrument;
41 use webmention::{Webmention, WebmentionError};
43 #[async_trait::async_trait(?Send)]
44 impl PerformCrud for CreatePost {
45 type Response = PostResponse;
47 #[tracing::instrument(skip(context))]
48 async fn perform(&self, context: &Data<LemmyContext>) -> Result<PostResponse, LemmyError> {
49 let data: &CreatePost = self;
50 let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
51 let local_site = LocalSite::read(&mut context.pool()).await?;
53 let slur_regex = local_site_to_slur_regex(&local_site);
54 check_slurs(&data.name, &slur_regex)?;
55 check_slurs_opt(&data.body, &slur_regex)?;
56 honeypot_check(&data.honeypot)?;
58 let data_url = data.url.as_ref();
59 let url = data_url.map(clean_url_params).map(Into::into); // TODO no good way to handle a "clear"
61 is_valid_post_title(&data.name)?;
62 is_valid_body_field(&data.body, true)?;
63 check_url_scheme(&data.url)?;
66 local_user_view.person.id,
71 check_community_deleted_or_removed(data.community_id, &mut context.pool()).await?;
73 let community_id = data.community_id;
74 let community = Community::read(&mut context.pool(), community_id).await?;
75 if community.posting_restricted_to_mods {
76 let community_id = data.community_id;
77 let is_mod = CommunityView::is_mod_or_admin(
79 local_user_view.local_user.person_id,
84 return Err(LemmyErrorType::OnlyModsCanPostInCommunity)?;
88 // Fetch post links and pictrs cached image
89 let (metadata_res, thumbnail_url) =
90 fetch_site_data(context.client(), context.settings(), data_url, true).await;
91 let (embed_title, embed_description, embed_video_url) = metadata_res
92 .map(|u| (u.title, u.description, u.embed_video_url))
95 let language_id = match data.language_id {
96 Some(lid) => Some(lid),
98 default_post_language(
101 local_user_view.local_user.id,
106 CommunityLanguage::is_allowed_community_language(
113 let post_form = PostInsertForm::builder()
114 .name(data.name.trim().to_owned())
116 .body(data.body.clone())
117 .community_id(data.community_id)
118 .creator_id(local_user_view.person.id)
120 .embed_title(embed_title)
121 .embed_description(embed_description)
122 .embed_video_url(embed_video_url)
123 .language_id(language_id)
124 .thumbnail_url(thumbnail_url)
127 let inserted_post = Post::create(&mut context.pool(), &post_form)
129 .with_lemmy_type(LemmyErrorType::CouldntCreatePost)?;
131 let inserted_post_id = inserted_post.id;
132 let protocol_and_hostname = context.settings().get_protocol_and_hostname();
133 let apub_id = generate_local_apub_endpoint(
135 &inserted_post_id.to_string(),
136 &protocol_and_hostname,
138 let updated_post = Post::update(
141 &PostUpdateForm::builder().ap_id(Some(apub_id)).build(),
144 .with_lemmy_type(LemmyErrorType::CouldntCreatePost)?;
146 // They like their own post by default
147 let person_id = local_user_view.person.id;
148 let post_id = inserted_post.id;
149 let like_form = PostLikeForm {
155 PostLike::like(&mut context.pool(), &like_form)
157 .with_lemmy_type(LemmyErrorType::CouldntLikePost)?;
159 // Mark the post as read
160 mark_post_as_read(person_id, post_id, &mut context.pool()).await?;
162 if let Some(url) = updated_post.url.clone() {
163 let task = async move {
165 Webmention::new::<Url>(updated_post.ap_id.clone().into(), url.clone().into())?;
166 webmention.set_checked(true);
169 .instrument(tracing::info_span!("Sending webmention"))
172 Err(WebmentionError::NoEndpointDiscovered(_)) => Ok(()),
174 Err(e) => Err(e).with_lemmy_type(LemmyErrorType::CouldntSendWebmention),
177 if *SYNCHRONOUS_FEDERATION {
180 spawn_try_task(task);
184 build_post_response(context, community_id, person_id, post_id).await