2024-03-16 03:27:36 +01:00
|
|
|
use sea_orm::entity::prelude::*;
|
|
|
|
|
2024-04-14 03:39:52 +02:00
|
|
|
use apb::{Collection, Object, Actor, PublicKey, ActorType};
|
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)]
|
|
|
|
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-23 16:44:27 +01:00
|
|
|
pub following_count: i64,
|
|
|
|
pub followers_count: i64,
|
2024-04-18 04:09:13 +02:00
|
|
|
pub statuses_count: i64,
|
2024-03-23 16:44:27 +01:00
|
|
|
|
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-23 06:32:15 +01:00
|
|
|
impl Model {
|
|
|
|
pub fn new(object: &impl Actor) -> Result<Self, super::FieldError> {
|
|
|
|
let ap_id = object.id().ok_or(super::FieldError("id"))?.to_string();
|
2024-04-11 00:29:32 +02:00
|
|
|
let (domain, preferred_username) = split_user_id(&ap_id);
|
2024-03-23 06:32:15 +01:00
|
|
|
Ok(Model {
|
|
|
|
id: ap_id, preferred_username, domain,
|
|
|
|
actor_type: object.actor_type().ok_or(super::FieldError("type"))?,
|
|
|
|
name: object.name().map(|x| x.to_string()),
|
|
|
|
summary: object.summary().map(|x| x.to_string()),
|
2024-04-14 03:48:37 +02:00
|
|
|
icon: object.icon().get().map(|x| x.url().id().unwrap_or_default()),
|
2024-04-14 03:39:52 +02:00
|
|
|
image: object.image().get().map(|x| x.url().id().unwrap_or_default()),
|
2024-04-14 03:48:37 +02:00
|
|
|
inbox: object.inbox().id(),
|
|
|
|
outbox: object.outbox().id(),
|
2024-03-23 06:32:15 +01:00
|
|
|
shared_inbox: None, // TODO!!! parse endpoints
|
2024-04-06 16:56:13 +02:00
|
|
|
followers: object.followers().id(),
|
|
|
|
following: object.following().id(),
|
2024-03-23 06:32:15 +01:00
|
|
|
created: object.published().unwrap_or(chrono::Utc::now()),
|
|
|
|
updated: chrono::Utc::now(),
|
2024-03-23 16:44:27 +01:00
|
|
|
following_count: object.following().get().map(|f| f.total_items().unwrap_or(0)).unwrap_or(0) as i64,
|
|
|
|
followers_count: object.followers().get().map(|f| f.total_items().unwrap_or(0)).unwrap_or(0) as i64,
|
2024-04-18 04:09:13 +02:00
|
|
|
statuses_count: object.outbox().get().map(|o| o.total_items().unwrap_or(0)).unwrap_or(0) as i64,
|
2024-03-23 06:32:15 +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-21 01:09:33 +01:00
|
|
|
|
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-24 04:03:22 +01:00
|
|
|
|
|
|
|
#[sea_orm(has_many = "super::session::Entity")]
|
|
|
|
Session,
|
2024-03-24 04:03:44 +01:00
|
|
|
|
|
|
|
#[sea_orm(has_many = "super::addressing::Entity")]
|
|
|
|
Addressing,
|
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
|
|
|
|
2024-03-23 06:10:53 +01:00
|
|
|
impl Related<super::config::Entity> for Entity {
|
|
|
|
fn to() -> RelationDef {
|
|
|
|
Relation::Config.def()
|
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-23 06:10:53 +01:00
|
|
|
impl Related<super::credential::Entity> for Entity {
|
|
|
|
fn to() -> RelationDef {
|
|
|
|
Relation::Credential.def()
|
2024-03-21 01:09:33 +01:00
|
|
|
}
|
2024-03-16 03:27:36 +01:00
|
|
|
}
|
2024-03-16 20:09:06 +01:00
|
|
|
|
2024-03-24 04:03:22 +01:00
|
|
|
impl Related<super::session::Entity> for Entity {
|
|
|
|
fn to() -> RelationDef {
|
|
|
|
Relation::Session.def()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-24 04:03:44 +01:00
|
|
|
impl Related<super::addressing::Entity> for Entity {
|
|
|
|
fn to() -> RelationDef {
|
|
|
|
Relation::Addressing.def()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-23 06:10:53 +01:00
|
|
|
impl ActiveModelBehavior for ActiveModel {}
|
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())
|
|
|
|
}
|