From 6ad92ee862ea48500a25ce7f30d6022b81b5cc8c Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Mon, 18 Jan 2021 23:43:17 +0100 Subject: [PATCH 01/10] refactor models.DeleteComment and delete related reactions too --- models/issue_comment.go | 25 +++++++++++++++++++------ models/issue_reaction.go | 2 +- models/user.go | 3 +-- services/comments/comments.go | 2 +- 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/models/issue_comment.go b/models/issue_comment.go index dd979edcda526..3e6b65f48c2e7 100644 --- a/models/issue_comment.go +++ b/models/issue_comment.go @@ -1037,33 +1037,46 @@ func UpdateComment(c *Comment, doer *User) error { } // DeleteComment deletes the comment -func DeleteComment(comment *Comment, doer *User) error { +func DeleteComment(comment *Comment) error { sess := x.NewSession() defer sess.Close() if err := sess.Begin(); err != nil { return err } - if _, err := sess.Delete(&Comment{ + if err := deleteComment(sess, comment); err != nil { + return err + } + + return sess.Commit() +} + +func deleteComment(e Engine, comment *Comment) error { + + if _, err := e.Delete(&Comment{ ID: comment.ID, }); err != nil { return err } if comment.Type == CommentTypeComment { - if _, err := sess.Exec("UPDATE `issue` SET num_comments = num_comments - 1 WHERE id = ?", comment.IssueID); err != nil { + if _, err := e.Exec("UPDATE `issue` SET num_comments = num_comments - 1 WHERE id = ?", comment.IssueID); err != nil { return err } } - if _, err := sess.Where("comment_id = ?", comment.ID).Cols("is_deleted").Update(&Action{IsDeleted: true}); err != nil { + if _, err := e.Where("comment_id = ?", comment.ID).Cols("is_deleted").Update(&Action{IsDeleted: true}); err != nil { return err } - if err := comment.neuterCrossReferences(sess); err != nil { + if err := comment.neuterCrossReferences(e); err != nil { return err } - return sess.Commit() + if err := deleteReaction(e, &ReactionOptions{Comment: comment}); err != nil { + return err + } + + return nil } // CodeComments represents comments on code by using this structure: FILENAME -> LINE (+ == proposed; - == previous) -> COMMENTS diff --git a/models/issue_reaction.go b/models/issue_reaction.go index 104afce5c1929..e4d5e31e318d8 100644 --- a/models/issue_reaction.go +++ b/models/issue_reaction.go @@ -178,7 +178,7 @@ func CreateCommentReaction(doer *User, issue *Issue, comment *Comment, content s }) } -func deleteReaction(e *xorm.Session, opts *ReactionOptions) error { +func deleteReaction(e Engine, opts *ReactionOptions) error { reaction := &Reaction{ Type: opts.Type, UserID: opts.Doer.ID, diff --git a/models/user.go b/models/user.go index 584c9d032d74c..b3a1ebae34799 100644 --- a/models/user.go +++ b/models/user.go @@ -38,7 +38,6 @@ import ( "golang.org/x/crypto/scrypt" "golang.org/x/crypto/ssh" "xorm.io/builder" - "xorm.io/xorm" ) // UserType defines the user type @@ -1072,7 +1071,7 @@ func deleteBeans(e Engine, beans ...interface{}) (err error) { } // FIXME: need some kind of mechanism to record failure. HINT: system notice -func deleteUser(e *xorm.Session, u *User) error { +func deleteUser(e Engine, u *User) error { // Note: A user owns any repository or belongs to any organization // cannot perform delete operation. diff --git a/services/comments/comments.go b/services/comments/comments.go index ad79eec4fb91d..7cf91f284fe6c 100644 --- a/services/comments/comments.go +++ b/services/comments/comments.go @@ -44,7 +44,7 @@ func UpdateComment(c *models.Comment, doer *models.User, oldContent string) erro // DeleteComment deletes the comment func DeleteComment(comment *models.Comment, doer *models.User) error { - if err := models.DeleteComment(comment, doer); err != nil { + if err := models.DeleteComment(comment); err != nil { return err } From d813b88cc873e54596af75ee467996d948129154 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Mon, 18 Jan 2021 23:43:44 +0100 Subject: [PATCH 02/10] use deleteComment for UserDeleteWithCommentsMaxDays in DeleteUser --- models/user.go | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/models/user.go b/models/user.go index b3a1ebae34799..954a7465572fc 100644 --- a/models/user.go +++ b/models/user.go @@ -1152,10 +1152,21 @@ func deleteUser(e Engine, u *User) error { if setting.Service.UserDeleteWithCommentsMaxDays != 0 && u.CreatedUnix.AsTime().Add(time.Duration(setting.Service.UserDeleteWithCommentsMaxDays)*24*time.Hour).After(time.Now()) { - if err = deleteBeans(e, - &Comment{PosterID: u.ID}, - ); err != nil { - return fmt.Errorf("deleteBeans: %v", err) + const batchSize = 50 + for start := 0; ; start += batchSize { + comments := make([]*Comment, 0, batchSize) + if err = e.Where("type=? AND poster_id=?", CommentTypeComment, u.ID).Limit(batchSize, start).Find(&comments); err != nil { + return err + } + if len(comments) == 0 { + break + } + + for i := range comments { + if err = deleteComment(e, comments[i]); err != nil { + return err + } + } } } From 9c74390cf218b67dd46bc3b8b39501c9d9358452 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Mon, 18 Jan 2021 23:53:47 +0100 Subject: [PATCH 03/10] nits --- models/issue_assignees.go | 2 +- models/user.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/models/issue_assignees.go b/models/issue_assignees.go index 05e1797c19969..6716f2fc70f80 100644 --- a/models/issue_assignees.go +++ b/models/issue_assignees.go @@ -82,7 +82,7 @@ func isUserAssignedToIssue(e Engine, issue *Issue, user *User) (isAssigned bool, } // ClearAssigneeByUserID deletes all assignments of an user -func clearAssigneeByUserID(sess *xorm.Session, userID int64) (err error) { +func clearAssigneeByUserID(sess Engine, userID int64) (err error) { _, err = sess.Delete(&IssueAssignees{AssigneeID: userID}) return } diff --git a/models/user.go b/models/user.go index 954a7465572fc..6304ab767cb79 100644 --- a/models/user.go +++ b/models/user.go @@ -1209,13 +1209,13 @@ func deleteUser(e Engine, u *User) error { // Note: There are something just cannot be roll back, // so just keep error logs of those operations. path := UserPath(u.Name) - if err := util.RemoveAll(path); err != nil { + if err = util.RemoveAll(path); err != nil { return fmt.Errorf("Failed to RemoveAll %s: %v", path, err) } if len(u.Avatar) > 0 { avatarPath := u.CustomAvatarRelativePath() - if err := storage.Avatars.Delete(avatarPath); err != nil { + if err = storage.Avatars.Delete(avatarPath); err != nil { return fmt.Errorf("Failed to remove %s: %v", avatarPath, err) } } From 0de512a65bd0f5bd4cbead2c84633c0678a351bf Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Tue, 19 Jan 2021 00:01:10 +0100 Subject: [PATCH 04/10] Use time.Duration as other time settings have --- models/user.go | 4 ++-- modules/setting/service.go | 5 +++-- options/locale/locale_en-US.ini | 2 +- routers/user/setting/account.go | 6 +++--- templates/user/settings/account.tmpl | 2 +- 5 files changed, 10 insertions(+), 9 deletions(-) diff --git a/models/user.go b/models/user.go index 6304ab767cb79..bf8960a76e623 100644 --- a/models/user.go +++ b/models/user.go @@ -1150,8 +1150,8 @@ func deleteUser(e Engine, u *User) error { return fmt.Errorf("deleteBeans: %v", err) } - if setting.Service.UserDeleteWithCommentsMaxDays != 0 && - u.CreatedUnix.AsTime().Add(time.Duration(setting.Service.UserDeleteWithCommentsMaxDays)*24*time.Hour).After(time.Now()) { + if setting.Service.UserDeleteWithCommentsMaxTime != 0 && + u.CreatedUnix.AsTime().Add(setting.Service.UserDeleteWithCommentsMaxTime).After(time.Now()) { const batchSize = 50 for start := 0; ; start += batchSize { comments := make([]*Comment, 0, batchSize) diff --git a/modules/setting/service.go b/modules/setting/service.go index 86f46898ac109..506f5fe0ddf81 100644 --- a/modules/setting/service.go +++ b/modules/setting/service.go @@ -6,6 +6,7 @@ package setting import ( "regexp" + "time" "code.gitea.io/gitea/modules/structs" ) @@ -50,7 +51,7 @@ var Service struct { AutoWatchNewRepos bool AutoWatchOnChanges bool DefaultOrgMemberVisible bool - UserDeleteWithCommentsMaxDays int + UserDeleteWithCommentsMaxTime time.Duration // OpenID settings EnableOpenIDSignIn bool @@ -103,7 +104,7 @@ func newService() { Service.DefaultOrgVisibility = sec.Key("DEFAULT_ORG_VISIBILITY").In("public", structs.ExtractKeysFromMapString(structs.VisibilityModes)) Service.DefaultOrgVisibilityMode = structs.VisibilityModes[Service.DefaultOrgVisibility] Service.DefaultOrgMemberVisible = sec.Key("DEFAULT_ORG_MEMBER_VISIBLE").MustBool() - Service.UserDeleteWithCommentsMaxDays = sec.Key("USER_DELETE_WITH_COMMENTS_MAX_DAYS").MustInt(0) + Service.UserDeleteWithCommentsMaxTime = sec.Key("USER_DELETE_WITH_COMMENTS_MAX_DAYS").MustDuration(0) sec = Cfg.Section("openid") Service.EnableOpenIDSignIn = sec.Key("ENABLE_OPENID_SIGNIN").MustBool(!InstallLock) diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 69cfb290440b9..4f0406139671c 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -648,7 +648,7 @@ repos_none = You do not own any repositories delete_account = Delete Your Account delete_prompt = This operation will permanently delete your user account. It CAN NOT be undone. -delete_with_all_comments = Your account is younger than %d days. To avoid ghost comments, all issue/PR comments will be deleted with it. +delete_with_all_comments = Your account is younger than %d. To avoid ghost comments, all issue/PR comments will be deleted with it. confirm_delete_account = Confirm Deletion delete_account_title = Delete User Account delete_account_desc = Are you sure you want to permanently delete this user account? diff --git a/routers/user/setting/account.go b/routers/user/setting/account.go index 3b4191f0beeb2..42c2c59b7e752 100644 --- a/routers/user/setting/account.go +++ b/routers/user/setting/account.go @@ -302,8 +302,8 @@ func loadAccountData(ctx *context.Context) { ctx.Data["ActivationsPending"] = pendingActivation ctx.Data["CanAddEmails"] = !pendingActivation || !setting.Service.RegisterEmailConfirm - if setting.Service.UserDeleteWithCommentsMaxDays != 0 { - ctx.Data["UserDeleteWithCommentsMaxDays"] = setting.Service.UserDeleteWithCommentsMaxDays - ctx.Data["UserDeleteWithComments"] = ctx.User.CreatedUnix.AsTime().Add(time.Duration(setting.Service.UserDeleteWithCommentsMaxDays) * 24 * time.Hour).After(time.Now()) + if setting.Service.UserDeleteWithCommentsMaxTime != 0 { + ctx.Data["UserDeleteWithCommentsMaxTime"] = setting.Service.UserDeleteWithCommentsMaxTime.String() + ctx.Data["UserDeleteWithComments"] = ctx.User.CreatedUnix.AsTime().Add(setting.Service.UserDeleteWithCommentsMaxTime).After(time.Now()) } } diff --git a/templates/user/settings/account.tmpl b/templates/user/settings/account.tmpl index 4f7d8a50c705a..04ab539088397 100644 --- a/templates/user/settings/account.tmpl +++ b/templates/user/settings/account.tmpl @@ -174,7 +174,7 @@