fix(admin): sync moderation review state refresh

This commit is contained in:
iamxvbaba 2026-07-27 01:01:30 +08:00
parent e75fd04a28
commit 90649b5b67
4 changed files with 255 additions and 9 deletions

View file

@ -346,6 +346,20 @@ func (s *server) handleStarGiftCollectibleAnimationAPI(w http.ResponseWriter, r
}
func (s *server) proxyAdminJSON(w http.ResponseWriter, r *http.Request, apiPath string, maxBytes int64) {
s.proxyAdminJSONWithCache(w, r, apiPath, maxBytes, "private, max-age=30")
}
func (s *server) proxyAdminJSONNoStore(w http.ResponseWriter, r *http.Request, apiPath string, maxBytes int64) {
s.proxyAdminJSONWithCache(w, r, apiPath, maxBytes, "no-store")
}
func (s *server) proxyAdminJSONWithCache(
w http.ResponseWriter,
r *http.Request,
apiPath string,
maxBytes int64,
cacheControl string,
) {
req, err := http.NewRequestWithContext(r.Context(), http.MethodGet, s.cfg.AdminAPIURL+apiPath, nil)
if err != nil {
writeAPIError(w, http.StatusInternalServerError, err.Error())
@ -368,7 +382,7 @@ func (s *server) proxyAdminJSON(w http.ResponseWriter, r *http.Request, apiPath
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("Cache-Control", "private, max-age=30")
w.Header().Set("Cache-Control", cacheControl)
w.WriteHeader(http.StatusOK)
_, _ = w.Write(raw)
}
@ -378,7 +392,7 @@ func (s *server) handleModerationCasesAPI(w http.ResponseWriter, r *http.Request
if r.URL.RawQuery != "" {
apiPath += "?" + r.URL.RawQuery
}
s.proxyAdminJSON(w, r, apiPath, 4<<20)
s.proxyAdminJSONNoStore(w, r, apiPath, 4<<20)
}
func (s *server) handleModerationCaseAPI(w http.ResponseWriter, r *http.Request) {
@ -387,7 +401,7 @@ func (s *server) handleModerationCaseAPI(w http.ResponseWriter, r *http.Request)
writeAPIError(w, http.StatusBadRequest, "invalid moderation case id")
return
}
s.proxyAdminJSON(w, r, fmt.Sprintf("/v1/moderation/cases/%d", id), 4<<20)
s.proxyAdminJSONNoStore(w, r, fmt.Sprintf("/v1/moderation/cases/%d", id), 4<<20)
}
func (s *server) handleModerationReportAPI(w http.ResponseWriter, r *http.Request) {
@ -396,7 +410,7 @@ func (s *server) handleModerationReportAPI(w http.ResponseWriter, r *http.Reques
writeAPIError(w, http.StatusBadRequest, "invalid moderation report id")
return
}
s.proxyAdminJSON(w, r, fmt.Sprintf("/v1/moderation/reports/%d", id), 4<<20)
s.proxyAdminJSONNoStore(w, r, fmt.Sprintf("/v1/moderation/reports/%d", id), 4<<20)
}
func (s *server) handleClaimModerationCaseAPI(w http.ResponseWriter, r *http.Request) {

View file

@ -83,6 +83,67 @@ func TestSetAccountFrozenBFFForwardsClientVisibleState(t *testing.T) {
}
}
func TestModerationReadAPIDisablesBrowserCaching(t *testing.T) {
tests := []struct {
name string
requestPath string
upstreamPath string
invoke func(*server, http.ResponseWriter, *http.Request)
}{
{
name: "case list",
requestPath: "/api/moderation/cases?status=open",
upstreamPath: "/v1/moderation/cases?status=open",
invoke: (*server).handleModerationCasesAPI,
},
{
name: "case detail",
requestPath: "/api/moderation/cases/7",
upstreamPath: "/v1/moderation/cases/7",
invoke: func(s *server, w http.ResponseWriter, r *http.Request) {
r.SetPathValue("id", "7")
s.handleModerationCaseAPI(w, r)
},
},
{
name: "report detail",
requestPath: "/api/moderation/reports/9",
upstreamPath: "/v1/moderation/reports/9",
invoke: func(s *server, w http.ResponseWriter, r *http.Request) {
r.SetPathValue("id", "9")
s.handleModerationReportAPI(w, r)
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if got := r.URL.RequestURI(); got != test.upstreamPath {
t.Fatalf("upstream request URI = %q, want %q", got, test.upstreamPath)
}
if got := r.Header.Get("Authorization"); got != "Bearer secret" {
t.Fatalf("upstream authorization = %q", got)
}
_, _ = w.Write([]byte(`{}`))
}))
defer upstream.Close()
srv := &server{cfg: uiConfig{AdminAPIURL: upstream.URL, AdminAPIToken: "secret"}}
req := httptest.NewRequest(http.MethodGet, test.requestPath, nil)
rec := httptest.NewRecorder()
test.invoke(srv, rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status=%d body=%s", rec.Code, rec.Body.String())
}
if got := rec.Header().Get("Cache-Control"); got != "no-store" {
t.Fatalf("Cache-Control = %q, want no-store", got)
}
})
}
}
func TestStarGiftRowJSONPreservesInt64AsDecimalStrings(t *testing.T) {
const maxInt64 = int64(9223372036854775807)
raw, err := json.Marshal(StarGiftRow{