1 use crate::{data::Data, signatures::PublicKey};
2 use activitystreams::chrono::NaiveDateTime;
4 pub use lemmy_apub_lib_derive::*;
5 use lemmy_utils::{location_info, LemmyError};
8 #[async_trait::async_trait(?Send)]
9 pub trait ActivityHandler {
13 data: &Data<Self::DataType>,
14 request_counter: &mut i32,
15 ) -> Result<(), LemmyError>;
19 data: &Data<Self::DataType>,
20 request_counter: &mut i32,
21 ) -> Result<(), LemmyError>;
24 #[async_trait::async_trait(?Send)]
25 pub trait ApubObject {
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(
36 data: &Self::DataType,
37 ) -> Result<Option<Self>, LemmyError>
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>;
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>;
48 apub: &Self::ApubType,
49 expected_domain: &Url,
50 data: &Self::DataType,
51 request_counter: &mut i32,
52 ) -> Result<(), LemmyError>;
54 /// Converts an object from ActivityPub type to Lemmy internal type.
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
62 data: &Self::DataType,
63 request_counter: &mut i32,
64 ) -> Result<Self, LemmyError>
69 /// Common methods provided by ActivityPub actors (community and person). Not all methods are
70 /// implemented by all actors.
72 fn actor_id(&self) -> Url;
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>;
78 fn inbox_url(&self) -> Url;
80 fn shared_inbox_url(&self) -> Option<Url>;
82 fn shared_inbox_or_inbox_url(&self) -> Url {
83 self.shared_inbox_url().unwrap_or_else(|| self.inbox_url())
86 fn get_public_key(&self) -> Result<PublicKey, LemmyError> {
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!())?,