Files
pages/internal/server/notification_push.go
T
Anya Tokugawa d5d21b7841
Build and publish container images / images (push) Successful in 9m34s
Release container images / release (push) Successful in 36s
Refactor architecture into eight components and consolidate reliability work
Extract Grammaratur, Dregarnuhr, Geduldh, Mestionora, Ordoschnellic, Schutzaria, Kunstzeal and Seheweit behind compatible application contracts.

Include accumulated editor resilience, navigation and theme UX, remote CLI, storage/schema and DR groundwork, tests and design/operations documentation.

Verified: uncached full Go suite; go vet ./...; race tests for all eight components; 11 targeted Chromium tests; Node suites; 19 Python CLI tests; reproducible editor build. No push, release tag or deployment in this checkpoint.
2026-11-01 15:23:31 +09:00

100 lines
3.5 KiB
Go

package server
import (
"context"
"database/sql"
"encoding/json"
"errors"
"net/http"
"time"
webpush "github.com/SherClockHolmes/webpush-go"
"pageswiki/internal/ordoschnellic"
"pageswiki/internal/store"
)
type browserPushInput struct {
Subscription webpush.Subscription `json:"subscription"`
Focused bool `json:"focused"`
Seen []string `json:"seen"`
}
type storedBrowserPush = ordoschnellic.StoredBrowserPush
func validPushSubscription(sub webpush.Subscription) bool {
return ordoschnellic.ValidPushSubscription(sub)
}
func (s *Server) notificationPushKeys(ctx context.Context) (private, public string, err error) {
err = s.store.DB.QueryRowContext(ctx, `SELECT private_key,public_key FROM notification_push_keys WHERE id=1`).Scan(&private, &public)
if !errors.Is(err, sql.ErrNoRows) {
return
}
private, public, err = webpush.GenerateVAPIDKeys()
if err != nil {
return
}
_, err = s.store.DB.ExecContext(ctx, `INSERT OR IGNORE INTO notification_push_keys(id,private_key,public_key) VALUES(1,?,?)`, private, public)
if err != nil {
return
}
err = s.store.DB.QueryRowContext(ctx, `SELECT private_key,public_key FROM notification_push_keys WHERE id=1`).Scan(&private, &public)
return
}
func (s *Server) handleNotificationPush(w http.ResponseWriter, r *http.Request, domain, owner string) {
ctx, cancel := context.WithTimeout(r.Context(), 5*time.Second)
defer cancel()
switch r.Method {
case "GET":
_, pub, err := s.notificationPushKeys(ctx)
if err != nil {
writeEventJSON(w, 503, map[string]string{"error": "push unavailable"})
return
}
writeEventJSON(w, 200, map[string]string{"public_key": pub})
return
case "POST", "DELETE":
if !validBrowserMutationOrigin(r) {
http.Error(w, "invalid origin", 403)
return
}
var input browserPushInput
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 8192)).Decode(&input); err != nil || !validPushSubscription(input.Subscription) || len(input.Seen) > 30 {
writeEventJSON(w, 400, map[string]string{"error": "invalid push subscription"})
return
}
var err error
if r.Method == "DELETE" {
err = s.store.DeleteNotificationPush(ctx, domain, owner, input.Subscription.Endpoint)
} else {
raw, _ := json.Marshal(storedBrowserPush{Subscription: input.Subscription, Origin: s.socialOrigin(r)})
err = s.store.SaveNotificationPush(ctx, domain, owner, s.authTokenFromRequest(r), input.Subscription.Endpoint, string(raw), input.Focused)
if err == nil && input.Focused {
err = s.store.AcknowledgeNotificationPush(ctx, domain, owner, input.Subscription.Endpoint, input.Seen)
}
}
if err != nil {
writeEventJSON(w, 409, map[string]string{"error": "unable to save subscription (maximum 8 devices)"})
return
}
writeEventJSON(w, 200, map[string]bool{"ok": true})
return
}
w.Header().Set("Allow", "GET, POST, DELETE")
http.Error(w, "method not allowed", 405)
}
// Compatibility entry points for existing callers; event registration passes
// its worker context through deliverNotificationPushContext.
func (s *Server) deliverNotificationPush(job *store.EventJob) error {
return s.deliverNotificationPushContext(context.Background(), job)
}
func (s *Server) sendNotificationPush(ctx context.Context, d *store.NotificationPushDelivery, sub storedBrowserPush, client webpush.HTTPClient) (success, retry, expired bool) {
copy := *d
raw, _ := json.Marshal(sub)
copy.Subscription = string(raw)
outcome := (ordoschnellic.WebPush{Keys: s.notificationPushKeys, Client: client}).Send(ctx, &copy)
return outcome.Success, outcome.Retry, outcome.Expired
}