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, pub avatar_url: Option, pub has_unread_notifications: u64, pub language: String, pub timezone: String, } impl AuthService { pub async fn auth_me(&self, ctx: Session) -> Result { 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()), }) } }