1 use crate::{data::Data, signatures::PublicKey};
2 use activitystreams::chrono::NaiveDateTime;
3 pub use lemmy_apub_lib_derive::*;
4 use lemmy_utils::LemmyError;
7 #[async_trait::async_trait(?Send)]
8 pub trait ActivityHandler {
12 data: &Data<Self::DataType>,
13 request_counter: &mut i32,
14 ) -> Result<(), LemmyError>;
18 data: &Data<Self::DataType>,
19 request_counter: &mut i32,
20 ) -> Result<(), LemmyError>;
23 #[async_trait::async_trait(?Send)]
24 pub trait ApubObject {
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(
35 data: &Self::DataType,
36 ) -> Result<Option<Self>, LemmyError>
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>;
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>;
47 apub: &Self::ApubType,
48 expected_domain: &Url,
49 data: &Self::DataType,
50 request_counter: &mut i32,
51 ) -> Result<(), LemmyError>;
53 /// Converts an object from ActivityPub type to Lemmy internal type.
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
61 data: &Self::DataType,
62 request_counter: &mut i32,
63 ) -> Result<Self, LemmyError>
68 /// Common methods provided by ActivityPub actors (community and person). Not all methods are
69 /// implemented by all actors.
71 fn actor_id(&self) -> Url;
73 fn public_key(&self) -> String;
74 fn private_key(&self) -> Option<String>;
76 fn inbox_url(&self) -> Url;
78 fn shared_inbox_url(&self) -> Option<Url>;
80 fn shared_inbox_or_inbox_url(&self) -> Url {
81 self.shared_inbox_url().unwrap_or_else(|| self.inbox_url())
84 fn get_public_key(&self) -> Result<PublicKey, LemmyError> {
86 id: format!("{}#main-key", self.actor_id()),
87 owner: Box::new(self.actor_id()),
88 public_key_pem: self.public_key(),