1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 generate_local_apub_endpoint,
5 private_message::{CreatePrivateMessage, PrivateMessageResponse},
8 get_interface_language,
9 get_local_user_view_from_jwt,
10 local_site_to_slur_regex,
13 websocket::{send::send_pm_ws_message, UserOperationCrud},
17 use lemmy_apub::protocol::activities::{
18 create_or_update::chat_message::CreateOrUpdateChatMessage,
21 use lemmy_db_schema::{
23 local_site::LocalSite,
24 private_message::{PrivateMessage, PrivateMessageInsertForm, PrivateMessageUpdateForm},
28 use lemmy_db_views::structs::LocalUserView;
29 use lemmy_utils::{error::LemmyError, utils::remove_slurs, ConnectionId};
31 #[async_trait::async_trait(?Send)]
32 impl PerformCrud for CreatePrivateMessage {
33 type Response = PrivateMessageResponse;
35 #[tracing::instrument(skip(self, context, websocket_id))]
38 context: &Data<LemmyContext>,
39 websocket_id: Option<ConnectionId>,
40 ) -> Result<PrivateMessageResponse, LemmyError> {
41 let data: &CreatePrivateMessage = self;
43 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
44 let local_site = LocalSite::read(context.pool()).await?;
46 let content_slurs_removed = remove_slurs(
47 &data.content.clone(),
48 &local_site_to_slur_regex(&local_site),
51 check_person_block(local_user_view.person.id, data.recipient_id, context.pool()).await?;
53 let private_message_form = PrivateMessageInsertForm::builder()
54 .content(content_slurs_removed.clone())
55 .creator_id(local_user_view.person.id)
56 .recipient_id(data.recipient_id)
59 let inserted_private_message =
60 match PrivateMessage::create(context.pool(), &private_message_form).await {
61 Ok(private_message) => private_message,
63 return Err(LemmyError::from_error_message(
65 "couldnt_create_private_message",
70 let inserted_private_message_id = inserted_private_message.id;
71 let protocol_and_hostname = context.settings().get_protocol_and_hostname();
72 let apub_id = generate_local_apub_endpoint(
73 EndpointType::PrivateMessage,
74 &inserted_private_message_id.to_string(),
75 &protocol_and_hostname,
77 let updated_private_message = PrivateMessage::update(
79 inserted_private_message.id,
80 &PrivateMessageUpdateForm::builder()
85 .map_err(|e| LemmyError::from_error_message(e, "couldnt_create_private_message"))?;
87 CreateOrUpdateChatMessage::send(
88 updated_private_message.into(),
89 &local_user_view.person.into(),
90 CreateOrUpdateType::Create,
95 let res = send_pm_ws_message(
96 inserted_private_message.id,
97 UserOperationCrud::CreatePrivateMessage,
103 // Send email to the local recipient, if one exists
104 if res.private_message_view.recipient.local {
105 let recipient_id = data.recipient_id;
106 let local_recipient = LocalUserView::read_person(context.pool(), recipient_id).await?;
107 let lang = get_interface_language(&local_recipient);
108 let inbox_link = format!("{}/inbox", context.settings().get_protocol_and_hostname());
111 &lang.notification_private_message_subject(&local_recipient.person.name),
112 &lang.notification_private_message_body(
114 &content_slurs_removed,
115 &local_recipient.person.name,