2021-03-25 19:19:40 +00:00
|
|
|
use crate::PerformCrud;
|
|
|
|
use actix_web::web::Data;
|
|
|
|
use lemmy_api_common::{
|
2022-11-26 20:47:13 +00:00
|
|
|
generate_local_apub_endpoint,
|
2022-09-19 22:58:42 +00:00
|
|
|
private_message::{CreatePrivateMessage, PrivateMessageResponse},
|
2022-05-03 17:44:13 +00:00
|
|
|
utils::{
|
|
|
|
check_person_block,
|
2022-08-18 19:11:19 +00:00
|
|
|
get_interface_language,
|
2022-05-03 17:44:13 +00:00
|
|
|
get_local_user_view_from_jwt,
|
2022-10-27 09:24:07 +00:00
|
|
|
local_site_to_slur_regex,
|
2022-05-03 17:44:13 +00:00
|
|
|
send_email_to_user,
|
|
|
|
},
|
2022-11-26 02:04:46 +00:00
|
|
|
websocket::{send::send_pm_ws_message, UserOperationCrud},
|
2022-11-26 20:47:13 +00:00
|
|
|
EndpointType,
|
2022-11-26 02:04:46 +00:00
|
|
|
LemmyContext,
|
2021-03-25 19:19:40 +00:00
|
|
|
};
|
2022-11-26 20:47:13 +00:00
|
|
|
use lemmy_apub::protocol::activities::{
|
|
|
|
create_or_update::chat_message::CreateOrUpdateChatMessage,
|
|
|
|
CreateOrUpdateType,
|
2021-07-31 20:58:11 +00:00
|
|
|
};
|
2021-10-16 13:33:38 +00:00
|
|
|
use lemmy_db_schema::{
|
2022-10-27 09:24:07 +00:00
|
|
|
source::{
|
|
|
|
local_site::LocalSite,
|
|
|
|
private_message::{PrivateMessage, PrivateMessageInsertForm, PrivateMessageUpdateForm},
|
|
|
|
},
|
2021-10-16 13:33:38 +00:00
|
|
|
traits::Crud,
|
|
|
|
};
|
2022-05-03 17:44:13 +00:00
|
|
|
use lemmy_db_views::structs::LocalUserView;
|
2022-06-02 14:33:41 +00:00
|
|
|
use lemmy_utils::{error::LemmyError, utils::remove_slurs, ConnectionId};
|
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?;
|
2022-11-09 10:05:00 +00:00
|
|
|
let local_site = LocalSite::read(context.pool()).await?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2022-10-27 09:24:07 +00:00
|
|
|
let content_slurs_removed = remove_slurs(
|
2022-11-19 04:33:54 +00:00
|
|
|
&data.content.clone(),
|
2022-10-27 09:24:07 +00:00
|
|
|
&local_site_to_slur_regex(&local_site),
|
|
|
|
);
|
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?;
|
|
|
|
|
2022-10-27 09:24:07 +00:00
|
|
|
let private_message_form = PrivateMessageInsertForm::builder()
|
2022-11-19 04:33:54 +00:00
|
|
|
.content(content_slurs_removed.clone())
|
2022-10-27 09:24:07 +00:00
|
|
|
.creator_id(local_user_view.person.id)
|
|
|
|
.recipient_id(data.recipient_id)
|
|
|
|
.build();
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
let inserted_private_message =
|
|
|
|
match PrivateMessage::create(context.pool(), &private_message_form).await {
|
|
|
|
Ok(private_message) => private_message,
|
|
|
|
Err(e) => {
|
|
|
|
return Err(LemmyError::from_error_message(
|
|
|
|
e,
|
|
|
|
"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();
|
2022-11-09 10:05:00 +00:00
|
|
|
let apub_id = generate_local_apub_endpoint(
|
|
|
|
EndpointType::PrivateMessage,
|
|
|
|
&inserted_private_message_id.to_string(),
|
|
|
|
&protocol_and_hostname,
|
|
|
|
)?;
|
|
|
|
let updated_private_message = PrivateMessage::update(
|
2021-03-25 19:19:40 +00:00
|
|
|
context.pool(),
|
2022-11-09 10:05:00 +00:00
|
|
|
inserted_private_message.id,
|
|
|
|
&PrivateMessageUpdateForm::builder()
|
|
|
|
.ap_id(Some(apub_id))
|
|
|
|
.build(),
|
2021-03-25 19:19:40 +00:00
|
|
|
)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
|
|
|
.map_err(|e| LemmyError::from_error_message(e, "couldnt_create_private_message"))?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2022-12-01 20:52:49 +00:00
|
|
|
CreateOrUpdateChatMessage::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;
|
2022-11-09 10:05:00 +00:00
|
|
|
let local_recipient = LocalUserView::read_person(context.pool(), recipient_id).await?;
|
2022-08-18 19:11:19 +00:00
|
|
|
let lang = get_interface_language(&local_recipient);
|
2022-03-24 15:25:51 +00:00
|
|
|
let inbox_link = format!("{}/inbox", context.settings().get_protocol_and_hostname());
|
2021-03-25 19:19:40 +00:00
|
|
|
send_email_to_user(
|
|
|
|
&local_recipient,
|
2022-05-13 16:24:29 +00:00
|
|
|
&lang.notification_private_message_subject(&local_recipient.person.name),
|
|
|
|
&lang.notification_private_message_body(
|
2022-03-24 15:25:51 +00:00
|
|
|
&inbox_link,
|
2022-05-13 16:24:29 +00:00
|
|
|
&content_slurs_removed,
|
|
|
|
&local_recipient.person.name,
|
2022-03-24 15:25:51 +00:00
|
|
|
),
|
2022-06-22 20:24:54 +00:00
|
|
|
context.settings(),
|
2021-03-25 19:19:40 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
}
|