10890d2
use models::users::user::{self, ProfileVisibility};
use sea_orm::{ActiveModelTrait, EntityTrait, Set};
use time::OffsetDateTime;
use crate::auth::errors::AuthError;
use crate::auth::services::AuthService;
/// Ensure the local settings row for an authenticated account exists.
///
/// Identity fields are not copied here: KenApi reads username, display name,
/// email, account state and avatar from KenOAuth on demand. This row only
/// anchors local preferences and foreign keys.
pub async fn upsert(
service: &AuthService,
public: &auth::protos::auth::common::v1::UserPublic,
) -> Result<user::Model, AuthError> {
let db = service.db().write();
let now = OffsetDateTime::now_utc();
let existing = user::Entity::find_by_id(public.user_id).one(db).await?;
match existing {
Some(row) => {
let mut am: user::ActiveModel = row.into();
am.last_login_at = Set(Some(now));
am.updated_at = Set(now);
Ok(am.update(db).await?)
}
None => Ok(user::ActiveModel {
uid: Set(public.user_id),
is_restricted: Set(false),
visibility: Set(ProfileVisibility::Public),
last_login_at: Set(Some(now)),
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(now),
}
.insert(db)
.await?),
}
}