feat(service): expand service layer with new domain operations
- Add IM service modules: audit, channel roles, custom emojis, forum tags, integrations, invitations, repo links, slash commands, stages, voice, webhooks - Add PR service modules: review requests, templates - Add repo service modules: contributors, release assets, git extras (archive, branch rename, commit extras, diff/merge, tag, tree) - Add user service: social (follow/block) - Add internal auth service - Update existing service modules with expanded functionality - Remove deleted IM modules: articles, delivery trace, drafts, follows, messages, polls, presence, reactions, threads
This commit is contained in:
+190
-40
@@ -3,11 +3,12 @@ use serde::{Deserialize, Serialize};
|
||||
use crate::error::AppError;
|
||||
use crate::models::common::Visibility;
|
||||
use crate::models::users::User;
|
||||
use crate::pb::email::{EmailAddress, SendEmailRequest};
|
||||
use crate::service::UserService;
|
||||
use crate::session::Session;
|
||||
|
||||
use super::util::{merge_optional_text, parse_enum};
|
||||
use crate::service::util::extract_storage_key_from_url;
|
||||
use crate::service::util::{extract_storage_key_from_url, sha256_hex};
|
||||
|
||||
#[derive(Deserialize, Serialize, Clone, Debug, utoipa::ToSchema)]
|
||||
pub struct UpdateUserAccountParams {
|
||||
@@ -17,20 +18,9 @@ pub struct UpdateUserAccountParams {
|
||||
pub visibility: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, Clone, Debug, utoipa::ToSchema)]
|
||||
pub struct UploadUserAvatarParams {
|
||||
pub data: Vec<u8>,
|
||||
pub content_type: Option<String>,
|
||||
pub file_name: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, Clone, Debug, utoipa::ToSchema)]
|
||||
pub struct UserAvatarResponse {
|
||||
pub avatar_url: String,
|
||||
pub storage_key: String,
|
||||
}
|
||||
|
||||
impl UserService {
|
||||
const RESTORE_TOKEN_VALIDITY_DAYS: i64 = 30;
|
||||
|
||||
pub async fn user_account(&self, ctx: &Session) -> Result<User, AppError> {
|
||||
let user_uid = ctx.user().ok_or(AppError::Unauthorized)?;
|
||||
crate::models::users::User::find_by_id(self.ctx.db.reader(), user_uid)
|
||||
@@ -83,11 +73,13 @@ impl UserService {
|
||||
pub async fn user_upload_avatar(
|
||||
&self,
|
||||
ctx: &Session,
|
||||
params: UploadUserAvatarParams,
|
||||
) -> Result<UserAvatarResponse, AppError> {
|
||||
data: Vec<u8>,
|
||||
content_type: Option<String>,
|
||||
file_name: Option<String>,
|
||||
) -> Result<(String, String), AppError> {
|
||||
let user_uid = ctx.user().ok_or(AppError::Unauthorized)?;
|
||||
let ext = avatar_extension(params.content_type.as_deref(), params.file_name.as_deref())?;
|
||||
validate_avatar_size(params.data.len(), self.ctx.config.s3_max_upload_size()?)?;
|
||||
let ext = avatar_extension(content_type.as_deref(), file_name.as_deref())?;
|
||||
validate_avatar_size(data.len(), self.ctx.config.s3_max_upload_size()?)?;
|
||||
|
||||
let current = crate::models::users::User::find_by_id(self.ctx.db.reader(), user_uid)
|
||||
.await
|
||||
@@ -96,7 +88,7 @@ impl UserService {
|
||||
let old_avatar_url = current.avatar_url.clone();
|
||||
|
||||
let storage_key = format!("users/{}/avatar/{}.{}", user_uid, uuid::Uuid::now_v7(), ext);
|
||||
self.ctx.storage.put(&storage_key, params.data).await?;
|
||||
self.ctx.storage.put(&storage_key, data).await?;
|
||||
let avatar_url = self.ctx.storage.public_url(&storage_key).ok_or_else(|| {
|
||||
AppError::Config("APP_S3_PUBLIC_URL is required for avatar upload".into())
|
||||
})?;
|
||||
@@ -123,10 +115,7 @@ impl UserService {
|
||||
let _ = self.ctx.storage.delete(&old_key).await;
|
||||
}
|
||||
|
||||
Ok(UserAvatarResponse {
|
||||
avatar_url,
|
||||
storage_key,
|
||||
})
|
||||
Ok((avatar_url, storage_key))
|
||||
}
|
||||
|
||||
pub async fn user_delete_account(&self, ctx: &Session) -> Result<(), AppError> {
|
||||
@@ -158,6 +147,120 @@ impl UserService {
|
||||
));
|
||||
}
|
||||
|
||||
let has_verified_email: bool = sqlx::query_scalar(
|
||||
"SELECT EXISTS(SELECT 1 FROM user_mail WHERE user_id = $1 AND is_verified = true AND deleted_at IS NULL)",
|
||||
)
|
||||
.bind(user_uid)
|
||||
.fetch_one(self.ctx.db.reader())
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
if !has_verified_email {
|
||||
return Err(AppError::BadRequest(
|
||||
"please add and verify an email address before deleting your account".into(),
|
||||
));
|
||||
}
|
||||
|
||||
let primary_email: Option<String> = sqlx::query_scalar(
|
||||
"SELECT email FROM user_mail WHERE user_id = $1 AND is_verified = true AND is_primary = true AND deleted_at IS NULL LIMIT 1",
|
||||
)
|
||||
.bind(user_uid)
|
||||
.fetch_optional(self.ctx.db.reader())
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
let fallback_email: Option<String> = sqlx::query_scalar(
|
||||
"SELECT email FROM user_mail WHERE user_id = $1 AND is_verified = true AND deleted_at IS NULL ORDER BY created_at LIMIT 1",
|
||||
)
|
||||
.bind(user_uid)
|
||||
.fetch_optional(self.ctx.db.reader())
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
let email = primary_email.or(fallback_email);
|
||||
|
||||
let now = chrono::Utc::now();
|
||||
let restore_token = uuid::Uuid::now_v7().to_string();
|
||||
let token_hash = sha256_hex(restore_token.as_bytes());
|
||||
let expires_at = now + chrono::Duration::days(Self::RESTORE_TOKEN_VALIDITY_DAYS);
|
||||
|
||||
let mut txn = self
|
||||
.ctx
|
||||
.db
|
||||
.writer()
|
||||
.begin()
|
||||
.await
|
||||
.map_err(|_| AppError::TxnError)?;
|
||||
|
||||
for statement in [
|
||||
"UPDATE user_personal_access_token SET revoked_at = $1 WHERE user_id = $2 AND revoked_at IS NULL",
|
||||
"UPDATE user_session SET revoked_at = $1 WHERE user_id = $2 AND revoked_at IS NULL",
|
||||
"UPDATE user_ssh_key SET revoked_at = $1 WHERE user_id = $2 AND revoked_at IS NULL",
|
||||
"UPDATE user_gpg_key SET revoked_at = $1 WHERE user_id = $2 AND revoked_at IS NULL",
|
||||
"UPDATE workspace_member SET status = 'deleted' WHERE user_id = $2 AND status != 'deleted'",
|
||||
"UPDATE repo_member SET status = 'deleted' WHERE user_id = $2 AND status != 'deleted'",
|
||||
"UPDATE user_2fa SET deleted_at = $1 WHERE user_id = $2 AND deleted_at IS NULL",
|
||||
"UPDATE user_activity SET deleted_at = $1 WHERE user_id = $2 AND deleted_at IS NULL",
|
||||
"UPDATE user_appearance SET deleted_at = $1 WHERE user_id = $2 AND deleted_at IS NULL",
|
||||
"UPDATE user_block SET deleted_at = $1 WHERE (user_id = $2 OR blocked_user_id = $2) AND deleted_at IS NULL",
|
||||
"UPDATE user_device SET deleted_at = $1 WHERE user_id = $2 AND deleted_at IS NULL",
|
||||
"UPDATE user_follow SET deleted_at = $1 WHERE (user_id = $2 OR following_user_id = $2) AND deleted_at IS NULL",
|
||||
"UPDATE user_mail SET deleted_at = $1 WHERE user_id = $2 AND deleted_at IS NULL",
|
||||
"UPDATE user_notify_setting SET deleted_at = $1 WHERE user_id = $2 AND deleted_at IS NULL",
|
||||
"UPDATE user_oauth SET deleted_at = $1 WHERE user_id = $2 AND deleted_at IS NULL",
|
||||
"UPDATE user_password SET deleted_at = $1 WHERE user_id = $2 AND deleted_at IS NULL",
|
||||
"UPDATE user_password_reset SET deleted_at = $1 WHERE user_id = $2 AND deleted_at IS NULL",
|
||||
"UPDATE user_presence SET deleted_at = $1 WHERE user_id = $2 AND deleted_at IS NULL",
|
||||
"UPDATE user_profile SET deleted_at = $1 WHERE user_id = $2 AND deleted_at IS NULL",
|
||||
"UPDATE user_security_log SET deleted_at = $1 WHERE user_id = $2 AND deleted_at IS NULL",
|
||||
] {
|
||||
sqlx::query(statement)
|
||||
.bind(now)
|
||||
.bind(user_uid)
|
||||
.execute(&mut *txn)
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
}
|
||||
|
||||
let result = sqlx::query(
|
||||
"UPDATE \"user\" SET deleted_at = $1, is_active = false, status = 'deleted', \
|
||||
restore_token_hash = $2, restore_token_expires_at = $3, updated_at = $1 \
|
||||
WHERE id = $4 AND deleted_at IS NULL",
|
||||
)
|
||||
.bind(now)
|
||||
.bind(&token_hash)
|
||||
.bind(expires_at)
|
||||
.bind(user_uid)
|
||||
.execute(&mut *txn)
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
if result.rows_affected() == 0 {
|
||||
return Err(AppError::UserNotFound);
|
||||
}
|
||||
|
||||
txn.commit().await.map_err(|_| AppError::TxnError)?;
|
||||
|
||||
if let Some(email) = email {
|
||||
let _ = self.send_restore_email(&email, &restore_token).await;
|
||||
}
|
||||
|
||||
ctx.clear();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn user_restore(&self, token: &str) -> Result<(), AppError> {
|
||||
let token_hash = sha256_hex(token.as_bytes());
|
||||
|
||||
let user_id: Option<uuid::Uuid> = sqlx::query_scalar(
|
||||
"SELECT id FROM \"user\" WHERE restore_token_hash = $1 \
|
||||
AND deleted_at IS NOT NULL \
|
||||
AND restore_token_expires_at > NOW()",
|
||||
)
|
||||
.bind(&token_hash)
|
||||
.fetch_optional(self.ctx.db.reader())
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
|
||||
let user_uid =
|
||||
user_id.ok_or(AppError::NotFound("invalid or expired restore link".into()))?;
|
||||
|
||||
let now = chrono::Utc::now();
|
||||
let mut txn = self
|
||||
.ctx
|
||||
@@ -168,20 +271,26 @@ impl UserService {
|
||||
.map_err(|_| AppError::TxnError)?;
|
||||
|
||||
for statement in [
|
||||
"DELETE FROM user_personal_access_token WHERE user_id = $1",
|
||||
"DELETE FROM user_security_log WHERE user_id = $1",
|
||||
"DELETE FROM user_session WHERE user_id = $1",
|
||||
"DELETE FROM user_device WHERE user_id = $1",
|
||||
"DELETE FROM user_oauth WHERE user_id = $1",
|
||||
"DELETE FROM user_ssh_key WHERE user_id = $1",
|
||||
"DELETE FROM user_gpg_key WHERE user_id = $1",
|
||||
"DELETE FROM user_2fa WHERE user_id = $1",
|
||||
"DELETE FROM user_notify_setting WHERE user_id = $1",
|
||||
"DELETE FROM user_appearance WHERE user_id = $1",
|
||||
"DELETE FROM user_profile WHERE user_id = $1",
|
||||
"DELETE FROM user_mail WHERE user_id = $1",
|
||||
"DELETE FROM workspace_member WHERE user_id = $1",
|
||||
"DELETE FROM repo_member WHERE user_id = $1",
|
||||
"UPDATE user_personal_access_token SET revoked_at = NULL WHERE user_id = $1 AND revoked_at IS NOT NULL",
|
||||
"UPDATE user_session SET revoked_at = NULL WHERE user_id = $1 AND revoked_at IS NOT NULL",
|
||||
"UPDATE user_ssh_key SET revoked_at = NULL WHERE user_id = $1 AND revoked_at IS NOT NULL",
|
||||
"UPDATE user_gpg_key SET revoked_at = NULL WHERE user_id = $1 AND revoked_at IS NOT NULL",
|
||||
"UPDATE workspace_member SET status = 'active' WHERE user_id = $1 AND status = 'deleted'",
|
||||
"UPDATE repo_member SET status = 'active' WHERE user_id = $1 AND status = 'deleted'",
|
||||
"UPDATE user_2fa SET deleted_at = NULL WHERE user_id = $1 AND deleted_at IS NOT NULL",
|
||||
"UPDATE user_activity SET deleted_at = NULL WHERE user_id = $1 AND deleted_at IS NOT NULL",
|
||||
"UPDATE user_appearance SET deleted_at = NULL WHERE user_id = $1 AND deleted_at IS NOT NULL",
|
||||
"UPDATE user_block SET deleted_at = NULL WHERE (user_id = $1 OR blocked_user_id = $1) AND deleted_at IS NOT NULL",
|
||||
"UPDATE user_device SET deleted_at = NULL WHERE user_id = $1 AND deleted_at IS NOT NULL",
|
||||
"UPDATE user_follow SET deleted_at = NULL WHERE (user_id = $1 OR following_user_id = $1) AND deleted_at IS NOT NULL",
|
||||
"UPDATE user_mail SET deleted_at = NULL WHERE user_id = $1 AND deleted_at IS NOT NULL",
|
||||
"UPDATE user_notify_setting SET deleted_at = NULL WHERE user_id = $1 AND deleted_at IS NOT NULL",
|
||||
"UPDATE user_oauth SET deleted_at = NULL WHERE user_id = $1 AND deleted_at IS NOT NULL",
|
||||
"UPDATE user_password SET deleted_at = NULL WHERE user_id = $1 AND deleted_at IS NOT NULL",
|
||||
"UPDATE user_password_reset SET deleted_at = NULL WHERE user_id = $1 AND deleted_at IS NOT NULL",
|
||||
"UPDATE user_presence SET deleted_at = NULL WHERE user_id = $1 AND deleted_at IS NOT NULL",
|
||||
"UPDATE user_profile SET deleted_at = NULL WHERE user_id = $1 AND deleted_at IS NOT NULL",
|
||||
"UPDATE user_security_log SET deleted_at = NULL WHERE user_id = $1 AND deleted_at IS NOT NULL",
|
||||
] {
|
||||
sqlx::query(statement)
|
||||
.bind(user_uid)
|
||||
@@ -191,7 +300,9 @@ impl UserService {
|
||||
}
|
||||
|
||||
let result = sqlx::query(
|
||||
"UPDATE \"user\" SET deleted_at = $1, is_active = false, status = 'deleted', updated_at = $1 WHERE id = $2 AND deleted_at IS NULL",
|
||||
"UPDATE \"user\" SET deleted_at = NULL, is_active = true, status = 'active', \
|
||||
restore_token_hash = NULL, restore_token_expires_at = NULL, updated_at = $1 \
|
||||
WHERE id = $2 AND deleted_at IS NOT NULL",
|
||||
)
|
||||
.bind(now)
|
||||
.bind(user_uid)
|
||||
@@ -199,14 +310,53 @@ impl UserService {
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
if result.rows_affected() == 0 {
|
||||
return Err(AppError::UserNotFound);
|
||||
return Err(AppError::NotFound("user not found".into()));
|
||||
}
|
||||
|
||||
txn.commit().await.map_err(|_| AppError::TxnError)?;
|
||||
ctx.clear();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn send_restore_email(&self, email: &str, token: &str) -> Result<(), AppError> {
|
||||
let app_url = self
|
||||
.ctx
|
||||
.config
|
||||
.get_env::<String>("APP_URL")
|
||||
.ok()
|
||||
.flatten()
|
||||
.unwrap_or_else(|| "http://localhost:8000".to_string());
|
||||
let base = app_url.trim_end_matches('/');
|
||||
let restore_url = format!("{}/account/restore?token={}", base, token);
|
||||
let mut mail = self
|
||||
.ctx
|
||||
.registry
|
||||
.get_email_client()
|
||||
.ok_or(AppError::Config("mail service not available".into()))?;
|
||||
mail.send_email(tonic::Request::new(SendEmailRequest {
|
||||
to: vec![EmailAddress {
|
||||
email: email.to_string(),
|
||||
name: String::new(),
|
||||
}],
|
||||
subject: "Account Deletion - Restore Link".into(),
|
||||
text_body: format!(
|
||||
"Your account has been marked for deletion.\n\n\
|
||||
If you did not request this, you can restore your account within 30 days \
|
||||
by visiting the following link:\n\n\
|
||||
{}\n\n\
|
||||
This link expires in 30 days. After that, your data will be retained but \
|
||||
the restore link will no longer work.",
|
||||
restore_url,
|
||||
),
|
||||
..Default::default()
|
||||
}))
|
||||
.await
|
||||
.map(|_| ())
|
||||
.map_err(|e| {
|
||||
tracing::warn!(?e, "failed to send restore email");
|
||||
AppError::InternalServerError(e.to_string())
|
||||
})
|
||||
}
|
||||
|
||||
async fn ensure_username_available(
|
||||
&self,
|
||||
username: &str,
|
||||
|
||||
Reference in New Issue
Block a user