Better account deletion (fixes #730)

This commit is contained in:
Felix Ableitner 2020-11-26 17:03:33 +01:00
parent a30be1ca5d
commit c986f5961c
7 changed files with 36 additions and 6 deletions

View file

@ -782,9 +782,9 @@ impl Perform for ResolveCommentReport {
let resolved = data.resolved; let resolved = data.resolved;
let resolve_fun = move |conn: &'_ _| { let resolve_fun = move |conn: &'_ _| {
if resolved { if resolved {
CommentReport::resolve(conn, report_id.clone(), user_id) CommentReport::resolve(conn, report_id, user_id)
} else { } else {
CommentReport::unresolve(conn, report_id.clone(), user_id) CommentReport::unresolve(conn, report_id, user_id)
} }
}; };

View file

@ -839,9 +839,9 @@ impl Perform for ResolvePostReport {
let resolved = data.resolved; let resolved = data.resolved;
let resolve_fun = move |conn: &'_ _| { let resolve_fun = move |conn: &'_ _| {
if resolved { if resolved {
PostReport::resolve(conn, report_id.clone(), user_id) PostReport::resolve(conn, report_id, user_id)
} else { } else {
PostReport::unresolve(conn, report_id.clone(), user_id) PostReport::unresolve(conn, report_id, user_id)
} }
}; };

View file

@ -890,6 +890,11 @@ impl Perform for DeleteAccount {
return Err(APIError::err("couldnt_update_post").into()); return Err(APIError::err("couldnt_update_post").into());
} }
blocking(context.pool(), move |conn| {
User_::delete_account(conn, user_id)
})
.await??;
Ok(LoginResponse { Ok(LoginResponse {
jwt: data.auth.to_owned(), jwt: data.auth.to_owned(),
}) })

View file

@ -467,6 +467,7 @@ table! {
public_key -> Nullable<Text>, public_key -> Nullable<Text>,
last_refreshed_at -> Timestamp, last_refreshed_at -> Timestamp,
banner -> Nullable<Text>, banner -> Nullable<Text>,
deleted -> Bool,
} }
} }

View file

@ -37,6 +37,7 @@ pub struct User_ {
pub public_key: Option<String>, pub public_key: Option<String>,
pub last_refreshed_at: chrono::NaiveDateTime, pub last_refreshed_at: chrono::NaiveDateTime,
pub banner: Option<String>, pub banner: Option<String>,
pub deleted: bool,
} }
#[derive(Insertable, AsChangeset, Clone)] #[derive(Insertable, AsChangeset, Clone)]
@ -70,7 +71,10 @@ pub struct UserForm {
impl Crud<UserForm> for User_ { impl Crud<UserForm> for User_ {
fn read(conn: &PgConnection, user_id: i32) -> Result<Self, Error> { 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> { fn delete(conn: &PgConnection, user_id: i32) -> Result<usize, Error> {
diesel::delete(user_.find(user_id)).execute(conn) 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> { pub fn read_from_name(conn: &PgConnection, from_user_name: &str) -> Result<Self, Error> {
user_ user_
.filter(local.eq(true)) .filter(local.eq(true))
.filter(deleted.eq(false))
.filter(name.eq(from_user_name)) .filter(name.eq(from_user_name))
.first::<Self>(conn) .first::<Self>(conn)
} }
@ -132,7 +137,10 @@ impl User_ {
pub fn read_from_actor_id(conn: &PgConnection, object_id: &str) -> Result<Self, Error> { pub fn read_from_actor_id(conn: &PgConnection, object_id: &str) -> Result<Self, Error> {
use crate::schema::user_::dsl::*; 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( 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> { pub fn find_by_username(conn: &PgConnection, username: &str) -> Result<User_, Error> {
user_ user_
.filter(deleted.eq(false))
.filter(local.eq(true)) .filter(local.eq(true))
.filter(name.ilike(username)) .filter(name.ilike(username))
.first::<User_>(conn) .first::<User_>(conn)
@ -155,6 +164,7 @@ impl User_ {
pub fn find_by_email(conn: &PgConnection, from_email: &str) -> Result<User_, Error> { pub fn find_by_email(conn: &PgConnection, from_email: &str) -> Result<User_, Error> {
user_ user_
.filter(deleted.eq(false))
.filter(local.eq(true)) .filter(local.eq(true))
.filter(email.eq(from_email)) .filter(email.eq(from_email))
.first::<User_>(conn) .first::<User_>(conn)
@ -177,6 +187,18 @@ impl User_ {
.set(user_form) .set(user_form)
.get_result::<Self>(conn) .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),
deleted.eq(true),
updated.eq(naive_now()),
))
.get_result::<Self>(conn)
}
} }
#[cfg(test)] #[cfg(test)]

View file

@ -0,0 +1 @@
ALTER TABLE user_ DROP COLUMN deleted;

View file

@ -0,0 +1 @@
ALTER TABLE user_ ADD COLUMN deleted BOOLEAN DEFAULT FALSE NOT NULL;