2023-03-07 22:01:36 +00:00
|
|
|
use crate::{
|
2023-07-03 13:05:18 +00:00
|
|
|
activities::create_post::CreatePost,
|
|
|
|
database::DatabaseHandle,
|
|
|
|
error::Error,
|
|
|
|
generate_object_id,
|
|
|
|
objects::person::DbUser,
|
2023-03-07 22:01:36 +00:00
|
|
|
};
|
|
|
|
use activitypub_federation::{
|
2023-03-16 01:11:48 +00:00
|
|
|
config::Data,
|
2023-03-07 22:01:36 +00:00
|
|
|
fetch::object_id::ObjectId,
|
|
|
|
kinds::{object::NoteType, public},
|
2023-03-09 21:09:44 +00:00
|
|
|
protocol::{helpers::deserialize_one_or_many, verification::verify_domains_match},
|
2023-03-16 20:41:29 +00:00
|
|
|
traits::{Actor, Object},
|
2023-03-07 22:01:36 +00:00
|
|
|
};
|
|
|
|
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]
|
2023-03-16 20:41:29 +00:00
|
|
|
impl Object for DbPost {
|
2023-03-07 22:01:36 +00:00
|
|
|
type DataType = DatabaseHandle;
|
2023-03-16 20:41:29 +00:00
|
|
|
type Kind = Note;
|
2023-03-07 22:01:36 +00:00
|
|
|
type Error = Error;
|
|
|
|
|
2023-03-16 20:41:29 +00:00
|
|
|
async fn read_from_id(
|
2023-03-07 22:01:36 +00:00
|
|
|
_object_id: Url,
|
2023-03-16 01:11:48 +00:00
|
|
|
_data: &Data<Self::DataType>,
|
2023-03-07 22:01:36 +00:00
|
|
|
) -> Result<Option<Self>, Self::Error> {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
|
2023-03-16 20:41:29 +00:00
|
|
|
async fn into_json(self, _data: &Data<Self::DataType>) -> Result<Self::Kind, Self::Error> {
|
2023-03-07 22:01:36 +00:00
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
2023-03-09 21:09:44 +00:00
|
|
|
async fn verify(
|
2023-03-16 20:41:29 +00:00
|
|
|
json: &Self::Kind,
|
2023-03-09 21:09:44 +00:00
|
|
|
expected_domain: &Url,
|
2023-03-16 01:11:48 +00:00
|
|
|
_data: &Data<Self::DataType>,
|
2023-03-09 21:09:44 +00:00
|
|
|
) -> Result<(), Self::Error> {
|
2023-03-16 20:41:29 +00:00
|
|
|
verify_domains_match(json.id.inner(), expected_domain)?;
|
2023-03-09 21:09:44 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-03-16 20:41:29 +00:00
|
|
|
async fn from_json(json: Self::Kind, data: &Data<Self::DataType>) -> Result<Self, Self::Error> {
|
2023-03-07 22:01:36 +00:00
|
|
|
println!(
|
|
|
|
"Received post with content {} and id {}",
|
2023-03-16 20:41:29 +00:00
|
|
|
&json.content, &json.id
|
2023-03-07 22:01:36 +00:00
|
|
|
);
|
2023-03-16 20:41:29 +00:00
|
|
|
let creator = json.attributed_to.dereference(data).await?;
|
2023-03-07 22:01:36 +00:00
|
|
|
let post = DbPost {
|
2023-03-16 20:41:29 +00:00
|
|
|
text: json.content,
|
|
|
|
ap_id: json.id.clone(),
|
|
|
|
creator: json.attributed_to.clone(),
|
2023-03-07 22:01:36 +00:00
|
|
|
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),
|
2023-03-16 20:41:29 +00:00
|
|
|
in_reply_to: Some(json.id.clone()),
|
2023-03-07 22:01:36 +00:00
|
|
|
tag: vec![mention],
|
|
|
|
};
|
|
|
|
CreatePost::send(note, creator.shared_inbox_or_inbox(), data).await?;
|
|
|
|
|
|
|
|
Ok(post)
|
|
|
|
}
|
|
|
|
}
|