]> Untitled Git - lemmy.git/blob - crates/api_crud/src/user/delete.rs
Running clippy --fix (#1647)
[lemmy.git] / crates / api_crud / src / user / delete.rs
1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use bcrypt::verify;
4 use lemmy_api_common::{blocking, get_local_user_view_from_jwt, person::*};
5 use lemmy_db_queries::source::{comment::Comment_, person::Person_, post::Post_};
6 use lemmy_db_schema::source::{comment::Comment, person::*, post::Post};
7 use lemmy_utils::{ApiError, ConnectionId, LemmyError};
8 use lemmy_websocket::LemmyContext;
9
10 #[async_trait::async_trait(?Send)]
11 impl PerformCrud for DeleteAccount {
12   type Response = LoginResponse;
13
14   async fn perform(
15     &self,
16     context: &Data<LemmyContext>,
17     _websocket_id: Option<ConnectionId>,
18   ) -> Result<LoginResponse, LemmyError> {
19     let data: &DeleteAccount = self;
20     let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
21
22     // Verify the password
23     let valid: bool = verify(
24       &data.password,
25       &local_user_view.local_user.password_encrypted,
26     )
27     .unwrap_or(false);
28     if !valid {
29       return Err(ApiError::err("password_incorrect").into());
30     }
31
32     // Comments
33     let person_id = local_user_view.person.id;
34     let permadelete = move |conn: &'_ _| Comment::permadelete_for_creator(conn, person_id);
35     if blocking(context.pool(), permadelete).await?.is_err() {
36       return Err(ApiError::err("couldnt_update_comment").into());
37     }
38
39     // Posts
40     let permadelete = move |conn: &'_ _| Post::permadelete_for_creator(conn, person_id);
41     if blocking(context.pool(), permadelete).await?.is_err() {
42       return Err(ApiError::err("couldnt_update_post").into());
43     }
44
45     blocking(context.pool(), move |conn| {
46       Person::delete_account(conn, person_id)
47     })
48     .await??;
49
50     Ok(LoginResponse {
51       jwt: data.auth.to_owned(),
52     })
53   }
54 }