1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
5 get_local_user_view_from_jwt,
6 person::{CreatePrivateMessage, PrivateMessageResponse},
9 use lemmy_apub::{generate_apub_endpoint, ApubObjectType, EndpointType};
10 use lemmy_db_queries::{source::private_message::PrivateMessage_, Crud};
11 use lemmy_db_schema::source::private_message::{PrivateMessage, PrivateMessageForm};
12 use lemmy_db_views::{local_user_view::LocalUserView, private_message_view::PrivateMessageView};
13 use lemmy_utils::{utils::remove_slurs, ApiError, ConnectionId, LemmyError};
14 use lemmy_websocket::{messages::SendUserRoomMessage, LemmyContext, UserOperationCrud};
16 #[async_trait::async_trait(?Send)]
17 impl PerformCrud for CreatePrivateMessage {
18 type Response = PrivateMessageResponse;
22 context: &Data<LemmyContext>,
23 websocket_id: Option<ConnectionId>,
24 ) -> Result<PrivateMessageResponse, LemmyError> {
25 let data: &CreatePrivateMessage = self;
26 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
28 let content_slurs_removed = remove_slurs(&data.content.to_owned());
30 let private_message_form = PrivateMessageForm {
31 content: content_slurs_removed.to_owned(),
32 creator_id: local_user_view.person.id,
33 recipient_id: data.recipient_id,
34 ..PrivateMessageForm::default()
37 let inserted_private_message = match blocking(context.pool(), move |conn| {
38 PrivateMessage::create(conn, &private_message_form)
42 Ok(private_message) => private_message,
44 return Err(ApiError::err("couldnt_create_private_message").into());
48 let inserted_private_message_id = inserted_private_message.id;
49 let updated_private_message = blocking(
51 move |conn| -> Result<PrivateMessage, LemmyError> {
52 let apub_id = generate_apub_endpoint(
53 EndpointType::PrivateMessage,
54 &inserted_private_message_id.to_string(),
56 Ok(PrivateMessage::update_ap_id(
58 inserted_private_message_id,
64 .map_err(|_| ApiError::err("couldnt_create_private_message"))?;
66 updated_private_message
67 .send_create(&local_user_view.person, context)
70 let private_message_view = blocking(context.pool(), move |conn| {
71 PrivateMessageView::read(conn, inserted_private_message.id)
75 let res = PrivateMessageResponse {
79 // Send notifications to the local recipient, if one exists
80 let recipient_id = data.recipient_id;
81 if let Ok(local_recipient) = blocking(context.pool(), move |conn| {
82 LocalUserView::read_person(conn, recipient_id)
88 "Private Message from",
90 &content_slurs_removed,
93 let local_recipient_id = local_recipient.local_user.id;
94 context.chat_server().do_send(SendUserRoomMessage {
95 op: UserOperationCrud::CreatePrivateMessage,
96 response: res.clone(),