]> Untitled Git - lemmy.git/blob - crates/apub_lib/src/traits.rs
Merge pull request #1926 from LemmyNet/replace-activitystreams-lib
[lemmy.git] / crates / apub_lib / src / traits.rs
1 use crate::{data::Data, signatures::PublicKey};
2 use activitystreams::chrono::NaiveDateTime;
3 use anyhow::Context;
4 pub use lemmy_apub_lib_derive::*;
5 use lemmy_utils::{location_info, LemmyError};
6 use url::Url;
7
8 #[async_trait::async_trait(?Send)]
9 pub trait ActivityHandler {
10   type DataType;
11   async fn verify(
12     &self,
13     data: &Data<Self::DataType>,
14     request_counter: &mut i32,
15   ) -> Result<(), LemmyError>;
16
17   async fn receive(
18     self,
19     data: &Data<Self::DataType>,
20     request_counter: &mut i32,
21   ) -> Result<(), LemmyError>;
22 }
23
24 #[async_trait::async_trait(?Send)]
25 pub trait ApubObject {
26   type DataType;
27   type ApubType;
28   type TombstoneType;
29
30   /// If this object should be refetched after a certain interval, it should return the last refresh
31   /// time here. This is mainly used to update remote actors.
32   fn last_refreshed_at(&self) -> Option<NaiveDateTime>;
33   /// Try to read the object with given ID from local database. Returns Ok(None) if it doesn't exist.
34   async fn read_from_apub_id(
35     object_id: Url,
36     data: &Self::DataType,
37   ) -> Result<Option<Self>, LemmyError>
38   where
39     Self: Sized;
40   /// Marks the object as deleted in local db. Called when a tombstone is received.
41   async fn delete(self, data: &Self::DataType) -> Result<(), LemmyError>;
42
43   /// Trait for converting an object or actor into the respective ActivityPub type.
44   async fn into_apub(self, data: &Self::DataType) -> Result<Self::ApubType, LemmyError>;
45   fn to_tombstone(&self) -> Result<Self::TombstoneType, LemmyError>;
46
47   async fn verify(
48     apub: &Self::ApubType,
49     expected_domain: &Url,
50     data: &Self::DataType,
51     request_counter: &mut i32,
52   ) -> Result<(), LemmyError>;
53
54   /// Converts an object from ActivityPub type to Lemmy internal type.
55   ///
56   /// * `apub` The object to read from
57   /// * `context` LemmyContext which holds DB pool, HTTP client etc
58   /// * `expected_domain` Domain where the object was received from. None in case of mod action.
59   /// * `mod_action_allowed` True if the object can be a mod activity, ignore `expected_domain` in this case
60   async fn from_apub(
61     apub: Self::ApubType,
62     data: &Self::DataType,
63     request_counter: &mut i32,
64   ) -> Result<Self, LemmyError>
65   where
66     Self: Sized;
67 }
68
69 /// Common methods provided by ActivityPub actors (community and person). Not all methods are
70 /// implemented by all actors.
71 pub trait ActorType {
72   fn actor_id(&self) -> Url;
73
74   // TODO: this should not be an option (needs db migration in lemmy)
75   fn public_key(&self) -> Option<String>;
76   fn private_key(&self) -> Option<String>;
77
78   fn inbox_url(&self) -> Url;
79
80   fn shared_inbox_url(&self) -> Option<Url>;
81
82   fn shared_inbox_or_inbox_url(&self) -> Url {
83     self.shared_inbox_url().unwrap_or_else(|| self.inbox_url())
84   }
85
86   fn get_public_key(&self) -> Result<PublicKey, LemmyError> {
87     Ok(PublicKey {
88       id: format!("{}#main-key", self.actor_id()),
89       owner: Box::new(self.actor_id()),
90       public_key_pem: self.public_key().context(location_info!())?,
91     })
92   }
93 }