diff options
Diffstat (limited to 'fedi/live_federation/activities/create_post.rs')
-rw-r--r-- | fedi/live_federation/activities/create_post.rs | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/fedi/live_federation/activities/create_post.rs b/fedi/live_federation/activities/create_post.rs new file mode 100644 index 0000000..66928a6 --- /dev/null +++ b/fedi/live_federation/activities/create_post.rs | |||
@@ -0,0 +1,69 @@ | |||
1 | use crate::{ | ||
2 | database::DatabaseHandle, | ||
3 | error::Error, | ||
4 | objects::{person::DbUser, post::Note}, | ||
5 | utils::generate_object_id, | ||
6 | DbPost, | ||
7 | }; | ||
8 | use activitypub_federation::{ | ||
9 | activity_queue::send_activity, | ||
10 | config::Data, | ||
11 | fetch::object_id::ObjectId, | ||
12 | kinds::activity::CreateType, | ||
13 | protocol::{context::WithContext, helpers::deserialize_one_or_many}, | ||
14 | traits::{ActivityHandler, Object}, | ||
15 | }; | ||
16 | use serde::{Deserialize, Serialize}; | ||
17 | use url::Url; | ||
18 | |||
19 | #[derive(Deserialize, Serialize, Debug)] | ||
20 | #[serde(rename_all = "camelCase")] | ||
21 | pub struct CreatePost { | ||
22 | pub(crate) actor: ObjectId<DbUser>, | ||
23 | #[serde(deserialize_with = "deserialize_one_or_many")] | ||
24 | pub(crate) to: Vec<Url>, | ||
25 | pub(crate) object: Note, | ||
26 | #[serde(rename = "type")] | ||
27 | pub(crate) kind: CreateType, | ||
28 | pub(crate) id: Url, | ||
29 | } | ||
30 | |||
31 | impl CreatePost { | ||
32 | pub async fn send(note: Note, inbox: Url, data: &Data<DatabaseHandle>) -> Result<(), Error> { | ||
33 | print!("Sending reply to {}", ¬e.attributed_to); | ||
34 | let create = CreatePost { | ||
35 | actor: note.attributed_to.clone(), | ||
36 | to: note.to.clone(), | ||
37 | object: note, | ||
38 | kind: CreateType::Create, | ||
39 | id: generate_object_id(data.domain())?, | ||
40 | }; | ||
41 | let create_with_context = WithContext::new_default(create); | ||
42 | send_activity(create_with_context, &data.local_user(), vec![inbox], data).await?; | ||
43 | Ok(()) | ||
44 | } | ||
45 | } | ||
46 | |||
47 | #[async_trait::async_trait] | ||
48 | impl ActivityHandler for CreatePost { | ||
49 | type DataType = DatabaseHandle; | ||
50 | type Error = crate::error::Error; | ||
51 | |||
52 | fn id(&self) -> &Url { | ||
53 | &self.id | ||
54 | } | ||
55 | |||
56 | fn actor(&self) -> &Url { | ||
57 | self.actor.inner() | ||
58 | } | ||
59 | |||
60 | async fn verify(&self, data: &Data<Self::DataType>) -> Result<(), Self::Error> { | ||
61 | DbPost::verify(&self.object, &self.id, data).await?; | ||
62 | Ok(()) | ||
63 | } | ||
64 | |||
65 | async fn receive(self, data: &Data<Self::DataType>) -> Result<(), Self::Error> { | ||
66 | DbPost::from_json(self.object, data).await?; | ||
67 | Ok(()) | ||
68 | } | ||
69 | } | ||