2024-03-16 03:27:36 +01:00
|
|
|
use sea_orm::entity::prelude::*;
|
2024-03-21 19:15:19 +01:00
|
|
|
use crate::activitypub::jsonld::LD;
|
|
|
|
use crate::activitystream::key::{PublicKey as _, PublicKeyMut as _};
|
|
|
|
use crate::activitystream::object::actor::ActorMut as _;
|
|
|
|
use crate::activitystream::object::document::{DocumentMut as _, DocumentType};
|
2024-03-16 03:27:36 +01:00
|
|
|
|
2024-03-21 19:15:19 +01:00
|
|
|
use crate::activitystream::object::ObjectMut as _;
|
|
|
|
use crate::activitystream::{BaseMut as _, Object as _};
|
2024-03-21 01:09:33 +01:00
|
|
|
use crate::{activitypub, activitystream::{object::actor::ActorType, BaseType, Node, ObjectType}};
|
2024-03-16 05:45:58 +01:00
|
|
|
|
2024-03-16 03:27:36 +01:00
|
|
|
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
|
|
|
|
#[sea_orm(table_name = "users")]
|
|
|
|
pub struct Model {
|
|
|
|
#[sea_orm(primary_key)]
|
2024-03-21 01:09:33 +01:00
|
|
|
/// must be full AP ID, since they are unique over the network
|
2024-03-16 03:27:36 +01:00
|
|
|
pub id: String,
|
2024-03-21 01:09:33 +01:00
|
|
|
pub domain: String,
|
2024-03-16 05:45:58 +01:00
|
|
|
pub actor_type: ActorType,
|
2024-03-21 02:50:48 +01:00
|
|
|
pub preferred_username: String,
|
2024-03-16 05:45:58 +01:00
|
|
|
|
2024-03-21 02:50:48 +01:00
|
|
|
pub name: Option<String>,
|
2024-03-21 01:09:33 +01:00
|
|
|
pub summary: Option<String>,
|
|
|
|
pub image: Option<String>,
|
|
|
|
pub icon: Option<String>,
|
|
|
|
|
|
|
|
pub inbox: Option<String>,
|
|
|
|
pub shared_inbox: Option<String>,
|
|
|
|
pub outbox: Option<String>,
|
|
|
|
pub following: Option<String>,
|
|
|
|
pub followers: Option<String>,
|
|
|
|
|
2024-03-21 02:11:31 +01:00
|
|
|
pub public_key: String,
|
|
|
|
pub private_key: Option<String>,
|
|
|
|
|
2024-03-21 01:09:33 +01:00
|
|
|
pub created: ChronoDateTimeUtc,
|
|
|
|
pub updated: ChronoDateTimeUtc,
|
|
|
|
|
|
|
|
// TODO these are also suggested
|
|
|
|
// pub liked: Option<String>,
|
|
|
|
// pub streams: Option<String>,
|
2024-03-16 03:27:36 +01:00
|
|
|
}
|
|
|
|
|
2024-03-21 01:09:33 +01:00
|
|
|
use crate::activitystream::object::{actor::Actor, collection::Collection, document::Image};
|
|
|
|
|
2024-03-16 03:27:36 +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(has_many = "super::object::Entity")]
|
|
|
|
Object,
|
2024-03-23 05:01:45 +01:00
|
|
|
|
|
|
|
#[sea_orm(has_one = "super::config::Entity")]
|
|
|
|
Config,
|
|
|
|
|
|
|
|
#[sea_orm(has_one = "super::credential::Entity")]
|
|
|
|
Credential,
|
2024-03-21 01:09:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Related<super::activity::Entity> for Entity {
|
|
|
|
fn to() -> RelationDef {
|
|
|
|
Relation::Activity.def()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Related<super::object::Entity> for Entity {
|
|
|
|
fn to() -> RelationDef {
|
|
|
|
Relation::Object.def()
|
|
|
|
}
|
|
|
|
}
|
2024-03-16 03:27:36 +01:00
|
|
|
|
|
|
|
impl ActiveModelBehavior for ActiveModel {}
|
|
|
|
|
2024-03-21 01:09:33 +01:00
|
|
|
impl crate::activitystream::Base for Model {
|
2024-03-16 03:27:36 +01:00
|
|
|
fn id(&self) -> Option<&str> {
|
|
|
|
Some(&self.id)
|
|
|
|
}
|
|
|
|
|
2024-03-19 06:49:21 +01:00
|
|
|
fn base_type(&self) -> Option<BaseType> {
|
|
|
|
Some(BaseType::Object(ObjectType::Actor(self.actor_type)))
|
2024-03-16 05:45:58 +01:00
|
|
|
}
|
2024-03-20 05:44:10 +01:00
|
|
|
|
|
|
|
fn underlying_json_object(self) -> serde_json::Value {
|
2024-03-21 19:15:19 +01:00
|
|
|
serde_json::Value::new_object()
|
2024-03-21 01:09:33 +01:00
|
|
|
.set_id(Some(&self.id))
|
|
|
|
.set_actor_type(Some(self.actor_type))
|
2024-03-21 02:50:48 +01:00
|
|
|
.set_name(self.name.as_deref())
|
2024-03-21 01:09:33 +01:00
|
|
|
.set_summary(self.summary.as_deref())
|
|
|
|
.set_icon(self.icon())
|
|
|
|
.set_image(self.image())
|
2024-03-21 16:47:15 +01:00
|
|
|
.set_published(Some(self.created))
|
2024-03-21 02:50:48 +01:00
|
|
|
.set_preferred_username(Some(&self.preferred_username))
|
2024-03-21 01:09:33 +01:00
|
|
|
.set_inbox(self.inbox())
|
|
|
|
.set_outbox(self.outbox())
|
|
|
|
.set_following(self.following())
|
|
|
|
.set_followers(self.followers())
|
2024-03-21 02:11:31 +01:00
|
|
|
.set_public_key(self.public_key())
|
2024-03-21 02:36:41 +01:00
|
|
|
.set_discoverable(Some(true))
|
2024-03-21 02:45:24 +01:00
|
|
|
.set_endpoints(None) // TODO dirty fix to put an empty object
|
2024-03-20 05:44:10 +01:00
|
|
|
}
|
2024-03-19 06:49:21 +01:00
|
|
|
}
|
2024-03-16 05:45:58 +01:00
|
|
|
|
2024-03-21 01:09:33 +01:00
|
|
|
impl crate::activitystream::object::Object for Model {
|
|
|
|
fn name(&self) -> Option<&str> {
|
2024-03-21 02:50:48 +01:00
|
|
|
self.name.as_deref()
|
2024-03-16 03:27:36 +01:00
|
|
|
}
|
2024-03-21 01:09:33 +01:00
|
|
|
|
|
|
|
fn summary(&self) -> Option<&str> {
|
|
|
|
self.summary.as_deref()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn icon(&self) -> Node<impl Image> {
|
|
|
|
match &self.icon {
|
2024-03-21 02:32:13 +01:00
|
|
|
Some(x) => Node::object(
|
2024-03-21 19:15:19 +01:00
|
|
|
serde_json::Value::new_object()
|
2024-03-21 02:32:13 +01:00
|
|
|
.set_document_type(Some(DocumentType::Image))
|
|
|
|
.set_url(Node::link(x.clone()))
|
|
|
|
),
|
2024-03-21 01:09:33 +01:00
|
|
|
None => Node::Empty,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn image(&self) -> Node<impl Image> {
|
|
|
|
match &self.image {
|
2024-03-21 02:32:13 +01:00
|
|
|
Some(x) => Node::object(
|
2024-03-21 19:15:19 +01:00
|
|
|
serde_json::Value::new_object()
|
2024-03-21 02:32:13 +01:00
|
|
|
.set_document_type(Some(DocumentType::Image))
|
|
|
|
.set_url(Node::link(x.clone()))
|
|
|
|
),
|
2024-03-21 01:09:33 +01:00
|
|
|
None => Node::Empty,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn published(&self) -> Option<chrono::DateTime<chrono::Utc>> {
|
|
|
|
Some(self.created)
|
|
|
|
}
|
2024-03-16 03:27:36 +01:00
|
|
|
}
|
2024-03-16 20:09:06 +01:00
|
|
|
|
2024-03-21 01:09:33 +01:00
|
|
|
impl crate::activitystream::object::actor::Actor for Model {
|
2024-03-19 06:49:21 +01:00
|
|
|
fn actor_type(&self) -> Option<ActorType> {
|
|
|
|
Some(self.actor_type)
|
|
|
|
}
|
2024-03-21 01:09:33 +01:00
|
|
|
|
|
|
|
fn preferred_username(&self) -> Option<&str> {
|
2024-03-21 02:50:48 +01:00
|
|
|
Some(&self.preferred_username)
|
2024-03-21 01:09:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn inbox(&self) -> Node<impl Collection> {
|
2024-03-21 02:50:48 +01:00
|
|
|
Node::link(self.inbox.clone().unwrap_or(format!("https://{}/users/{}/inbox", self.domain, self.preferred_username)))
|
2024-03-21 01:09:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn outbox(&self) -> Node<impl Collection> {
|
2024-03-21 02:50:48 +01:00
|
|
|
Node::link(self.outbox.clone().unwrap_or(format!("https://{}/users/{}/outbox", self.domain, self.preferred_username)))
|
2024-03-21 01:09:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn following(&self) -> Node<impl Collection> {
|
2024-03-21 02:50:48 +01:00
|
|
|
Node::link(self.following.clone().unwrap_or(format!("https://{}/users/{}/following", self.domain, self.preferred_username)))
|
2024-03-21 01:09:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn followers(&self) -> Node<impl Collection> {
|
2024-03-21 02:50:48 +01:00
|
|
|
Node::link(self.following.clone().unwrap_or(format!("https://{}/users/{}/followers", self.domain, self.preferred_username)))
|
2024-03-21 01:09:33 +01:00
|
|
|
}
|
2024-03-21 02:11:31 +01:00
|
|
|
|
|
|
|
fn public_key(&self) -> Node<impl crate::activitystream::key::PublicKey> {
|
|
|
|
Node::object(
|
2024-03-21 19:15:19 +01:00
|
|
|
serde_json::Value::new_object()
|
2024-03-21 02:45:24 +01:00
|
|
|
.set_id(Some(&format!("{}#main-key", self.id))) // TODO is this some standard??
|
2024-03-21 02:11:31 +01:00
|
|
|
.set_public_key_pem(&self.public_key)
|
|
|
|
.set_owner(Some(&self.id))
|
|
|
|
)
|
|
|
|
}
|
2024-03-19 06:49:21 +01:00
|
|
|
}
|
|
|
|
|
2024-03-16 20:09:06 +01:00
|
|
|
impl Model {
|
2024-03-19 06:49:21 +01:00
|
|
|
pub fn new(object: &impl Actor) -> Result<Self, super::FieldError> {
|
2024-03-21 01:09:33 +01:00
|
|
|
let ap_id = object.id().ok_or(super::FieldError("id"))?.to_string();
|
2024-03-21 02:50:48 +01:00
|
|
|
let (domain, preferred_username) = activitypub::split_id(&ap_id);
|
2024-03-16 20:09:06 +01:00
|
|
|
Ok(Model {
|
2024-03-21 02:50:48 +01:00
|
|
|
id: ap_id, preferred_username, domain,
|
2024-03-19 06:49:21 +01:00
|
|
|
actor_type: object.actor_type().ok_or(super::FieldError("type"))?,
|
2024-03-21 02:50:48 +01:00
|
|
|
name: object.name().map(|x| x.to_string()),
|
2024-03-21 01:09:33 +01:00
|
|
|
summary: object.summary().map(|x| x.to_string()),
|
|
|
|
icon: object.icon().id().map(|x| x.to_string()),
|
|
|
|
image: object.image().id().map(|x| x.to_string()),
|
|
|
|
inbox: object.inbox().id().map(|x| x.to_string()),
|
|
|
|
outbox: object.inbox().id().map(|x| x.to_string()),
|
|
|
|
shared_inbox: None, // TODO!!! parse endpoints
|
|
|
|
followers: object.followers().id().map(|x| x.to_string()),
|
|
|
|
following: object.following().id().map(|x| x.to_string()),
|
|
|
|
created: object.published().unwrap_or(chrono::Utc::now()),
|
|
|
|
updated: chrono::Utc::now(),
|
2024-03-21 02:11:31 +01:00
|
|
|
public_key: object.public_key().get().ok_or(super::FieldError("publicKey"))?.public_key_pem().to_string(),
|
|
|
|
private_key: None, // there's no way to transport privkey over AP json, must come from DB
|
2024-03-16 20:09:06 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|