activitypub-federation-rust/examples/local_federation/activities/create_post.rs

63 lines
1.5 KiB
Rust
Raw Normal View History

2022-06-02 16:47:12 +05:30
use crate::{
instance::DatabaseHandle,
objects::{person::DbUser, post::Note},
DbPost,
2022-06-02 16:47:12 +05:30
};
use activitypub_federation::{
config::Data,
2023-03-06 06:47:34 +05:30
fetch::object_id::ObjectId,
kinds::activity::CreateType,
protocol::helpers::deserialize_one_or_many,
2023-03-17 02:11:29 +05:30
traits::{ActivityHandler, Object},
2022-06-02 16:47:12 +05:30
};
use serde::{Deserialize, Serialize};
use url::Url;
#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
2023-03-08 03:31:36 +05:30
pub struct CreatePost {
pub(crate) actor: ObjectId<DbUser>,
2022-06-02 16:47:12 +05:30
#[serde(deserialize_with = "deserialize_one_or_many")]
pub(crate) to: Vec<Url>,
pub(crate) object: Note,
#[serde(rename = "type")]
pub(crate) kind: CreateType,
pub(crate) id: Url,
}
2023-03-08 03:31:36 +05:30
impl CreatePost {
pub fn new(note: Note, id: Url) -> CreatePost {
CreatePost {
2022-06-02 16:47:12 +05:30
actor: note.attributed_to.clone(),
to: note.to.clone(),
object: note,
kind: CreateType::Create,
id,
}
}
}
#[async_trait::async_trait]
2023-03-08 03:31:36 +05:30
impl ActivityHandler for CreatePost {
type DataType = DatabaseHandle;
2022-06-02 16:47:12 +05:30
type Error = crate::error::Error;
fn id(&self) -> &Url {
&self.id
}
fn actor(&self) -> &Url {
self.actor.inner()
}
async fn verify(&self, data: &Data<Self::DataType>) -> Result<(), Self::Error> {
DbPost::verify(&self.object, &self.id, data).await?;
Ok(())
}
async fn receive(self, data: &Data<Self::DataType>) -> Result<(), Self::Error> {
2023-03-17 02:11:29 +05:30
DbPost::from_json(self.object, data).await?;
2022-06-02 16:47:12 +05:30
Ok(())
}
}