From f2772b5920967f5dacc3de27dee2bc1b464533e2 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Mon, 13 Feb 2023 13:11:41 +0800 Subject: [PATCH] Move delete user to service (#22478) Move delete user to service Co-authored-by: delvh Co-authored-by: Jason Song --- models/db/context.go | 26 +++++++++++++ models/user.go => services/user/delete.go | 46 ++++++++++++----------- services/user/user.go | 2 +- 3 files changed, 51 insertions(+), 23 deletions(-) rename models/user.go => services/user/delete.go (77%) diff --git a/models/db/context.go b/models/db/context.go index 911dbd1c6f..4b3f7f0ee7 100644 --- a/models/db/context.go +++ b/models/db/context.go @@ -7,6 +7,7 @@ import ( "context" "database/sql" + "xorm.io/builder" "xorm.io/xorm" "xorm.io/xorm/schemas" ) @@ -183,6 +184,31 @@ func DeleteByBean(ctx context.Context, bean interface{}) (int64, error) { return GetEngine(ctx).Delete(bean) } +// DeleteByID deletes the given bean with the given ID +func DeleteByID(ctx context.Context, id int64, bean interface{}) (int64, error) { + return GetEngine(ctx).ID(id).NoAutoTime().Delete(bean) +} + +// FindIDs finds the IDs for the given table name satisfying the given condition +// By passing a different value than "id" for "idCol", you can query for foreign IDs, i.e. the repo IDs which satisfy the condition +func FindIDs(ctx context.Context, tableName, idCol string, cond builder.Cond) ([]int64, error) { + ids := make([]int64, 0, 10) + if err := GetEngine(ctx).Table(tableName). + Cols(idCol). + Where(cond). + Find(&ids); err != nil { + return nil, err + } + return ids, nil +} + +// DecrByIDs decreases the given column for entities of the "bean" type with one of the given ids by one +// Timestamps of the entities won't be updated +func DecrByIDs(ctx context.Context, ids []int64, decrCol string, bean interface{}) error { + _, err := GetEngine(ctx).Decr(decrCol).In("id", ids).NoAutoCondition().NoAutoTime().Update(bean) + return err +} + // DeleteBeans deletes all given beans, beans should contain delete conditions. func DeleteBeans(ctx context.Context, beans ...interface{}) (err error) { e := GetEngine(ctx) diff --git a/models/user.go b/services/user/delete.go similarity index 77% rename from models/user.go rename to services/user/delete.go index 746553c35b..01e3c37b39 100644 --- a/models/user.go +++ b/services/user/delete.go @@ -1,8 +1,7 @@ -// Copyright 2014 The Gogs Authors. All rights reserved. -// Copyright 2019 The Gitea Authors. All rights reserved. +// Copyright 2023 The Gitea Authors. All rights reserved. // SPDX-License-Identifier: MIT -package models +package user import ( "context" @@ -23,47 +22,50 @@ import ( repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/setting" + + "xorm.io/builder" ) -// DeleteUser deletes models associated to an user. -func DeleteUser(ctx context.Context, u *user_model.User, purge bool) (err error) { +// deleteUser deletes models associated to an user. +func deleteUser(ctx context.Context, u *user_model.User, purge bool) (err error) { e := db.GetEngine(ctx) // ***** START: Watch ***** - watchedRepoIDs := make([]int64, 0, 10) - if err = e.Table("watch").Cols("watch.repo_id"). - Where("watch.user_id = ?", u.ID).And("watch.mode <>?", repo_model.WatchModeDont).Find(&watchedRepoIDs); err != nil { + watchedRepoIDs, err := db.FindIDs(ctx, "watch", "watch.repo_id", + builder.Eq{"watch.user_id": u.ID}. + And(builder.Neq{"watch.mode": repo_model.WatchModeDont})) + if err != nil { return fmt.Errorf("get all watches: %w", err) } - if _, err = e.Decr("num_watches").In("id", watchedRepoIDs).NoAutoTime().Update(new(repo_model.Repository)); err != nil { + if err = db.DecrByIDs(ctx, watchedRepoIDs, "num_watches", new(repo_model.Repository)); err != nil { return fmt.Errorf("decrease repository num_watches: %w", err) } // ***** END: Watch ***** // ***** START: Star ***** - starredRepoIDs := make([]int64, 0, 10) - if err = e.Table("star").Cols("star.repo_id"). - Where("star.uid = ?", u.ID).Find(&starredRepoIDs); err != nil { + starredRepoIDs, err := db.FindIDs(ctx, "star", "star.repo_id", + builder.Eq{"star.uid": u.ID}) + if err != nil { return fmt.Errorf("get all stars: %w", err) - } else if _, err = e.Decr("num_stars").In("id", starredRepoIDs).NoAutoTime().Update(new(repo_model.Repository)); err != nil { + } else if err = db.DecrByIDs(ctx, starredRepoIDs, "num_stars", new(repo_model.Repository)); err != nil { return fmt.Errorf("decrease repository num_stars: %w", err) } // ***** END: Star ***** // ***** START: Follow ***** - followeeIDs := make([]int64, 0, 10) - if err = e.Table("follow").Cols("follow.follow_id"). - Where("follow.user_id = ?", u.ID).Find(&followeeIDs); err != nil { + followeeIDs, err := db.FindIDs(ctx, "follow", "follow.follow_id", + builder.Eq{"follow.user_id": u.ID}) + if err != nil { return fmt.Errorf("get all followees: %w", err) - } else if _, err = e.Decr("num_followers").In("id", followeeIDs).Update(new(user_model.User)); err != nil { + } else if err = db.DecrByIDs(ctx, followeeIDs, "num_followers", new(user_model.User)); err != nil { return fmt.Errorf("decrease user num_followers: %w", err) } - followerIDs := make([]int64, 0, 10) - if err = e.Table("follow").Cols("follow.user_id"). - Where("follow.follow_id = ?", u.ID).Find(&followerIDs); err != nil { + followerIDs, err := db.FindIDs(ctx, "follow", "follow.user_id", + builder.Eq{"follow.follow_id": u.ID}) + if err != nil { return fmt.Errorf("get all followers: %w", err) - } else if _, err = e.Decr("num_following").In("id", followerIDs).Update(new(user_model.User)); err != nil { + } else if err = db.DecrByIDs(ctx, followerIDs, "num_following", new(user_model.User)); err != nil { return fmt.Errorf("decrease user num_following: %w", err) } // ***** END: Follow ***** @@ -181,7 +183,7 @@ func DeleteUser(ctx context.Context, u *user_model.User, purge bool) (err error) } // ***** END: ExternalLoginUser ***** - if _, err = e.ID(u.ID).Delete(new(user_model.User)); err != nil { + if _, err = db.DeleteByID(ctx, u.ID, new(user_model.User)); err != nil { return fmt.Errorf("delete: %w", err) } diff --git a/services/user/user.go b/services/user/user.go index c95eb67a85..f0b8fe1c31 100644 --- a/services/user/user.go +++ b/services/user/user.go @@ -163,7 +163,7 @@ func DeleteUser(ctx context.Context, u *user_model.User, purge bool) error { return models.ErrUserOwnPackages{UID: u.ID} } - if err := models.DeleteUser(ctx, u, purge); err != nil { + if err := deleteUser(ctx, u, purge); err != nil { return fmt.Errorf("DeleteUser: %w", err) }