2 activities::receive::verify_activity_domains_valid,
3 check_is_apub_id_valid,
4 fetcher::get_or_fetch_and_upsert_user,
5 inbox::get_activity_to_and_cc,
10 activity::{ActorAndObjectRefExt, Create, Delete, Undo, Update},
11 base::{AsBase, ExtendsExt},
15 use anyhow::{anyhow, Context};
17 source::private_message::PrivateMessage_,
18 views::private_message_view::PrivateMessageView,
20 use lemmy_db_schema::source::private_message::PrivateMessage;
21 use lemmy_structs::{blocking, user::PrivateMessageResponse};
22 use lemmy_utils::{location_info, LemmyError};
23 use lemmy_websocket::{messages::SendUserRoomMessage, LemmyContext, UserOperation};
26 pub(crate) async fn receive_create_private_message(
27 context: &LemmyContext,
30 request_counter: &mut i32,
31 ) -> Result<(), LemmyError> {
32 check_private_message_activity_valid(&create, context, request_counter).await?;
34 let note = NoteExt::from_any_base(
38 .context(location_info!())?
41 .context(location_info!())?;
44 PrivateMessage::from_apub(¬e, context, expected_domain, request_counter).await?;
46 let message = blocking(&context.pool(), move |conn| {
47 PrivateMessageView::read(conn, private_message.id)
51 let res = PrivateMessageResponse {
52 private_message_view: message,
55 let recipient_id = res.private_message_view.recipient.id;
57 context.chat_server().do_send(SendUserRoomMessage {
58 op: UserOperation::CreatePrivateMessage,
67 pub(crate) async fn receive_update_private_message(
68 context: &LemmyContext,
71 request_counter: &mut i32,
72 ) -> Result<(), LemmyError> {
73 check_private_message_activity_valid(&update, context, request_counter).await?;
78 .context(location_info!())?
80 let note = NoteExt::from_any_base(object)?.context(location_info!())?;
83 PrivateMessage::from_apub(¬e, context, expected_domain, request_counter).await?;
85 let private_message_id = private_message.id;
86 let message = blocking(&context.pool(), move |conn| {
87 PrivateMessageView::read(conn, private_message_id)
91 let res = PrivateMessageResponse {
92 private_message_view: message,
95 let recipient_id = res.private_message_view.recipient.id;
97 context.chat_server().do_send(SendUserRoomMessage {
98 op: UserOperation::EditPrivateMessage,
107 pub(crate) async fn receive_delete_private_message(
108 context: &LemmyContext,
110 private_message: PrivateMessage,
111 request_counter: &mut i32,
112 ) -> Result<(), LemmyError> {
113 check_private_message_activity_valid(&delete, context, request_counter).await?;
115 let deleted_private_message = blocking(context.pool(), move |conn| {
116 PrivateMessage::update_deleted(conn, private_message.id, true)
120 let message = blocking(&context.pool(), move |conn| {
121 PrivateMessageView::read(&conn, deleted_private_message.id)
125 let res = PrivateMessageResponse {
126 private_message_view: message,
128 let recipient_id = res.private_message_view.recipient.id;
129 context.chat_server().do_send(SendUserRoomMessage {
130 op: UserOperation::EditPrivateMessage,
139 pub(crate) async fn receive_undo_delete_private_message(
140 context: &LemmyContext,
142 expected_domain: &Url,
143 private_message: PrivateMessage,
144 request_counter: &mut i32,
145 ) -> Result<(), LemmyError> {
146 check_private_message_activity_valid(&undo, context, request_counter).await?;
147 let object = undo.object().to_owned().one().context(location_info!())?;
148 let delete = Delete::from_any_base(object)?.context(location_info!())?;
149 verify_activity_domains_valid(&delete, expected_domain, true)?;
150 check_private_message_activity_valid(&delete, context, request_counter).await?;
152 let deleted_private_message = blocking(context.pool(), move |conn| {
153 PrivateMessage::update_deleted(conn, private_message.id, false)
157 let message = blocking(&context.pool(), move |conn| {
158 PrivateMessageView::read(&conn, deleted_private_message.id)
162 let res = PrivateMessageResponse {
163 private_message_view: message,
165 let recipient_id = res.private_message_view.recipient.id;
166 context.chat_server().do_send(SendUserRoomMessage {
167 op: UserOperation::EditPrivateMessage,
176 async fn check_private_message_activity_valid<T, Kind>(
178 context: &LemmyContext,
179 request_counter: &mut i32,
180 ) -> Result<(), LemmyError>
182 T: AsBase<Kind> + AsObject<Kind> + ActorAndObjectRefExt,
184 let to_and_cc = get_activity_to_and_cc(activity);
185 if to_and_cc.len() != 1 {
186 return Err(anyhow!("Private message can only be addressed to one user").into());
188 if to_and_cc.contains(&public()) {
189 return Err(anyhow!("Private message cant be public").into());
191 let user_id = activity
194 .single_xsd_any_uri()
195 .context(location_info!())?;
196 check_is_apub_id_valid(&user_id)?;
197 // check that the sender is a user, not a community
198 get_or_fetch_and_upsert_user(&user_id, &context, request_counter).await?;