1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 private_message::{CreatePrivateMessage, PrivateMessageResponse},
7 get_interface_language,
8 get_local_user_view_from_jwt,
9 local_site_to_slur_regex,
14 generate_local_apub_endpoint,
15 protocol::activities::{
16 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};
30 use lemmy_websocket::{send::send_pm_ws_message, LemmyContext, UserOperationCrud};
32 #[async_trait::async_trait(?Send)]
33 impl PerformCrud for CreatePrivateMessage {
34 type Response = PrivateMessageResponse;
36 #[tracing::instrument(skip(self, context, websocket_id))]
39 context: &Data<LemmyContext>,
40 websocket_id: Option<ConnectionId>,
41 ) -> Result<PrivateMessageResponse, LemmyError> {
42 let data: &CreatePrivateMessage = self;
44 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
45 let local_site = LocalSite::read(context.pool()).await?;
47 let content_slurs_removed = remove_slurs(
48 &data.content.clone(),
49 &local_site_to_slur_regex(&local_site),
52 check_person_block(local_user_view.person.id, data.recipient_id, context.pool()).await?;
54 let private_message_form = PrivateMessageInsertForm::builder()
55 .content(content_slurs_removed.clone())
56 .creator_id(local_user_view.person.id)
57 .recipient_id(data.recipient_id)
60 let inserted_private_message =
61 match PrivateMessage::create(context.pool(), &private_message_form).await {
62 Ok(private_message) => private_message,
64 return Err(LemmyError::from_error_message(
66 "couldnt_create_private_message",
71 let inserted_private_message_id = inserted_private_message.id;
72 let protocol_and_hostname = context.settings().get_protocol_and_hostname();
73 let apub_id = generate_local_apub_endpoint(
74 EndpointType::PrivateMessage,
75 &inserted_private_message_id.to_string(),
76 &protocol_and_hostname,
78 let updated_private_message = PrivateMessage::update(
80 inserted_private_message.id,
81 &PrivateMessageUpdateForm::builder()
86 .map_err(|e| LemmyError::from_error_message(e, "couldnt_create_private_message"))?;
88 CreateOrUpdateChatMessage::send(
89 updated_private_message.into(),
90 &local_user_view.person.into(),
91 CreateOrUpdateType::Create,
96 let res = send_pm_ws_message(
97 inserted_private_message.id,
98 UserOperationCrud::CreatePrivateMessage,
104 // Send email to the local recipient, if one exists
105 if res.private_message_view.recipient.local {
106 let recipient_id = data.recipient_id;
107 let local_recipient = LocalUserView::read_person(context.pool(), recipient_id).await?;
108 let lang = get_interface_language(&local_recipient);
109 let inbox_link = format!("{}/inbox", context.settings().get_protocol_and_hostname());
112 &lang.notification_private_message_subject(&local_recipient.person.name),
113 &lang.notification_private_message_body(
115 &content_slurs_removed,
116 &local_recipient.person.name,