2024-04-20 04:26:31 +02:00
|
|
|
use apb::{BaseMut, ObjectMut, Collection};
|
2024-03-16 05:45:58 +01:00
|
|
|
use sea_orm::entity::prelude::*;
|
|
|
|
|
2024-04-19 03:28:39 +02:00
|
|
|
use crate::routes::activitypub::jsonld::LD;
|
|
|
|
|
2024-03-21 20:36:46 +01:00
|
|
|
use super::Audience;
|
2024-03-16 05:45:58 +01:00
|
|
|
|
|
|
|
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
|
|
|
|
#[sea_orm(table_name = "objects")]
|
|
|
|
pub struct Model {
|
|
|
|
#[sea_orm(primary_key)]
|
|
|
|
pub id: String,
|
2024-04-06 16:56:13 +02:00
|
|
|
pub object_type: apb::ObjectType,
|
2024-03-16 05:45:58 +01:00
|
|
|
pub attributed_to: Option<String>,
|
|
|
|
pub name: Option<String>,
|
|
|
|
pub summary: Option<String>,
|
|
|
|
pub content: Option<String>,
|
2024-03-22 06:35:50 +01:00
|
|
|
pub likes: i64,
|
|
|
|
pub shares: i64,
|
|
|
|
pub comments: i64,
|
2024-03-21 19:25:37 +01:00
|
|
|
pub context: Option<String>,
|
2024-04-18 04:09:13 +02:00
|
|
|
pub in_reply_to: Option<String>,
|
2024-03-21 20:36:46 +01:00
|
|
|
pub cc: Audience,
|
|
|
|
pub bcc: Audience,
|
|
|
|
pub to: Audience,
|
|
|
|
pub bto: Audience,
|
2024-03-16 05:45:58 +01:00
|
|
|
pub published: ChronoDateTimeUtc,
|
2024-04-24 05:19:39 +02:00
|
|
|
|
|
|
|
pub sensitive: bool,
|
2024-03-16 05:45:58 +01:00
|
|
|
}
|
|
|
|
|
2024-03-23 06:32:15 +01:00
|
|
|
impl Model {
|
2024-04-06 16:56:13 +02:00
|
|
|
pub fn new(object: &impl apb::Object) -> Result<Self, super::FieldError> {
|
2024-03-23 06:32:15 +01:00
|
|
|
Ok(Model {
|
|
|
|
id: object.id().ok_or(super::FieldError("id"))?.to_string(),
|
|
|
|
object_type: object.object_type().ok_or(super::FieldError("type"))?,
|
2024-04-06 16:56:13 +02:00
|
|
|
attributed_to: object.attributed_to().id(),
|
2024-03-23 06:32:15 +01:00
|
|
|
name: object.name().map(|x| x.to_string()),
|
|
|
|
summary: object.summary().map(|x| x.to_string()),
|
|
|
|
content: object.content().map(|x| x.to_string()),
|
2024-04-06 16:56:13 +02:00
|
|
|
context: object.context().id(),
|
2024-04-18 04:09:13 +02:00
|
|
|
in_reply_to: object.in_reply_to().id(),
|
2024-03-23 06:32:15 +01:00
|
|
|
published: object.published().ok_or(super::FieldError("published"))?,
|
2024-04-20 04:26:31 +02:00
|
|
|
comments: object.replies().get().map(|x| x.total_items().unwrap_or(0)).unwrap_or(0) as i64,
|
2024-03-23 06:32:15 +01:00
|
|
|
likes: 0,
|
|
|
|
shares: 0,
|
|
|
|
to: object.to().into(),
|
|
|
|
bto: object.bto().into(),
|
|
|
|
cc: object.cc().into(),
|
|
|
|
bcc: object.bcc().into(),
|
2024-04-24 05:19:39 +02:00
|
|
|
|
|
|
|
sensitive: object.sensitive().unwrap_or(false),
|
2024-03-23 06:32:15 +01:00
|
|
|
})
|
|
|
|
}
|
2024-04-21 22:58:33 +02:00
|
|
|
|
2024-04-19 03:28:39 +02:00
|
|
|
pub fn ap(self) -> serde_json::Value {
|
|
|
|
serde_json::Value::new_object()
|
|
|
|
.set_id(Some(&self.id))
|
|
|
|
.set_object_type(Some(self.object_type))
|
|
|
|
.set_attributed_to(apb::Node::maybe_link(self.attributed_to))
|
|
|
|
.set_name(self.name.as_deref())
|
|
|
|
.set_summary(self.summary.as_deref())
|
|
|
|
.set_content(self.content.as_deref())
|
|
|
|
.set_context(apb::Node::maybe_link(self.context.clone()))
|
|
|
|
.set_in_reply_to(apb::Node::maybe_link(self.in_reply_to.clone()))
|
|
|
|
.set_published(Some(self.published))
|
|
|
|
.set_to(apb::Node::links(self.to.0.clone()))
|
|
|
|
.set_bto(apb::Node::Empty)
|
|
|
|
.set_cc(apb::Node::links(self.cc.0.clone()))
|
|
|
|
.set_bcc(apb::Node::Empty)
|
2024-04-24 05:19:39 +02:00
|
|
|
.set_sensitive(Some(self.sensitive))
|
2024-04-19 03:28:39 +02:00
|
|
|
}
|
2024-03-23 06:32:15 +01:00
|
|
|
}
|
|
|
|
|
2024-03-16 05:45:58 +01:00
|
|
|
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
2024-03-21 01:09:33 +01:00
|
|
|
pub enum Relation {
|
|
|
|
#[sea_orm(has_many = "super::activity::Entity")]
|
|
|
|
Activity,
|
|
|
|
|
|
|
|
#[sea_orm(
|
|
|
|
belongs_to = "super::user::Entity",
|
|
|
|
from = "Column::AttributedTo",
|
|
|
|
to = "super::user::Column::Id",
|
|
|
|
)]
|
|
|
|
User,
|
2024-03-24 04:03:44 +01:00
|
|
|
|
|
|
|
#[sea_orm(has_many = "super::addressing::Entity")]
|
|
|
|
Addressing,
|
2024-04-21 22:58:33 +02:00
|
|
|
|
|
|
|
#[sea_orm(has_many = "super::attachment::Entity")]
|
|
|
|
Attachment,
|
2024-03-21 01:09:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Related<super::activity::Entity> for Entity {
|
|
|
|
fn to() -> RelationDef {
|
|
|
|
Relation::Activity.def()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Related<super::user::Entity> for Entity {
|
|
|
|
fn to() -> RelationDef {
|
|
|
|
Relation::User.def()
|
|
|
|
}
|
|
|
|
}
|
2024-03-16 05:45:58 +01:00
|
|
|
|
2024-03-24 04:03:44 +01:00
|
|
|
impl Related<super::addressing::Entity> for Entity {
|
|
|
|
fn to() -> RelationDef {
|
|
|
|
Relation::Addressing.def()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-21 22:58:33 +02:00
|
|
|
impl Related<super::attachment::Entity> for Entity {
|
|
|
|
fn to() -> RelationDef {
|
|
|
|
Relation::Attachment.def()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-16 05:45:58 +01:00
|
|
|
impl ActiveModelBehavior for ActiveModel {}
|