2023-07-19 13:49:41 +00:00
|
|
|
use activitypub_federation::config::Data;
|
|
|
|
use actix_web::web::Json;
|
2021-04-24 22:26:50 +00:00
|
|
|
use lemmy_api_common::{
|
2023-06-06 16:27:22 +00:00
|
|
|
build_response::build_post_response,
|
2022-11-28 14:29:33 +00:00
|
|
|
context::LemmyContext,
|
2022-05-03 17:44:13 +00:00
|
|
|
post::{CreatePost, PostResponse},
|
2024-01-25 14:22:11 +00:00
|
|
|
request::fetch_link_metadata_opt,
|
2023-07-19 13:49:41 +00:00
|
|
|
send_activity::{ActivityChannel, SendActivityData},
|
2022-05-03 17:44:13 +00:00
|
|
|
utils::{
|
2023-10-13 13:48:18 +00:00
|
|
|
check_community_user_action,
|
2022-11-28 14:29:33 +00:00
|
|
|
generate_local_apub_endpoint,
|
2022-05-03 17:44:13 +00:00
|
|
|
honeypot_check,
|
2022-10-27 09:24:07 +00:00
|
|
|
local_site_to_slur_regex,
|
2022-05-03 17:44:13 +00:00
|
|
|
mark_post_as_read,
|
2024-01-25 14:22:11 +00:00
|
|
|
process_markdown_opt,
|
|
|
|
proxy_image_link_opt_apub,
|
2022-11-28 14:29:33 +00:00
|
|
|
EndpointType,
|
2022-05-03 17:44:13 +00:00
|
|
|
},
|
2021-07-30 14:35:32 +00:00
|
|
|
};
|
2021-10-16 13:33:38 +00:00
|
|
|
use lemmy_db_schema::{
|
2022-10-06 18:27:58 +00:00
|
|
|
impls::actor_language::default_post_language,
|
2022-04-28 20:32:32 +00:00
|
|
|
source::{
|
2022-10-06 18:27:58 +00:00
|
|
|
actor_language::CommunityLanguage,
|
2022-04-28 20:32:32 +00:00
|
|
|
community::Community,
|
2022-10-27 09:24:07 +00:00
|
|
|
local_site::LocalSite,
|
|
|
|
post::{Post, PostInsertForm, PostLike, PostLikeForm, PostUpdateForm},
|
2022-04-28 20:32:32 +00:00
|
|
|
},
|
2021-10-16 13:33:38 +00:00
|
|
|
traits::{Crud, Likeable},
|
|
|
|
};
|
2023-09-21 10:42:28 +00:00
|
|
|
use lemmy_db_views::structs::LocalUserView;
|
2022-05-03 17:44:13 +00:00
|
|
|
use lemmy_db_views_actor::structs::CommunityView;
|
2021-03-25 19:19:40 +00:00
|
|
|
use lemmy_utils::{
|
2023-07-10 14:50:07 +00:00
|
|
|
error::{LemmyError, LemmyErrorExt, LemmyErrorType},
|
2023-07-10 10:27:49 +00:00
|
|
|
spawn_try_task,
|
2023-02-16 04:05:14 +00:00
|
|
|
utils::{
|
2024-01-25 14:22:11 +00:00
|
|
|
slurs::check_slurs,
|
2023-07-06 12:29:51 +00:00
|
|
|
validation::{check_url_scheme, clean_url_params, is_valid_body_field, is_valid_post_title},
|
2023-02-16 04:05:14 +00:00
|
|
|
},
|
2021-03-25 19:19:40 +00:00
|
|
|
};
|
2023-07-10 10:27:49 +00:00
|
|
|
use tracing::Instrument;
|
2021-11-05 00:24:10 +00:00
|
|
|
use url::Url;
|
|
|
|
use webmention::{Webmention, WebmentionError};
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2023-07-19 13:49:41 +00:00
|
|
|
#[tracing::instrument(skip(context))]
|
|
|
|
pub async fn create_post(
|
|
|
|
data: Json<CreatePost>,
|
|
|
|
context: Data<LemmyContext>,
|
2023-09-21 10:42:28 +00:00
|
|
|
local_user_view: LocalUserView,
|
2023-07-19 13:49:41 +00:00
|
|
|
) -> Result<Json<PostResponse>, LemmyError> {
|
|
|
|
let local_site = LocalSite::read(&mut context.pool()).await?;
|
|
|
|
|
|
|
|
let slur_regex = local_site_to_slur_regex(&local_site);
|
|
|
|
check_slurs(&data.name, &slur_regex)?;
|
2024-01-25 14:22:11 +00:00
|
|
|
let body = process_markdown_opt(&data.body, &slur_regex, &context).await?;
|
2023-07-19 13:49:41 +00:00
|
|
|
honeypot_check(&data.honeypot)?;
|
|
|
|
|
|
|
|
let data_url = data.url.as_ref();
|
2024-01-25 14:22:11 +00:00
|
|
|
let url = data_url.map(clean_url_params); // TODO no good way to handle a "clear"
|
2023-07-19 13:49:41 +00:00
|
|
|
|
|
|
|
is_valid_post_title(&data.name)?;
|
2024-01-25 14:22:11 +00:00
|
|
|
is_valid_body_field(&body, true)?;
|
2023-07-19 13:49:41 +00:00
|
|
|
check_url_scheme(&data.url)?;
|
|
|
|
|
2023-10-13 13:48:18 +00:00
|
|
|
check_community_user_action(
|
|
|
|
&local_user_view.person,
|
2023-07-19 13:49:41 +00:00
|
|
|
data.community_id,
|
|
|
|
&mut context.pool(),
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
let community_id = data.community_id;
|
|
|
|
let community = Community::read(&mut context.pool(), community_id).await?;
|
|
|
|
if community.posting_restricted_to_mods {
|
|
|
|
let community_id = data.community_id;
|
|
|
|
let is_mod = CommunityView::is_mod_or_admin(
|
2023-07-11 13:09:59 +00:00
|
|
|
&mut context.pool(),
|
2023-07-19 13:49:41 +00:00
|
|
|
local_user_view.local_user.person_id,
|
|
|
|
community_id,
|
2023-07-11 13:09:59 +00:00
|
|
|
)
|
|
|
|
.await?;
|
2023-07-19 13:49:41 +00:00
|
|
|
if !is_mod {
|
2023-08-31 13:01:08 +00:00
|
|
|
Err(LemmyErrorType::OnlyModsCanPostInCommunity)?
|
2023-07-19 13:49:41 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2023-07-19 13:49:41 +00:00
|
|
|
// Fetch post links and pictrs cached image
|
2024-01-25 14:22:11 +00:00
|
|
|
let metadata = fetch_link_metadata_opt(url.as_ref(), true, &context).await;
|
|
|
|
let url = proxy_image_link_opt_apub(url, &context).await?;
|
2023-07-19 13:49:41 +00:00
|
|
|
|
2023-07-21 13:08:25 +00:00
|
|
|
// Only need to check if language is allowed in case user set it explicitly. When using default
|
|
|
|
// language, it already only returns allowed languages.
|
|
|
|
CommunityLanguage::is_allowed_community_language(
|
|
|
|
&mut context.pool(),
|
|
|
|
data.language_id,
|
|
|
|
community_id,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
// attempt to set default language if none was provided
|
2023-07-19 13:49:41 +00:00
|
|
|
let language_id = match data.language_id {
|
|
|
|
Some(lid) => Some(lid),
|
|
|
|
None => {
|
|
|
|
default_post_language(
|
2023-07-11 13:09:59 +00:00
|
|
|
&mut context.pool(),
|
2022-11-09 10:05:00 +00:00
|
|
|
community_id,
|
2023-07-19 13:49:41 +00:00
|
|
|
local_user_view.local_user.id,
|
2022-11-09 10:05:00 +00:00
|
|
|
)
|
2023-07-19 13:49:41 +00:00
|
|
|
.await?
|
2022-04-28 20:32:32 +00:00
|
|
|
}
|
2023-07-19 13:49:41 +00:00
|
|
|
};
|
2022-08-18 19:11:19 +00:00
|
|
|
|
2023-07-19 13:49:41 +00:00
|
|
|
let post_form = PostInsertForm::builder()
|
2023-10-11 14:48:19 +00:00
|
|
|
.name(data.name.trim().to_string())
|
2023-07-19 13:49:41 +00:00
|
|
|
.url(url)
|
2024-01-25 14:22:11 +00:00
|
|
|
.body(body)
|
2023-07-19 13:49:41 +00:00
|
|
|
.community_id(data.community_id)
|
|
|
|
.creator_id(local_user_view.person.id)
|
|
|
|
.nsfw(data.nsfw)
|
2024-01-25 14:22:11 +00:00
|
|
|
.embed_title(metadata.opengraph_data.title)
|
|
|
|
.embed_description(metadata.opengraph_data.description)
|
|
|
|
.embed_video_url(metadata.opengraph_data.embed_video_url)
|
2023-07-19 13:49:41 +00:00
|
|
|
.language_id(language_id)
|
2024-01-25 14:22:11 +00:00
|
|
|
.thumbnail_url(metadata.thumbnail)
|
2023-07-19 13:49:41 +00:00
|
|
|
.build();
|
|
|
|
|
|
|
|
let inserted_post = Post::create(&mut context.pool(), &post_form)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2023-07-10 14:50:07 +00:00
|
|
|
.with_lemmy_type(LemmyErrorType::CouldntCreatePost)?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2023-07-19 13:49:41 +00:00
|
|
|
let inserted_post_id = inserted_post.id;
|
|
|
|
let protocol_and_hostname = context.settings().get_protocol_and_hostname();
|
|
|
|
let apub_id = generate_local_apub_endpoint(
|
|
|
|
EndpointType::Post,
|
|
|
|
&inserted_post_id.to_string(),
|
|
|
|
&protocol_and_hostname,
|
|
|
|
)?;
|
|
|
|
let updated_post = Post::update(
|
|
|
|
&mut context.pool(),
|
|
|
|
inserted_post_id,
|
2023-08-08 09:41:41 +00:00
|
|
|
&PostUpdateForm {
|
|
|
|
ap_id: Some(apub_id),
|
|
|
|
..Default::default()
|
|
|
|
},
|
2023-07-19 13:49:41 +00:00
|
|
|
)
|
|
|
|
.await
|
|
|
|
.with_lemmy_type(LemmyErrorType::CouldntCreatePost)?;
|
|
|
|
|
|
|
|
// They like their own post by default
|
|
|
|
let person_id = local_user_view.person.id;
|
|
|
|
let post_id = inserted_post.id;
|
|
|
|
let like_form = PostLikeForm {
|
|
|
|
post_id,
|
|
|
|
person_id,
|
|
|
|
score: 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
PostLike::like(&mut context.pool(), &like_form)
|
|
|
|
.await
|
|
|
|
.with_lemmy_type(LemmyErrorType::CouldntLikePost)?;
|
|
|
|
|
|
|
|
ActivityChannel::submit_activity(SendActivityData::CreatePost(updated_post.clone()), &context)
|
|
|
|
.await?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2023-07-19 13:49:41 +00:00
|
|
|
// Mark the post as read
|
|
|
|
mark_post_as_read(person_id, post_id, &mut context.pool()).await?;
|
|
|
|
|
|
|
|
if let Some(url) = updated_post.url.clone() {
|
2023-09-20 14:18:31 +00:00
|
|
|
spawn_try_task(async move {
|
2023-07-19 13:49:41 +00:00
|
|
|
let mut webmention =
|
|
|
|
Webmention::new::<Url>(updated_post.ap_id.clone().into(), url.clone().into())?;
|
|
|
|
webmention.set_checked(true);
|
|
|
|
match webmention
|
|
|
|
.send()
|
|
|
|
.instrument(tracing::info_span!("Sending webmention"))
|
|
|
|
.await
|
|
|
|
{
|
|
|
|
Err(WebmentionError::NoEndpointDiscovered(_)) => Ok(()),
|
|
|
|
Ok(_) => Ok(()),
|
|
|
|
Err(e) => Err(e).with_lemmy_type(LemmyErrorType::CouldntSendWebmention),
|
2021-09-27 14:49:47 +00:00
|
|
|
}
|
2023-09-20 14:18:31 +00:00
|
|
|
});
|
2023-07-19 13:49:41 +00:00
|
|
|
};
|
2021-09-27 14:49:47 +00:00
|
|
|
|
2023-10-13 13:48:18 +00:00
|
|
|
build_post_response(&context, community_id, &local_user_view.person, post_id).await
|
2021-03-25 19:19:40 +00:00
|
|
|
}
|