diff options
author | clarkzjw <[email protected]> | 2023-07-26 12:37:38 -0700 |
---|---|---|
committer | clarkzjw <[email protected]> | 2023-07-26 12:37:38 -0700 |
commit | af2f09ea4cbb97d3ee91e30bb58e85508989d63a (patch) | |
tree | 671ead9c450a0abf71efc00ba1f2966ae6e60e02 /fedi/live_federation/objects/post.rs | |
parent | f847de64eb8f724fa512801b43a26522afff61ae (diff) | |
download | photo-af2f09ea4cbb97d3ee91e30bb58e85508989d63a.tar.gz |
add example from https://github.com/LemmyNet/activitypub-federation-rust
Diffstat (limited to 'fedi/live_federation/objects/post.rs')
-rw-r--r-- | fedi/live_federation/objects/post.rs | 104 |
1 files changed, 104 insertions, 0 deletions
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 | } | ||