]> Untitled Git - lemmy.git/blob - crates/apub/src/objects/private_message.rs
Simplify lemmy_context() function (dont return errors)
[lemmy.git] / crates / apub / src / objects / private_message.rs
1 use crate::{
2   extensions::context::lemmy_context,
3   fetcher::person::get_or_fetch_and_upsert_person,
4   objects::{
5     check_object_domain,
6     create_tombstone,
7     get_object_from_apub,
8     get_source_markdown_value,
9     set_content_and_source,
10     FromApub,
11     FromApubToForm,
12     ToApub,
13   },
14   NoteExt,
15 };
16 use activitystreams::{
17   object::{kind::NoteType, ApObject, Note, Tombstone},
18   prelude::*,
19 };
20 use anyhow::Context;
21 use lemmy_api_common::blocking;
22 use lemmy_db_queries::{Crud, DbPool};
23 use lemmy_db_schema::source::{
24   person::Person,
25   private_message::{PrivateMessage, PrivateMessageForm},
26 };
27 use lemmy_utils::{location_info, utils::convert_datetime, LemmyError};
28 use lemmy_websocket::LemmyContext;
29 use url::Url;
30
31 #[async_trait::async_trait(?Send)]
32 impl ToApub for PrivateMessage {
33   type ApubType = NoteExt;
34
35   async fn to_apub(&self, pool: &DbPool) -> Result<NoteExt, LemmyError> {
36     let mut private_message = ApObject::new(Note::new());
37
38     let creator_id = self.creator_id;
39     let creator = blocking(pool, move |conn| Person::read(conn, creator_id)).await??;
40
41     let recipient_id = self.recipient_id;
42     let recipient = blocking(pool, move |conn| Person::read(conn, recipient_id)).await??;
43
44     private_message
45       .set_many_contexts(lemmy_context())
46       .set_id(self.ap_id.to_owned().into_inner())
47       .set_published(convert_datetime(self.published))
48       .set_to(recipient.actor_id.into_inner())
49       .set_attributed_to(creator.actor_id.into_inner());
50
51     set_content_and_source(&mut private_message, &self.content)?;
52
53     if let Some(u) = self.updated {
54       private_message.set_updated(convert_datetime(u));
55     }
56
57     Ok(private_message)
58   }
59
60   fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
61     create_tombstone(
62       self.deleted,
63       self.ap_id.to_owned().into(),
64       self.updated,
65       NoteType::Note,
66     )
67   }
68 }
69
70 #[async_trait::async_trait(?Send)]
71 impl FromApub for PrivateMessage {
72   type ApubType = NoteExt;
73
74   async fn from_apub(
75     note: &NoteExt,
76     context: &LemmyContext,
77     expected_domain: Url,
78     request_counter: &mut i32,
79     mod_action_allowed: bool,
80   ) -> Result<PrivateMessage, LemmyError> {
81     get_object_from_apub(
82       note,
83       context,
84       expected_domain,
85       request_counter,
86       mod_action_allowed,
87     )
88     .await
89   }
90 }
91
92 #[async_trait::async_trait(?Send)]
93 impl FromApubToForm<NoteExt> for PrivateMessageForm {
94   async fn from_apub(
95     note: &NoteExt,
96     context: &LemmyContext,
97     expected_domain: Url,
98     request_counter: &mut i32,
99     _mod_action_allowed: bool,
100   ) -> Result<PrivateMessageForm, LemmyError> {
101     let creator_actor_id = note
102       .attributed_to()
103       .context(location_info!())?
104       .clone()
105       .single_xsd_any_uri()
106       .context(location_info!())?;
107
108     let creator =
109       get_or_fetch_and_upsert_person(&creator_actor_id, context, request_counter).await?;
110     let recipient_actor_id = note
111       .to()
112       .context(location_info!())?
113       .clone()
114       .single_xsd_any_uri()
115       .context(location_info!())?;
116     let recipient =
117       get_or_fetch_and_upsert_person(&recipient_actor_id, context, request_counter).await?;
118     let ap_id = Some(check_object_domain(note, expected_domain, false)?);
119
120     let content = get_source_markdown_value(note)?.context(location_info!())?;
121
122     Ok(PrivateMessageForm {
123       creator_id: creator.id,
124       recipient_id: recipient.id,
125       content,
126       published: note.published().map(|u| u.to_owned().naive_local()),
127       updated: note.updated().map(|u| u.to_owned().naive_local()),
128       deleted: None,
129       read: None,
130       ap_id,
131       local: Some(false),
132     })
133   }
134 }