1
0
Fork 0
forked from alemi/upub

feat: add audiences to objects too

This commit is contained in:
əlemi 2024-03-21 20:36:46 +01:00
parent d53644ea1f
commit 8ad5738579
Signed by: alemi
GPG key ID: A4895B84D311642C
4 changed files with 54 additions and 19 deletions

View file

@ -77,6 +77,10 @@ impl MigrationTrait for Migration {
.col(ColumnDef::new(Objects::Summary).string().null())
.col(ColumnDef::new(Objects::Content).string().null())
.col(ColumnDef::new(Objects::Context).string().null())
.col(ColumnDef::new(Objects::To).json().null())
.col(ColumnDef::new(Objects::Bto).json().null())
.col(ColumnDef::new(Objects::Cc).json().null())
.col(ColumnDef::new(Objects::Bcc).json().null())
.col(ColumnDef::new(Objects::Published).string().not_null())
.to_owned()
).await?;
@ -148,5 +152,9 @@ enum Objects {
Summary,
Content,
Context,
Cc,
Bcc,
To,
Bto,
Published,
}

View file

@ -1,22 +1,8 @@
use sea_orm::{entity::prelude::*, FromJsonQueryResult};
use sea_orm::entity::prelude::*;
use crate::{activitypub::jsonld::LD, activitystream::{link::Link, object::{activity::{Activity, ActivityMut, ActivityType}, actor::Actor, Object, ObjectMut, ObjectType}, Base, BaseMut, BaseType, Node}};
#[derive(Clone, Debug, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize, FromJsonQueryResult)]
pub struct Audience(pub Vec<String>);
impl<T : Link> From<Node<T>> for Audience {
fn from(value: Node<T>) -> Self {
Audience(
match value {
Node::Empty => vec![],
Node::Link(l) => vec![l.href().to_string()],
Node::Object(o) => if let Some(id) = o.id() { vec![id.to_string()] } else { vec![] },
Node::Array(arr) => arr.into_iter().filter_map(|l| Some(l.id()?.to_string())).collect(),
}
)
}
}
use super::Audience;
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "activities")]
@ -26,8 +12,8 @@ pub struct Model {
pub id: String,
pub activity_type: ActivityType,
pub actor: String, // TODO relates to USER
pub object: Option<String>, // TODO relates to NOTES maybe????? maybe other tables??????
pub actor: String,
pub object: Option<String>,
pub target: Option<String>, // TODO relates to USER maybe??
pub cc: Audience,

View file

@ -7,5 +7,20 @@ pub mod faker;
#[error("missing required field: '{0}'")]
pub struct FieldError(pub &'static str);
#[derive(Clone, Debug, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize, sea_orm::FromJsonQueryResult)]
pub struct Audience(pub Vec<String>);
use crate::activitystream::{Link, Node};
impl<T : Link> From<Node<T>> for Audience {
fn from(value: Node<T>) -> Self {
Audience(
match value {
Node::Empty => vec![],
Node::Link(l) => vec![l.href().to_string()],
Node::Object(o) => if let Some(id) = o.id() { vec![id.to_string()] } else { vec![] },
Node::Array(arr) => arr.into_iter().filter_map(|l| Some(l.id()?.to_string())).collect(),
}
)
}
}

View file

@ -1,6 +1,8 @@
use sea_orm::entity::prelude::*;
use crate::{activitypub::jsonld::LD, activitystream::{object::{ObjectMut, ObjectType}, BaseMut, BaseType, Node}};
use crate::{activitypub::jsonld::LD, activitystream::{object::{ObjectMut, ObjectType}, BaseMut, BaseType, Link, Node}};
use super::Audience;
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "objects")]
@ -14,6 +16,10 @@ pub struct Model {
pub summary: Option<String>,
pub content: Option<String>,
pub context: Option<String>,
pub cc: Audience,
pub bcc: Audience,
pub to: Audience,
pub bto: Audience,
pub published: ChronoDateTimeUtc,
}
@ -91,6 +97,22 @@ impl crate::activitystream::object::Object for Model {
Node::maybe_link(self.context.clone())
}
fn to(&self) -> Node<impl Link> {
Node::links(self.to.0.clone())
}
fn bto(&self) -> Node<impl Link> {
Node::links(self.bto.0.clone())
}
fn cc(&self) -> Node<impl Link> {
Node::links(self.cc.0.clone())
}
fn bcc(&self) -> Node<impl Link> {
Node::links(self.bcc.0.clone())
}
fn published (&self) -> Option<chrono::DateTime<chrono::Utc>> {
Some(self.published)
}
@ -107,6 +129,10 @@ impl Model {
content: object.content().map(|x| x.to_string()),
context: object.context().id().map(|x| x.to_string()),
published: object.published().ok_or(super::FieldError("published"))?,
to: object.to().into(),
bto: object.bto().into(),
cc: object.cc().into(),
bcc: object.bcc().into(),
})
}
}