1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
6 get_local_user_view_from_jwt,
7 person::{CreatePrivateMessage, PrivateMessageResponse},
12 private_message::create_or_update::CreateOrUpdatePrivateMessage,
15 generate_apub_endpoint,
18 use lemmy_db_schema::{
19 source::private_message::{PrivateMessage, PrivateMessageForm},
22 use lemmy_db_views::local_user_view::LocalUserView;
23 use lemmy_utils::{utils::remove_slurs, ApiError, ConnectionId, LemmyError};
24 use lemmy_websocket::{send::send_pm_ws_message, LemmyContext, UserOperationCrud};
26 #[async_trait::async_trait(?Send)]
27 impl PerformCrud for CreatePrivateMessage {
28 type Response = PrivateMessageResponse;
32 context: &Data<LemmyContext>,
33 websocket_id: Option<ConnectionId>,
34 ) -> Result<PrivateMessageResponse, LemmyError> {
35 let data: &CreatePrivateMessage = self;
37 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
39 let content_slurs_removed =
40 remove_slurs(&data.content.to_owned(), &context.settings().slur_regex());
42 check_person_block(local_user_view.person.id, data.recipient_id, context.pool()).await?;
44 let private_message_form = PrivateMessageForm {
45 content: content_slurs_removed.to_owned(),
46 creator_id: local_user_view.person.id,
47 recipient_id: data.recipient_id,
48 ..PrivateMessageForm::default()
51 let inserted_private_message = match blocking(context.pool(), move |conn| {
52 PrivateMessage::create(conn, &private_message_form)
56 Ok(private_message) => private_message,
58 return Err(ApiError::err("couldnt_create_private_message", e).into());
62 let inserted_private_message_id = inserted_private_message.id;
63 let protocol_and_hostname = context.settings().get_protocol_and_hostname();
64 let updated_private_message = blocking(
66 move |conn| -> Result<PrivateMessage, LemmyError> {
67 let apub_id = generate_apub_endpoint(
68 EndpointType::PrivateMessage,
69 &inserted_private_message_id.to_string(),
70 &protocol_and_hostname,
72 Ok(PrivateMessage::update_ap_id(
74 inserted_private_message_id,
80 .map_err(|e| ApiError::err("couldnt_create_private_message", e))?;
82 CreateOrUpdatePrivateMessage::send(
83 &updated_private_message,
84 &local_user_view.person,
85 CreateOrUpdateType::Create,
90 let res = send_pm_ws_message(
91 inserted_private_message.id,
92 UserOperationCrud::CreatePrivateMessage,
98 // Send email to the local recipient, if one exists
99 if res.private_message_view.recipient.local {
100 let recipient_id = data.recipient_id;
101 let local_recipient = blocking(context.pool(), move |conn| {
102 LocalUserView::read_person(conn, recipient_id)
107 "Private Message from",
109 &content_slurs_removed,