use proto::auth::common::v1::UserPublic; use crate::error::ExportError; #[derive(Debug, Clone, serde::Serialize, serde::Deserialize, utoipa::ToSchema)] pub struct UserDto { pub user_id: i64, pub username: String, pub display_name: String, pub avatar_url: String, } impl UserDto { pub fn from_public(public: &UserPublic) -> Self { Self { user_id: public.user_id, username: public.username.clone(), display_name: if public.displayname.trim().is_empty() { public.username.clone() } else { public.displayname.clone() }, avatar_url: crate::urls::resolve_user_avatar_url(public.user_id, &public.avatar_url), } } } /// Create the local settings/anchor row on demand. It contains no identity /// projection; it exists so local preferences and foreign keys have a stable /// row even when the user never went through KenApi's login callback. pub async fn ensure_local_user( database: &impl sea_orm::ConnectionTrait, uid: i64, ) -> Result<(), ExportError> { use models::users::user::{self, ProfileVisibility}; use sea_orm::{ActiveModelTrait, EntityTrait, Set}; let db = database; if user::Entity::find_by_id(uid).one(db).await?.is_some() { return Ok(()); } user::ActiveModel { uid: Set(uid), is_restricted: Set(false), visibility: Set(ProfileVisibility::Public), last_login_at: Set(None), last_login_ip_hash: Set(None), local_avatar_url: Set(String::new()), timezone: Set(None), locale: Set(None), language: Set(None), version: Set(1), updated_at: Set(time::OffsetDateTime::now_utc()), } .insert(db) .await?; Ok(()) }