]> Untitled Git - lemmy.git/commitdiff
Better account deletion (fixes #730) (#143)
authornutomic <nutomic@noreply.yerbamate.ml>
Fri, 27 Nov 2020 21:00:18 +0000 (21:00 +0000)
committerdessalines <dessalines@noreply.yerbamate.ml>
Fri, 27 Nov 2020 21:00:18 +0000 (21:00 +0000)
Also clear bio

Better account deletion (fixes #730)

Co-authored-by: Felix Ableitner <me@nutomic.com>
Reviewed-on: https://yerbamate.ml/LemmyNet/lemmy/pulls/143

lemmy_api/src/comment.rs
lemmy_api/src/post.rs
lemmy_api/src/user.rs
lemmy_db/src/schema.rs
lemmy_db/src/user.rs
migrations/2020-11-26-134531_delete_user/down.sql [new file with mode: 0644]
migrations/2020-11-26-134531_delete_user/up.sql [new file with mode: 0644]

index b1107d0dc00c907422f1f3e178595436bcc0336a..e74fa808e6ef30ce76e074156d0a3b0463cf213d 100644 (file)
@@ -782,9 +782,9 @@ impl Perform for ResolveCommentReport {
     let resolved = data.resolved;
     let resolve_fun = move |conn: &'_ _| {
       if resolved {
-        CommentReport::resolve(conn, report_id.clone(), user_id)
+        CommentReport::resolve(conn, report_id, user_id)
       } else {
-        CommentReport::unresolve(conn, report_id.clone(), user_id)
+        CommentReport::unresolve(conn, report_id, user_id)
       }
     };
 
index 707c833593d937c1a0d7ff7702ed44d8400b96be..d5515b5cd48a2fff6c93c0d9a510758a679c2e68 100644 (file)
@@ -839,9 +839,9 @@ impl Perform for ResolvePostReport {
     let resolved = data.resolved;
     let resolve_fun = move |conn: &'_ _| {
       if resolved {
-        PostReport::resolve(conn, report_id.clone(), user_id)
+        PostReport::resolve(conn, report_id, user_id)
       } else {
-        PostReport::unresolve(conn, report_id.clone(), user_id)
+        PostReport::unresolve(conn, report_id, user_id)
       }
     };
 
index 3b0b6d3fe47f4b9f925642478ab2b371e4ef8da3..0d96c2a2f1d9c178a44978b558eca8ce9b807495 100644 (file)
@@ -890,6 +890,11 @@ impl Perform for DeleteAccount {
       return Err(APIError::err("couldnt_update_post").into());
     }
 
+    blocking(context.pool(), move |conn| {
+      User_::delete_account(conn, user_id)
+    })
+    .await??;
+
     Ok(LoginResponse {
       jwt: data.auth.to_owned(),
     })
index 400c87d420ab3e97ac0f9f8e3f8e54029cb9998f..49bbc46fb1c74fa29af44032c3d603ca43efc73d 100644 (file)
@@ -467,6 +467,7 @@ table! {
         public_key -> Nullable<Text>,
         last_refreshed_at -> Timestamp,
         banner -> Nullable<Text>,
+        deleted -> Bool,
     }
 }
 
index 378bbad9c77db4272fc32c51112cc753d3558f8b..2c4c67ea2f53f51364d620ffde60100da4e1f48f 100644 (file)
@@ -37,6 +37,7 @@ pub struct User_ {
   pub public_key: Option<String>,
   pub last_refreshed_at: chrono::NaiveDateTime,
   pub banner: Option<String>,
+  pub deleted: bool,
 }
 
 #[derive(Insertable, AsChangeset, Clone)]
@@ -70,7 +71,10 @@ pub struct UserForm {
 
 impl Crud<UserForm> for User_ {
   fn read(conn: &PgConnection, user_id: i32) -> Result<Self, Error> {
-    user_.find(user_id).first::<Self>(conn)
+    user_
+      .filter(deleted.eq(false))
+      .find(user_id)
+      .first::<Self>(conn)
   }
   fn delete(conn: &PgConnection, user_id: i32) -> Result<usize, Error> {
     diesel::delete(user_.find(user_id)).execute(conn)
@@ -114,6 +118,7 @@ impl User_ {
   pub fn read_from_name(conn: &PgConnection, from_user_name: &str) -> Result<Self, Error> {
     user_
       .filter(local.eq(true))
+      .filter(deleted.eq(false))
       .filter(name.eq(from_user_name))
       .first::<Self>(conn)
   }
@@ -132,7 +137,10 @@ impl User_ {
 
   pub fn read_from_actor_id(conn: &PgConnection, object_id: &str) -> Result<Self, Error> {
     use crate::schema::user_::dsl::*;
-    user_.filter(actor_id.eq(object_id)).first::<Self>(conn)
+    user_
+      .filter(deleted.eq(false))
+      .filter(actor_id.eq(object_id))
+      .first::<Self>(conn)
   }
 
   pub fn find_by_email_or_username(
@@ -148,6 +156,7 @@ impl User_ {
 
   pub fn find_by_username(conn: &PgConnection, username: &str) -> Result<User_, Error> {
     user_
+      .filter(deleted.eq(false))
       .filter(local.eq(true))
       .filter(name.ilike(username))
       .first::<User_>(conn)
@@ -155,6 +164,7 @@ impl User_ {
 
   pub fn find_by_email(conn: &PgConnection, from_email: &str) -> Result<User_, Error> {
     user_
+      .filter(deleted.eq(false))
       .filter(local.eq(true))
       .filter(email.eq(from_email))
       .first::<User_>(conn)
@@ -177,6 +187,19 @@ impl User_ {
       .set(user_form)
       .get_result::<Self>(conn)
   }
+
+  pub fn delete_account(conn: &PgConnection, user_id: i32) -> Result<User_, Error> {
+    diesel::update(user_.find(user_id))
+      .set((
+        preferred_username.eq::<Option<String>>(None),
+        email.eq::<Option<String>>(None),
+        matrix_user_id.eq::<Option<String>>(None),
+        bio.eq::<Option<String>>(None),
+        deleted.eq(true),
+        updated.eq(naive_now()),
+      ))
+      .get_result::<Self>(conn)
+  }
 }
 
 #[cfg(test)]
diff --git a/migrations/2020-11-26-134531_delete_user/down.sql b/migrations/2020-11-26-134531_delete_user/down.sql
new file mode 100644 (file)
index 0000000..09b0587
--- /dev/null
@@ -0,0 +1 @@
+ALTER TABLE user_ DROP COLUMN deleted;
diff --git a/migrations/2020-11-26-134531_delete_user/up.sql b/migrations/2020-11-26-134531_delete_user/up.sql
new file mode 100644 (file)
index 0000000..1d752c5
--- /dev/null
@@ -0,0 +1 @@
+ALTER TABLE user_ ADD COLUMN deleted BOOLEAN DEFAULT FALSE NOT NULL;