2023-03-08 03:31:36 +05:30
|
|
|
use crate::{
|
|
|
|
database::DatabaseHandle,
|
|
|
|
error::Error,
|
|
|
|
objects::{person::DbUser, post::Note},
|
|
|
|
utils::generate_object_id,
|
|
|
|
DbPost,
|
|
|
|
};
|
|
|
|
use activitypub_federation::{
|
|
|
|
activity_queue::send_activity,
|
2023-03-16 06:41:48 +05:30
|
|
|
config::Data,
|
2023-03-08 03:31:36 +05:30
|
|
|
fetch::object_id::ObjectId,
|
|
|
|
kinds::activity::CreateType,
|
|
|
|
protocol::{context::WithContext, helpers::deserialize_one_or_many},
|
|
|
|
traits::{ActivityHandler, ApubObject},
|
|
|
|
};
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use url::Url;
|
|
|
|
|
|
|
|
#[derive(Deserialize, Serialize, Debug)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct CreatePost {
|
|
|
|
pub(crate) actor: ObjectId<DbUser>,
|
|
|
|
#[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,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CreatePost {
|
2023-03-16 06:41:48 +05:30
|
|
|
pub async fn send(note: Note, inbox: Url, data: &Data<DatabaseHandle>) -> Result<(), Error> {
|
2023-03-08 03:31:36 +05:30
|
|
|
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);
|
2023-03-16 06:41:48 +05:30
|
|
|
send_activity(create_with_context, &data.local_user(), vec![inbox], data).await?;
|
2023-03-08 03:31:36 +05:30
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2023-03-16 06:41:48 +05:30
|
|
|
async fn verify(&self, data: &Data<Self::DataType>) -> Result<(), Self::Error> {
|
2023-03-10 02:39:44 +05:30
|
|
|
DbPost::verify(&self.object, &self.id, data).await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-03-16 06:41:48 +05:30
|
|
|
async fn receive(self, data: &Data<Self::DataType>) -> Result<(), Self::Error> {
|
2023-03-08 03:31:36 +05:30
|
|
|
DbPost::from_apub(self.object, data).await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|