Files
scrabble-game/backend/internal/server/handlers_feedback.go
T
Ilia Denisov b78ce42922
CI / changes (pull_request) Successful in 2s
CI / unit (pull_request) Successful in 10s
CI / integration (pull_request) Successful in 16s
CI / ui (pull_request) Successful in 54s
CI / gate (pull_request) Successful in 0s
CI / deploy (pull_request) Successful in 1m19s
feat(feedback): capture the app version; show version + local Filed time
Each feedback submission now carries the client app version (__APP_VERSION__),
snapshotted like the interface language: FlatBuffers FeedbackSubmitRequest gains
a version field → gateway transcode → backend, persisted in a new nullable
feedback_messages.app_version column (migration 00003, additive so an image
rollback stays DB-safe). The operator console detail shows the app version and
renders the Filed time in UTC plus the sender's time zone (fmtTimeIn).

Touches: fbs schema + regenerated Go/TS codegen, codec + transport (the client
attaches its build), gateway transcode + backendclient, feedback store/service,
admin view + template, docs (ARCHITECTURE §15, FUNCTIONAL + _ru). Verified:
feedback integration tests (migration + version round-trip), codec round-trip,
check/unit/build green.
2026-06-22 17:02:05 +02:00

109 lines
3.5 KiB
Go

package server
import (
"encoding/base64"
"net/http"
"github.com/gin-gonic/gin"
)
// feedbackSubmitRequest is the player's feedback submission. Attachment is the
// base64-encoded file bytes (empty for none); AttachmentName carries the original
// file name (its extension is the allow-list key); Channel is the submitting
// platform (telegram/ios/android/web).
type feedbackSubmitRequest struct {
Body string `json:"body"`
Attachment string `json:"attachment"`
AttachmentName string `json:"attachment_name"`
Channel string `json:"channel"`
// Version is the client's app version (pkg/version / the SPA build), snapshotted so the
// operator sees which build a report came from.
Version string `json:"version"`
}
// feedbackReplyDTO is the operator's reply shown back to the player.
type feedbackReplyDTO struct {
Body string `json:"body"`
RepliedAtUnix int64 `json:"replied_at_unix"`
}
// feedbackStateResponse is the player's feedback screen state. BlockedReason is
// "" (can send), "pending" or "banned"; Reply is omitted when there is none.
type feedbackStateResponse struct {
CanSend bool `json:"can_send"`
BlockedReason string `json:"blocked_reason"`
Reply *feedbackReplyDTO `json:"reply,omitempty"`
}
// feedbackUnreadResponse reports whether the player has an undelivered reply, for
// the lobby/Info badge.
type feedbackUnreadResponse struct {
ReplyUnread bool `json:"reply_unread"`
}
// handleFeedbackSubmit stores a feedback message from the authenticated player.
// The sender IP comes from the gateway-forwarded X-Forwarded-For header. Guests
// and feedback-banned accounts are refused (also gated at the gateway).
func (s *Server) handleFeedbackSubmit(c *gin.Context) {
uid, ok := userID(c)
if !ok {
abortBadRequest(c, "missing identity")
return
}
var req feedbackSubmitRequest
if err := c.ShouldBindJSON(&req); err != nil {
abortBadRequest(c, "invalid request body")
return
}
var attachment []byte
if req.Attachment != "" {
data, err := base64.StdEncoding.DecodeString(req.Attachment)
if err != nil {
abortBadRequest(c, "invalid attachment encoding")
return
}
attachment = data
}
if err := s.feedback.Submit(c.Request.Context(), uid, req.Body, attachment, req.AttachmentName, req.Channel, req.Version, clientIP(c)); err != nil {
s.abortErr(c, err)
return
}
c.JSON(http.StatusOK, okResponse{OK: true})
}
// handleFeedbackState returns the player's feedback screen state and marks any
// pending operator replies delivered (clearing the badge).
func (s *Server) handleFeedbackState(c *gin.Context) {
uid, ok := userID(c)
if !ok {
abortBadRequest(c, "missing identity")
return
}
st, err := s.feedback.State(c.Request.Context(), uid)
if err != nil {
s.abortErr(c, err)
return
}
resp := feedbackStateResponse{CanSend: st.CanSend, BlockedReason: st.BlockedReason}
if st.Reply != nil {
resp.Reply = &feedbackReplyDTO{Body: st.Reply.Body, RepliedAtUnix: st.Reply.RepliedAt.Unix()}
}
c.JSON(http.StatusOK, resp)
}
// handleFeedbackUnread reports whether the player has an undelivered operator
// reply, for the lobby/Info badge. It has no side effect.
func (s *Server) handleFeedbackUnread(c *gin.Context) {
uid, ok := userID(c)
if !ok {
abortBadRequest(c, "missing identity")
return
}
unread, err := s.feedback.ReplyUnread(c.Request.Context(), uid)
if err != nil {
s.abortErr(c, err)
return
}
c.JSON(http.StatusOK, feedbackUnreadResponse{ReplyUnread: unread})
}