Move delete user to service (#22478)

Move delete user to service

Co-authored-by: delvh <dev.lh@web.de>
Co-authored-by: Jason Song <i@wolfogre.com>
This commit is contained in:
Lunny Xiao 2023-02-13 13:11:41 +08:00 committed by GitHub
parent eb5e1bcd21
commit f2772b5920
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 23 deletions

View file

@ -7,6 +7,7 @@ import (
"context" "context"
"database/sql" "database/sql"
"xorm.io/builder"
"xorm.io/xorm" "xorm.io/xorm"
"xorm.io/xorm/schemas" "xorm.io/xorm/schemas"
) )
@ -183,6 +184,31 @@ func DeleteByBean(ctx context.Context, bean interface{}) (int64, error) {
return GetEngine(ctx).Delete(bean) 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. // DeleteBeans deletes all given beans, beans should contain delete conditions.
func DeleteBeans(ctx context.Context, beans ...interface{}) (err error) { func DeleteBeans(ctx context.Context, beans ...interface{}) (err error) {
e := GetEngine(ctx) e := GetEngine(ctx)

View file

@ -1,8 +1,7 @@
// Copyright 2014 The Gogs Authors. All rights reserved. // Copyright 2023 The Gitea Authors. All rights reserved.
// Copyright 2019 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT // SPDX-License-Identifier: MIT
package models package user
import ( import (
"context" "context"
@ -23,47 +22,50 @@ import (
repo_model "code.gitea.io/gitea/models/repo" repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user" user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"xorm.io/builder"
) )
// DeleteUser deletes models associated to an user. // deleteUser deletes models associated to an user.
func DeleteUser(ctx context.Context, u *user_model.User, purge bool) (err error) { func deleteUser(ctx context.Context, u *user_model.User, purge bool) (err error) {
e := db.GetEngine(ctx) e := db.GetEngine(ctx)
// ***** START: Watch ***** // ***** START: Watch *****
watchedRepoIDs := make([]int64, 0, 10) watchedRepoIDs, err := db.FindIDs(ctx, "watch", "watch.repo_id",
if err = e.Table("watch").Cols("watch.repo_id"). builder.Eq{"watch.user_id": u.ID}.
Where("watch.user_id = ?", u.ID).And("watch.mode <>?", repo_model.WatchModeDont).Find(&watchedRepoIDs); err != nil { And(builder.Neq{"watch.mode": repo_model.WatchModeDont}))
if err != nil {
return fmt.Errorf("get all watches: %w", err) 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) return fmt.Errorf("decrease repository num_watches: %w", err)
} }
// ***** END: Watch ***** // ***** END: Watch *****
// ***** START: Star ***** // ***** START: Star *****
starredRepoIDs := make([]int64, 0, 10) starredRepoIDs, err := db.FindIDs(ctx, "star", "star.repo_id",
if err = e.Table("star").Cols("star.repo_id"). builder.Eq{"star.uid": u.ID})
Where("star.uid = ?", u.ID).Find(&starredRepoIDs); err != nil { if err != nil {
return fmt.Errorf("get all stars: %w", err) 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) return fmt.Errorf("decrease repository num_stars: %w", err)
} }
// ***** END: Star ***** // ***** END: Star *****
// ***** START: Follow ***** // ***** START: Follow *****
followeeIDs := make([]int64, 0, 10) followeeIDs, err := db.FindIDs(ctx, "follow", "follow.follow_id",
if err = e.Table("follow").Cols("follow.follow_id"). builder.Eq{"follow.user_id": u.ID})
Where("follow.user_id = ?", u.ID).Find(&followeeIDs); err != nil { if err != nil {
return fmt.Errorf("get all followees: %w", err) 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) return fmt.Errorf("decrease user num_followers: %w", err)
} }
followerIDs := make([]int64, 0, 10) followerIDs, err := db.FindIDs(ctx, "follow", "follow.user_id",
if err = e.Table("follow").Cols("follow.user_id"). builder.Eq{"follow.follow_id": u.ID})
Where("follow.follow_id = ?", u.ID).Find(&followerIDs); err != nil { if err != nil {
return fmt.Errorf("get all followers: %w", err) 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) return fmt.Errorf("decrease user num_following: %w", err)
} }
// ***** END: Follow ***** // ***** END: Follow *****
@ -181,7 +183,7 @@ func DeleteUser(ctx context.Context, u *user_model.User, purge bool) (err error)
} }
// ***** END: ExternalLoginUser ***** // ***** 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) return fmt.Errorf("delete: %w", err)
} }

View file

@ -163,7 +163,7 @@ func DeleteUser(ctx context.Context, u *user_model.User, purge bool) error {
return models.ErrUserOwnPackages{UID: u.ID} 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) return fmt.Errorf("DeleteUser: %w", err)
} }