From af2f09ea4cbb97d3ee91e30bb58e85508989d63a Mon Sep 17 00:00:00 2001 From: clarkzjw Date: Wed, 26 Jul 2023 12:37:38 -0700 Subject: add example from https://github.com/LemmyNet/activitypub-federation-rust --- fedi/live_federation/activities/create_post.rs | 69 ++++++++++++++++++++++++++ fedi/live_federation/activities/mod.rs | 1 + 2 files changed, 70 insertions(+) create mode 100644 fedi/live_federation/activities/create_post.rs create mode 100644 fedi/live_federation/activities/mod.rs (limited to 'fedi/live_federation/activities') 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 @@ +use crate::{ + database::DatabaseHandle, + error::Error, + objects::{person::DbUser, post::Note}, + utils::generate_object_id, + DbPost, +}; +use activitypub_federation::{ + activity_queue::send_activity, + config::Data, + fetch::object_id::ObjectId, + kinds::activity::CreateType, + protocol::{context::WithContext, helpers::deserialize_one_or_many}, + traits::{ActivityHandler, Object}, +}; +use serde::{Deserialize, Serialize}; +use url::Url; + +#[derive(Deserialize, Serialize, Debug)] +#[serde(rename_all = "camelCase")] +pub struct CreatePost { + pub(crate) actor: ObjectId, + #[serde(deserialize_with = "deserialize_one_or_many")] + pub(crate) to: Vec, + pub(crate) object: Note, + #[serde(rename = "type")] + pub(crate) kind: CreateType, + pub(crate) id: Url, +} + +impl CreatePost { + pub async fn send(note: Note, inbox: Url, data: &Data) -> Result<(), Error> { + print!("Sending reply to {}", ¬e.attributed_to); + let create = CreatePost { + actor: note.attributed_to.clone(), + to: note.to.clone(), + object: note, + kind: CreateType::Create, + id: generate_object_id(data.domain())?, + }; + let create_with_context = WithContext::new_default(create); + send_activity(create_with_context, &data.local_user(), vec![inbox], data).await?; + Ok(()) + } +} + +#[async_trait::async_trait] +impl ActivityHandler for CreatePost { + type DataType = DatabaseHandle; + type Error = crate::error::Error; + + fn id(&self) -> &Url { + &self.id + } + + fn actor(&self) -> &Url { + self.actor.inner() + } + + async fn verify(&self, data: &Data) -> Result<(), Self::Error> { + DbPost::verify(&self.object, &self.id, data).await?; + Ok(()) + } + + async fn receive(self, data: &Data) -> Result<(), Self::Error> { + DbPost::from_json(self.object, data).await?; + Ok(()) + } +} diff --git a/fedi/live_federation/activities/mod.rs b/fedi/live_federation/activities/mod.rs new file mode 100644 index 0000000..7e15ee0 --- /dev/null +++ b/fedi/live_federation/activities/mod.rs @@ -0,0 +1 @@ +pub mod create_post; -- cgit v1.2.3