Files
2026-06-07 11:30:56 +08:00

48 lines
1.5 KiB
Rust

use serde::{Deserialize, Serialize};
use crate::error::AppError;
use crate::service::AuthService;
use crate::session::Session;
#[derive(Deserialize, Serialize, Clone, Debug, utoipa::ToSchema)]
pub struct ContextMe {
pub id: uuid::Uuid,
pub username: String,
pub display_name: Option<String>,
pub avatar_url: Option<String>,
pub has_unread_notifications: u64,
pub language: String,
pub timezone: String,
}
impl AuthService {
pub async fn auth_me(&self, ctx: Session) -> Result<ContextMe, AppError> {
let user_id = ctx.user().ok_or(AppError::Unauthorized)?;
let user = self.auth_find_user_by_uid(user_id).await?;
let profile =
crate::models::users::UserProfile::find_by_user_id(self.ctx.db.reader(), user_id)
.await
.map_err(AppError::Database)
.ok()
.flatten();
Ok(ContextMe {
id: user.id,
username: user.username,
display_name: user.display_name.filter(|n| !n.is_empty()),
avatar_url: user.avatar_url.filter(|u| !u.is_empty()),
has_unread_notifications: 0,
language: profile
.as_ref()
.and_then(|p| p.language.clone())
.filter(|v| !v.is_empty())
.unwrap_or_else(|| "en".to_string()),
timezone: profile
.as_ref()
.and_then(|p| p.timezone.clone())
.filter(|v| !v.is_empty())
.unwrap_or_else(|| "UTC".to_string()),
})
}
}