feat: init

This commit is contained in:
zhenyi
2026-06-07 11:30:56 +08:00
commit 563381c1ca
361 changed files with 41327 additions and 0 deletions
+40
View File
@@ -0,0 +1,40 @@
pub mod user;
pub mod user_2fa;
pub mod user_activity;
pub mod user_appearance;
pub mod user_block;
pub mod user_device;
pub mod user_follow;
pub mod user_gpg_key;
pub mod user_mail;
pub mod user_notify_setting;
pub mod user_oauth;
pub mod user_password;
pub mod user_password_reset;
pub mod user_personal_access_token;
pub mod user_presence;
pub mod user_profile;
pub mod user_queries;
pub mod user_security_log;
pub mod user_session;
pub mod user_ssh_key;
pub use user::User;
pub use user_2fa::User2Fa;
pub use user_activity::UserActivity;
pub use user_appearance::UserAppearance;
pub use user_block::UserBlock;
pub use user_device::UserDevice;
pub use user_follow::UserFollow;
pub use user_gpg_key::UserGpgKey;
pub use user_mail::UserMail;
pub use user_notify_setting::UserNotifySetting;
pub use user_oauth::UserOAuth;
pub use user_password::UserPassword;
pub use user_password_reset::UserPasswordReset;
pub use user_personal_access_token::UserPersonalAccessToken;
pub use user_presence::UserPresence;
pub use user_profile::UserProfile;
pub use user_security_log::UserSecurityLog;
pub use user_session::UserSession;
pub use user_ssh_key::UserSshKey;
+22
View File
@@ -0,0 +1,22 @@
use crate::models::common::{Role, Status, Visibility};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, sqlx::FromRow)]
pub struct User {
pub id: Uuid,
pub username: String,
pub display_name: Option<String>,
pub avatar_url: Option<String>,
pub bio: Option<String>,
pub status: Status,
pub role: Role,
pub visibility: Visibility,
pub is_active: bool,
pub is_bot: bool,
pub last_login_at: Option<DateTime<Utc>>,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
pub deleted_at: Option<DateTime<Utc>>,
}
+13
View File
@@ -0,0 +1,13 @@
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, sqlx::FromRow)]
pub struct User2Fa {
pub user_id: Uuid,
pub secret: Option<String>,
pub backup_codes: String,
pub enabled: bool,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
}
+25
View File
@@ -0,0 +1,25 @@
use crate::models::common::{ActivityType, JsonValue};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, sqlx::FromRow)]
pub struct UserActivity {
pub id: Uuid,
pub user_id: Uuid,
pub activity_type: ActivityType,
pub name: String,
pub details: Option<String>,
pub state: Option<String>,
pub application_id: Option<String>,
pub assets: Option<JsonValue>,
pub party_id: Option<String>,
pub party_current_size: Option<i32>,
pub party_max_size: Option<i32>,
pub large_image_url: Option<String>,
pub small_image_url: Option<String>,
pub start_at: Option<DateTime<Utc>>,
pub end_at: Option<DateTime<Utc>>,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
}
+18
View File
@@ -0,0 +1,18 @@
use crate::models::common::{ColorScheme, Density, FontSize, Theme};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, sqlx::FromRow)]
pub struct UserAppearance {
pub user_id: Uuid,
pub theme: Theme,
pub color_scheme: ColorScheme,
pub density: Density,
pub font_size: FontSize,
pub editor_theme: Option<String>,
pub markdown_preview: bool,
pub reduced_motion: bool,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
}
+11
View File
@@ -0,0 +1,11 @@
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, sqlx::FromRow)]
pub struct UserBlock {
pub blocker_id: Uuid,
pub blocked_id: Uuid,
pub reason: Option<String>,
pub created_at: DateTime<Utc>,
}
+19
View File
@@ -0,0 +1,19 @@
use crate::models::common::DeviceType;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, sqlx::FromRow)]
pub struct UserDevice {
pub id: Uuid,
pub user_id: Uuid,
pub device_name: String,
pub device_type: DeviceType,
pub fingerprint: Option<String>,
pub ip_address: Option<String>,
pub user_agent: Option<String>,
pub trusted: bool,
pub last_seen_at: Option<DateTime<Utc>>,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
}
+10
View File
@@ -0,0 +1,10 @@
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, sqlx::FromRow)]
pub struct UserFollow {
pub follower_id: Uuid,
pub following_id: Uuid,
pub created_at: DateTime<Utc>,
}
+18
View File
@@ -0,0 +1,18 @@
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, sqlx::FromRow)]
pub struct UserGpgKey {
pub id: Uuid,
pub user_id: Uuid,
pub key_id: String,
pub public_key: String,
pub fingerprint: String,
pub primary_email: Option<String>,
pub expires_at: Option<DateTime<Utc>>,
pub verified_at: Option<DateTime<Utc>>,
pub revoked_at: Option<DateTime<Utc>>,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
}
+16
View File
@@ -0,0 +1,16 @@
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, sqlx::FromRow)]
pub struct UserMail {
pub id: Uuid,
pub user_id: Uuid,
pub email: String,
pub is_primary: bool,
pub is_verified: bool,
pub verification_token_hash: Option<String>,
pub verified_at: Option<DateTime<Utc>>,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
}
+18
View File
@@ -0,0 +1,18 @@
use crate::models::common::DigestFrequency;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, sqlx::FromRow)]
pub struct UserNotifySetting {
pub user_id: Uuid,
pub email_notifications: bool,
pub web_notifications: bool,
pub mention_notifications: bool,
pub review_notifications: bool,
pub security_notifications: bool,
pub marketing_emails: bool,
pub digest_frequency: DigestFrequency,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
}
+19
View File
@@ -0,0 +1,19 @@
use crate::models::common::Provider;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, sqlx::FromRow)]
pub struct UserOAuth {
pub id: Uuid,
pub user_id: Uuid,
pub provider: Provider,
pub provider_user_id: String,
pub provider_username: Option<String>,
pub provider_email: Option<String>,
pub access_token_ciphertext: Option<String>,
pub refresh_token_ciphertext: Option<String>,
pub token_expires_at: Option<DateTime<Utc>>,
pub linked_at: DateTime<Utc>,
pub last_used_at: Option<DateTime<Utc>>,
}
+16
View File
@@ -0,0 +1,16 @@
use crate::models::common::PasswordAlgorithm;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, sqlx::FromRow)]
pub struct UserPassword {
pub user_id: Uuid,
pub password_hash: String,
pub password_algo: PasswordAlgorithm,
pub password_salt: Option<String>,
pub must_change_password: bool,
pub password_updated_at: DateTime<Utc>,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
}
+15
View File
@@ -0,0 +1,15 @@
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, sqlx::FromRow)]
pub struct UserPasswordReset {
pub id: Uuid,
pub user_id: Uuid,
pub token_hash: String,
pub requested_ip: Option<String>,
pub user_agent: Option<String>,
pub expires_at: DateTime<Utc>,
pub used_at: Option<DateTime<Utc>>,
pub created_at: DateTime<Utc>,
}
@@ -0,0 +1,18 @@
use crate::models::common::Scope;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, sqlx::FromRow)]
pub struct UserPersonalAccessToken {
pub id: Uuid,
pub user_id: Uuid,
pub name: String,
pub token_hash: String,
pub scopes: Vec<Scope>,
pub last_used_at: Option<DateTime<Utc>>,
pub expires_at: Option<DateTime<Utc>>,
pub revoked_at: Option<DateTime<Utc>>,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
}
+19
View File
@@ -0,0 +1,19 @@
use crate::models::common::{DeviceType, PresenceStatus};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, sqlx::FromRow)]
pub struct UserPresence {
pub id: Uuid,
pub user_id: Uuid,
pub status: PresenceStatus,
pub custom_status_text: Option<String>,
pub custom_status_emoji: Option<String>,
pub device_type: Option<DeviceType>,
pub ip_address: Option<String>,
pub last_active_at: DateTime<Utc>,
pub last_seen_at: Option<DateTime<Utc>>,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
}
+34
View File
@@ -0,0 +1,34 @@
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, sqlx::FromRow)]
pub struct UserProfile {
pub user_id: Uuid,
pub full_name: Option<String>,
pub company: Option<String>,
pub location: Option<String>,
pub website_url: Option<String>,
pub twitter_username: Option<String>,
pub timezone: Option<String>,
pub language: Option<String>,
pub profile_readme: Option<String>,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
}
impl UserProfile {
pub async fn find_by_user_id(
pool: &sqlx::PgPool,
user_id: Uuid,
) -> Result<Option<Self>, sqlx::Error> {
sqlx::query_as::<_, UserProfile>(
"SELECT user_id, full_name, company, location, website_url, twitter_username, \
timezone, language, profile_readme, created_at, updated_at \
FROM user_profile WHERE user_id = $1",
)
.bind(user_id)
.fetch_optional(pool)
.await
}
}
+99
View File
@@ -0,0 +1,99 @@
//! SQL query methods for the `User` entity.
//!
//! These methods operate on the database directly, returning `sqlx::Error`.
//! The service layer is responsible for converting errors into `AppError`.
use sqlx::PgPool;
use uuid::Uuid;
use super::user::User;
impl User {
/// Find an active, non-deleted user by primary key.
pub async fn find_by_id(pool: &PgPool, id: Uuid) -> Result<Option<Self>, sqlx::Error> {
sqlx::query_as::<_, User>(
r#"SELECT id, username, display_name, avatar_url, bio, status, role, visibility,
is_active, is_bot, last_login_at, created_at, updated_at, deleted_at
FROM "user"
WHERE id = $1 AND is_active = true AND status = 'active' AND deleted_at IS NULL"#,
)
.bind(id)
.fetch_optional(pool)
.await
}
/// Find an active user by username (case-insensitive).
pub async fn find_by_username(
pool: &PgPool,
username: &str,
) -> Result<Option<Self>, sqlx::Error> {
sqlx::query_as::<_, User>(
r#"SELECT id, username, display_name, avatar_url, bio, status, role, visibility,
is_active, is_bot, last_login_at, created_at, updated_at, deleted_at
FROM "user"
WHERE lower(username) = lower($1)
AND is_active = true AND status = 'active' AND deleted_at IS NULL"#,
)
.bind(username)
.fetch_optional(pool)
.await
}
/// Find an active user by verified email (case-insensitive).
pub async fn find_by_email(pool: &PgPool, email: &str) -> Result<Option<Self>, sqlx::Error> {
sqlx::query_as::<_, User>(
r#"SELECT u.id, u.username, u.display_name, u.avatar_url, u.bio, u.status, u.role,
u.visibility, u.is_active, u.is_bot, u.last_login_at,
u.created_at, u.updated_at, u.deleted_at
FROM "user" u
INNER JOIN user_mail e ON e.user_id = u.id
WHERE lower(e.email) = lower($1)
AND e.is_verified = true
AND u.is_active = true AND u.status = 'active' AND u.deleted_at IS NULL"#,
)
.bind(email)
.fetch_optional(pool)
.await
}
/// Check if a username already exists (excluding a given user id).
pub async fn username_exists(
pool: &PgPool,
username: &str,
exclude_id: Option<Uuid>,
) -> Result<bool, sqlx::Error> {
let exists = if let Some(id) = exclude_id {
sqlx::query_scalar::<_, bool>(
r#"SELECT EXISTS(
SELECT 1 FROM "user"
WHERE lower(username) = lower($1) AND id <> $2 AND deleted_at IS NULL
)"#,
)
.bind(username)
.bind(id)
.fetch_one(pool)
.await?
} else {
sqlx::query_scalar::<_, bool>(
r#"SELECT EXISTS(
SELECT 1 FROM "user"
WHERE lower(username) = lower($1) AND deleted_at IS NULL
)"#,
)
.bind(username)
.fetch_one(pool)
.await?
};
Ok(exists)
}
/// Update the last_login_at timestamp.
pub async fn touch_login(pool: &PgPool, id: Uuid) -> Result<(), sqlx::Error> {
sqlx::query(r#"UPDATE "user" SET last_login_at = $1, updated_at = $1 WHERE id = $2"#)
.bind(chrono::Utc::now())
.bind(id)
.execute(pool)
.await?;
Ok(())
}
}
+16
View File
@@ -0,0 +1,16 @@
use crate::models::common::{EventType, JsonValue};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, sqlx::FromRow)]
pub struct UserSecurityLog {
pub id: Uuid,
pub user_id: Uuid,
pub event_type: EventType,
pub description: Option<String>,
pub ip_address: Option<String>,
pub user_agent: Option<String>,
pub metadata: Option<JsonValue>,
pub created_at: DateTime<Utc>,
}
+17
View File
@@ -0,0 +1,17 @@
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, sqlx::FromRow)]
pub struct UserSession {
pub id: Uuid,
pub user_id: Uuid,
pub session_token_hash: String,
pub refresh_token_hash: Option<String>,
pub ip_address: Option<String>,
pub user_agent: Option<String>,
pub last_active_at: DateTime<Utc>,
pub expires_at: DateTime<Utc>,
pub revoked_at: Option<DateTime<Utc>>,
pub created_at: DateTime<Utc>,
}
+19
View File
@@ -0,0 +1,19 @@
use crate::models::common::KeyType;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, sqlx::FromRow)]
pub struct UserSshKey {
pub id: Uuid,
pub user_id: Uuid,
pub title: String,
pub public_key: String,
pub fingerprint_sha256: String,
pub key_type: KeyType,
pub last_used_at: Option<DateTime<Utc>>,
pub expires_at: Option<DateTime<Utc>>,
pub revoked_at: Option<DateTime<Utc>>,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
}