2 activities::generate_activity_id,
3 activity_queue::send_activity,
5 check_is_apub_id_valid,
7 fetcher::get_or_fetch_and_upsert_user,
14 use activitystreams::{
16 kind::{CreateType, DeleteType, UndoType, UpdateType},
22 object::{kind::NoteType, Note, Tombstone},
27 private_message::{PrivateMessage, PrivateMessageForm},
32 use lemmy_structs::blocking;
33 use lemmy_utils::{location_info, utils::convert_datetime, LemmyError};
34 use lemmy_websocket::LemmyContext;
37 #[async_trait::async_trait(?Send)]
38 impl ToApub for PrivateMessage {
41 async fn to_apub(&self, pool: &DbPool) -> Result<Note, LemmyError> {
42 let mut private_message = Note::new();
44 let creator_id = self.creator_id;
45 let creator = blocking(pool, move |conn| User_::read(conn, creator_id)).await??;
47 let recipient_id = self.recipient_id;
48 let recipient = blocking(pool, move |conn| User_::read(conn, recipient_id)).await??;
51 .set_context(activitystreams::context())
52 .set_id(Url::parse(&self.ap_id.to_owned())?)
53 .set_published(convert_datetime(self.published))
54 .set_content(self.content.to_owned())
55 .set_to(recipient.actor_id)
56 .set_attributed_to(creator.actor_id);
58 if let Some(u) = self.updated {
59 private_message.set_updated(convert_datetime(u));
65 fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
66 create_tombstone(self.deleted, &self.ap_id, self.updated, NoteType::Note)
70 #[async_trait::async_trait(?Send)]
71 impl FromApub for PrivateMessageForm {
74 /// Parse an ActivityPub note received from another instance into a Lemmy Private message
77 context: &LemmyContext,
78 expected_domain: Option<Url>,
79 ) -> Result<PrivateMessageForm, LemmyError> {
80 let creator_actor_id = note
82 .context(location_info!())?
85 .context(location_info!())?;
87 let creator = get_or_fetch_and_upsert_user(&creator_actor_id, context).await?;
88 let recipient_actor_id = note
90 .context(location_info!())?
93 .context(location_info!())?;
94 let recipient = get_or_fetch_and_upsert_user(&recipient_actor_id, context).await?;
95 let ap_id = note.id_unchecked().context(location_info!())?.to_string();
96 check_is_apub_id_valid(&Url::parse(&ap_id)?)?;
98 Ok(PrivateMessageForm {
99 creator_id: creator.id,
100 recipient_id: recipient.id,
103 .context(location_info!())?
104 .as_single_xsd_string()
105 .context(location_info!())?
107 published: note.published().map(|u| u.to_owned().naive_local()),
108 updated: note.updated().map(|u| u.to_owned().naive_local()),
111 ap_id: Some(check_actor_domain(note, expected_domain)?),
117 #[async_trait::async_trait(?Send)]
118 impl ApubObjectType for PrivateMessage {
119 /// Send out information about a newly created private message
120 async fn send_create(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
121 let note = self.to_apub(context.pool()).await?;
123 let recipient_id = self.recipient_id;
124 let recipient = blocking(context.pool(), move |conn| User_::read(conn, recipient_id)).await??;
126 let mut create = Create::new(creator.actor_id.to_owned(), note.into_any_base()?);
127 let to = recipient.get_inbox_url()?;
129 .set_context(activitystreams::context())
130 .set_id(generate_activity_id(CreateType::Create)?)
133 insert_activity(creator.id, create.clone(), true, context.pool()).await?;
135 send_activity(context.activity_queue(), create, creator, vec![to])?;
139 /// Send out information about an edited post, to the followers of the community.
140 async fn send_update(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
141 let note = self.to_apub(context.pool()).await?;
143 let recipient_id = self.recipient_id;
144 let recipient = blocking(context.pool(), move |conn| User_::read(conn, recipient_id)).await??;
146 let mut update = Update::new(creator.actor_id.to_owned(), note.into_any_base()?);
147 let to = recipient.get_inbox_url()?;
149 .set_context(activitystreams::context())
150 .set_id(generate_activity_id(UpdateType::Update)?)
153 insert_activity(creator.id, update.clone(), true, context.pool()).await?;
155 send_activity(context.activity_queue(), update, creator, vec![to])?;
159 async fn send_delete(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
160 let note = self.to_apub(context.pool()).await?;
162 let recipient_id = self.recipient_id;
163 let recipient = blocking(context.pool(), move |conn| User_::read(conn, recipient_id)).await??;
165 let mut delete = Delete::new(creator.actor_id.to_owned(), note.into_any_base()?);
166 let to = recipient.get_inbox_url()?;
168 .set_context(activitystreams::context())
169 .set_id(generate_activity_id(DeleteType::Delete)?)
172 insert_activity(creator.id, delete.clone(), true, context.pool()).await?;
174 send_activity(context.activity_queue(), delete, creator, vec![to])?;
178 async fn send_undo_delete(
181 context: &LemmyContext,
182 ) -> Result<(), LemmyError> {
183 let note = self.to_apub(context.pool()).await?;
185 let recipient_id = self.recipient_id;
186 let recipient = blocking(context.pool(), move |conn| User_::read(conn, recipient_id)).await??;
188 let mut delete = Delete::new(creator.actor_id.to_owned(), note.into_any_base()?);
189 let to = recipient.get_inbox_url()?;
191 .set_context(activitystreams::context())
192 .set_id(generate_activity_id(DeleteType::Delete)?)
195 // Undo that fake activity
196 let mut undo = Undo::new(creator.actor_id.to_owned(), delete.into_any_base()?);
198 .set_context(activitystreams::context())
199 .set_id(generate_activity_id(UndoType::Undo)?)
202 insert_activity(creator.id, undo.clone(), true, context.pool()).await?;
204 send_activity(context.activity_queue(), undo, creator, vec![to])?;
208 async fn send_remove(&self, _mod_: &User_, _context: &LemmyContext) -> Result<(), LemmyError> {
212 async fn send_undo_remove(
215 _context: &LemmyContext,
216 ) -> Result<(), LemmyError> {