2023-08-02 16:52:41 +00:00
|
|
|
use activitypub_federation::config::Data;
|
|
|
|
use actix_web::web::Json;
|
2021-03-25 19:19:40 +00:00
|
|
|
use lemmy_api_common::{
|
2022-11-28 14:29:33 +00:00
|
|
|
context::LemmyContext,
|
2022-09-19 22:58:42 +00:00
|
|
|
private_message::{CreatePrivateMessage, PrivateMessageResponse},
|
2023-08-02 16:52:41 +00:00
|
|
|
send_activity::{ActivityChannel, SendActivityData},
|
2022-05-03 17:44:13 +00:00
|
|
|
utils::{
|
|
|
|
check_person_block,
|
2022-11-28 14:29:33 +00:00
|
|
|
generate_local_apub_endpoint,
|
2022-08-18 19:11:19 +00:00
|
|
|
get_interface_language,
|
2022-10-27 09:24:07 +00:00
|
|
|
local_site_to_slur_regex,
|
2022-05-03 17:44:13 +00:00
|
|
|
send_email_to_user,
|
2022-11-28 14:29:33 +00:00
|
|
|
EndpointType,
|
2022-05-03 17:44:13 +00:00
|
|
|
},
|
2021-07-31 20:58:11 +00:00
|
|
|
};
|
2021-10-16 13:33:38 +00:00
|
|
|
use lemmy_db_schema::{
|
2022-10-27 09:24:07 +00:00
|
|
|
source::{
|
|
|
|
local_site::LocalSite,
|
|
|
|
private_message::{PrivateMessage, PrivateMessageInsertForm, PrivateMessageUpdateForm},
|
|
|
|
},
|
2021-10-16 13:33:38 +00:00
|
|
|
traits::Crud,
|
|
|
|
};
|
2023-06-06 16:27:22 +00:00
|
|
|
use lemmy_db_views::structs::{LocalUserView, PrivateMessageView};
|
2023-04-15 14:45:11 +00:00
|
|
|
use lemmy_utils::{
|
2023-07-10 14:50:07 +00:00
|
|
|
error::{LemmyError, LemmyErrorExt, LemmyErrorType},
|
2023-10-11 14:48:19 +00:00
|
|
|
utils::{markdown::markdown_to_html, slurs::remove_slurs, validation::is_valid_body_field},
|
2023-04-15 14:45:11 +00:00
|
|
|
};
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2023-08-02 16:52:41 +00:00
|
|
|
#[tracing::instrument(skip(context))]
|
|
|
|
pub async fn create_private_message(
|
|
|
|
data: Json<CreatePrivateMessage>,
|
|
|
|
context: Data<LemmyContext>,
|
2023-09-21 10:42:28 +00:00
|
|
|
local_user_view: LocalUserView,
|
2023-08-02 16:52:41 +00:00
|
|
|
) -> Result<Json<PrivateMessageResponse>, LemmyError> {
|
|
|
|
let local_site = LocalSite::read(&mut context.pool()).await?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2023-10-11 14:48:19 +00:00
|
|
|
let content = remove_slurs(&data.content, &local_site_to_slur_regex(&local_site));
|
2023-08-02 16:52:41 +00:00
|
|
|
is_valid_body_field(&Some(content.clone()), false)?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2023-08-02 16:52:41 +00:00
|
|
|
check_person_block(
|
|
|
|
local_user_view.person.id,
|
|
|
|
data.recipient_id,
|
|
|
|
&mut context.pool(),
|
|
|
|
)
|
|
|
|
.await?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2023-08-02 16:52:41 +00:00
|
|
|
let private_message_form = PrivateMessageInsertForm::builder()
|
|
|
|
.content(content.clone())
|
|
|
|
.creator_id(local_user_view.person.id)
|
|
|
|
.recipient_id(data.recipient_id)
|
|
|
|
.build();
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2023-08-02 16:52:41 +00:00
|
|
|
let inserted_private_message = PrivateMessage::create(&mut context.pool(), &private_message_form)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2023-07-10 14:50:07 +00:00
|
|
|
.with_lemmy_type(LemmyErrorType::CouldntCreatePrivateMessage)?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2023-08-02 16:52:41 +00:00
|
|
|
let inserted_private_message_id = inserted_private_message.id;
|
|
|
|
let protocol_and_hostname = context.settings().get_protocol_and_hostname();
|
|
|
|
let apub_id = generate_local_apub_endpoint(
|
|
|
|
EndpointType::PrivateMessage,
|
|
|
|
&inserted_private_message_id.to_string(),
|
|
|
|
&protocol_and_hostname,
|
|
|
|
)?;
|
|
|
|
PrivateMessage::update(
|
|
|
|
&mut context.pool(),
|
|
|
|
inserted_private_message.id,
|
2023-08-08 09:41:41 +00:00
|
|
|
&PrivateMessageUpdateForm {
|
|
|
|
ap_id: Some(apub_id),
|
|
|
|
..Default::default()
|
|
|
|
},
|
2023-08-02 16:52:41 +00:00
|
|
|
)
|
|
|
|
.await
|
|
|
|
.with_lemmy_type(LemmyErrorType::CouldntCreatePrivateMessage)?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2023-08-02 16:52:41 +00:00
|
|
|
let view = PrivateMessageView::read(&mut context.pool(), inserted_private_message.id).await?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2023-08-02 16:52:41 +00:00
|
|
|
// Send email to the local recipient, if one exists
|
|
|
|
if view.recipient.local {
|
|
|
|
let recipient_id = data.recipient_id;
|
|
|
|
let local_recipient = LocalUserView::read_person(&mut context.pool(), recipient_id).await?;
|
|
|
|
let lang = get_interface_language(&local_recipient);
|
|
|
|
let inbox_link = format!("{}/inbox", context.settings().get_protocol_and_hostname());
|
|
|
|
let sender_name = &local_user_view.person.name;
|
2023-10-11 14:48:19 +00:00
|
|
|
let content = markdown_to_html(&content);
|
2023-08-02 16:52:41 +00:00
|
|
|
send_email_to_user(
|
|
|
|
&local_recipient,
|
|
|
|
&lang.notification_private_message_subject(sender_name),
|
|
|
|
&lang.notification_private_message_body(inbox_link, &content, sender_name),
|
|
|
|
context.settings(),
|
|
|
|
)
|
|
|
|
.await;
|
2021-03-25 19:19:40 +00:00
|
|
|
}
|
2023-08-02 16:52:41 +00:00
|
|
|
|
|
|
|
ActivityChannel::submit_activity(
|
|
|
|
SendActivityData::CreatePrivateMessage(view.clone()),
|
|
|
|
&context,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(Json(PrivateMessageResponse {
|
|
|
|
private_message_view: view,
|
|
|
|
}))
|
2021-03-25 19:19:40 +00:00
|
|
|
}
|