]> Untitled Git - lemmy.git/blob - crates/apub/src/objects/mod.rs
Rewrite delete activities (#1699)
[lemmy.git] / crates / apub / src / objects / mod.rs
1 use crate::fetcher::person::get_or_fetch_and_upsert_person;
2 use activitystreams::{
3   base::BaseExt,
4   object::{kind::ImageType, Tombstone, TombstoneExt},
5 };
6 use anyhow::anyhow;
7 use chrono::NaiveDateTime;
8 use lemmy_apub_lib::values::MediaTypeMarkdown;
9 use lemmy_db_queries::DbPool;
10 use lemmy_utils::{utils::convert_datetime, LemmyError};
11 use lemmy_websocket::LemmyContext;
12 use url::Url;
13
14 pub(crate) mod comment;
15 pub(crate) mod community;
16 pub(crate) mod person;
17 pub(crate) mod post;
18 pub(crate) mod private_message;
19
20 /// Trait for converting an object or actor into the respective ActivityPub type.
21 #[async_trait::async_trait(?Send)]
22 pub(crate) trait ToApub {
23   type ApubType;
24   async fn to_apub(&self, pool: &DbPool) -> Result<Self::ApubType, LemmyError>;
25   fn to_tombstone(&self) -> Result<Tombstone, LemmyError>;
26 }
27
28 #[async_trait::async_trait(?Send)]
29 pub(crate) trait FromApub {
30   type ApubType;
31   /// Converts an object from ActivityPub type to Lemmy internal type.
32   ///
33   /// * `apub` The object to read from
34   /// * `context` LemmyContext which holds DB pool, HTTP client etc
35   /// * `expected_domain` Domain where the object was received from. None in case of mod action.
36   /// * `mod_action_allowed` True if the object can be a mod activity, ignore `expected_domain` in this case
37   async fn from_apub(
38     apub: &Self::ApubType,
39     context: &LemmyContext,
40     expected_domain: &Url,
41     request_counter: &mut i32,
42   ) -> Result<Self, LemmyError>
43   where
44     Self: Sized;
45 }
46
47 #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
48 #[serde(rename_all = "camelCase")]
49 pub struct Source {
50   content: String,
51   media_type: MediaTypeMarkdown,
52 }
53
54 #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
55 #[serde(rename_all = "camelCase")]
56 pub struct ImageObject {
57   #[serde(rename = "type")]
58   kind: ImageType,
59   url: Url,
60 }
61
62 /// Updated is actually the deletion time
63 fn create_tombstone<T>(
64   deleted: bool,
65   object_id: Url,
66   updated: Option<NaiveDateTime>,
67   former_type: T,
68 ) -> Result<Tombstone, LemmyError>
69 where
70   T: ToString,
71 {
72   if deleted {
73     if let Some(updated) = updated {
74       let mut tombstone = Tombstone::new();
75       tombstone.set_id(object_id);
76       tombstone.set_former_type(former_type.to_string());
77       tombstone.set_deleted(convert_datetime(updated));
78       Ok(tombstone)
79     } else {
80       Err(anyhow!("Cant convert to tombstone because updated time was None.").into())
81     }
82   } else {
83     Err(anyhow!("Cant convert object to tombstone if it wasnt deleted").into())
84   }
85 }