2023-02-19 17:56:01 +05:30
|
|
|
use crate::{error::Error, generate_object_id, instance::DatabaseHandle, objects::person::DbUser};
|
2022-06-02 16:47:12 +05:30
|
|
|
use activitypub_federation::{
|
2023-03-02 04:49:10 +05:30
|
|
|
config::RequestData,
|
2022-06-02 16:47:12 +05:30
|
|
|
core::object_id::ObjectId,
|
2023-02-19 17:56:01 +05:30
|
|
|
kinds::{object::NoteType, public},
|
|
|
|
protocol::helpers::deserialize_one_or_many,
|
2022-06-02 16:47:12 +05:30
|
|
|
traits::ApubObject,
|
|
|
|
};
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use url::Url;
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
2023-02-19 17:56:01 +05:30
|
|
|
pub struct DbPost {
|
2022-06-02 16:47:12 +05:30
|
|
|
pub text: String,
|
2023-02-19 17:56:01 +05:30
|
|
|
pub ap_id: ObjectId<DbPost>,
|
|
|
|
pub creator: ObjectId<DbUser>,
|
2022-06-02 16:47:12 +05:30
|
|
|
pub local: bool,
|
|
|
|
}
|
|
|
|
|
2023-02-19 17:56:01 +05:30
|
|
|
impl DbPost {
|
|
|
|
pub fn new(text: String, creator: ObjectId<DbUser>) -> Result<DbPost, Error> {
|
|
|
|
let ap_id = generate_object_id(creator.inner().domain().unwrap())?.into();
|
|
|
|
Ok(DbPost {
|
2022-06-02 16:47:12 +05:30
|
|
|
text,
|
2023-02-19 17:56:01 +05:30
|
|
|
ap_id,
|
2022-06-02 16:47:12 +05:30
|
|
|
creator,
|
|
|
|
local: true,
|
2023-02-19 17:56:01 +05:30
|
|
|
})
|
2022-06-02 16:47:12 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize, Serialize, Debug)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct Note {
|
|
|
|
#[serde(rename = "type")]
|
|
|
|
kind: NoteType,
|
2023-02-19 17:56:01 +05:30
|
|
|
id: ObjectId<DbPost>,
|
|
|
|
pub(crate) attributed_to: ObjectId<DbUser>,
|
2022-06-02 16:47:12 +05:30
|
|
|
#[serde(deserialize_with = "deserialize_one_or_many")]
|
|
|
|
pub(crate) to: Vec<Url>,
|
|
|
|
content: String,
|
|
|
|
}
|
|
|
|
|
2022-11-29 02:49:56 +05:30
|
|
|
#[async_trait::async_trait]
|
2023-02-19 17:56:01 +05:30
|
|
|
impl ApubObject for DbPost {
|
2023-02-11 18:02:35 +05:30
|
|
|
type DataType = DatabaseHandle;
|
2022-06-02 16:47:12 +05:30
|
|
|
type ApubType = Note;
|
2023-02-11 18:35:37 +05:30
|
|
|
type Error = Error;
|
2022-06-02 16:47:12 +05:30
|
|
|
|
|
|
|
async fn read_from_apub_id(
|
|
|
|
_object_id: Url,
|
2023-02-11 18:02:35 +05:30
|
|
|
_data: &RequestData<Self::DataType>,
|
2022-06-02 16:47:12 +05:30
|
|
|
) -> Result<Option<Self>, Self::Error> {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
|
2023-02-11 18:02:35 +05:30
|
|
|
async fn into_apub(
|
|
|
|
self,
|
|
|
|
data: &RequestData<Self::DataType>,
|
|
|
|
) -> Result<Self::ApubType, Self::Error> {
|
2022-06-02 18:12:29 +05:30
|
|
|
let creator = self.creator.dereference_local(data).await?;
|
2022-06-02 16:47:12 +05:30
|
|
|
Ok(Note {
|
|
|
|
kind: Default::default(),
|
|
|
|
id: self.ap_id,
|
|
|
|
attributed_to: self.creator,
|
|
|
|
to: vec![public(), creator.followers_url()?],
|
|
|
|
content: self.text,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn from_apub(
|
|
|
|
apub: Self::ApubType,
|
2023-02-11 18:02:35 +05:30
|
|
|
data: &RequestData<Self::DataType>,
|
2022-06-02 16:47:12 +05:30
|
|
|
) -> Result<Self, Self::Error> {
|
2023-02-19 17:56:01 +05:30
|
|
|
let post = DbPost {
|
2022-06-02 16:47:12 +05:30
|
|
|
text: apub.content,
|
|
|
|
ap_id: apub.id,
|
|
|
|
creator: apub.attributed_to,
|
|
|
|
local: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut lock = data.posts.lock().unwrap();
|
|
|
|
lock.push(post.clone());
|
|
|
|
Ok(post)
|
|
|
|
}
|
|
|
|
}
|