2021-03-25 19:19:40 +00:00
|
|
|
use crate::PerformCrud;
|
|
|
|
use actix_web::web::Data;
|
|
|
|
use lemmy_api_common::{
|
|
|
|
blocking,
|
2021-08-19 20:54:15 +00:00
|
|
|
check_person_block,
|
2021-03-25 19:19:40 +00:00
|
|
|
get_local_user_view_from_jwt,
|
|
|
|
person::{CreatePrivateMessage, PrivateMessageResponse},
|
|
|
|
};
|
2021-07-31 20:58:11 +00:00
|
|
|
use lemmy_apub::{
|
2021-10-29 10:32:42 +00:00
|
|
|
generate_local_apub_endpoint,
|
|
|
|
protocol::activities::{
|
2021-07-31 20:58:11 +00:00
|
|
|
private_message::create_or_update::CreateOrUpdatePrivateMessage,
|
|
|
|
CreateOrUpdateType,
|
|
|
|
},
|
|
|
|
EndpointType,
|
|
|
|
};
|
2021-10-16 13:33:38 +00:00
|
|
|
use lemmy_db_schema::{
|
|
|
|
source::private_message::{PrivateMessage, PrivateMessageForm},
|
|
|
|
traits::Crud,
|
|
|
|
};
|
2021-08-17 18:04:58 +00:00
|
|
|
use lemmy_db_views::local_user_view::LocalUserView;
|
2021-12-06 14:54:47 +00:00
|
|
|
use lemmy_utils::{utils::remove_slurs, ConnectionId, LemmyError};
|
2021-10-18 21:36:44 +00:00
|
|
|
use lemmy_websocket::{
|
|
|
|
send::{send_email_to_user, send_pm_ws_message},
|
|
|
|
LemmyContext,
|
|
|
|
UserOperationCrud,
|
|
|
|
};
|
2021-03-25 19:19:40 +00:00
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl PerformCrud for CreatePrivateMessage {
|
|
|
|
type Response = PrivateMessageResponse;
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip(self, context, websocket_id))]
|
2021-03-25 19:19:40 +00:00
|
|
|
async fn perform(
|
|
|
|
&self,
|
|
|
|
context: &Data<LemmyContext>,
|
|
|
|
websocket_id: Option<ConnectionId>,
|
|
|
|
) -> Result<PrivateMessageResponse, LemmyError> {
|
2021-07-05 16:07:26 +00:00
|
|
|
let data: &CreatePrivateMessage = self;
|
2021-09-22 15:57:09 +00:00
|
|
|
let local_user_view =
|
|
|
|
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2021-09-22 15:57:09 +00:00
|
|
|
let content_slurs_removed =
|
|
|
|
remove_slurs(&data.content.to_owned(), &context.settings().slur_regex());
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2021-08-19 20:54:15 +00:00
|
|
|
check_person_block(local_user_view.person.id, data.recipient_id, context.pool()).await?;
|
|
|
|
|
2021-03-25 19:19:40 +00:00
|
|
|
let private_message_form = PrivateMessageForm {
|
|
|
|
content: content_slurs_removed.to_owned(),
|
|
|
|
creator_id: local_user_view.person.id,
|
|
|
|
recipient_id: data.recipient_id,
|
2021-03-29 20:24:50 +00:00
|
|
|
..PrivateMessageForm::default()
|
2021-03-25 19:19:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let inserted_private_message = match blocking(context.pool(), move |conn| {
|
|
|
|
PrivateMessage::create(conn, &private_message_form)
|
|
|
|
})
|
|
|
|
.await?
|
|
|
|
{
|
|
|
|
Ok(private_message) => private_message,
|
2021-10-13 19:50:21 +00:00
|
|
|
Err(e) => {
|
2021-12-06 14:54:47 +00:00
|
|
|
return Err(LemmyError::from(e).with_message("couldnt_create_private_message"));
|
2021-03-25 19:19:40 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let inserted_private_message_id = inserted_private_message.id;
|
2021-09-22 15:57:09 +00:00
|
|
|
let protocol_and_hostname = context.settings().get_protocol_and_hostname();
|
2021-04-16 13:10:43 +00:00
|
|
|
let updated_private_message = blocking(
|
2021-03-25 19:19:40 +00:00
|
|
|
context.pool(),
|
|
|
|
move |conn| -> Result<PrivateMessage, LemmyError> {
|
2021-10-25 16:09:21 +00:00
|
|
|
let apub_id = generate_local_apub_endpoint(
|
2021-03-25 19:19:40 +00:00
|
|
|
EndpointType::PrivateMessage,
|
|
|
|
&inserted_private_message_id.to_string(),
|
2021-09-22 15:57:09 +00:00
|
|
|
&protocol_and_hostname,
|
2021-03-25 19:19:40 +00:00
|
|
|
)?;
|
|
|
|
Ok(PrivateMessage::update_ap_id(
|
2021-07-05 16:07:26 +00:00
|
|
|
conn,
|
2021-03-25 19:19:40 +00:00
|
|
|
inserted_private_message_id,
|
|
|
|
apub_id,
|
|
|
|
)?)
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.await?
|
2021-12-06 14:54:47 +00:00
|
|
|
.map_err(LemmyError::from)
|
|
|
|
.map_err(|e| e.with_message("couldnt_create_private_message"))?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2021-07-31 20:58:11 +00:00
|
|
|
CreateOrUpdatePrivateMessage::send(
|
2021-11-06 12:37:55 +00:00
|
|
|
updated_private_message.into(),
|
2021-10-18 21:36:44 +00:00
|
|
|
&local_user_view.person.into(),
|
2021-07-31 20:58:11 +00:00
|
|
|
CreateOrUpdateType::Create,
|
|
|
|
context,
|
|
|
|
)
|
|
|
|
.await?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2021-08-17 18:04:58 +00:00
|
|
|
let res = send_pm_ws_message(
|
|
|
|
inserted_private_message.id,
|
|
|
|
UserOperationCrud::CreatePrivateMessage,
|
|
|
|
websocket_id,
|
|
|
|
context,
|
|
|
|
)
|
|
|
|
.await?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2021-08-17 18:04:58 +00:00
|
|
|
// Send email to the local recipient, if one exists
|
|
|
|
if res.private_message_view.recipient.local {
|
|
|
|
let recipient_id = data.recipient_id;
|
|
|
|
let local_recipient = blocking(context.pool(), move |conn| {
|
|
|
|
LocalUserView::read_person(conn, recipient_id)
|
|
|
|
})
|
|
|
|
.await??;
|
2021-03-25 19:19:40 +00:00
|
|
|
send_email_to_user(
|
|
|
|
&local_recipient,
|
|
|
|
"Private Message from",
|
|
|
|
"Private Message",
|
|
|
|
&content_slurs_removed,
|
2021-09-22 15:57:09 +00:00
|
|
|
&context.settings(),
|
2021-03-25 19:19:40 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
}
|