فهرست منبع

feat(i18n): accept ru for the user language and push subscriptions

ksantd 2 هفته پیش
والد
کامیت
ccfcc2290f

+ 14 - 1
server-go/internal/httpapi/httpapi_test.go

@@ -553,12 +553,25 @@ func TestUsersCRUD(t *testing.T) {
 	if body["language"] != "es" {
 	if body["language"] != "es" {
 		t.Fatalf("language: %v", body)
 		t.Fatalf("language: %v", body)
 	}
 	}
+	// ru también es válido (nuevo idioma del panel)
+	req, _ = http.NewRequest("PUT", srv.URL+"/api/users/me/language", strings.NewReader(`{"language":"ru"}`))
+	req.Header.Set("Cookie", "session="+anaCookie)
+	res, _ = http.DefaultClient.Do(req)
+	res.Body.Close()
+	if res.StatusCode != 204 {
+		t.Fatalf("me/language ru: got %d want 204", res.StatusCode)
+	}
+	res = get(t, srv.URL, "/api/auth/me", anaCookie)
+	body = readJSON(t, res)
+	if body["language"] != "ru" {
+		t.Fatalf("language ru: %v", body)
+	}
 	// Idioma inválido → 400 literal
 	// Idioma inválido → 400 literal
 	req, _ = http.NewRequest("PUT", srv.URL+"/api/users/me/language", strings.NewReader(`{"language":"fr"}`))
 	req, _ = http.NewRequest("PUT", srv.URL+"/api/users/me/language", strings.NewReader(`{"language":"fr"}`))
 	req.Header.Set("Cookie", "session="+anaCookie)
 	req.Header.Set("Cookie", "session="+anaCookie)
 	res, _ = http.DefaultClient.Do(req)
 	res, _ = http.DefaultClient.Do(req)
 	body = readJSON(t, res)
 	body = readJSON(t, res)
-	if res.StatusCode != 400 || body["message"] != "language debe ser auto|es|en" {
+	if res.StatusCode != 400 || body["message"] != "language must be auto|es|en|ru" {
 		t.Fatalf("language inválido: got %d %v", res.StatusCode, body)
 		t.Fatalf("language inválido: got %d %v", res.StatusCode, body)
 	}
 	}
 
 

+ 3 - 3
server-go/internal/httpapi/push.go

@@ -40,7 +40,7 @@ type pushSubscribeBody struct {
 		Auth   string `json:"auth"`
 		Auth   string `json:"auth"`
 		P256dh string `json:"p256dh"`
 		P256dh string `json:"p256dh"`
 	} `json:"keys"`
 	} `json:"keys"`
-	// Language (#1334): idioma efectivo de la UI en el alta (es|en); el push
+	// Language (#1334): idioma efectivo de la UI en el alta (es|en|ru); el push
 	// se compone en ese idioma por suscriptor.
 	// se compone en ese idioma por suscriptor.
 	Language string `json:"language"`
 	Language string `json:"language"`
 }
 }
