Skip to content

Commit 8328b59

Browse files
6543moqmar
authored andcommitted
Implement ghost comment mitigation (go-gitea#14349)
* Implement ghost comment mitigation Adds a config option USER_DELETE_WITH_COMMENTS_MAX_DAYS to the [service] section. See https://codeberg.org/Codeberg/Discussion/issues/24 for the underlying issue. * cleanup * use setting module correctly * add to docs Co-authored-by: Moritz Marquardt <[email protected]>
1 parent 656ed56 commit 8328b59

File tree

8 files changed

+28
-1
lines changed

8 files changed

+28
-1
lines changed

custom/conf/app.example.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,9 @@ AUTO_WATCH_NEW_REPOS = true
671671
; Default value for AutoWatchOnChanges
672672
; Make the user watch a repository When they commit for the first time
673673
AUTO_WATCH_ON_CHANGES = false
674+
; Default value for the minimum age a user has to exist before deletion to keep issue comments.
675+
; If a user deletes his account before that amount of days, his comments will be deleted as well.
676+
USER_DELETE_WITH_COMMENTS_MAX_DAYS = 0
674677

675678
[webhook]
676679
; Hook task queue length, increase if webhook shooting starts hanging

docs/content/doc/advanced/config-cheat-sheet.en-us.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@ relation to port exhaustion.
474474
- `ALLOW_ONLY_EXTERNAL_REGISTRATION`: **false** Set to true to force registration only using third-party services.
475475
- `NO_REPLY_ADDRESS`: **DOMAIN** Default value for the domain part of the user's email address in the git log if he has set KeepEmailPrivate to true.
476476
The user's email will be replaced with a concatenation of the user name in lower case, "@" and NO_REPLY_ADDRESS.
477+
- `USER_DELETE_WITH_COMMENTS_MAX_DAYS`: **0** If a user deletes his account before that amount of days, his comments will be deleted as well.
477478

478479
## SSH Minimum Key Sizes (`ssh.minimum_key_sizes`)
479480

models/user.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1141,6 +1141,15 @@ func deleteUser(e Engine, u *User) error {
11411141
return fmt.Errorf("deleteBeans: %v", err)
11421142
}
11431143

1144+
if setting.Service.UserDeleteWithCommentsMaxDays != 0 &&
1145+
u.CreatedUnix.AsTime().Add(time.Duration(setting.Service.UserDeleteWithCommentsMaxDays)*24*time.Hour).After(time.Now()) {
1146+
if err = deleteBeans(e,
1147+
&Comment{PosterID: u.ID},
1148+
); err != nil {
1149+
return fmt.Errorf("deleteBeans: %v", err)
1150+
}
1151+
}
1152+
11441153
// ***** START: PublicKey *****
11451154
if _, err = e.Delete(&PublicKey{OwnerID: u.ID}); err != nil {
11461155
return fmt.Errorf("deletePublicKeys: %v", err)
@@ -1208,7 +1217,8 @@ func deleteUser(e Engine, u *User) error {
12081217
}
12091218

12101219
// DeleteUser completely and permanently deletes everything of a user,
1211-
// but issues/comments/pulls will be kept and shown as someone has been deleted.
1220+
// but issues/comments/pulls will be kept and shown as someone has been deleted,
1221+
// unless the user is younger than USER_DELETE_WITH_COMMENTS_MAX_DAYS.
12121222
func DeleteUser(u *User) (err error) {
12131223
if u.IsOrganization() {
12141224
return fmt.Errorf("%s is an organization not a user", u.Name)

modules/setting/service.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ var Service struct {
4949
AutoWatchNewRepos bool
5050
AutoWatchOnChanges bool
5151
DefaultOrgMemberVisible bool
52+
UserDeleteWithCommentsMaxDays int
5253

5354
// OpenID settings
5455
EnableOpenIDSignIn bool
@@ -96,6 +97,7 @@ func newService() {
9697
Service.DefaultOrgVisibility = sec.Key("DEFAULT_ORG_VISIBILITY").In("public", structs.ExtractKeysFromMapString(structs.VisibilityModes))
9798
Service.DefaultOrgVisibilityMode = structs.VisibilityModes[Service.DefaultOrgVisibility]
9899
Service.DefaultOrgMemberVisible = sec.Key("DEFAULT_ORG_MEMBER_VISIBLE").MustBool()
100+
Service.UserDeleteWithCommentsMaxDays = sec.Key("USER_DELETE_WITH_COMMENTS_MAX_DAYS").MustInt(0)
99101

100102
sec = Cfg.Section("openid")
101103
Service.EnableOpenIDSignIn = sec.Key("ENABLE_OPENID_SIGNIN").MustBool(!InstallLock)

options/locale/locale_de-DE.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,7 @@ repos_none=Du besitzt keine Repositories
626626

627627
delete_account=Konto löschen
628628
delete_prompt=Wenn du fortfährst, wird dein Account permanent gelöscht. Dies <strong>KANN NICHT</strong> rückgängig gemacht werden.
629+
delete_with_all_comments = Dein Account ist jünger als %d Tage. Um Geisterkommentare zu vermeiden, werden alle Issue/PR-Kommentare zusammen mit deinem Benutzeraccount gelöscht.
629630
confirm_delete_account=Löschen bestätigen
630631
delete_account_title=Benutzerkonto löschen
631632
delete_account_desc=Bist du sicher, dass du diesen Account dauerhaft löschen möchtest?

options/locale/locale_en-US.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,7 @@ repos_none = You do not own any repositories
640640

641641
delete_account = Delete Your Account
642642
delete_prompt = This operation will permanently delete your user account. It <strong>CAN NOT</strong> be undone.
643+
delete_with_all_comments = Your account is younger than %d days. To avoid ghost comments, all issue/PR comments will be deleted with it.
643644
confirm_delete_account = Confirm Deletion
644645
delete_account_title = Delete User Account
645646
delete_account_desc = Are you sure you want to permanently delete this user account?

routers/user/setting/account.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package setting
77

88
import (
99
"errors"
10+
"time"
1011

1112
"code.gitea.io/gitea/models"
1213
"code.gitea.io/gitea/modules/auth"
@@ -301,4 +302,9 @@ func loadAccountData(ctx *context.Context) {
301302
ctx.Data["EmailNotificationsPreference"] = ctx.User.EmailNotifications()
302303
ctx.Data["ActivationsPending"] = pendingActivation
303304
ctx.Data["CanAddEmails"] = !pendingActivation || !setting.Service.RegisterEmailConfirm
305+
306+
if setting.Service.UserDeleteWithCommentsMaxDays != 0 {
307+
ctx.Data["UserDeleteWithCommentsMaxDays"] = setting.Service.UserDeleteWithCommentsMaxDays
308+
ctx.Data["UserDeleteWithComments"] = ctx.User.CreatedUnix.AsTime().Add(time.Duration(setting.Service.UserDeleteWithCommentsMaxDays) * 24 * time.Hour).After(time.Now())
309+
}
304310
}

templates/user/settings/account.tmpl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,9 @@
173173
<div class="ui attached error segment">
174174
<div class="ui red message">
175175
<p class="text left">{{svg "octicon-alert"}} {{.i18n.Tr "settings.delete_prompt" | Str2html}}</p>
176+
{{ if .UserDeleteWithComments }}
177+
<p class="text left" style="font-weight: bold;">{{.i18n.Tr "settings.delete_with_all_comments" .UserDeleteWithCommentsMaxDays | Str2html}}</p>
178+
{{ end }}
176179
</div>
177180
<form class="ui form ignore-dirty" id="delete-form" action="{{AppSubUrl}}/user/settings/account/delete" method="post">
178181
{{.CsrfTokenHtml}}

0 commit comments

Comments
 (0)