Skip to content

Commit 605381e

Browse files
ashimokawaCodeberg Admins @ build
authored andcommitted
CB/feat: Simple rate limiting for issues
CB/feat: Make dirty rate limiting more flexible (Otto)
1 parent 82ed3cb commit 605381e

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

models/issues/comment.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"regexp"
1313
"strconv"
1414
"strings"
15+
"time"
1516
"unicode/utf8"
1617

1718
"code.gitea.io/gitea/models/db"
@@ -792,6 +793,14 @@ func (c *Comment) LoadPushCommits(ctx context.Context) (err error) {
792793
// CreateCommentCtx creates comment with context
793794
func CreateCommentCtx(ctx context.Context, opts *CreateCommentOptions) (_ *Comment, err error) {
794795
e := db.GetEngine(ctx)
796+
// codeberg-specific
797+
if opts.Type == CommentTypeComment {
798+
count, _ := e.Table("comment").Where("poster_id = ?", opts.Doer.ID).And("created_unix>?", time.Now().Unix()-120).And("type=0").Count(&Comment{})
799+
if count > 10 {
800+
return nil, fmt.Errorf("NewComment: Rate Limited " + opts.Doer.Name)
801+
}
802+
}
803+
795804
var LabelID int64
796805
if opts.Label != nil {
797806
LabelID = opts.Label.ID

models/issues/issue.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"sort"
1313
"strconv"
1414
"strings"
15+
"time"
1516

1617
"code.gitea.io/gitea/models/db"
1718
"code.gitea.io/gitea/models/foreignreference"
@@ -965,6 +966,12 @@ type NewIssueOptions struct {
965966
// NewIssueWithIndex creates issue with given index
966967
func NewIssueWithIndex(ctx context.Context, doer *user_model.User, opts NewIssueOptions) (err error) {
967968
e := db.GetEngine(ctx)
969+
// codeberg specific
970+
count, err := e.Table("issue").Where("poster_id = ?", doer.ID).And("created_unix>?", time.Now().Unix()-180).Count(new(Issue))
971+
if count > 6 {
972+
return fmt.Errorf("NewIssue: Rate Limited" + doer.Name)
973+
}
974+
968975
opts.Issue.Title = strings.TrimSpace(opts.Issue.Title)
969976

970977
if opts.Issue.MilestoneID > 0 {

0 commit comments

Comments
 (0)