1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
5 post::{CreatePost, PostResponse},
6 request::fetch_site_data,
9 check_community_deleted_or_removed,
10 generate_local_apub_endpoint,
11 get_local_user_view_from_jwt,
13 local_site_to_slur_regex,
17 websocket::UserOperationCrud,
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;
33 slurs::{check_slurs, check_slurs_opt},
34 validation::{clean_url_params, is_valid_post_title},
38 use tracing::{warn, Instrument};
40 use webmention::{Webmention, WebmentionError};
42 #[async_trait::async_trait(?Send)]
43 impl PerformCrud for CreatePost {
44 type Response = PostResponse;
46 #[tracing::instrument(skip(context, websocket_id))]
49 context: &Data<LemmyContext>,
50 websocket_id: Option<ConnectionId>,
51 ) -> Result<PostResponse, LemmyError> {
52 let data: &CreatePost = self;
54 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
55 let local_site = LocalSite::read(context.pool()).await?;
57 let slur_regex = local_site_to_slur_regex(&local_site);
58 check_slurs(&data.name, &slur_regex)?;
59 check_slurs_opt(&data.body, &slur_regex)?;
60 honeypot_check(&data.honeypot)?;
62 let data_url = data.url.as_ref();
63 let url = data_url.map(clean_url_params).map(Into::into); // TODO no good way to handle a "clear"
65 if !is_valid_post_title(&data.name) {
66 return Err(LemmyError::from_message("invalid_post_title"));
69 check_community_ban(local_user_view.person.id, data.community_id, context.pool()).await?;
70 check_community_deleted_or_removed(data.community_id, context.pool()).await?;
72 let community_id = data.community_id;
73 let community = Community::read(context.pool(), community_id).await?;
74 if community.posting_restricted_to_mods {
75 let community_id = data.community_id;
76 let is_mod = CommunityView::is_mod_or_admin(
78 local_user_view.local_user.person_id,
83 return Err(LemmyError::from_message("only_mods_can_post_in_community"));
87 // Fetch post links and pictrs cached image
88 let (metadata_res, thumbnail_url) =
89 fetch_site_data(context.client(), context.settings(), data_url).await;
90 let (embed_title, embed_description, embed_video_url) = metadata_res
91 .map(|u| (u.title, u.description, u.embed_video_url))
94 let language_id = match data.language_id {
95 Some(lid) => Some(lid),
97 default_post_language(context.pool(), community_id, local_user_view.local_user.id).await?
100 CommunityLanguage::is_allowed_community_language(context.pool(), language_id, community_id)
103 let post_form = PostInsertForm::builder()
104 .name(data.name.trim().to_owned())
106 .body(data.body.clone())
107 .community_id(data.community_id)
108 .creator_id(local_user_view.person.id)
110 .embed_title(embed_title)
111 .embed_description(embed_description)
112 .embed_video_url(embed_video_url)
113 .language_id(language_id)
114 .thumbnail_url(thumbnail_url)
117 let inserted_post = match Post::create(context.pool(), &post_form).await {
120 let err_type = if e.to_string() == "value too long for type character varying(200)" {
121 "post_title_too_long"
123 "couldnt_create_post"
126 return Err(LemmyError::from_error_message(e, err_type));
130 let inserted_post_id = inserted_post.id;
131 let protocol_and_hostname = context.settings().get_protocol_and_hostname();
132 let apub_id = generate_local_apub_endpoint(
134 &inserted_post_id.to_string(),
135 &protocol_and_hostname,
137 let updated_post = Post::update(
140 &PostUpdateForm::builder().ap_id(Some(apub_id)).build(),
143 .map_err(|e| LemmyError::from_error_message(e, "couldnt_create_post"))?;
145 // They like their own post by default
146 let person_id = local_user_view.person.id;
147 let post_id = inserted_post.id;
148 let like_form = PostLikeForm {
154 PostLike::like(context.pool(), &like_form)
156 .map_err(|e| LemmyError::from_error_message(e, "couldnt_like_post"))?;
158 // Mark the post as read
159 mark_post_as_read(person_id, post_id, context.pool()).await?;
161 if let Some(url) = &updated_post.url {
163 Webmention::new::<Url>(updated_post.ap_id.clone().into(), url.clone().into())?;
164 webmention.set_checked(true);
167 .instrument(tracing::info_span!("Sending webmention"))
171 Err(WebmentionError::NoEndpointDiscovered(_)) => {}
172 Err(e) => warn!("Failed to send webmention: {}", e),
177 .send_post_ws_message(
178 &UserOperationCrud::CreatePost,
181 Some(local_user_view.person.id),