summaryrefslogtreecommitdiffstats
path: root/models/user.go
diff options
context:
space:
mode:
authorEthan Koenig <etk39@cornell.edu>2017-05-20 04:48:22 -0400
committerLunny Xiao <xiaolunwen@gmail.com>2017-05-20 16:48:22 +0800
commitcf02cd7ba0c94165743660cf30f0cbb5a73a385e (patch)
tree6f09dce7c705041df1e042750137e379b0af66dc /models/user.go
parent85a73965256411c36ecc050eeb7967d3c8ef20d5 (diff)
downloadgitea-cf02cd7ba0c94165743660cf30f0cbb5a73a385e.tar.gz
gitea-cf02cd7ba0c94165743660cf30f0cbb5a73a385e.zip
Fix and test for delete user (#1713)
* Fix and test for delete user * Run updates in batches * Unit test
Diffstat (limited to 'models/user.go')
-rw-r--r--models/user.go44
1 files changed, 24 insertions, 20 deletions
diff --git a/models/user.go b/models/user.go
index 75329d8ff0..5fe72f923a 100644
--- a/models/user.go
+++ b/models/user.go
@@ -924,38 +924,41 @@ func deleteUser(e *xorm.Session, u *User) error {
}
// ***** START: Watch *****
- watches := make([]*Watch, 0, 10)
- if err = e.Find(&watches, &Watch{UserID: u.ID}); err != nil {
+ watchedRepoIDs := make([]int64, 0, 10)
+ if err = e.Table("watch").Cols("watch.repo_id").
+ Where("watch.user_id = ?", u.ID).Find(&watchedRepoIDs); err != nil {
return fmt.Errorf("get all watches: %v", err)
}
- for i := range watches {
- if _, err = e.Exec("UPDATE `repository` SET num_watches=num_watches-1 WHERE id=?", watches[i].RepoID); err != nil {
- return fmt.Errorf("decrease repository watch number[%d]: %v", watches[i].RepoID, err)
- }
+ if _, err = e.Decr("num_watches").In("id", watchedRepoIDs).Update(new(Repository)); err != nil {
+ return fmt.Errorf("decrease repository num_watches: %v", err)
}
// ***** END: Watch *****
// ***** START: Star *****
- stars := make([]*Star, 0, 10)
- if err = e.Find(&stars, &Star{UID: u.ID}); err != nil {
+ starredRepoIDs := make([]int64, 0, 10)
+ if err = e.Table("star").Cols("star.repo_id").
+ Where("star.uid = ?", u.ID).Find(&starredRepoIDs); err != nil {
return fmt.Errorf("get all stars: %v", err)
- }
- for i := range stars {
- if _, err = e.Exec("UPDATE `repository` SET num_stars=num_stars-1 WHERE id=?", stars[i].RepoID); err != nil {
- return fmt.Errorf("decrease repository star number[%d]: %v", stars[i].RepoID, err)
- }
+ } else if _, err = e.Decr("num_watches").In("id", starredRepoIDs).Update(new(Repository)); err != nil {
+ return fmt.Errorf("decrease repository num_stars: %v", err)
}
// ***** END: Star *****
// ***** START: Follow *****
- followers := make([]*Follow, 0, 10)
- if err = e.Find(&followers, &Follow{UserID: u.ID}); err != nil {
- return fmt.Errorf("get all followers: %v", err)
+ followeeIDs := make([]int64, 0, 10)
+ if err = e.Table("follow").Cols("follow.follow_id").
+ Where("follow.user_id = ?", u.ID).Find(&followeeIDs); err != nil {
+ return fmt.Errorf("get all followees: %v", err)
+ } else if _, err = e.Decr("num_followers").In("id", followeeIDs).Update(new(User)); err != nil {
+ return fmt.Errorf("decrease user num_followers: %v", err)
}
- for i := range followers {
- if _, err = e.Exec("UPDATE `user` SET num_followers=num_followers-1 WHERE id=?", followers[i].UserID); err != nil {
- return fmt.Errorf("decrease user follower number[%d]: %v", followers[i].UserID, 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 {
+ return fmt.Errorf("get all followers: %v", err)
+ } else if _, err = e.Decr("num_following").In("id", followerIDs).Update(new(User)); err != nil {
+ return fmt.Errorf("decrease user num_following: %v", err)
}
// ***** END: Follow *****
@@ -965,6 +968,7 @@ func deleteUser(e *xorm.Session, u *User) error {
&Access{UserID: u.ID},
&Watch{UserID: u.ID},
&Star{UID: u.ID},
+ &Follow{UserID: u.ID},
&Follow{FollowID: u.ID},
&Action{UserID: u.ID},
&IssueUser{UID: u.ID},