Files
scrabble-game/backend/internal/server/handlers_blocks.go
T
Ilia Denisov 64be0572b3
CI / changes (pull_request) Successful in 3s
CI / unit (pull_request) Successful in 9s
CI / integration (pull_request) Successful in 17s
CI / ui (pull_request) Successful in 52s
CI / gate (pull_request) Successful in 0s
CI / deploy (pull_request) Successful in 1m21s
fix(social): robot blocks
Blocking an auto-match opponent who is secretly a pooled robot is recorded  instead in a separate `robot_blocks` table.

Now blocking behaves the same in that game (struck name, hidden composer) and lists the blocked opponent under the name you saw, but is recorded only against that game — the disguise holds, the shared robot is never globally blocked, and the matchmaker keeps pairing you with robots (so you can never block yourself out of opponents).

- the shared robot account is never put in `blocks`
- the matchmaker keeps it free and it is not blocked under its other per-game names
- the blocked list and the in-game card still show it by joining that table; an unblock deletes the row
2026-06-18 13:12:19 +02:00

112 lines
3.3 KiB
Go

package server
import (
"net/http"
"strings"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
// The /api/v1/user/blocks/* handlers wire the per-user block list. A block is asymmetric:
// the blocker stops seeing everything from the blocked user (chat, nudge, requests,
// invitations, matchmaking) while the blocked user notices nothing; it overrides — but does
// not delete — any friendship (an unblock restores it). They reuse the friend handlers'
// targetIDRequest and account-ref resolution.
// blockListDTO is the accounts the caller has blocked, plus the per-game disguised-robot
// blocks (not real accounts), which the blocked list shows as distinct personalities.
type blockListDTO struct {
Blocked []accountRefDTO `json:"blocked"`
Robots []robotBlockDTO `json:"robots"`
}
// robotBlockDTO is one per-game disguised-robot block: the row id (used to unblock it), the
// game name the player saw, and the game + seat it was blocked in (so the in-game card can
// re-mark that seat).
type robotBlockDTO struct {
ID string `json:"id"`
DisplayName string `json:"display_name"`
GameID string `json:"game_id"`
Seat int `json:"seat"`
}
// handleBlock blocks the body-supplied account.
func (s *Server) handleBlock(c *gin.Context) {
uid, ok := userID(c)
if !ok {
abortBadRequest(c, "missing identity")
return
}
var req targetIDRequest
if err := c.ShouldBindJSON(&req); err != nil {
abortBadRequest(c, "invalid request body")
return
}
target, ok := parseUUIDField(req.AccountID)
if !ok {
abortBadRequest(c, "invalid account id")
return
}
// An in-game block carries the game id, so a disguised-robot opponent is recorded as a
// per-game block (BlockInGame); a settings-screen block (no game) blocks a human directly.
var err error
if strings.TrimSpace(req.GameID) == "" {
err = s.social.Block(c.Request.Context(), uid, target)
} else if gameID, ok := parseUUIDField(req.GameID); !ok {
abortBadRequest(c, "invalid game id")
return
} else {
err = s.social.BlockInGame(c.Request.Context(), uid, target, gameID)
}
if err != nil {
s.abortErr(c, err)
return
}
c.JSON(http.StatusOK, okResponse{OK: true})
}
// handleUnblock removes the caller's block on the :id account.
func (s *Server) handleUnblock(c *gin.Context) {
uid, ok := userID(c)
if !ok {
abortBadRequest(c, "missing identity")
return
}
target, err := uuid.Parse(c.Param("id"))
if err != nil {
abortBadRequest(c, "invalid account id")
return
}
if err := s.social.Unblock(c.Request.Context(), uid, target); err != nil {
s.abortErr(c, err)
return
}
c.JSON(http.StatusOK, okResponse{OK: true})
}
// handleListBlocks returns the accounts the caller has blocked.
func (s *Server) handleListBlocks(c *gin.Context) {
uid, ok := userID(c)
if !ok {
abortBadRequest(c, "missing identity")
return
}
ctx := c.Request.Context()
ids, err := s.social.ListBlocks(ctx, uid)
if err != nil {
s.abortErr(c, err)
return
}
robots, err := s.social.ListRobotBlocks(ctx, uid)
if err != nil {
s.abortErr(c, err)
return
}
dto := blockListDTO{Blocked: s.accountRefs(ctx, ids)}
for _, r := range robots {
dto.Robots = append(dto.Robots, robotBlockDTO{ID: r.ID.String(), DisplayName: r.DisplayName, GameID: r.GameID.String(), Seat: r.Seat})
}
c.JSON(http.StatusOK, dto)
}