diff options
Diffstat (limited to 'fedi/live_federation/objects')
-rw-r--r-- | fedi/live_federation/objects/mod.rs | 2 | ||||
-rw-r--r-- | fedi/live_federation/objects/person.rs | 140 | ||||
-rw-r--r-- | fedi/live_federation/objects/post.rs | 104 |
3 files changed, 246 insertions, 0 deletions
diff --git a/fedi/live_federation/objects/mod.rs b/fedi/live_federation/objects/mod.rs new file mode 100644 index 0000000..b5239ab --- /dev/null +++ b/fedi/live_federation/objects/mod.rs | |||
@@ -0,0 +1,2 @@ | |||
1 | pub mod person; | ||
2 | pub mod post; | ||
diff --git a/fedi/live_federation/objects/person.rs b/fedi/live_federation/objects/person.rs new file mode 100644 index 0000000..d9439ea --- /dev/null +++ b/fedi/live_federation/objects/person.rs | |||
@@ -0,0 +1,140 @@ | |||
1 | use crate::{activities::create_post::CreatePost, database::DatabaseHandle, error::Error}; | ||
2 | use activitypub_federation::{ | ||
3 | config::Data, | ||
4 | fetch::object_id::ObjectId, | ||
5 | http_signatures::generate_actor_keypair, | ||
6 | kinds::actor::PersonType, | ||
7 | protocol::{public_key::PublicKey, verification::verify_domains_match}, | ||
8 | traits::{ActivityHandler, Actor, Object}, | ||
9 | }; | ||
10 | use chrono::{DateTime, Utc}; | ||
11 | use serde::{Deserialize, Serialize}; | ||
12 | use std::fmt::Debug; | ||
13 | use url::Url; | ||
14 | |||
15 | #[derive(Debug, Clone)] | ||
16 | pub struct DbUser { | ||
17 | pub name: String, | ||
18 | pub ap_id: ObjectId<DbUser>, | ||
19 | pub inbox: Url, | ||
20 | // exists for all users (necessary to verify http signatures) | ||
21 | pub public_key: String, | ||
22 | // exists only for local users | ||
23 | pub private_key: Option<String>, | ||
24 | last_refreshed_at: DateTime<Utc>, | ||
25 | pub followers: Vec<Url>, | ||
26 | pub local: bool, | ||
27 | } | ||
28 | |||
29 | /// List of all activities which this actor can receive. | ||
30 | #[derive(Deserialize, Serialize, Debug)] | ||
31 | #[serde(untagged)] | ||
32 | #[enum_delegate::implement(ActivityHandler)] | ||
33 | pub enum PersonAcceptedActivities { | ||
34 | CreateNote(CreatePost), | ||
35 | } | ||
36 | |||
37 | impl DbUser { | ||
38 | pub fn new(hostname: &str, name: &str) -> Result<DbUser, Error> { | ||
39 | let ap_id = Url::parse(&format!("https://{}/{}", hostname, &name))?.into(); | ||
40 | let inbox = Url::parse(&format!("https://{}/{}/inbox", hostname, &name))?; | ||
41 | let keypair = generate_actor_keypair()?; | ||
42 | Ok(DbUser { | ||
43 | name: name.to_string(), | ||
44 | ap_id, | ||
45 | inbox, | ||
46 | public_key: keypair.public_key, | ||
47 | private_key: Some(keypair.private_key), | ||
48 | last_refreshed_at: Utc::now(), | ||
49 | followers: vec![], | ||
50 | local: true, | ||
51 | }) | ||
52 | } | ||
53 | } | ||
54 | |||
55 | #[derive(Clone, Debug, Deserialize, Serialize)] | ||
56 | #[serde(rename_all = "camelCase")] | ||
57 | pub struct Person { | ||
58 | #[serde(rename = "type")] | ||
59 | kind: PersonType, | ||
60 | preferred_username: String, | ||
61 | id: ObjectId<DbUser>, | ||
62 | inbox: Url, | ||
63 | public_key: PublicKey, | ||
64 | } | ||
65 | |||
66 | #[async_trait::async_trait] | ||
67 | impl Object for DbUser { | ||
68 | type DataType = DatabaseHandle; | ||
69 | type Kind = Person; | ||
70 | type Error = Error; | ||
71 | |||
72 | fn last_refreshed_at(&self) -> Option<DateTime<Utc>> { | ||
73 | Some(self.last_refreshed_at) | ||
74 | } | ||
75 | |||
76 | async fn read_from_id( | ||
77 | object_id: Url, | ||
78 | data: &Data<Self::DataType>, | ||
79 | ) -> Result<Option<Self>, Self::Error> { | ||
80 | let users = data.users.lock().unwrap(); | ||
81 | let res = users | ||
82 | .clone() | ||
83 | .into_iter() | ||
84 | .find(|u| u.ap_id.inner() == &object_id); | ||
85 | Ok(res) | ||
86 | } | ||
87 | |||
88 | async fn into_json(self, _data: &Data<Self::DataType>) -> Result<Self::Kind, Self::Error> { | ||
89 | Ok(Person { | ||
90 | preferred_username: self.name.clone(), | ||
91 | kind: Default::default(), | ||
92 | id: self.ap_id.clone(), | ||
93 | inbox: self.inbox.clone(), | ||
94 | public_key: self.public_key(), | ||
95 | }) | ||
96 | } | ||
97 | |||
98 | async fn verify( | ||
99 | json: &Self::Kind, | ||
100 | expected_domain: &Url, | ||
101 | _data: &Data<Self::DataType>, | ||
102 | ) -> Result<(), Self::Error> { | ||
103 | verify_domains_match(json.id.inner(), expected_domain)?; | ||
104 | Ok(()) | ||
105 | } | ||
106 | |||
107 | async fn from_json( | ||
108 | json: Self::Kind, | ||
109 | _data: &Data<Self::DataType>, | ||
110 | ) -> Result<Self, Self::Error> { | ||
111 | Ok(DbUser { | ||
112 | name: json.preferred_username, | ||
113 | ap_id: json.id, | ||
114 | inbox: json.inbox, | ||
115 | public_key: json.public_key.public_key_pem, | ||
116 | private_key: None, | ||
117 | last_refreshed_at: Utc::now(), | ||
118 | followers: vec![], | ||
119 | local: false, | ||
120 | }) | ||
121 | } | ||
122 | } | ||
123 | |||
124 | impl Actor for DbUser { | ||
125 | fn id(&self) -> Url { | ||
126 | self.ap_id.inner().clone() | ||
127 | } | ||
128 | |||
129 | fn public_key_pem(&self) -> &str { | ||
130 | &self.public_key | ||
131 | } | ||
132 | |||
133 | fn private_key_pem(&self) -> Option<String> { | ||
134 | self.private_key.clone() | ||
135 | } | ||
136 | |||
137 | fn inbox(&self) -> Url { | ||
138 | self.inbox.clone() | ||
139 | } | ||
140 | } | ||
diff --git a/fedi/live_federation/objects/post.rs b/fedi/live_federation/objects/post.rs new file mode 100644 index 0000000..9a08b9d --- /dev/null +++ b/fedi/live_federation/objects/post.rs | |||
@@ -0,0 +1,104 @@ | |||
1 | use crate::{ | ||
2 | activities::create_post::CreatePost, | ||
3 | database::DatabaseHandle, | ||
4 | error::Error, | ||
5 | generate_object_id, | ||
6 | objects::person::DbUser, | ||
7 | }; | ||
8 | use activitypub_federation::{ | ||
9 | config::Data, | ||
10 | fetch::object_id::ObjectId, | ||
11 | kinds::{object::NoteType, public}, | ||
12 | protocol::{helpers::deserialize_one_or_many, verification::verify_domains_match}, | ||
13 | traits::{Actor, Object}, | ||
14 | }; | ||
15 | use activitystreams_kinds::link::MentionType; | ||
16 | use serde::{Deserialize, Serialize}; | ||
17 | use url::Url; | ||
18 | |||
19 | #[derive(Clone, Debug)] | ||
20 | pub struct DbPost { | ||
21 | pub text: String, | ||
22 | pub ap_id: ObjectId<DbPost>, | ||
23 | pub creator: ObjectId<DbUser>, | ||
24 | pub local: bool, | ||
25 | } | ||
26 | |||
27 | #[derive(Deserialize, Serialize, Debug)] | ||
28 | #[serde(rename_all = "camelCase")] | ||
29 | pub struct Note { | ||
30 | #[serde(rename = "type")] | ||
31 | kind: NoteType, | ||
32 | id: ObjectId<DbPost>, | ||
33 | pub(crate) attributed_to: ObjectId<DbUser>, | ||
34 | #[serde(deserialize_with = "deserialize_one_or_many")] | ||
35 | pub(crate) to: Vec<Url>, | ||
36 | content: String, | ||
37 | in_reply_to: Option<ObjectId<DbPost>>, | ||
38 | tag: Vec<Mention>, | ||
39 | } | ||
40 | |||
41 | #[derive(Clone, Debug, Deserialize, Serialize)] | ||
42 | pub struct Mention { | ||
43 | pub href: Url, | ||
44 | #[serde(rename = "type")] | ||
45 | pub kind: MentionType, | ||
46 | } | ||
47 | |||
48 | #[async_trait::async_trait] | ||
49 | impl Object for DbPost { | ||
50 | type DataType = DatabaseHandle; | ||
51 | type Kind = Note; | ||
52 | type Error = Error; | ||
53 | |||
54 | async fn read_from_id( | ||
55 | _object_id: Url, | ||
56 | _data: &Data<Self::DataType>, | ||
57 | ) -> Result<Option<Self>, Self::Error> { | ||
58 | Ok(None) | ||
59 | } | ||
60 | |||
61 | async fn into_json(self, _data: &Data<Self::DataType>) -> Result<Self::Kind, Self::Error> { | ||
62 | unimplemented!() | ||
63 | } | ||
64 | |||
65 | async fn verify( | ||
66 | json: &Self::Kind, | ||
67 | expected_domain: &Url, | ||
68 | _data: &Data<Self::DataType>, | ||
69 | ) -> Result<(), Self::Error> { | ||
70 | verify_domains_match(json.id.inner(), expected_domain)?; | ||
71 | Ok(()) | ||
72 | } | ||
73 | |||
74 | async fn from_json(json: Self::Kind, data: &Data<Self::DataType>) -> Result<Self, Self::Error> { | ||
75 | println!( | ||
76 | "Received post with content {} and id {}", | ||
77 | &json.content, &json.id | ||
78 | ); | ||
79 | let creator = json.attributed_to.dereference(data).await?; | ||
80 | let post = DbPost { | ||
81 | text: json.content, | ||
82 | ap_id: json.id.clone(), | ||
83 | creator: json.attributed_to.clone(), | ||
84 | local: false, | ||
85 | }; | ||
86 | |||
87 | let mention = Mention { | ||
88 | href: creator.ap_id.clone().into_inner(), | ||
89 | kind: Default::default(), | ||
90 | }; | ||
91 | let note = Note { | ||
92 | kind: Default::default(), | ||
93 | id: generate_object_id(data.domain())?.into(), | ||
94 | attributed_to: data.local_user().ap_id, | ||
95 | to: vec![public()], | ||
96 | content: format!("Hello {}", creator.name), | ||
97 | in_reply_to: Some(json.id.clone()), | ||
98 | tag: vec![mention], | ||
99 | }; | ||
100 | CreatePost::send(note, creator.shared_inbox_or_inbox(), data).await?; | ||
101 | |||
102 | Ok(post) | ||
103 | } | ||
104 | } | ||