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 18, 2024
2 parents bcb15aa + ba6fa0d commit 3f8e099
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 20 deletions.
10 changes: 7 additions & 3 deletions front/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,13 @@ func (h *Handler) alias(w text.Writer, r *Request, args ...string) {

now := time.Now()

if (r.User.Updated != nil && now.Sub(r.User.Updated.Time) < h.Config.MinActorEditInterval) || (r.User.Updated == nil && now.Sub(r.User.Published.Time) < h.Config.MinActorEditInterval) {
r.Log.Warn("Throttled request to set alias")
w.Status(40, "Please try again later")
can := r.User.Published.Time.Add(h.Config.MinActorEditInterval)
if r.User.Updated != nil {
can = r.User.Updated.Time.Add(h.Config.MinActorEditInterval)
}
if now.Before(can) {
r.Log.Warn("Throttled request to set alias", "can", can)
w.Statusf(40, "Please wait for %s", time.Until(can).Truncate(time.Second).String())
return
}

Expand Down
10 changes: 7 additions & 3 deletions front/avatar.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,13 @@ func (h *Handler) uploadAvatar(w text.Writer, r *Request, args ...string) {

now := time.Now()

if (r.User.Updated != nil && now.Sub(r.User.Updated.Time) < h.Config.MinActorEditInterval) || (r.User.Updated == nil && now.Sub(r.User.Published.Time) < h.Config.MinActorEditInterval) {
r.Log.Warn("Throttled request to set avatar")
w.Status(40, "Please try again later")
can := r.User.Published.Time.Add(h.Config.MinActorEditInterval)
if r.User.Updated != nil {
can = r.User.Updated.Time.Add(h.Config.MinActorEditInterval)
}
if now.Before(can) {
r.Log.Warn("Throttled request to set avatar", "can", can)
w.Statusf(40, "Please wait for %s", time.Until(can).Truncate(time.Second).String())
return
}

Expand Down
10 changes: 7 additions & 3 deletions front/bio.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@ func (h *Handler) doBio(w text.Writer, r *Request, readInput func(text.Writer, *

now := time.Now()

if (r.User.Updated != nil && now.Sub(r.User.Updated.Time) < h.Config.MinActorEditInterval) || (r.User.Updated == nil && now.Sub(r.User.Published.Time) < h.Config.MinActorEditInterval) {
r.Log.Warn("Throttled request to set summary")
w.Status(40, "Please try again later")
can := r.User.Published.Time.Add(h.Config.MinActorEditInterval)
if r.User.Updated != nil {
can = r.User.Updated.Time.Add(h.Config.MinActorEditInterval)
}
if now.Before(can) {
r.Log.Warn("Throttled request to set summary", "can", can)
w.Statusf(40, "Please wait for %s", time.Until(can).Truncate(time.Second).String())
return
}

Expand Down
10 changes: 7 additions & 3 deletions front/move.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,13 @@ func (h *Handler) move(w text.Writer, r *Request, args ...string) {

now := time.Now()

if (r.User.Updated != nil && now.Sub(r.User.Updated.Time) < h.Config.MinActorEditInterval) || (r.User.Updated == nil && now.Sub(r.User.Published.Time) < h.Config.MinActorEditInterval) {
r.Log.Warn("Throttled request to move account")
w.Status(40, "Please try again later")
can := r.User.Published.Time.Add(h.Config.MinActorEditInterval)
if r.User.Updated != nil {
can = r.User.Updated.Time.Add(h.Config.MinActorEditInterval)
}
if now.Before(can) {
r.Log.Warn("Throttled request to move account", "can", can)
w.Statusf(40, "Please wait for %s", time.Until(can).Truncate(time.Second).String())
return
}

Expand Down
10 changes: 7 additions & 3 deletions front/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,13 @@ func (h *Handler) name(w text.Writer, r *Request, args ...string) {

now := time.Now()

if (r.User.Updated != nil && now.Sub(r.User.Updated.Time) < h.Config.MinActorEditInterval) || (r.User.Updated == nil && now.Sub(r.User.Published.Time) < h.Config.MinActorEditInterval) {
r.Log.Warn("Throttled request to set name")
w.Status(40, "Please try again later")
can := r.User.Published.Time.Add(h.Config.MinActorEditInterval)
if r.User.Updated != nil {
can = r.User.Updated.Time.Add(h.Config.MinActorEditInterval)
}
if now.Before(can) {
r.Log.Warn("Throttled request to set name", "can", can)
w.Statusf(40, "Please wait for %s", time.Until(can).Truncate(time.Second).String())
return
}

Expand Down
4 changes: 2 additions & 2 deletions test/avatar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestAvatar_NewUser(t *testing.T) {
assert := assert.New(t)

server.Alice.Published = &ap.Time{Time: time.Now().Add(-time.Second * 5)}
assert.Equal("40 Please try again later\r\n", server.Upload("/users/upload/avatar;mime=image/gif;size=63", server.Alice, avatar))
assert.Regexp(`^40 Please wait for \S+\r\n$`, server.Upload("/users/upload/avatar;mime=image/gif;size=63", server.Alice, avatar))
}

func TestAvatar_ChangedRecently(t *testing.T) {
Expand All @@ -55,7 +55,7 @@ func TestAvatar_ChangedRecently(t *testing.T) {

server.Alice.Published.Time = server.Alice.Published.Time.Add(-time.Hour)
server.Alice.Updated = &ap.Time{Time: time.Now().Add(-time.Second * 5)}
assert.Equal("40 Please try again later\r\n", server.Upload("/users/upload/avatar;mime=image/gif;size=63", server.Alice, avatar))
assert.Regexp(`^40 Please wait for \S+\r\n$`, server.Upload("/users/upload/avatar;mime=image/gif;size=63", server.Alice, avatar))
}

func TestAvatar_HappyFlowSizeFirst(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion test/bio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestBio_Throttled(t *testing.T) {
assert := assert.New(t)

summary := server.Handle("/users/bio?Hello%20world", server.Alice)
assert.Equal("40 Please try again later\r\n", summary)
assert.Regexp(`^40 Please wait for \S+\r\n$`, summary)
}

func TestBio_HappyFlow(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion test/move_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ func TestMove_LocalToLocalAliasThrottled(t *testing.T) {
assert := assert.New(t)

alias := server.Handle("/users/alias?bob%40localhost.localdomain%3a8443", server.Alice)
assert.Equal("40 Please try again later\r\n", alias)
assert.Regexp(`^40 Please wait for \S+\r\n$`, alias)
}

func TestMove_LocalToLocal(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion test/name_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestName_Throttled(t *testing.T) {
assert := assert.New(t)

summary := server.Handle("/users/name?Jane%20Doe", server.Alice)
assert.Equal("40 Please try again later\r\n", summary)
assert.Regexp(`^40 Please wait for \S+\r\n$`, summary)
}

func TestName_HappyFlow(t *testing.T) {
Expand Down

0 comments on commit 3f8e099

Please sign in to comment.