2 activities::receive::verify_activity_domains_valid,
3 check_is_apub_id_valid,
4 fetcher::user::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};
16 use lemmy_api_structs::{blocking, user::PrivateMessageResponse};
17 use lemmy_db_queries::source::private_message::PrivateMessage_;
18 use lemmy_db_schema::source::private_message::PrivateMessage;
19 use lemmy_db_views::private_message_view::PrivateMessageView;
20 use lemmy_utils::{location_info, LemmyError};
21 use lemmy_websocket::{messages::SendUserRoomMessage, LemmyContext, UserOperation};
24 pub(crate) async fn receive_create_private_message(
25 context: &LemmyContext,
28 request_counter: &mut i32,
29 ) -> Result<(), LemmyError> {
30 check_private_message_activity_valid(&create, context, request_counter).await?;
32 let note = NoteExt::from_any_base(
36 .context(location_info!())?
39 .context(location_info!())?;
42 PrivateMessage::from_apub(¬e, context, expected_domain, request_counter).await?;
44 let message = blocking(&context.pool(), move |conn| {
45 PrivateMessageView::read(conn, private_message.id)
49 let res = PrivateMessageResponse {
50 private_message_view: message,
53 let recipient_id = res.private_message_view.recipient.id;
55 context.chat_server().do_send(SendUserRoomMessage {
56 op: UserOperation::CreatePrivateMessage,
65 pub(crate) async fn receive_update_private_message(
66 context: &LemmyContext,
69 request_counter: &mut i32,
70 ) -> Result<(), LemmyError> {
71 check_private_message_activity_valid(&update, context, request_counter).await?;
76 .context(location_info!())?
78 let note = NoteExt::from_any_base(object)?.context(location_info!())?;
81 PrivateMessage::from_apub(¬e, context, expected_domain, request_counter).await?;
83 let private_message_id = private_message.id;
84 let message = blocking(&context.pool(), move |conn| {
85 PrivateMessageView::read(conn, private_message_id)
89 let res = PrivateMessageResponse {
90 private_message_view: message,
93 let recipient_id = res.private_message_view.recipient.id;
95 context.chat_server().do_send(SendUserRoomMessage {
96 op: UserOperation::EditPrivateMessage,
105 pub(crate) async fn receive_delete_private_message(
106 context: &LemmyContext,
108 private_message: PrivateMessage,
109 request_counter: &mut i32,
110 ) -> Result<(), LemmyError> {
111 check_private_message_activity_valid(&delete, context, request_counter).await?;
113 let deleted_private_message = blocking(context.pool(), move |conn| {
114 PrivateMessage::update_deleted(conn, private_message.id, true)
118 let message = blocking(&context.pool(), move |conn| {
119 PrivateMessageView::read(&conn, deleted_private_message.id)
123 let res = PrivateMessageResponse {
124 private_message_view: message,
126 let recipient_id = res.private_message_view.recipient.id;
127 context.chat_server().do_send(SendUserRoomMessage {
128 op: UserOperation::EditPrivateMessage,
137 pub(crate) async fn receive_undo_delete_private_message(
138 context: &LemmyContext,
140 expected_domain: &Url,
141 private_message: PrivateMessage,
142 request_counter: &mut i32,
143 ) -> Result<(), LemmyError> {
144 check_private_message_activity_valid(&undo, context, request_counter).await?;
145 let object = undo.object().to_owned().one().context(location_info!())?;
146 let delete = Delete::from_any_base(object)?.context(location_info!())?;
147 verify_activity_domains_valid(&delete, expected_domain, true)?;
148 check_private_message_activity_valid(&delete, context, request_counter).await?;
150 let deleted_private_message = blocking(context.pool(), move |conn| {
151 PrivateMessage::update_deleted(conn, private_message.id, false)
155 let message = blocking(&context.pool(), move |conn| {
156 PrivateMessageView::read(&conn, deleted_private_message.id)
160 let res = PrivateMessageResponse {
161 private_message_view: message,
163 let recipient_id = res.private_message_view.recipient.id;
164 context.chat_server().do_send(SendUserRoomMessage {
165 op: UserOperation::EditPrivateMessage,
174 async fn check_private_message_activity_valid<T, Kind>(
176 context: &LemmyContext,
177 request_counter: &mut i32,
178 ) -> Result<(), LemmyError>
180 T: AsBase<Kind> + AsObject<Kind> + ActorAndObjectRefExt,
182 let to_and_cc = get_activity_to_and_cc(activity);
183 if to_and_cc.len() != 1 {
184 return Err(anyhow!("Private message can only be addressed to one user").into());
186 if to_and_cc.contains(&public()) {
187 return Err(anyhow!("Private message cant be public").into());
189 let user_id = activity
192 .single_xsd_any_uri()
193 .context(location_info!())?;
194 check_is_apub_id_valid(&user_id)?;
195 // check that the sender is a user, not a community
196 get_or_fetch_and_upsert_user(&user_id, &context, request_counter).await?;