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 {
51 private_message_view: message,
54 let recipient_id = res.private_message_view.recipient.id;
56 context.chat_server().do_send(SendUserRoomMessage {
57 op: UserOperation::CreatePrivateMessage,
66 pub(crate) async fn receive_update_private_message(
67 context: &LemmyContext,
70 request_counter: &mut i32,
71 ) -> Result<(), LemmyError> {
72 check_private_message_activity_valid(&update, context, request_counter).await?;
77 .context(location_info!())?
79 let note = NoteExt::from_any_base(object)?.context(location_info!())?;
82 PrivateMessage::from_apub(¬e, context, expected_domain, request_counter).await?;
84 let private_message_id = private_message.id;
85 let message = blocking(&context.pool(), move |conn| {
86 PrivateMessageView::read(conn, private_message_id)
90 let res = PrivateMessageResponse {
91 private_message_view: message,
94 let recipient_id = res.private_message_view.recipient.id;
96 context.chat_server().do_send(SendUserRoomMessage {
97 op: UserOperation::EditPrivateMessage,
106 pub(crate) async fn receive_delete_private_message(
107 context: &LemmyContext,
109 private_message: PrivateMessage,
110 request_counter: &mut i32,
111 ) -> Result<(), LemmyError> {
112 check_private_message_activity_valid(&delete, context, request_counter).await?;
114 let deleted_private_message = blocking(context.pool(), move |conn| {
115 PrivateMessage::update_deleted(conn, private_message.id, true)
119 let message = blocking(&context.pool(), move |conn| {
120 PrivateMessageView::read(&conn, deleted_private_message.id)
124 let res = PrivateMessageResponse {
125 private_message_view: message,
127 let recipient_id = res.private_message_view.recipient.id;
128 context.chat_server().do_send(SendUserRoomMessage {
129 op: UserOperation::EditPrivateMessage,
138 pub(crate) async fn receive_undo_delete_private_message(
139 context: &LemmyContext,
141 expected_domain: &Url,
142 private_message: PrivateMessage,
143 request_counter: &mut i32,
144 ) -> Result<(), LemmyError> {
145 check_private_message_activity_valid(&undo, context, request_counter).await?;
146 let object = undo.object().to_owned().one().context(location_info!())?;
147 let delete = Delete::from_any_base(object)?.context(location_info!())?;
148 verify_activity_domains_valid(&delete, expected_domain, true)?;
149 check_private_message_activity_valid(&delete, context, request_counter).await?;
151 let deleted_private_message = blocking(context.pool(), move |conn| {
152 PrivateMessage::update_deleted(conn, private_message.id, false)
156 let message = blocking(&context.pool(), move |conn| {
157 PrivateMessageView::read(&conn, deleted_private_message.id)
161 let res = PrivateMessageResponse {
162 private_message_view: message,
164 let recipient_id = res.private_message_view.recipient.id;
165 context.chat_server().do_send(SendUserRoomMessage {
166 op: UserOperation::EditPrivateMessage,
175 async fn check_private_message_activity_valid<T, Kind>(
177 context: &LemmyContext,
178 request_counter: &mut i32,
179 ) -> Result<(), LemmyError>
181 T: AsBase<Kind> + AsObject<Kind> + ActorAndObjectRefExt,
183 let to_and_cc = get_activity_to_and_cc(activity);
184 if to_and_cc.len() != 1 {
185 return Err(anyhow!("Private message can only be addressed to one user").into());
187 if to_and_cc.contains(&public()) {
188 return Err(anyhow!("Private message cant be public").into());
190 let user_id = activity
193 .single_xsd_any_uri()
194 .context(location_info!())?;
195 check_is_apub_id_valid(&user_id)?;
196 // check that the sender is a user, not a community
197 get_or_fetch_and_upsert_user(&user_id, &context, request_counter).await?;