From ba6fa0d7733911267238ecc552a6dbb8344a7b56 Mon Sep 17 00:00:00 2001 From: Dima Krasner Date: Fri, 18 Oct 2024 17:50:05 +0300 Subject: [PATCH] be more user-friendly when profile change is throttled --- front/alias.go | 10 +++++++--- front/avatar.go | 10 +++++++--- front/bio.go | 10 +++++++--- front/move.go | 10 +++++++--- front/name.go | 10 +++++++--- test/avatar_test.go | 4 ++-- test/bio_test.go | 2 +- test/move_test.go | 2 +- test/name_test.go | 2 +- 9 files changed, 40 insertions(+), 20 deletions(-) diff --git a/front/alias.go b/front/alias.go index d04b27d0..ca1c327b 100644 --- a/front/alias.go +++ b/front/alias.go @@ -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 } diff --git a/front/avatar.go b/front/avatar.go index b31fee1c..0d195611 100644 --- a/front/avatar.go +++ b/front/avatar.go @@ -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 } diff --git a/front/bio.go b/front/bio.go index 4aef6019..7fb793c8 100644 --- a/front/bio.go +++ b/front/bio.go @@ -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 } diff --git a/front/move.go b/front/move.go index a51153b2..5922a409 100644 --- a/front/move.go +++ b/front/move.go @@ -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 } diff --git a/front/name.go b/front/name.go index 296d7563..61ca39cc 100644 --- a/front/name.go +++ b/front/name.go @@ -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 } diff --git a/test/avatar_test.go b/test/avatar_test.go index 81d11735..d7ae4342 100644 --- a/test/avatar_test.go +++ b/test/avatar_test.go @@ -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) { @@ -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) { diff --git a/test/bio_test.go b/test/bio_test.go index 7cc6aaa3..586d89c5 100644 --- a/test/bio_test.go +++ b/test/bio_test.go @@ -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) { diff --git a/test/move_test.go b/test/move_test.go index 9ae0b605..044bbc73 100644 --- a/test/move_test.go +++ b/test/move_test.go @@ -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) { diff --git a/test/name_test.go b/test/name_test.go index 0f588609..daeebc32 100644 --- a/test/name_test.go +++ b/test/name_test.go @@ -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) {