]> Untitled Git - lemmy.git/blob - crates/api_crud/src/user/delete.rs
Upgrading deps (#1995)
[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_schema::source::{comment::Comment, person::Person, post::Post};
6 use lemmy_utils::{ConnectionId, LemmyError};
7 use lemmy_websocket::LemmyContext;
8
9 #[async_trait::async_trait(?Send)]
10 impl PerformCrud for DeleteAccount {
11   type Response = LoginResponse;
12
13   #[tracing::instrument(skip(self, context, _websocket_id))]
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 =
21       get_local_user_view_from_jwt(data.auth.as_ref(), context.pool(), context.secret()).await?;
22
23     // Verify the password
24     let valid: bool = verify(
25       &data.password,
26       &local_user_view.local_user.password_encrypted,
27     )
28     .unwrap_or(false);
29     if !valid {
30       return Err(LemmyError::from_message("password_incorrect"));
31     }
32
33     // Comments
34     let person_id = local_user_view.person.id;
35     let permadelete = move |conn: &'_ _| Comment::permadelete_for_creator(conn, person_id);
36     blocking(context.pool(), permadelete)
37       .await?
38       .map_err(LemmyError::from)
39       .map_err(|e| e.with_message("couldnt_update_comment"))?;
40
41     // Posts
42     let permadelete = move |conn: &'_ _| Post::permadelete_for_creator(conn, person_id);
43     blocking(context.pool(), permadelete)
44       .await?
45       .map_err(LemmyError::from)
46       .map_err(|e| e.with_message("couldnt_update_post"))?;
47
48     blocking(context.pool(), move |conn| {
49       Person::delete_account(conn, person_id)
50     })
51     .await??;
52
53     Ok(LoginResponse {
54       jwt: data.auth.clone(),
55     })
56   }
57 }