2023-03-08 03:31:36 +05:30
|
|
|
use crate::{
|
|
|
|
activities::create_post::CreatePost,
|
|
|
|
database::DatabaseHandle,
|
|
|
|
error::Error,
|
|
|
|
generate_object_id,
|
|
|
|
objects::person::DbUser,
|
|
|
|
};
|
|
|
|
use activitypub_federation::{
|
2023-03-16 06:41:48 +05:30
|
|
|
config::Data,
|
2023-03-08 03:31:36 +05:30
|
|
|
fetch::object_id::ObjectId,
|
|
|
|
kinds::{object::NoteType, public},
|
2023-03-10 02:39:44 +05:30
|
|
|
protocol::{helpers::deserialize_one_or_many, verification::verify_domains_match},
|
2023-03-08 03:31:36 +05:30
|
|
|
traits::{Actor, ApubObject},
|
|
|
|
};
|
|
|
|
use activitystreams_kinds::link::MentionType;
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use url::Url;
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct DbPost {
|
|
|
|
pub text: String,
|
|
|
|
pub ap_id: ObjectId<DbPost>,
|
|
|
|
pub creator: ObjectId<DbUser>,
|
|
|
|
pub local: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize, Serialize, Debug)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct Note {
|
|
|
|
#[serde(rename = "type")]
|
|
|
|
kind: NoteType,
|
|
|
|
id: ObjectId<DbPost>,
|
|
|
|
pub(crate) attributed_to: ObjectId<DbUser>,
|
|
|
|
#[serde(deserialize_with = "deserialize_one_or_many")]
|
|
|
|
pub(crate) to: Vec<Url>,
|
|
|
|
content: String,
|
|
|
|
in_reply_to: Option<ObjectId<DbPost>>,
|
|
|
|
tag: Vec<Mention>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
|
|
|
pub struct Mention {
|
|
|
|
pub href: Url,
|
|
|
|
#[serde(rename = "type")]
|
|
|
|
pub kind: MentionType,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait]
|
|
|
|
impl ApubObject for DbPost {
|
|
|
|
type DataType = DatabaseHandle;
|
|
|
|
type ApubType = Note;
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
async fn read_from_apub_id(
|
|
|
|
_object_id: Url,
|
2023-03-16 06:41:48 +05:30
|
|
|
_data: &Data<Self::DataType>,
|
2023-03-08 03:31:36 +05:30
|
|
|
) -> Result<Option<Self>, Self::Error> {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
|
2023-03-16 06:41:48 +05:30
|
|
|
async fn into_apub(self, _data: &Data<Self::DataType>) -> Result<Self::ApubType, Self::Error> {
|
2023-03-08 03:31:36 +05:30
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
2023-03-10 02:39:44 +05:30
|
|
|
async fn verify(
|
|
|
|
apub: &Self::ApubType,
|
|
|
|
expected_domain: &Url,
|
2023-03-16 06:41:48 +05:30
|
|
|
_data: &Data<Self::DataType>,
|
2023-03-10 02:39:44 +05:30
|
|
|
) -> Result<(), Self::Error> {
|
|
|
|
verify_domains_match(apub.id.inner(), expected_domain)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-03-08 03:31:36 +05:30
|
|
|
async fn from_apub(
|
|
|
|
apub: Self::ApubType,
|
2023-03-16 06:41:48 +05:30
|
|
|
data: &Data<Self::DataType>,
|
2023-03-08 03:31:36 +05:30
|
|
|
) -> Result<Self, Self::Error> {
|
|
|
|
println!(
|
|
|
|
"Received post with content {} and id {}",
|
|
|
|
&apub.content, &apub.id
|
|
|
|
);
|
|
|
|
let creator = apub.attributed_to.dereference(data).await?;
|
|
|
|
let post = DbPost {
|
|
|
|
text: apub.content,
|
|
|
|
ap_id: apub.id.clone(),
|
|
|
|
creator: apub.attributed_to.clone(),
|
|
|
|
local: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
let mention = Mention {
|
|
|
|
href: creator.ap_id.clone().into_inner(),
|
|
|
|
kind: Default::default(),
|
|
|
|
};
|
|
|
|
let note = Note {
|
|
|
|
kind: Default::default(),
|
|
|
|
id: generate_object_id(data.domain())?.into(),
|
|
|
|
attributed_to: data.local_user().ap_id,
|
|
|
|
to: vec![public()],
|
|
|
|
content: format!("Hello {}", creator.name),
|
|
|
|
in_reply_to: Some(apub.id.clone()),
|
|
|
|
tag: vec![mention],
|
|
|
|
};
|
|
|
|
CreatePost::send(note, creator.shared_inbox_or_inbox(), data).await?;
|
|
|
|
|
|
|
|
Ok(post)
|
|
|
|
}
|
|
|
|
}
|