Replace Web Push and its service worker with page notifications over SSE
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"g.fc.run/misc/byre/internal/api"
|
||||
)
|
||||
|
||||
func (s *Server) UpdateNotificationPrefs(w http.ResponseWriter, r *http.Request) {
|
||||
req := api.NotificationPrefs{}
|
||||
if !readJSON(w, r, &req) {
|
||||
return
|
||||
}
|
||||
|
||||
u, err := s.store.UpdateNotificationPrefs(r.Context(), actor(r), req)
|
||||
if err != nil {
|
||||
writeStoreError(w, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, u)
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"g.fc.run/misc/byre/internal/api"
|
||||
"g.fc.run/misc/byre/internal/store"
|
||||
)
|
||||
|
||||
func (s *Server) UpdateNotificationPrefs(w http.ResponseWriter, r *http.Request) {
|
||||
req := api.NotificationPrefs{}
|
||||
if !readJSON(w, r, &req) {
|
||||
return
|
||||
}
|
||||
|
||||
u, err := s.store.UpdateNotificationPrefs(r.Context(), actor(r), req)
|
||||
if err != nil {
|
||||
writeStoreError(w, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, u)
|
||||
}
|
||||
|
||||
func (s *Server) AddPushSubscription(w http.ResponseWriter, r *http.Request) {
|
||||
req := api.PushSubscription{}
|
||||
if !readJSON(w, r, &req) {
|
||||
return
|
||||
}
|
||||
|
||||
endpoint, err := url.Parse(req.Endpoint)
|
||||
if err != nil || endpoint.Scheme != "https" || endpoint.Host == "" || req.P256dh == "" || req.Auth == "" {
|
||||
writeError(w, http.StatusBadRequest, errors.New("an https endpoint, p256dh and auth are required"))
|
||||
return
|
||||
}
|
||||
|
||||
err = s.store.AddPushSubscription(r.Context(), store.PushSubscription{
|
||||
Endpoint: req.Endpoint,
|
||||
UserLogin: actor(r),
|
||||
P256dh: req.P256dh,
|
||||
Auth: req.Auth,
|
||||
Subscriber: "https://" + r.Host,
|
||||
})
|
||||
if err != nil {
|
||||
writeStoreError(w, err)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func (s *Server) RemovePushSubscription(w http.ResponseWriter, r *http.Request, params api.RemovePushSubscriptionParams) {
|
||||
err := s.store.RemovePushSubscription(r.Context(), params.Endpoint, actor(r), actor(r))
|
||||
if err != nil {
|
||||
writeStoreError(w, err)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func (s *Server) GetPushKey(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, http.StatusOK, api.PushKey{PublicKey: s.notifier.PublicKey()})
|
||||
}
|
||||
@@ -18,7 +18,6 @@ import (
|
||||
"g.fc.run/misc/byre/internal/agents"
|
||||
"g.fc.run/misc/byre/internal/api"
|
||||
"g.fc.run/misc/byre/internal/git"
|
||||
"g.fc.run/misc/byre/internal/push"
|
||||
"g.fc.run/misc/byre/internal/store"
|
||||
"g.fc.run/misc/byre/internal/todo"
|
||||
)
|
||||
@@ -26,7 +25,6 @@ import (
|
||||
type Server struct {
|
||||
store *store.Store
|
||||
agents *agents.Manager
|
||||
notifier *push.Notifier
|
||||
hub *hub
|
||||
terminals *terminals
|
||||
startedAt time.Time
|
||||
@@ -57,16 +55,9 @@ func New(dataDir string, portFirst int, spa fs.FS) (*Server, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
s.notifier, err = push.New(s.store, pushWindow)
|
||||
if err != nil {
|
||||
s.store.Close()
|
||||
return nil, fmt.Errorf("start push notifier: %w", err)
|
||||
}
|
||||
|
||||
s.agents = agents.New(s.store, filepath.Join(dataDir, "worktrees"), filepath.Join(dataDir, "leads"), filepath.Join(dataDir, "codex"))
|
||||
err = s.agents.Recover(context.Background())
|
||||
if err != nil {
|
||||
s.notifier.Close()
|
||||
s.store.Close()
|
||||
return nil, fmt.Errorf("recover sessions: %w", err)
|
||||
}
|
||||
@@ -75,11 +66,8 @@ func New(dataDir string, portFirst int, spa fs.FS) (*Server, error) {
|
||||
return s, nil
|
||||
}
|
||||
|
||||
const pushWindow = 3 * time.Second
|
||||
|
||||
func (s *Server) publish(ev api.Event) {
|
||||
s.hub.publish(ev)
|
||||
s.notifier.Observe(ev)
|
||||
s.agents.Observe(ev)
|
||||
if (ev.Kind == "task.merged" || ev.Kind == "task.discarded") && ev.TaskId != nil {
|
||||
s.terminals.close(*ev.TaskId)
|
||||
@@ -90,15 +78,11 @@ func (s *Server) publish(ev api.Event) {
|
||||
func (s *Server) Close() error {
|
||||
s.terminals.closeAll()
|
||||
s.agents.Close()
|
||||
s.notifier.Close()
|
||||
return s.store.Close()
|
||||
}
|
||||
|
||||
func (s *Server) Handler() http.Handler {
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("GET /sw.js", func(w http.ResponseWriter, r *http.Request) {
|
||||
http.ServeFileFS(w, r, s.spa, "dist/sw.js")
|
||||
})
|
||||
api.HandlerWithOptions(s, api.StdHTTPServerOptions{
|
||||
BaseRouter: mux,
|
||||
Middlewares: []api.MiddlewareFunc{s.identify},
|
||||
|
||||
Reference in New Issue
Block a user