Add web push notifications for new inbox items
This commit is contained in:
@@ -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},
|
||||
|
||||
Reference in New Issue
Block a user