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()})
|
||||
}
|
||||
Reference in New Issue
Block a user