activitypub/src/objects/post.rs

110 lines
3.3 KiB
Rust
Raw Normal View History

2024-04-09 19:48:18 +02:00
use crate::{
2024-04-18 04:03:52 +02:00
activities::create_post::CreatePost,
database::StateHandle,
entities::{post, user},
error::Error,
objects::person::DbUser,
utils::generate_object_id,
2024-04-09 19:48:18 +02:00
};
use activitypub_federation::{
config::Data,
fetch::object_id::ObjectId,
kinds::{object::NoteType, public},
protocol::{helpers::deserialize_one_or_many, verification::verify_domains_match},
traits::{Actor, Object},
};
use activitystreams_kinds::link::MentionType;
2024-04-18 03:41:52 +02:00
use sea_orm::{ActiveModelTrait, Set};
2024-04-09 19:48:18 +02:00
use serde::{Deserialize, Serialize};
2024-04-18 19:01:14 +02:00
use tracing::info;
2024-04-09 19:48:18 +02:00
use url::Url;
2024-04-15 01:07:15 +02:00
#[derive(Clone, Debug, Serialize, Deserialize)]
2024-04-09 19:48:18 +02:00
pub struct DbPost {
pub text: String,
2024-04-18 03:41:52 +02:00
pub ap_id: ObjectId<post::Model>,
pub creator: ObjectId<user::Model>,
2024-04-09 19:48:18 +02:00
pub local: bool,
}
#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Note {
#[serde(rename = "type")]
2024-05-04 17:54:11 +02:00
pub(crate) kind: NoteType,
pub(crate) id: ObjectId<post::Model>,
2024-04-18 03:41:52 +02:00
pub(crate) attributed_to: ObjectId<user::Model>,
2024-04-09 19:48:18 +02:00
#[serde(deserialize_with = "deserialize_one_or_many")]
pub(crate) to: Vec<Url>,
2024-05-04 17:54:11 +02:00
pub(crate) content: String,
pub(crate) in_reply_to: Option<ObjectId<post::Model>>,
pub(crate) tag: Vec<Mention>,
pub(crate) sensitive: bool,
2024-05-19 07:17:13 +02:00
pub(crate) cc: Option<Vec<Url>>,
2024-04-09 19:48:18 +02:00
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Mention {
pub href: Url,
#[serde(rename = "type")]
pub kind: MentionType,
}
#[async_trait::async_trait]
2024-04-18 03:41:52 +02:00
impl Object for post::Model {
type DataType = StateHandle;
2024-04-09 19:48:18 +02:00
type Kind = Note;
type Error = Error;
async fn read_from_id(
_object_id: Url,
_data: &Data<Self::DataType>,
) -> Result<Option<Self>, Self::Error> {
Ok(None)
}
async fn into_json(self, _data: &Data<Self::DataType>) -> Result<Self::Kind, Self::Error> {
todo!()
2024-04-09 19:48:18 +02:00
}
async fn verify(
json: &Self::Kind,
expected_domain: &Url,
_data: &Data<Self::DataType>,
) -> Result<(), Self::Error> {
verify_domains_match(json.id.inner(), expected_domain)?;
Ok(())
}
async fn from_json(json: Self::Kind, data: &Data<Self::DataType>) -> Result<Self, Self::Error> {
println!(
"Received post with content {} and id {}",
&json.content, &json.id
);
let creator = json.attributed_to.dereference(data).await?;
2024-04-18 03:41:52 +02:00
let post: post::ActiveModel = post::ActiveModel {
content: Set(json.content.clone()),
id: Set(json.id.to_string()),
creator: Set(creator.id.to_string()),
created_at: Set(chrono::Utc::now()), //TODO: make this use the real timestamp
content_type: Set("text/plain".to_string()), // TODO: make this use the real content type
2024-04-18 03:41:52 +02:00
local: Set(false),
visibility: Set("public".to_string()), // TODO: make this use the real visibility
sensitive: Set(json.sensitive.clone()),
url: Set(json.id.clone().to_string()),
2024-04-18 03:41:52 +02:00
..Default::default()
2024-04-09 19:48:18 +02:00
};
2024-04-18 04:03:52 +02:00
let post = post
.insert(data.app_data().database_connection.clone().as_ref())
2024-04-18 19:01:14 +02:00
.await;
if let Err(err) = post {
eprintln!("Error inserting post: {:?}", err);
return Err(err.into());
}
info!("Post inserted: {:?}", post.as_ref().unwrap());
Ok(post.unwrap())
2024-04-09 19:48:18 +02:00
}
}