107 lines
3.8 KiB
Go
107 lines
3.8 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) {
|
|
if !s.store.SupportsNotificationPush() {
|
|
return "", "", store.ErrBackendFeatureUnavailable
|
|
}
|
|
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) {
|
|
if !s.store.SupportsNotificationPush() {
|
|
writeEventJSON(w, http.StatusServiceUnavailable, map[string]string{"error": "push disabled for this database backend"})
|
|
return
|
|
}
|
|
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, ©)
|
|
return outcome.Success, outcome.Retry, outcome.Expired
|
|
}
|