@@ -62,8 +62,8 @@ func (s *server) handlePushSubscribe(w http.ResponseWriter, r *http.Request) {
 	if body.Endpoint == "" || len(body.Endpoint) > maxEndpointLen ||
 	if body.Endpoint == "" || len(body.Endpoint) > maxEndpointLen ||
 		body.Keys.Auth == "" || len(body.Keys.Auth) > maxPushKeyLen ||
 		body.Keys.Auth == "" || len(body.Keys.Auth) > maxPushKeyLen ||
 		body.Keys.P256dh == "" || len(body.Keys.P256dh) > maxPushKeyLen ||
 		body.Keys.P256dh == "" || len(body.Keys.P256dh) > maxPushKeyLen ||
-		(body.Language != "es" && body.Language != "en") {
-		writeError(w, http.StatusBadRequest, "invalid_body", "endpoint, keys.auth, keys.p256dh y language (es|en) son obligatorios")
+		(body.Language != "es" && body.Language != "en" && body.Language != "ru") {
+		writeError(w, http.StatusBadRequest, "invalid_body", "endpoint, keys.auth, keys.p256dh and language (es|en|ru) are required")
 		return
 		return
 	}
 	}
 	if err := push.ValidateEndpoint(body.Endpoint); err != nil {
 	if err := push.ValidateEndpoint(body.Endpoint); err != nil {

+ 11 - 1
server-go/internal/httpapi/push_api_test.go

@@ -58,7 +58,7 @@ func TestPushEndpointsContract(t *testing.T) {
 		return n
 		return n
 	}
 	}
 
 
-	// POST subscribe: 201 la primera vez. #1334: language (es|en) obligatorio.
+	// POST subscribe: 201 la primera vez. #1334: language (es|en|ru) obligatorio.
 	body := `{"endpoint":"https://push.example/abc","keys":{"auth":"a1","p256dh":"p1"},"language":"en"}`
 	body := `{"endpoint":"https://push.example/abc","keys":{"auth":"a1","p256dh":"p1"},"language":"en"}`
 	res = doJSON(t, "POST", ts.URL, "/api/push/subscribe", cookie, body)
 	res = doJSON(t, "POST", ts.URL, "/api/push/subscribe", cookie, body)
 	if res.StatusCode != 201 {
 	if res.StatusCode != 201 {
@@ -88,6 +88,16 @@ func TestPushEndpointsContract(t *testing.T) {
 	if err := ts.db.QueryRow("SELECT keys_auth FROM push_subscriptions WHERE endpoint = ?", "https://push.example/abc").Scan(&auth); err != nil || auth != "a2" {
 	if err := ts.db.QueryRow("SELECT keys_auth FROM push_subscriptions WHERE endpoint = ?", "https://push.example/abc").Scan(&auth); err != nil || auth != "a2" {
 		t.Fatalf("upsert no actualizó claves: auth=%q err=%v", auth, err)
 		t.Fatalf("upsert no actualizó claves: auth=%q err=%v", auth, err)
 	}
 	}
+	// ru también es un idioma válido: upsert y persistencia.
+	body = `{"endpoint":"https://push.example/abc","keys":{"auth":"a3","p256dh":"p3"},"language":"ru"}`
+	res = doJSON(t, "POST", ts.URL, "/api/push/subscribe", cookie, body)
+	if res.StatusCode != 200 {
+		t.Fatalf("subscribe upsert ru: %d, esperaba 200", res.StatusCode)
+	}
+	res.Body.Close()
+	if err := ts.db.QueryRow("SELECT language FROM push_subscriptions WHERE endpoint = ?", "https://push.example/abc").Scan(&lang); err != nil || lang != "ru" {
+		t.Fatalf("upsert ru no persistió language: lang=%q err=%v", lang, err)
+	}
 	// Cuerpos inválidos → 400.
 	// Cuerpos inválidos → 400.
 	for _, bad := range []string{
 	for _, bad := range []string{
 		`nope`,
 		`nope`,

+ 6 - 6
server-go/internal/httpapi/users.go

@@ -102,18 +102,18 @@ func (s *server) handleMyDisplayName(w http.ResponseWriter, r *http.Request) {
 	w.WriteHeader(http.StatusNoContent)
 	w.WriteHeader(http.StatusNoContent)
 }
 }
 
 
-// handleMyLanguage: {language: auto|es|en} → 204 (fuera del gate admin).
+// handleMyLanguage: {language: auto|es|en|ru} → 204 (fuera del gate admin).
 func (s *server) handleMyLanguage(w http.ResponseWriter, r *http.Request) {
 func (s *server) handleMyLanguage(w http.ResponseWriter, r *http.Request) {
 	me := auth.UserFromContext(r.Context())
 	me := auth.UserFromContext(r.Context())
 	var body struct {
 	var body struct {
 		Language string `json:"language"`
 		Language string `json:"language"`
 	}
 	}
 	if st := readJSONBody(w, r, &body); st != 0 {
 	if st := readJSONBody(w, r, &body); st != 0 {
-		writeBodyError(w, st, "invalid_input", "language debe ser auto|es|en")
+		writeBodyError(w, st, "invalid_input", "language must be auto|es|en|ru")
 		return
 		return
 	}
 	}
-	if body.Language != "auto" && body.Language != "es" && body.Language != "en" {
-		writeError(w, http.StatusBadRequest, "invalid_input", "language debe ser auto|es|en")
+	if body.Language != "auto" && body.Language != "es" && body.Language != "en" && body.Language != "ru" {
+		writeError(w, http.StatusBadRequest, "invalid_input", "language must be auto|es|en|ru")
 		return
 		return
 	}
 	}
 	_, _ = s.db.Exec("UPDATE users SET language = ? WHERE id = ?", body.Language, me.ID)
 	_, _ = s.db.Exec("UPDATE users SET language = ? WHERE id = ?", body.Language, me.ID)
@@ -208,9 +208,9 @@ func (s *server) handleCreateUser(w http.ResponseWriter, r *http.Request) {
 	}
 	}
 	language := "auto"
 	language := "auto"
 	if body.Language != nil {
 	if body.Language != nil {
-		if *body.Language != "auto" && *body.Language != "es" && *body.Language != "en" {
+		if *body.Language != "auto" && *body.Language != "es" && *body.Language != "en" && *body.Language != "ru" {
 			writeError(w, http.StatusBadRequest, "invalid_input",
 			writeError(w, http.StatusBadRequest, "invalid_input",
-				"Invalid enum value. Expected 'auto' | 'es' | 'en'")
+				"Invalid enum value. Expected 'auto' | 'es' | 'en' | 'ru'")
 			return
 			return
 		}
 		}
 		language = *body.Language
 		language = *body.Language

+ 4 - 1
server-go/internal/push/notifier.go

@@ -298,8 +298,11 @@ func (n *Notifier) SendTest(s Subscription) TestResult {
 // testEvent compone el aviso de prueba en el idioma de la suscripcion.
 // testEvent compone el aviso de prueba en el idioma de la suscripcion.
 func testEvent(lang string) alerts.AlertEvent {
 func testEvent(lang string) alerts.AlertEvent {
 	title, body := "Test notification", "Push notifications work on this device."
 	title, body := "Test notification", "Push notifications work on this device."
-	if lang == "es" {
+	switch lang {
+	case "es":
 		title, body = "Notificacion de prueba", "Las notificaciones push funcionan en este dispositivo."
 		title, body = "Notificacion de prueba", "Las notificaciones push funcionan en este dispositivo."
+	case "ru":
+		title, body = "Тестовое уведомление", "Push-уведомления работают на этом устройстве."
 	}
 	}
 	return alerts.AlertEvent{
 	return alerts.AlertEvent{
 		ID:          fmt.Sprintf("push-test-%d", time.Now().UnixNano()),
 		ID:          fmt.Sprintf("push-test-%d", time.Now().UnixNano()),