Files
scrabble-game/backend/internal/social/admin.go
T
Ilia Denisov 81b9e1529e
CI / changes (pull_request) Successful in 2s
CI / unit (pull_request) Successful in 9s
CI / integration (pull_request) Successful in 15s
CI / ui (pull_request) Successful in 51s
CI / gate (pull_request) Successful in 0s
CI / deploy (pull_request) Successful in 1m19s
feat(social): asymmetric per-user block, in-game block control, admin lists
Make a per-user block one-directional and non-destructive: the blocker stops
receiving everything from the blocked user (chat, nudge, friend requests,
invitations) and the matchmaker never pairs them, while the blocked user
notices nothing — their sends still persist by the normal rules but are never
delivered or surfaced (born-read). A block no longer deletes the friendship
(an unblock cleanly restores it) and instant-reads any unread the blocked user
had left for the blocker.

- backend: a directional blockExists guard across chat/nudge/friends/invitations
  (store-but-hide for the blocked->blocker direction, refuse blocker->blocked);
  the matchmaker excludes a block-related pair (both directions) from auto-match;
  user_blocked/user_unblocked notifications to the blocker only (in-app only).
- ui: the opponent score card gains a block ✖️ control mirroring add-friend
  (red "Block?" confirm, mutual-hide while confirming, struck name, hidden chat
  composer when blocked); optimistic apply + event confirm + rollback for both.
- admin: the user card gains cross-linked blocks / blocked-by / friends lists.
- docs: FUNCTIONAL(+ru), ARCHITECTURE §10 + decision record, UI_DESIGN, PRERELEASE.
2026-06-18 11:50:34 +02:00

80 lines
3.1 KiB
Go

package social
import (
"context"
"fmt"
"time"
"github.com/google/uuid"
)
// AdminRelation is one entry in the admin user card's blocks / blocked-by / friends lists:
// the other account, its display name, and when the relationship was recorded. It is the
// unfiltered truth — the asymmetric block suppression that hides relationships from players
// never applies to the operator console.
type AdminRelation struct {
AccountID uuid.UUID
DisplayName string
At time.Time
}
// AdminBlocksBy returns the accounts blockerID has blocked, with display names and block
// times, newest first — the admin card's "blocks" list.
func (svc *Service) AdminBlocksBy(ctx context.Context, blockerID uuid.UUID) ([]AdminRelation, error) {
return svc.store.adminBlocks(ctx, blockerID, true)
}
// AdminBlockedBy returns the accounts that have blocked blockedID, with names and times,
// newest first — the admin card's "blocked by" list.
func (svc *Service) AdminBlockedBy(ctx context.Context, blockedID uuid.UUID) ([]AdminRelation, error) {
return svc.store.adminBlocks(ctx, blockedID, false)
}
// AdminFriends returns accountID's accepted friendships in either direction, with the other
// account's name and when the friendship was accepted, newest first — the admin card's
// "friends" list.
func (svc *Service) AdminFriends(ctx context.Context, accountID uuid.UUID) ([]AdminRelation, error) {
const q = `SELECT other_id, a.display_name, at FROM (
SELECT CASE WHEN f.requester_id = $1 THEN f.addressee_id ELSE f.requester_id END AS other_id,
COALESCE(f.responded_at, f.created_at) AS at
FROM backend.friendships f
WHERE f.status = 'accepted' AND (f.requester_id = $1 OR f.addressee_id = $1)
) rel
JOIN backend.accounts a ON a.account_id = rel.other_id
ORDER BY at DESC`
return svc.store.adminRelations(ctx, q, accountID)
}
// adminBlocks reads the blocks touching id: when asBlocker the rows where id is the blocker
// (the other side is who they blocked), otherwise the rows where id is the blocked (the
// other side is who blocked them).
func (s *Store) adminBlocks(ctx context.Context, id uuid.UUID, asBlocker bool) ([]AdminRelation, error) {
q := `SELECT b.blocked_id, a.display_name, b.created_at
FROM backend.blocks b JOIN backend.accounts a ON a.account_id = b.blocked_id
WHERE b.blocker_id = $1 ORDER BY b.created_at DESC`
if !asBlocker {
q = `SELECT b.blocker_id, a.display_name, b.created_at
FROM backend.blocks b JOIN backend.accounts a ON a.account_id = b.blocker_id
WHERE b.blocked_id = $1 ORDER BY b.created_at DESC`
}
return s.adminRelations(ctx, q, id)
}
// adminRelations runs an admin list query of the (account_id, display_name, at) shape.
func (s *Store) adminRelations(ctx context.Context, query string, id uuid.UUID) ([]AdminRelation, error) {
rows, err := s.db.QueryContext(ctx, query, id)
if err != nil {
return nil, fmt.Errorf("social: admin relations: %w", err)
}
defer rows.Close()
var out []AdminRelation
for rows.Next() {
var r AdminRelation
if err := rows.Scan(&r.AccountID, &r.DisplayName, &r.At); err != nil {
return nil, fmt.Errorf("social: scan admin relation: %w", err)
}
out = append(out, r)
}
return out, rows.Err()
}