gitdataai/libs/service/auth/me.rs

44 lines
1.4 KiB
Rust

use crate::AppService;
use crate::error::AppError;
use models::users::user_preferences;
use sea_orm::*;
use serde::{Deserialize, Serialize};
use session::Session;
use uuid::Uuid;
#[derive(Deserialize, Serialize, Clone, Debug, utoipa::ToSchema)]
pub struct ContextMe {
pub uid: 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.utils_find_user_by_uid(user_id).await?;
let preferences = user_preferences::Entity::find_by_id(user_id)
.one(&self.db)
.await?;
Ok(ContextMe {
uid: user.uid,
username: user.username,
display_name: user.display_name,
avatar_url: user.avatar_url,
has_unread_notifications: 0,
language: preferences
.as_ref()
.map(|prefs| prefs.language.clone())
.unwrap_or_else(|| "en".to_string()),
timezone: preferences
.as_ref()
.map(|prefs| prefs.timezone.clone())
.unwrap_or_else(|| "UTC".to_string()),
})
}
}