Stage 8 polish: profile validation, finished-game UI, badge + Safari fixes
Owner-review follow-up on the Stage 8 branch: - Friend code is copyable (📋 + toast). The lobby notification badge is fixed — it had inherited the hamburger-bar style — into a proper round count dot. - Safari: min-width:0 on flex text inputs (friend code, profile, chat) so they shrink instead of pushing the adjacent button off-screen. - Profile editing is validated on both the UI and the backend: display-name format (letters joined by single space/./_ separators, no leading/trailing/adjacent separators, <=32 runes), a UTC-offset timezone picker (account.ResolveZone parses ±HH:MM or a legacy IANA name), a 10-minute away grid capped at 12h (wrap-aware), and email format; Save is disabled and invalid fields red-bordered until valid. Language stays in Settings. - In a game, an "add to friends" menu item flips to a disabled "request sent"; chat send/nudge became ⬆️/🛎️ icon buttons. - A finished game drops its last-word highlight, hides Check word / Drop game, disables zoom, and draws an inert (greyed) footer instead of hiding it. Tests: account validators (name/away/zone), UI profileValidation, e2e for the finished-game footer/menu and the copy control. Docs (PLAN, ARCHITECTURE, FUNCTIONAL +ru, UI_DESIGN) updated for the display-name rule, UTC-offset timezone and the 12h away window.
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
@@ -16,8 +17,18 @@ import (
|
||||
"scrabble/backend/internal/postgres/jet/backend/table"
|
||||
)
|
||||
|
||||
// maxDisplayName caps a display name's length in runes.
|
||||
const maxDisplayName = 64
|
||||
// maxDisplayName caps an editable display name's length in runes (the column itself
|
||||
// is unbounded; auto-provisioned platform names bypass this editor validation).
|
||||
const maxDisplayName = 32
|
||||
|
||||
// maxAwayWindow bounds the daily away window's duration (midnight-wrap aware).
|
||||
const maxAwayWindow = 12 * time.Hour
|
||||
|
||||
// displayNameRe enforces the editable display-name format (Stage 8): Unicode letters
|
||||
// joined by single space / "." / "_" separators, where a "." or "_" may be followed
|
||||
// by a single space. No leading or trailing separator and no two adjacent separators,
|
||||
// except "<dot|underscore> <space>". So "Name_P. Last" is valid, "Name P._Last" is not.
|
||||
var displayNameRe = regexp.MustCompile(`^\p{L}+(?:(?:[._] ?| )\p{L}+)*$`)
|
||||
|
||||
// ErrInvalidProfile is returned when a profile update carries an unacceptable
|
||||
// field (an unknown language, an invalid timezone, or an over-long display name).
|
||||
@@ -46,12 +57,15 @@ func (s *Store) UpdateProfile(ctx context.Context, id uuid.UUID, p ProfileUpdate
|
||||
return Account{}, fmt.Errorf("%w: preferred_language %q", ErrInvalidProfile, p.PreferredLanguage)
|
||||
}
|
||||
tz := strings.TrimSpace(p.TimeZone)
|
||||
if _, err := time.LoadLocation(tz); err != nil {
|
||||
return Account{}, fmt.Errorf("%w: time_zone %q: %v", ErrInvalidProfile, p.TimeZone, err)
|
||||
if !validZone(tz) {
|
||||
return Account{}, fmt.Errorf("%w: time_zone %q", ErrInvalidProfile, p.TimeZone)
|
||||
}
|
||||
name := strings.TrimSpace(p.DisplayName)
|
||||
if utf8.RuneCountInString(name) > maxDisplayName {
|
||||
return Account{}, fmt.Errorf("%w: display name exceeds %d characters", ErrInvalidProfile, maxDisplayName)
|
||||
name, err := ValidateDisplayName(p.DisplayName)
|
||||
if err != nil {
|
||||
return Account{}, err
|
||||
}
|
||||
if err := validateAwayWindow(p.AwayStart, p.AwayEnd); err != nil {
|
||||
return Account{}, err
|
||||
}
|
||||
|
||||
stmt := table.Accounts.UPDATE(
|
||||
@@ -74,3 +88,35 @@ func (s *Store) UpdateProfile(ctx context.Context, id uuid.UUID, p ProfileUpdate
|
||||
}
|
||||
return modelToAccount(row), nil
|
||||
}
|
||||
|
||||
// ValidateDisplayName trims surrounding whitespace and checks the editable
|
||||
// display-name length (<= maxDisplayName runes) and format (displayNameRe),
|
||||
// returning the cleaned name or ErrInvalidProfile. It is exported so the gateway
|
||||
// boundary could reuse it; the UI mirrors the same rule.
|
||||
func ValidateDisplayName(raw string) (string, error) {
|
||||
name := strings.TrimSpace(raw)
|
||||
if name == "" {
|
||||
return "", fmt.Errorf("%w: display name is empty", ErrInvalidProfile)
|
||||
}
|
||||
if utf8.RuneCountInString(name) > maxDisplayName {
|
||||
return "", fmt.Errorf("%w: display name exceeds %d characters", ErrInvalidProfile, maxDisplayName)
|
||||
}
|
||||
if !displayNameRe.MatchString(name) {
|
||||
return "", fmt.Errorf("%w: display name has an invalid character or layout", ErrInvalidProfile)
|
||||
}
|
||||
return name, nil
|
||||
}
|
||||
|
||||
// validateAwayWindow checks that the daily away window's duration, wrapping across
|
||||
// midnight, does not exceed maxAwayWindow. A zero-length window (start == end) means
|
||||
// "no away time" and is allowed.
|
||||
func validateAwayWindow(start, end time.Time) error {
|
||||
mins := (end.Hour()*60 + end.Minute()) - (start.Hour()*60 + start.Minute())
|
||||
if mins < 0 {
|
||||
mins += 24 * 60
|
||||
}
|
||||
if time.Duration(mins)*time.Minute > maxAwayWindow {
|
||||
return fmt.Errorf("%w: away window exceeds %s", ErrInvalidProfile, maxAwayWindow)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
package account
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
// offsetZoneRe matches a fixed UTC offset like "+03:00" or "-05:30" — the form the
|
||||
// Stage 8 profile editor stores (an offset dropdown rather than an IANA name).
|
||||
var offsetZoneRe = regexp.MustCompile(`^([+-])(\d{2}):(\d{2})$`)
|
||||
|
||||
// parseOffsetZone parses a "±HH:MM" offset into a fixed-offset location, reporting
|
||||
// ok=false when name is not a well-formed offset within ±14:00.
|
||||
func parseOffsetZone(name string) (*time.Location, bool) {
|
||||
m := offsetZoneRe.FindStringSubmatch(name)
|
||||
if m == nil {
|
||||
return nil, false
|
||||
}
|
||||
h, _ := strconv.Atoi(m[2])
|
||||
min, _ := strconv.Atoi(m[3])
|
||||
if h > 14 || min > 59 || (h == 14 && min > 0) {
|
||||
return nil, false
|
||||
}
|
||||
secs := h*3600 + min*60
|
||||
if m[1] == "-" {
|
||||
secs = -secs
|
||||
}
|
||||
return time.FixedZone(name, secs), true
|
||||
}
|
||||
|
||||
// ResolveZone resolves a stored timezone — a fixed "±HH:MM" offset or an IANA name —
|
||||
// to a *time.Location, falling back to UTC when it is empty or unrecognised, so a
|
||||
// bad profile value never breaks the turn-timeout sweeper or the robot's sleep.
|
||||
func ResolveZone(name string) *time.Location {
|
||||
if name == "" {
|
||||
return time.UTC
|
||||
}
|
||||
if loc, ok := parseOffsetZone(name); ok {
|
||||
return loc
|
||||
}
|
||||
if loc, err := time.LoadLocation(name); err == nil {
|
||||
return loc
|
||||
}
|
||||
return time.UTC
|
||||
}
|
||||
|
||||
// validZone reports whether name is an acceptable timezone for a profile update —
|
||||
// either a "±HH:MM" offset or a loadable IANA location.
|
||||
func validZone(name string) bool {
|
||||
if _, ok := parseOffsetZone(name); ok {
|
||||
return true
|
||||
}
|
||||
_, err := time.LoadLocation(name)
|
||||
return err == nil
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
package account
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestValidateDisplayName(t *testing.T) {
|
||||
cases := map[string]struct {
|
||||
in string
|
||||
want string
|
||||
ok bool
|
||||
}{
|
||||
"plain": {"Kaya", "Kaya", true},
|
||||
"cyrillic": {"Кая", "Кая", true},
|
||||
"dot underscore mix": {"Name_P. Last", "Name_P. Last", true},
|
||||
"single dot": {"Mr.Smith", "Mr.Smith", true},
|
||||
"dot then space": {"Mr. Smith", "Mr. Smith", true},
|
||||
"trim surrounding": {" Kaya ", "Kaya", true},
|
||||
"adjacent specials": {"Name P._Last", "", false},
|
||||
"two spaces": {"Name Last", "", false},
|
||||
"leading special": {"_Name", "", false},
|
||||
"trailing special": {"Name.", "", false},
|
||||
"digit rejected": {"Name2", "", false},
|
||||
"blank": {" ", "", false},
|
||||
"too long": {strings.Repeat("a", 33), "", false},
|
||||
}
|
||||
for name, tc := range cases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
got, err := ValidateDisplayName(tc.in)
|
||||
if tc.ok != (err == nil) || (tc.ok && got != tc.want) {
|
||||
t.Fatalf("ValidateDisplayName(%q) = (%q, err=%v), want (%q, ok=%v)", tc.in, got, err, tc.want, tc.ok)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateAwayWindow(t *testing.T) {
|
||||
hm := func(h, m int) time.Time { return time.Date(0, 1, 1, h, m, 0, 0, time.UTC) }
|
||||
cases := map[string]struct {
|
||||
start, end time.Time
|
||||
ok bool
|
||||
}{
|
||||
"8h overnight": {hm(22, 0), hm(6, 0), true},
|
||||
"12h exact": {hm(0, 0), hm(12, 0), true},
|
||||
"13h daytime": {hm(8, 0), hm(21, 0), false},
|
||||
"zero window": {hm(7, 0), hm(7, 0), true},
|
||||
"13h wrap": {hm(20, 0), hm(9, 0), false},
|
||||
}
|
||||
for name, tc := range cases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
if err := validateAwayWindow(tc.start, tc.end); tc.ok != (err == nil) {
|
||||
t.Fatalf("validateAwayWindow = %v, want ok=%v", err, tc.ok)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveAndValidZone(t *testing.T) {
|
||||
offsetOf := func(name string) int {
|
||||
_, off := time.Date(2024, 1, 1, 12, 0, 0, 0, ResolveZone(name)).Zone()
|
||||
return off
|
||||
}
|
||||
if got := offsetOf("+03:00"); got != 3*3600 {
|
||||
t.Errorf("+03:00 offset = %d, want 10800", got)
|
||||
}
|
||||
if got := offsetOf("-05:30"); got != -(5*3600 + 30*60) {
|
||||
t.Errorf("-05:30 offset = %d", got)
|
||||
}
|
||||
if ResolveZone("nonsense-zone") != time.UTC {
|
||||
t.Error("unknown zone should resolve to UTC")
|
||||
}
|
||||
for _, ok := range []string{"+05:45", "-12:00", "+14:00", "Europe/Moscow", "UTC"} {
|
||||
if !validZone(ok) {
|
||||
t.Errorf("validZone(%q) = false, want true", ok)
|
||||
}
|
||||
}
|
||||
for _, bad := range []string{"+15:00", "03:00", "+3:00", "nope", "+05:99"} {
|
||||
if validZone(bad) {
|
||||
t.Errorf("validZone(%q) = true, want false", bad)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,8 @@ import (
|
||||
"time"
|
||||
|
||||
"go.uber.org/zap"
|
||||
|
||||
"scrabble/backend/internal/account"
|
||||
)
|
||||
|
||||
// effectiveDeadline is the instant a turn auto-resigns. It is the raw deadline
|
||||
@@ -57,17 +59,11 @@ func minutesOfDay(t time.Time) int {
|
||||
return t.Hour()*60 + t.Minute()
|
||||
}
|
||||
|
||||
// loadLocation resolves an IANA timezone name, falling back to UTC when it is
|
||||
// empty or unknown (so a bad profile value never breaks the sweeper).
|
||||
// loadLocation resolves a stored timezone (an IANA name or a "±HH:MM" offset),
|
||||
// falling back to UTC when it is empty or unknown (so a bad profile value never
|
||||
// breaks the sweeper). It defers to account.ResolveZone, the single source of truth.
|
||||
func loadLocation(name string) *time.Location {
|
||||
if name == "" {
|
||||
return time.UTC
|
||||
}
|
||||
loc, err := time.LoadLocation(name)
|
||||
if err != nil {
|
||||
return time.UTC
|
||||
}
|
||||
return loc
|
||||
return account.ResolveZone(name)
|
||||
}
|
||||
|
||||
// SweepTimeouts auto-resigns every active game whose current turn has exceeded
|
||||
|
||||
@@ -84,7 +84,7 @@ func TestFriendRequestRefusedByToggleAndBlock(t *testing.T) {
|
||||
|
||||
// Toggle: the addressee does not accept friend requests.
|
||||
a, b := provisionAccount(t), provisionAccount(t)
|
||||
if _, err := store.UpdateProfile(ctx, b, account.ProfileUpdate{PreferredLanguage: "en", TimeZone: "UTC", BlockFriendRequests: true}); err != nil {
|
||||
if _, err := store.UpdateProfile(ctx, b, account.ProfileUpdate{DisplayName: "Player", PreferredLanguage: "en", TimeZone: "UTC", BlockFriendRequests: true}); err != nil {
|
||||
t.Fatalf("set toggle: %v", err)
|
||||
}
|
||||
if err := svc.SendFriendRequest(ctx, a, b); !errors.Is(err, social.ErrRequestBlocked) {
|
||||
@@ -257,7 +257,7 @@ func TestChatPostListAndBlocks(t *testing.T) {
|
||||
if _, err := svc.PostMessage(ctx, other, seats2[0], "hi", ""); err != nil {
|
||||
t.Fatalf("post 2: %v", err)
|
||||
}
|
||||
if _, err := store.UpdateProfile(ctx, seats2[1], account.ProfileUpdate{PreferredLanguage: "en", TimeZone: "UTC", BlockChat: true}); err != nil {
|
||||
if _, err := store.UpdateProfile(ctx, seats2[1], account.ProfileUpdate{DisplayName: "Player", PreferredLanguage: "en", TimeZone: "UTC", BlockChat: true}); err != nil {
|
||||
t.Fatalf("set block_chat: %v", err)
|
||||
}
|
||||
if msgs, _ := svc.Messages(ctx, other, seats2[1]); len(msgs) != 0 {
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"math"
|
||||
"time"
|
||||
|
||||
"scrabble/backend/internal/account"
|
||||
"scrabble/backend/internal/engine"
|
||||
)
|
||||
|
||||
@@ -136,17 +137,11 @@ func asleep(opponentTZ string, drift time.Duration, now time.Time) bool {
|
||||
return h >= sleepStartHour && h < sleepEndHour
|
||||
}
|
||||
|
||||
// loadLocation resolves an IANA timezone name, falling back to UTC when it is
|
||||
// empty or unknown (so a bad opponent profile never breaks the driver).
|
||||
// loadLocation resolves a stored timezone (an IANA name or a "±HH:MM" offset),
|
||||
// falling back to UTC when it is empty or unknown (so a bad opponent profile never
|
||||
// breaks the driver). It defers to account.ResolveZone.
|
||||
func loadLocation(name string) *time.Location {
|
||||
if name == "" {
|
||||
return time.UTC
|
||||
}
|
||||
loc, err := time.LoadLocation(name)
|
||||
if err != nil {
|
||||
return time.UTC
|
||||
}
|
||||
return loc
|
||||
return account.ResolveZone(name)
|
||||
}
|
||||
|
||||
// selectMove chooses the robot's action given the ranked candidate plays, the
|
||||
|
||||
Reference in New Issue
Block a user