Skip to content

Commit 21da519

Browse files
6543moqmar
andauthored
Implement ghost comment mitigation (#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 ca63a9d commit 21da519

File tree

7 files changed

+27
-1
lines changed

7 files changed

+27
-1
lines changed

custom/conf/app.example.ini

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

692695
[webhook]
693696
; 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
@@ -1151,6 +1151,15 @@ func deleteUser(e *xorm.Session, u *User) error {
11511151
return fmt.Errorf("deleteBeans: %v", err)
11521152
}
11531153

1154+
if setting.Service.UserDeleteWithCommentsMaxDays != 0 &&
1155+
u.CreatedUnix.AsTime().Add(time.Duration(setting.Service.UserDeleteWithCommentsMaxDays)*24*time.Hour).After(time.Now()) {
1156+
if err = deleteBeans(e,
1157+
&Comment{PosterID: u.ID},
1158+
); err != nil {
1159+
return fmt.Errorf("deleteBeans: %v", err)
1160+
}
1161+
}
1162+
11541163
// ***** START: PublicKey *****
11551164
if _, err = e.Delete(&PublicKey{OwnerID: u.ID}); err != nil {
11561165
return fmt.Errorf("deletePublicKeys: %v", err)
@@ -1205,7 +1214,8 @@ func deleteUser(e *xorm.Session, u *User) error {
12051214
}
12061215

12071216
// DeleteUser completely and permanently deletes everything of a user,
1208-
// but issues/comments/pulls will be kept and shown as someone has been deleted.
1217+
// but issues/comments/pulls will be kept and shown as someone has been deleted,
1218+
// unless the user is younger than USER_DELETE_WITH_COMMENTS_MAX_DAYS.
12091219
func DeleteUser(u *User) (err error) {
12101220
if u.IsOrganization() {
12111221
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
@@ -50,6 +50,7 @@ var Service struct {
5050
AutoWatchNewRepos bool
5151
AutoWatchOnChanges bool
5252
DefaultOrgMemberVisible bool
53+
UserDeleteWithCommentsMaxDays int
5354

5455
// OpenID settings
5556
EnableOpenIDSignIn bool
@@ -102,6 +103,7 @@ func newService() {
102103
Service.DefaultOrgVisibility = sec.Key("DEFAULT_ORG_VISIBILITY").In("public", structs.ExtractKeysFromMapString(structs.VisibilityModes))
103104
Service.DefaultOrgVisibilityMode = structs.VisibilityModes[Service.DefaultOrgVisibility]
104105
Service.DefaultOrgMemberVisible = sec.Key("DEFAULT_ORG_MEMBER_VISIBLE").MustBool()
106+
Service.UserDeleteWithCommentsMaxDays = sec.Key("USER_DELETE_WITH_COMMENTS_MAX_DAYS").MustInt(0)
105107

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

options/locale/locale_en-US.ini

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

647647
delete_account = Delete Your Account
648648
delete_prompt = This operation will permanently delete your user account. It <strong>CAN NOT</strong> be undone.
649+
delete_with_all_comments = Your account is younger than %d days. To avoid ghost comments, all issue/PR comments will be deleted with it.
649650
confirm_delete_account = Confirm Deletion
650651
delete_account_title = Delete User Account
651652
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"
@@ -300,4 +301,9 @@ func loadAccountData(ctx *context.Context) {
300301
ctx.Data["EmailNotificationsPreference"] = ctx.User.EmailNotifications()
301302
ctx.Data["ActivationsPending"] = pendingActivation
302303
ctx.Data["CanAddEmails"] = !pendingActivation || !setting.Service.RegisterEmailConfirm
304+
305+
if setting.Service.UserDeleteWithCommentsMaxDays != 0 {
306+
ctx.Data["UserDeleteWithCommentsMaxDays"] = setting.Service.UserDeleteWithCommentsMaxDays
307+
ctx.Data["UserDeleteWithComments"] = ctx.User.CreatedUnix.AsTime().Add(time.Duration(setting.Service.UserDeleteWithCommentsMaxDays) * 24 * time.Hour).After(time.Now())
308+
}
303309
}

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)