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_structs::{blocking, user::PrivateMessageResponse};
21 use lemmy_utils::{location_info, LemmyError};
22 use lemmy_websocket::{messages::SendUserRoomMessage, LemmyContext, UserOperation};
25 pub(crate) async fn receive_create_private_message(
26 context: &LemmyContext,
29 request_counter: &mut i32,
30 ) -> Result<(), LemmyError> {
31 check_private_message_activity_valid(&create, context, request_counter).await?;
33 let note = NoteExt::from_any_base(
37 .context(location_info!())?
40 .context(location_info!())?;
43 PrivateMessage::from_apub(¬e, context, expected_domain, request_counter).await?;
45 let message = blocking(&context.pool(), move |conn| {
46 PrivateMessageView::read(conn, private_message.id)
50 let res = PrivateMessageResponse { message };
52 let recipient_id = res.message.recipient.id;
54 context.chat_server().do_send(SendUserRoomMessage {
55 op: UserOperation::CreatePrivateMessage,
64 pub(crate) async fn receive_update_private_message(
65 context: &LemmyContext,
68 request_counter: &mut i32,
69 ) -> Result<(), LemmyError> {
70 check_private_message_activity_valid(&update, context, request_counter).await?;
75 .context(location_info!())?
77 let note = NoteExt::from_any_base(object)?.context(location_info!())?;
80 PrivateMessage::from_apub(¬e, context, expected_domain, request_counter).await?;
82 let private_message_id = private_message.id;
83 let message = blocking(&context.pool(), move |conn| {
84 PrivateMessageView::read(conn, private_message_id)
88 let res = PrivateMessageResponse { message };
90 let recipient_id = res.message.recipient.id;
92 context.chat_server().do_send(SendUserRoomMessage {
93 op: UserOperation::EditPrivateMessage,
102 pub(crate) async fn receive_delete_private_message(
103 context: &LemmyContext,
105 private_message: PrivateMessage,
106 request_counter: &mut i32,
107 ) -> Result<(), LemmyError> {
108 check_private_message_activity_valid(&delete, context, request_counter).await?;
110 let deleted_private_message = blocking(context.pool(), move |conn| {
111 PrivateMessage::update_deleted(conn, private_message.id, true)
115 let message = blocking(&context.pool(), move |conn| {
116 PrivateMessageView::read(&conn, deleted_private_message.id)
120 let res = PrivateMessageResponse { message };
121 let recipient_id = res.message.recipient.id;
122 context.chat_server().do_send(SendUserRoomMessage {
123 op: UserOperation::EditPrivateMessage,
132 pub(crate) async fn receive_undo_delete_private_message(
133 context: &LemmyContext,
135 expected_domain: &Url,
136 private_message: PrivateMessage,
137 request_counter: &mut i32,
138 ) -> Result<(), LemmyError> {
139 check_private_message_activity_valid(&undo, context, request_counter).await?;
140 let object = undo.object().to_owned().one().context(location_info!())?;
141 let delete = Delete::from_any_base(object)?.context(location_info!())?;
142 verify_activity_domains_valid(&delete, expected_domain, true)?;
143 check_private_message_activity_valid(&delete, context, request_counter).await?;
145 let deleted_private_message = blocking(context.pool(), move |conn| {
146 PrivateMessage::update_deleted(conn, private_message.id, false)
150 let message = blocking(&context.pool(), move |conn| {
151 PrivateMessageView::read(&conn, deleted_private_message.id)
155 let res = PrivateMessageResponse { message };
156 let recipient_id = res.message.recipient.id;
157 context.chat_server().do_send(SendUserRoomMessage {
158 op: UserOperation::EditPrivateMessage,
167 async fn check_private_message_activity_valid<T, Kind>(
169 context: &LemmyContext,
170 request_counter: &mut i32,
171 ) -> Result<(), LemmyError>
173 T: AsBase<Kind> + AsObject<Kind> + ActorAndObjectRefExt,
175 let to_and_cc = get_activity_to_and_cc(activity);
176 if to_and_cc.len() != 1 {
177 return Err(anyhow!("Private message can only be addressed to one user").into());
179 if to_and_cc.contains(&public()) {
180 return Err(anyhow!("Private message cant be public").into());
182 let user_id = activity
185 .single_xsd_any_uri()
186 .context(location_info!())?;
187 check_is_apub_id_valid(&user_id)?;
188 // check that the sender is a user, not a community
189 get_or_fetch_and_upsert_user(&user_id, &context, request_counter).await?;