2023-08-02 16:52:41 +00:00
|
|
|
use activitypub_federation::config::Data;
|
|
|
|
use actix_web::web::Json;
|
2021-03-25 19:19:40 +00:00
|
|
|
use bcrypt::verify;
|
2022-05-03 17:44:13 +00:00
|
|
|
use lemmy_api_common::{
|
2022-11-28 14:29:33 +00:00
|
|
|
context::LemmyContext,
|
2023-10-16 16:36:53 +00:00
|
|
|
person::DeleteAccount,
|
2023-08-02 16:52:41 +00:00
|
|
|
send_activity::{ActivityChannel, SendActivityData},
|
2023-09-21 10:42:28 +00:00
|
|
|
utils::purge_user_account,
|
2023-10-16 16:36:53 +00:00
|
|
|
SuccessResponse,
|
2022-05-03 17:44:13 +00:00
|
|
|
};
|
2024-09-18 12:52:33 +00:00
|
|
|
use lemmy_db_schema::source::{
|
|
|
|
login_token::LoginToken,
|
|
|
|
oauth_account::OAuthAccount,
|
|
|
|
person::Person,
|
|
|
|
};
|
2023-09-21 10:42:28 +00:00
|
|
|
use lemmy_db_views::structs::LocalUserView;
|
2023-10-16 16:36:53 +00:00
|
|
|
use lemmy_utils::error::{LemmyErrorType, LemmyResult};
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2023-08-02 16:52:41 +00:00
|
|
|
#[tracing::instrument(skip(context))]
|
|
|
|
pub async fn delete_account(
|
|
|
|
data: Json<DeleteAccount>,
|
|
|
|
context: Data<LemmyContext>,
|
2023-09-21 10:42:28 +00:00
|
|
|
local_user_view: LocalUserView,
|
2023-10-16 16:36:53 +00:00
|
|
|
) -> LemmyResult<Json<SuccessResponse>> {
|
2023-08-02 16:52:41 +00:00
|
|
|
// Verify the password
|
2024-09-18 12:52:33 +00:00
|
|
|
let valid: bool = local_user_view
|
|
|
|
.local_user
|
|
|
|
.password_encrypted
|
|
|
|
.as_ref()
|
|
|
|
.and_then(|password_encrypted| verify(&data.password, password_encrypted).ok())
|
|
|
|
.unwrap_or(false);
|
2023-08-02 16:52:41 +00:00
|
|
|
if !valid {
|
2023-08-31 13:01:08 +00:00
|
|
|
Err(LemmyErrorType::IncorrectLogin)?
|
2023-08-02 16:52:41 +00:00
|
|
|
}
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2023-08-28 10:23:45 +00:00
|
|
|
if data.delete_content {
|
|
|
|
purge_user_account(local_user_view.person.id, &context).await?;
|
|
|
|
} else {
|
2024-09-18 12:52:33 +00:00
|
|
|
OAuthAccount::delete_user_accounts(&mut context.pool(), local_user_view.local_user.id).await?;
|
2023-08-28 10:23:45 +00:00
|
|
|
Person::delete_account(&mut context.pool(), local_user_view.person.id).await?;
|
|
|
|
}
|
|
|
|
|
2023-10-09 10:46:12 +00:00
|
|
|
LoginToken::invalidate_all(&mut context.pool(), local_user_view.local_user.id).await?;
|
|
|
|
|
2023-08-02 16:52:41 +00:00
|
|
|
ActivityChannel::submit_activity(
|
2023-08-28 10:23:45 +00:00
|
|
|
SendActivityData::DeleteUser(local_user_view.person, data.delete_content),
|
2023-08-02 16:52:41 +00:00
|
|
|
&context,
|
|
|
|
)
|
|
|
|
.await?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2023-10-16 16:36:53 +00:00
|
|
|
Ok(Json(SuccessResponse::default()))
|
2021-03-25 19:19:40 +00:00
|
|
|
}
|