gitdataai/lib/service/auth/me.rs
2026-05-30 01:38:40 +08:00

42 lines
1.2 KiB
Rust

use serde::{Deserialize, Serialize};
use session::Session;
use crate::{AppService, error::AppError};
#[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 AppService {
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 unread = self.unread_notifications_count(user_id).await.unwrap_or(0);
Ok(ContextMe {
id: user.id,
username: user.username,
display_name: if user.display_name.is_empty() {
None
} else {
Some(user.display_name)
},
avatar_url: if user.avatar_url.is_empty() {
None
} else {
Some(user.avatar_url)
},
has_unread_notifications: unread as u64,
language: "en".to_string(),
timezone: "UTC".to_string(),
})
}
}