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,
|
2022-05-03 17:44:13 +00:00
|
|
|
person::{DeleteAccount, DeleteAccountResponse},
|
2023-08-02 16:52:41 +00:00
|
|
|
send_activity::{ActivityChannel, SendActivityData},
|
2023-09-21 10:42:28 +00:00
|
|
|
utils::purge_user_account,
|
2022-05-03 17:44:13 +00:00
|
|
|
};
|
2023-08-28 10:23:45 +00:00
|
|
|
use lemmy_db_schema::source::person::Person;
|
2023-09-21 10:42:28 +00:00
|
|
|
use lemmy_db_views::structs::LocalUserView;
|
2023-07-10 14:50:07 +00:00
|
|
|
use lemmy_utils::error::{LemmyError, LemmyErrorType};
|
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-08-02 16:52:41 +00:00
|
|
|
) -> Result<Json<DeleteAccountResponse>, LemmyError> {
|
|
|
|
// Verify the password
|
|
|
|
let valid: bool = verify(
|
|
|
|
&data.password,
|
|
|
|
&local_user_view.local_user.password_encrypted,
|
|
|
|
)
|
|
|
|
.unwrap_or(false);
|
|
|
|
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 {
|
|
|
|
Person::delete_account(&mut context.pool(), local_user_view.person.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-08-02 16:52:41 +00:00
|
|
|
Ok(Json(DeleteAccountResponse {}))
|
2021-03-25 19:19:40 +00:00
|
|
|
}
|