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-16 06:41:48 +05:30
|
|
|
config::Data,
|
2023-03-06 06:47:34 +05:30
|
|
|
fetch::object_id::ObjectId,
|
2023-02-19 17:56:01 +05:30
|
|
|
kinds::{object::NoteType, public},
|
2023-03-10 02:39:44 +05:30
|
|
|
protocol::{helpers::deserialize_one_or_many, verification::verify_domains_match},
|
2023-03-17 02:11:29 +05:30
|
|
|
traits::Object,
|
2022-06-02 16:47:12 +05:30
|
|
|
};
|
|
|
|
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-03-17 02:11:29 +05:30
|
|
|
impl Object for DbPost {
|
2023-02-11 18:02:35 +05:30
|
|
|
type DataType = DatabaseHandle;
|
2023-03-17 02:11:29 +05:30
|
|
|
type Kind = Note;
|
2023-02-11 18:35:37 +05:30
|
|
|
type Error = Error;
|
2022-06-02 16:47:12 +05:30
|
|
|
|
2023-03-17 02:11:29 +05:30
|
|
|
async fn read_from_id(
|
2023-03-08 03:31:36 +05:30
|
|
|
object_id: Url,
|
2023-03-16 06:41:48 +05:30
|
|
|
data: &Data<Self::DataType>,
|
2022-06-02 16:47:12 +05:30
|
|
|
) -> Result<Option<Self>, Self::Error> {
|
2023-03-08 03:31:36 +05:30
|
|
|
let posts = data.posts.lock().unwrap();
|
|
|
|
let res = posts
|
|
|
|
.clone()
|
|
|
|
.into_iter()
|
|
|
|
.find(|u| u.ap_id.inner() == &object_id);
|
|
|
|
Ok(res)
|
2022-06-02 16:47:12 +05:30
|
|
|
}
|
|
|
|
|
2023-03-17 02:11:29 +05:30
|
|
|
async fn into_json(self, data: &Data<Self::DataType>) -> Result<Self::Kind, 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,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-03-10 02:39:44 +05:30
|
|
|
async fn verify(
|
2023-03-17 02:11:29 +05:30
|
|
|
json: &Self::Kind,
|
2023-03-10 02:39:44 +05:30
|
|
|
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> {
|
2023-03-17 02:11:29 +05:30
|
|
|
verify_domains_match(json.id.inner(), expected_domain)?;
|
2023-03-10 02:39:44 +05:30
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-03-17 02:11:29 +05:30
|
|
|
async fn from_json(json: Self::Kind, data: &Data<Self::DataType>) -> Result<Self, Self::Error> {
|
2023-02-19 17:56:01 +05:30
|
|
|
let post = DbPost {
|
2023-03-17 02:11:29 +05:30
|
|
|
text: json.content,
|
|
|
|
ap_id: json.id,
|
|
|
|
creator: json.attributed_to,
|
2022-06-02 16:47:12 +05:30
|
|
|
local: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut lock = data.posts.lock().unwrap();
|
|
|
|
lock.push(post.clone());
|
|
|
|
Ok(post)
|
|
|
|
}
|
|
|
|
}
|