Skip to content

Commit

Permalink
Merge branch 'main' into bookmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkr committed Oct 17, 2024
2 parents 85232aa + 60b0acb commit bcb15aa
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 13 deletions.
5 changes: 3 additions & 2 deletions front/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ func (h *Handler) doEdit(w text.Writer, r *Request, args []string, readInput inp
}

canEdit := lastEditTime.Add(h.Config.EditThrottleUnit * time.Duration(math.Pow(h.Config.EditThrottleFactor, float64(edits))))
if time.Now().Before(canEdit) {
until := time.Until(canEdit)
if until > 0 {
r.Log.Warn("Throttled request to edit post", "note", note.ID, "can", canEdit)
w.Status(40, "Please try again later")
w.Statusf(40, "Please wait for %s", until.Truncate(time.Second).String())
return
}

Expand Down
11 changes: 6 additions & 5 deletions front/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,17 @@ func (h *Handler) post(w text.Writer, r *Request, oldNote *ap.Object, inReplyTo

if today.Valid && today.Int64 >= h.Config.MaxPostsPerDay {
r.Log.Warn("User has exceeded the daily posts quota", "posts", today.Int64)
w.Status(40, "Please wait before posting again")
w.Status(40, "Reached daily posts quota")
return
}

if today.Valid && last.Valid {
t := time.Unix(last.Int64, 0)
interval := max(1, time.Duration(today.Int64/h.Config.PostThrottleFactor)) * h.Config.PostThrottleUnit
if now.Sub(t) < interval {
r.Log.Warn("User is posting too frequently", "last", t, "can", t.Add(interval))
w.Status(40, "Please wait before posting again")
can := t.Add(max(1, time.Duration(today.Int64/h.Config.PostThrottleFactor)) * h.Config.PostThrottleUnit)
until := time.Until(can)
if until > 0 {
r.Log.Warn("User is posting too frequently", "last", t, "can", can)
w.Statusf(40, "Please wait for %s", until.Truncate(time.Second).String())
return
}
}
Expand Down
4 changes: 2 additions & 2 deletions test/edit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestEdit_Throttling(t *testing.T) {
id := whisper[15 : len(whisper)-2]

edit := server.Handle(fmt.Sprintf("/users/edit/%s?Hello%%20followers", id), server.Bob)
assert.Equal("40 Please try again later\r\n", edit)
assert.Regexp(`^40 Please wait for \S+\r\n$`, edit)

users = server.Handle("/users", server.Alice)
assert.Contains(users, "No posts.")
Expand Down Expand Up @@ -98,7 +98,7 @@ func TestEdit_HappyFlow(t *testing.T) {
assert.Contains(users, "Hello followers")

edit = server.Handle(fmt.Sprintf("/users/edit/%s?Hello,%%20followers", id), server.Bob)
assert.Equal("40 Please try again later\r\n", edit)
assert.Regexp(`^40 Please wait for \S+\r\n$`, edit)

users = server.Handle("/users", server.Alice)
assert.NotContains(users, "No posts.")
Expand Down
2 changes: 1 addition & 1 deletion test/poll_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ func TestPoll_DeleteReply(t *testing.T) {
assert.Equal(1, valid)

edit := server.Handle(fmt.Sprintf("/users/edit/%s?chocolate", reply[15:len(reply)-2]), server.Alice)
assert.Equal("40 Please try again later\r\n", edit)
assert.Regexp(`^40 Please wait for \S+\r\n$`, edit)
}

func TestPoll_Update(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion test/say_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestSay_Throttling(t *testing.T) {
assert.Contains(outbox, "Hello world")

say = server.Handle("/users/say?Hello%20once%20more,%20world", server.Alice)
assert.Equal("40 Please wait before posting again\r\n", say)
assert.Regexp(`^40 Please wait for \S+\r\n$`, say)

outbox = server.Handle("/users/outbox/"+strings.TrimPrefix(server.Alice.ID, "https://"), server.Bob)
assert.Contains(outbox, "Hello world")
Expand Down
2 changes: 1 addition & 1 deletion test/upload_edit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestUploadEdit_HappyFlow(t *testing.T) {
assert.Contains(users, "Hello followers")

edit = server.Upload(fmt.Sprintf("/users/upload/edit/%s;mime=text/plain;size=16", id), server.Bob, []byte("Hello, followers"))
assert.Equal("40 Please try again later\r\n", edit)
assert.Regexp(`^40 Please wait for \S+\r\n$`, edit)

assert.NoError((inbox.FeedUpdater{Domain: domain, Config: server.cfg, DB: server.db}).Run(context.Background()))

Expand Down
2 changes: 1 addition & 1 deletion test/whisper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func TestWhisper_Throttling(t *testing.T) {
assert.Contains(outbox, "Hello world")

whisper = server.Handle("/users/whisper?Hello%20once%20more,%20world", server.Alice)
assert.Equal("40 Please wait before posting again\r\n", whisper)
assert.Regexp(`^40 Please wait for \S+\r\n$`, whisper)

outbox = server.Handle("/users/outbox/"+strings.TrimPrefix(server.Alice.ID, "https://"), server.Bob)
assert.Contains(outbox, "Hello world")
Expand Down

0 comments on commit bcb15aa

Please sign in to comment.