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