2024-05-25 05:31:10 +02:00
|
|
|
use sea_orm::{entity::prelude::*, QuerySelect, SelectColumns};
|
2024-03-16 03:27:36 +01:00
|
|
|
|
2024-05-15 18:51:34 +02:00
|
|
|
use apb::{Actor, ActorMut, ActorType, BaseMut, DocumentMut, Endpoints, EndpointsMut, Object, ObjectMut, PublicKey, PublicKeyMut};
|
2024-04-19 03:28:39 +02:00
|
|
|
|
2024-05-25 05:31:10 +02:00
|
|
|
use crate::{errors::UpubError, routes::activitypub::jsonld::LD};
|
2024-03-16 05:45:58 +01:00
|
|
|
|
2024-03-16 03:27:36 +01:00
|
|
|
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
|
2024-05-24 05:05:14 +02:00
|
|
|
#[sea_orm(table_name = "actors")]
|
2024-03-16 03:27:36 +01:00
|
|
|
pub struct Model {
|
|
|
|
#[sea_orm(primary_key)]
|
2024-05-25 04:37:17 +02:00
|
|
|
pub internal: i64,
|
2024-05-24 05:05:14 +02:00
|
|
|
#[sea_orm(unique)]
|
2024-05-25 04:37:17 +02:00
|
|
|
pub id: String,
|
2024-03-16 05:45:58 +01:00
|
|
|
pub actor_type: ActorType,
|
2024-05-25 04:37:17 +02:00
|
|
|
pub domain: String,
|
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>,
|
2024-05-24 05:05:14 +02:00
|
|
|
#[sea_orm(unique)]
|
|
|
|
pub preferred_username: String,
|
2024-03-21 01:09:33 +01:00
|
|
|
pub inbox: Option<String>,
|
|
|
|
pub shared_inbox: Option<String>,
|
|
|
|
pub outbox: Option<String>,
|
|
|
|
pub following: Option<String>,
|
|
|
|
pub followers: Option<String>,
|
2024-05-24 05:05:14 +02:00
|
|
|
pub following_count: i32,
|
|
|
|
pub followers_count: i32,
|
|
|
|
pub statuses_count: i32,
|
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,
|
2024-03-16 03:27:36 +01:00
|
|
|
}
|
|
|
|
|
2024-05-24 05:05:14 +02:00
|
|
|
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
|
|
|
pub enum Relation {
|
|
|
|
#[sea_orm(has_many = "super::activity::Entity")]
|
|
|
|
Activities,
|
|
|
|
#[sea_orm(has_many = "super::addressing::Entity")]
|
|
|
|
Addressing,
|
|
|
|
#[sea_orm(has_many = "super::announce::Entity")]
|
|
|
|
Announces,
|
|
|
|
#[sea_orm(has_many = "super::config::Entity")]
|
|
|
|
Configs,
|
|
|
|
#[sea_orm(has_many = "super::credential::Entity")]
|
|
|
|
Credentials,
|
|
|
|
#[sea_orm(has_many = "super::delivery::Entity")]
|
|
|
|
Deliveries,
|
|
|
|
#[sea_orm(
|
|
|
|
belongs_to = "super::instance::Entity",
|
2024-05-25 04:37:17 +02:00
|
|
|
from = "Column::Domain",
|
|
|
|
to = "super::instance::Column::Domain",
|
2024-05-24 05:05:14 +02:00
|
|
|
on_update = "Cascade",
|
|
|
|
on_delete = "NoAction"
|
|
|
|
)]
|
|
|
|
Instances,
|
|
|
|
#[sea_orm(has_many = "super::like::Entity")]
|
|
|
|
Likes,
|
|
|
|
#[sea_orm(has_many = "super::mention::Entity")]
|
|
|
|
Mentions,
|
|
|
|
#[sea_orm(has_many = "super::object::Entity")]
|
|
|
|
Objects,
|
|
|
|
#[sea_orm(has_many = "super::session::Entity")]
|
|
|
|
Sessions,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Related<super::activity::Entity> for Entity {
|
|
|
|
fn to() -> RelationDef {
|
|
|
|
Relation::Activities.def()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Related<super::addressing::Entity> for Entity {
|
|
|
|
fn to() -> RelationDef {
|
|
|
|
Relation::Addressing.def()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Related<super::announce::Entity> for Entity {
|
|
|
|
fn to() -> RelationDef {
|
|
|
|
Relation::Announces.def()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Related<super::config::Entity> for Entity {
|
|
|
|
fn to() -> RelationDef {
|
|
|
|
Relation::Configs.def()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Related<super::credential::Entity> for Entity {
|
|
|
|
fn to() -> RelationDef {
|
|
|
|
Relation::Credentials.def()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Related<super::delivery::Entity> for Entity {
|
|
|
|
fn to() -> RelationDef {
|
|
|
|
Relation::Deliveries.def()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Related<super::instance::Entity> for Entity {
|
|
|
|
fn to() -> RelationDef {
|
|
|
|
Relation::Instances.def()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Related<super::like::Entity> for Entity {
|
|
|
|
fn to() -> RelationDef {
|
|
|
|
Relation::Likes.def()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Related<super::mention::Entity> for Entity {
|
|
|
|
fn to() -> RelationDef {
|
|
|
|
Relation::Mentions.def()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Related<super::object::Entity> for Entity {
|
|
|
|
fn to() -> RelationDef {
|
|
|
|
Relation::Objects.def()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Related<super::session::Entity> for Entity {
|
|
|
|
fn to() -> RelationDef {
|
|
|
|
Relation::Sessions.def()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ActiveModelBehavior for ActiveModel {}
|
|
|
|
|
2024-05-25 05:31:10 +02:00
|
|
|
impl Entity {
|
|
|
|
pub fn find_by_ap_id(id: &str) -> Select<Entity> {
|
|
|
|
Entity::find().filter(Column::Id.eq(id))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn ap_to_internal(id: &str, db: &DatabaseConnection) -> crate::Result<i64> {
|
|
|
|
Entity::find()
|
|
|
|
.filter(Column::Id.eq(id))
|
|
|
|
.select_only()
|
|
|
|
.select_column(Column::Internal)
|
|
|
|
.into_tuple::<i64>()
|
|
|
|
.one(db)
|
|
|
|
.await?
|
|
|
|
.ok_or_else(UpubError::not_found)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-24 05:05:14 +02:00
|
|
|
impl ActiveModel {
|
2024-05-25 05:31:10 +02:00
|
|
|
pub fn new(object: &impl Actor) -> Result<Self, super::FieldError> {
|
2024-03-23 06:32:15 +01:00
|
|
|
let ap_id = object.id().ok_or(super::FieldError("id"))?.to_string();
|
2024-05-25 05:31:10 +02:00
|
|
|
let (domain, fallback_preferred_username) = split_user_id(&ap_id);
|
2024-05-24 05:05:14 +02:00
|
|
|
Ok(ActiveModel {
|
2024-05-25 05:31:10 +02:00
|
|
|
internal: sea_orm::ActiveValue::NotSet,
|
|
|
|
domain: sea_orm::ActiveValue::Set(domain),
|
|
|
|
id: sea_orm::ActiveValue::Set(ap_id),
|
2024-05-24 05:05:14 +02:00
|
|
|
preferred_username: sea_orm::ActiveValue::Set(object.preferred_username().unwrap_or(&fallback_preferred_username).to_string()),
|
|
|
|
actor_type: sea_orm::ActiveValue::Set(object.actor_type().ok_or(super::FieldError("type"))?),
|
|
|
|
name: sea_orm::ActiveValue::Set(object.name().map(|x| x.to_string())),
|
|
|
|
summary: sea_orm::ActiveValue::Set(object.summary().map(|x| x.to_string())),
|
|
|
|
icon: sea_orm::ActiveValue::Set(object.icon().get().and_then(|x| x.url().id())),
|
|
|
|
image: sea_orm::ActiveValue::Set(object.image().get().and_then(|x| x.url().id())),
|
|
|
|
inbox: sea_orm::ActiveValue::Set(object.inbox().id()),
|
|
|
|
outbox: sea_orm::ActiveValue::Set(object.outbox().id()),
|
|
|
|
shared_inbox: sea_orm::ActiveValue::Set(object.endpoints().get().and_then(|x| Some(x.shared_inbox()?.to_string()))),
|
|
|
|
followers: sea_orm::ActiveValue::Set(object.followers().id()),
|
|
|
|
following: sea_orm::ActiveValue::Set(object.following().id()),
|
|
|
|
created: sea_orm::ActiveValue::Set(object.published().unwrap_or(chrono::Utc::now())),
|
|
|
|
updated: sea_orm::ActiveValue::Set(chrono::Utc::now()),
|
2024-05-25 05:31:10 +02:00
|
|
|
following_count: sea_orm::ActiveValue::Set(object.following_count().unwrap_or(0) as i32),
|
|
|
|
followers_count: sea_orm::ActiveValue::Set(object.followers_count().unwrap_or(0) as i32),
|
|
|
|
statuses_count: sea_orm::ActiveValue::Set(object.statuses_count().unwrap_or(0) as i32),
|
2024-05-24 05:05:14 +02:00
|
|
|
public_key: sea_orm::ActiveValue::Set(object.public_key().get().ok_or(super::FieldError("publicKey"))?.public_key_pem().to_string()),
|
|
|
|
private_key: sea_orm::ActiveValue::Set(None), // there's no way to transport privkey over AP json, must come from DB
|
2024-03-23 06:32:15 +01:00
|
|
|
})
|
|
|
|
}
|
2024-05-24 05:05:14 +02:00
|
|
|
}
|
2024-04-19 03:28:39 +02:00
|
|
|
|
2024-05-24 05:05:14 +02:00
|
|
|
impl Model {
|
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_actor_type(Some(self.actor_type))
|
|
|
|
.set_name(self.name.as_deref())
|
|
|
|
.set_summary(self.summary.as_deref())
|
|
|
|
.set_icon(apb::Node::maybe_object(self.icon.map(|i|
|
|
|
|
serde_json::Value::new_object()
|
|
|
|
.set_document_type(Some(apb::DocumentType::Image))
|
|
|
|
.set_url(apb::Node::link(i.clone()))
|
|
|
|
)))
|
|
|
|
.set_image(apb::Node::maybe_object(self.image.map(|i|
|
|
|
|
serde_json::Value::new_object()
|
|
|
|
.set_document_type(Some(apb::DocumentType::Image))
|
|
|
|
.set_url(apb::Node::link(i.clone()))
|
|
|
|
)))
|
|
|
|
.set_published(Some(self.created))
|
|
|
|
.set_preferred_username(Some(&self.preferred_username))
|
2024-05-02 00:47:11 +02:00
|
|
|
.set_statuses_count(Some(self.statuses_count as u64))
|
|
|
|
.set_followers_count(Some(self.followers_count as u64))
|
|
|
|
.set_following_count(Some(self.following_count as u64))
|
2024-04-30 01:51:38 +02:00
|
|
|
.set_inbox(apb::Node::maybe_link(self.inbox))
|
|
|
|
.set_outbox(apb::Node::maybe_link(self.outbox))
|
|
|
|
.set_following(apb::Node::maybe_link(self.following))
|
|
|
|
.set_followers(apb::Node::maybe_link(self.followers))
|
|
|
|
.set_public_key(apb::Node::object(
|
|
|
|
serde_json::Value::new_object()
|
|
|
|
.set_id(Some(&format!("{}#main-key", self.id)))
|
|
|
|
.set_owner(Some(&self.id))
|
|
|
|
.set_public_key_pem(&self.public_key)
|
|
|
|
))
|
2024-05-15 18:51:34 +02:00
|
|
|
.set_endpoints(apb::Node::object(
|
|
|
|
serde_json::Value::new_object()
|
|
|
|
.set_shared_inbox(self.shared_inbox.as_deref())
|
|
|
|
))
|
2024-04-30 01:51:38 +02:00
|
|
|
.set_discoverable(Some(true))
|
2024-04-19 03:28:39 +02:00
|
|
|
}
|
2024-03-23 06:32:15 +01:00
|
|
|
}
|
2024-03-21 01:09:33 +01:00
|
|
|
|
2024-04-11 00:29:32 +02:00
|
|
|
fn split_user_id(id: &str) -> (String, String) {
|
|
|
|
let clean = id
|
|
|
|
.replace("http://", "")
|
|
|
|
.replace("https://", "");
|
|
|
|
let mut splits = clean.split('/');
|
|
|
|
let first = splits.next().unwrap_or("");
|
|
|
|
let last = splits.last().unwrap_or(first);
|
|
|
|
(first.to_string(), last.to_string())
|
|
|
|
}
|