Add web push notifications for new inbox items
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
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,12 +18,14 @@ 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"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
store *store.Store
|
||||
agents *agents.Manager
|
||||
notifier *push.Notifier
|
||||
hub *hub
|
||||
startedAt time.Time
|
||||
spa fs.FS
|
||||
@@ -47,14 +49,21 @@ func New(dataDir string, spa fs.FS) (*Server, error) {
|
||||
users: map[string]string{},
|
||||
}
|
||||
|
||||
s.store, err = store.Open(filepath.Join(dataDir, "byre.db"), s.hub.publish)
|
||||
s.store, err = store.Open(filepath.Join(dataDir, "byre.db"), s.publish)
|
||||
if err != nil {
|
||||
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, agents.DefaultLimits, filepath.Join(dataDir, "worktrees"))
|
||||
err = s.agents.Recover(context.Background())
|
||||
if err != nil {
|
||||
s.notifier.Close()
|
||||
s.store.Close()
|
||||
return nil, fmt.Errorf("recover sessions: %w", err)
|
||||
}
|
||||
@@ -62,13 +71,24 @@ func New(dataDir string, 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)
|
||||
}
|
||||
|
||||
func (s *Server) Close() error {
|
||||
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},
|
||||
|
||||
@@ -116,20 +116,22 @@ func TestServesEmbeddedSPA(t *testing.T) {
|
||||
t.Cleanup(hs.Close)
|
||||
|
||||
for path, contentType := range map[string]string{
|
||||
"/": "text/html",
|
||||
"/style.css": "text/css",
|
||||
"/dist/app.js": "text/javascript",
|
||||
"/dist/app.js.map": "",
|
||||
"/src/app.ts": "",
|
||||
"/dist/inbox.js": "text/javascript",
|
||||
"/dist/api.gen.js": "text/javascript",
|
||||
"/dist/tasks.js": "text/javascript",
|
||||
"/dist/navbar.js": "text/javascript",
|
||||
"/dist/panes.js": "text/javascript",
|
||||
"/dist/task.js": "text/javascript",
|
||||
"/dist/state.js": "text/javascript",
|
||||
"/dist/dom.js": "text/javascript",
|
||||
"/dist/api.js": "text/javascript",
|
||||
"/": "text/html",
|
||||
"/style.css": "text/css",
|
||||
"/dist/app.js": "text/javascript",
|
||||
"/dist/app.js.map": "",
|
||||
"/src/app.ts": "",
|
||||
"/dist/inbox.js": "text/javascript",
|
||||
"/dist/api.gen.js": "text/javascript",
|
||||
"/dist/tasks.js": "text/javascript",
|
||||
"/dist/navbar.js": "text/javascript",
|
||||
"/dist/panes.js": "text/javascript",
|
||||
"/dist/task.js": "text/javascript",
|
||||
"/dist/state.js": "text/javascript",
|
||||
"/dist/dom.js": "text/javascript",
|
||||
"/dist/api.js": "text/javascript",
|
||||
"/dist/settings.js": "text/javascript",
|
||||
"/sw.js": "text/javascript",
|
||||
"/fonts/atkinson-hyperlegible-mono-latin-wght-normal.woff2": "font/woff2",
|
||||
"/fonts/LICENSE": "",
|
||||
} {
|
||||
@@ -167,6 +169,40 @@ func TestMe(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestNotificationSettings(t *testing.T) {
|
||||
h := newHarness(t)
|
||||
|
||||
me := api.User{}
|
||||
h.do("GET", "/api/me", nil, &me)
|
||||
if me.NotificationPrefs != (api.NotificationPrefs{BlockingQuestions: true}) {
|
||||
t.Fatalf("unexpected default prefs %+v", me.NotificationPrefs)
|
||||
}
|
||||
|
||||
code := h.do("PUT", "/api/me/notifications", api.NotificationPrefs{BlockingQuestions: true, Reports: true}, &me)
|
||||
if code != http.StatusOK || me.NotificationPrefs != (api.NotificationPrefs{BlockingQuestions: true, Reports: true}) {
|
||||
t.Fatalf("update prefs: %d %+v", code, me.NotificationPrefs)
|
||||
}
|
||||
|
||||
key := api.PushKey{}
|
||||
h.do("GET", "/api/push/key", nil, &key)
|
||||
if len(key.PublicKey) < 80 {
|
||||
t.Fatalf("unexpected vapid key %q", key.PublicKey)
|
||||
}
|
||||
|
||||
code = h.do("POST", "/api/me/push-subscriptions", api.PushSubscription{Endpoint: "http://push.example/x", P256dh: "k", Auth: "a"}, nil)
|
||||
if code != http.StatusBadRequest {
|
||||
t.Fatalf("want 400 for a non-https endpoint, got %d", code)
|
||||
}
|
||||
code = h.do("POST", "/api/me/push-subscriptions", api.PushSubscription{Endpoint: "https://push.example/x", P256dh: "k", Auth: "a"}, nil)
|
||||
if code != http.StatusNoContent {
|
||||
t.Fatalf("subscribe: %d", code)
|
||||
}
|
||||
code = h.do("DELETE", "/api/me/push-subscriptions?endpoint=https%3A%2F%2Fpush.example%2Fx", nil, nil)
|
||||
if code != http.StatusNoContent {
|
||||
t.Fatalf("unsubscribe: %d", code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProjectAndTaskLifecycle(t *testing.T) {
|
||||
h := newHarness(t)
|
||||
p := h.createProject("demo")
|
||||
|
||||
Reference in New Issue
Block a user