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 generate_local_apub_endpoint,
14 objects::post::ApubPost,
15 protocol::activities::{create_or_update::post::CreateOrUpdatePost, CreateOrUpdateType},
18 use lemmy_db_schema::{
21 post::{Post, PostForm, PostLike, PostLikeForm},
23 traits::{Crud, Likeable},
25 use lemmy_db_views_actor::community_view::CommunityView;
27 request::fetch_site_data,
38 use lemmy_websocket::{send::send_post_ws_message, LemmyContext, UserOperationCrud};
39 use tracing::{warn, 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, websocket_id))]
50 context: &Data<LemmyContext>,
51 websocket_id: Option<ConnectionId>,
52 ) -> Result<PostResponse, LemmyError> {
53 let data: &CreatePost = self;
55 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
57 let slur_regex = &context.settings().slur_regex();
58 check_slurs(&data.name, slur_regex)?;
59 check_slurs_opt(&data.body, slur_regex)?;
60 honeypot_check(&data.honeypot)?;
62 if !is_valid_post_title(&data.name) {
63 return Err(LemmyError::from_message("invalid_post_title"));
66 check_community_ban(local_user_view.person.id, data.community_id, context.pool()).await?;
67 check_community_deleted_or_removed(data.community_id, context.pool()).await?;
69 let community_id = data.community_id;
70 let community = blocking(context.pool(), move |conn| {
71 Community::read(conn, community_id)
74 if community.posting_restricted_to_mods {
75 let community_id = data.community_id;
76 let is_mod = blocking(context.pool(), move |conn| {
77 CommunityView::is_mod_or_admin(conn, local_user_view.local_user.person_id, community_id)
81 return Err(LemmyError::from_message("only_mods_can_post_in_community"));
85 // Fetch post links and pictrs cached image
86 let data_url = data.url.as_ref();
87 let (metadata_res, pictrs_thumbnail) =
88 fetch_site_data(context.client(), &context.settings(), data_url).await;
89 let (embed_title, embed_description, embed_html) = metadata_res
90 .map(|u| (u.title, u.description, u.html))
91 .unwrap_or((None, None, None));
93 let post_form = PostForm {
94 name: data.name.trim().to_owned(),
95 url: data_url.map(|u| clean_url_params(u.to_owned()).into()),
96 body: clean_optional_text(&data.body),
97 community_id: data.community_id,
98 creator_id: local_user_view.person.id,
103 thumbnail_url: pictrs_thumbnail.map(|u| u.into()),
104 ..PostForm::default()
108 match blocking(context.pool(), move |conn| Post::create(conn, &post_form)).await? {
111 let err_type = if e.to_string() == "value too long for type character varying(200)" {
112 "post_title_too_long"
114 "couldnt_create_post"
117 return Err(LemmyError::from_error_message(e, err_type));
121 let inserted_post_id = inserted_post.id;
122 let protocol_and_hostname = context.settings().get_protocol_and_hostname();
123 let updated_post = blocking(context.pool(), move |conn| -> Result<Post, LemmyError> {
124 let apub_id = generate_local_apub_endpoint(
126 &inserted_post_id.to_string(),
127 &protocol_and_hostname,
129 Ok(Post::update_ap_id(conn, inserted_post_id, apub_id)?)
132 .map_err(|e| e.with_message("couldnt_create_post"))?;
134 // They like their own post by default
135 let person_id = local_user_view.person.id;
136 let post_id = inserted_post.id;
137 let like_form = PostLikeForm {
143 let like = move |conn: &'_ _| PostLike::like(conn, &like_form);
144 blocking(context.pool(), like)
146 .map_err(|e| LemmyError::from_error_message(e, "couldnt_like_post"))?;
148 // Mark the post as read
149 mark_post_as_read(person_id, post_id, context.pool()).await?;
151 if let Some(url) = &updated_post.url {
153 Webmention::new::<Url>(updated_post.ap_id.clone().into(), url.clone().into())?;
154 webmention.set_checked(true);
157 .instrument(tracing::info_span!("Sending webmention"))
161 Err(WebmentionError::NoEndpointDiscovered(_)) => {}
162 Err(e) => warn!("Failed to send webmention: {}", e),
166 let apub_post: ApubPost = updated_post.into();
167 CreateOrUpdatePost::send(
169 &local_user_view.person.clone().into(),
170 CreateOrUpdateType::Create,
175 send_post_ws_message(
177 UserOperationCrud::CreatePost,
179 Some(local_user_view.person.id),