use crate::{ schema::{person, person_alias_1, person_alias_2}, DbUrl, PersonId, }; use chrono::NaiveDateTime; use diesel::{ExpressionMethods, PgConnection, QueryDsl, RunQueryDsl}; use lemmy_apub_lib::traits::{ActorType, ApubObject}; use lemmy_utils::LemmyError; use serde::Serialize; use url::Url; #[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] #[table_name = "person"] pub struct Person { pub id: PersonId, pub name: String, pub display_name: Option, pub avatar: Option, pub banned: bool, pub published: chrono::NaiveDateTime, pub updated: Option, pub actor_id: DbUrl, pub bio: Option, pub local: bool, pub private_key: Option, pub public_key: Option, pub last_refreshed_at: chrono::NaiveDateTime, pub banner: Option, pub deleted: bool, pub inbox_url: DbUrl, pub shared_inbox_url: Option, pub matrix_user_id: Option, pub admin: bool, pub bot_account: bool, } /// A safe representation of person, without the sensitive info #[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] #[table_name = "person"] pub struct PersonSafe { pub id: PersonId, pub name: String, pub display_name: Option, pub avatar: Option, pub banned: bool, pub published: chrono::NaiveDateTime, pub updated: Option, pub actor_id: DbUrl, pub bio: Option, pub local: bool, pub banner: Option, pub deleted: bool, pub inbox_url: DbUrl, pub shared_inbox_url: Option, pub matrix_user_id: Option, pub admin: bool, pub bot_account: bool, } #[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] #[table_name = "person_alias_1"] pub struct PersonAlias1 { pub id: PersonId, pub name: String, pub display_name: Option, pub avatar: Option, pub banned: bool, pub published: chrono::NaiveDateTime, pub updated: Option, pub actor_id: DbUrl, pub bio: Option, pub local: bool, pub private_key: Option, pub public_key: Option, pub last_refreshed_at: chrono::NaiveDateTime, pub banner: Option, pub deleted: bool, pub inbox_url: DbUrl, pub shared_inbox_url: Option, pub matrix_user_id: Option, pub admin: bool, pub bot_account: bool, } #[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] #[table_name = "person_alias_1"] pub struct PersonSafeAlias1 { pub id: PersonId, pub name: String, pub display_name: Option, pub avatar: Option, pub banned: bool, pub published: chrono::NaiveDateTime, pub updated: Option, pub actor_id: DbUrl, pub bio: Option, pub local: bool, pub banner: Option, pub deleted: bool, pub inbox_url: DbUrl, pub shared_inbox_url: Option, pub matrix_user_id: Option, pub admin: bool, pub bot_account: bool, } #[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] #[table_name = "person_alias_2"] pub struct PersonAlias2 { pub id: PersonId, pub name: String, pub display_name: Option, pub avatar: Option, pub banned: bool, pub published: chrono::NaiveDateTime, pub updated: Option, pub actor_id: DbUrl, pub bio: Option, pub local: bool, pub private_key: Option, pub public_key: Option, pub last_refreshed_at: chrono::NaiveDateTime, pub banner: Option, pub deleted: bool, pub inbox_url: DbUrl, pub shared_inbox_url: Option, pub matrix_user_id: Option, pub admin: bool, pub bot_account: bool, } #[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] #[table_name = "person_alias_1"] pub struct PersonSafeAlias2 { pub id: PersonId, pub name: String, pub display_name: Option, pub avatar: Option, pub banned: bool, pub published: chrono::NaiveDateTime, pub updated: Option, pub actor_id: DbUrl, pub bio: Option, pub local: bool, pub banner: Option, pub deleted: bool, pub inbox_url: DbUrl, pub shared_inbox_url: Option, pub matrix_user_id: Option, pub admin: bool, pub bot_account: bool, } #[derive(Insertable, AsChangeset, Clone, Default)] #[table_name = "person"] pub struct PersonForm { pub name: String, pub display_name: Option>, pub avatar: Option>, pub banned: Option, pub published: Option, pub updated: Option, pub actor_id: Option, pub bio: Option>, pub local: Option, pub private_key: Option>, pub public_key: Option>, pub last_refreshed_at: Option, pub banner: Option>, pub deleted: Option, pub inbox_url: Option, pub shared_inbox_url: Option>, pub matrix_user_id: Option>, pub admin: Option, pub bot_account: Option, } impl ApubObject for Person { type DataType = PgConnection; fn last_refreshed_at(&self) -> Option { Some(self.last_refreshed_at) } fn read_from_apub_id(conn: &PgConnection, object_id: Url) -> Result, LemmyError> { use crate::schema::person::dsl::*; let object_id: DbUrl = object_id.into(); Ok( person .filter(deleted.eq(false)) .filter(actor_id.eq(object_id)) .first::(conn) .ok(), ) } } impl ActorType for Person { fn is_local(&self) -> bool { self.local } fn actor_id(&self) -> Url { self.actor_id.to_owned().into_inner() } fn name(&self) -> String { self.name.clone() } fn public_key(&self) -> Option { self.public_key.to_owned() } fn private_key(&self) -> Option { self.private_key.to_owned() } fn inbox_url(&self) -> Url { self.inbox_url.clone().into() } fn shared_inbox_url(&self) -> Option { self.shared_inbox_url.clone().map(|s| s.into_inner()) } }