fix wrong unsub logic

This commit is contained in:
2025-05-10 18:27:29 +01:00
parent 515a8e5b21
commit 96866dcbb5
2 changed files with 23 additions and 6 deletions

25
main.go
View File

@@ -22,6 +22,7 @@ import (
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
"slices"
"strings" "strings"
"sync" "sync"
"time" "time"
@@ -50,8 +51,9 @@ type summaryTemplateData struct {
// updateSubscription is the request body for creating/updating registration // updateSubscription is the request body for creating/updating registration
type updateSubscription struct { type updateSubscription struct {
Subscription webpush.Subscription `json:"subscription"` Subscription webpush.Subscription `json:"subscription"`
Locations []string `json:"locations"` Locations []string `json:"locations"`
RemoveLocations []string `json:"removeLocations"`
} }
// registeredSubscription represents a registered webpush subscription. // registeredSubscription represents a registered webpush subscription.
@@ -487,8 +489,16 @@ func updateRegisteredSubscription(state *state, id uuid.UUID, update *updateSubs
rows.Close() rows.Close()
// not very proud of this one
// ideally the list of locations should be stored in a separate table
// but since the list is very small, and im too lazy to bring in a separate table
// this should be fine for now
locs := strings.Split(locStr, ",") locs := strings.Split(locStr, ",")
locs = append(locs, update.Locations...) locs = append(locs, update.Locations...)
locs = slices.DeleteFunc(locs, func(l string) bool {
return slices.Contains(update.RemoveLocations, l)
})
locs = slices.Compact(locs)
_, err = state.db.Exec( _, err = state.db.Exec(
"UPDATE subscriptions SET subscription_json = ?, locations = ? WHERE id = ?", "UPDATE subscriptions SET subscription_json = ?, locations = ? WHERE id = ?",
@@ -508,6 +518,11 @@ func updateRegisteredSubscription(state *state, id uuid.UUID, update *updateSubs
for _, l := range update.Locations { for _, l := range update.Locations {
state.subscriptions[l] = append(state.subscriptions[l], reg) state.subscriptions[l] = append(state.subscriptions[l], reg)
} }
for _, l := range update.RemoveLocations {
state.subscriptions[l] = slices.DeleteFunc(state.subscriptions[l], func(s *registeredSubscription) bool {
return s.ID == reg.ID
})
}
state.subscriptionsMutex.Unlock() state.subscriptionsMutex.Unlock()
return reg, nil return reg, nil
@@ -524,9 +539,11 @@ func registerSubscription(state *state, sub *updateSubscription) (*registeredSub
return nil, err return nil, err
} }
locs := slices.Compact(sub.Locations)
_, err = state.db.Exec( _, err = state.db.Exec(
"INSERT INTO subscriptions (id, locations, subscription_json) VALUES (?, ?, ?);", "INSERT INTO subscriptions (id, locations, subscription_json) VALUES (?, ?, ?);",
id, strings.Join(sub.Locations, ","), string(j), id, strings.Join(locs, ","), string(j),
) )
if err != nil { if err != nil {
return nil, err return nil, err
@@ -535,7 +552,7 @@ func registerSubscription(state *state, sub *updateSubscription) (*registeredSub
reg := registeredSubscription{ reg := registeredSubscription{
ID: id, ID: id,
Subscription: &sub.Subscription, Subscription: &sub.Subscription,
Locations: sub.Locations, Locations: locs,
} }
state.subscriptionsMutex.Lock() state.subscriptionsMutex.Lock()

View File

@@ -52,10 +52,10 @@ async function onButtonClick() {
}, },
body: JSON.stringify({ body: JSON.stringify({
subscription: pushSub, subscription: pushSub,
locations: registeredSubscription.locations, removeLocations: [loc],
}) })
}).then(jsonOrThrow) }).then(jsonOrThrow)
localStorage.setItem(KEY_SUBSCRIPTION, newReg) localStorage.setItem(KEY_SUBSCRIPTION, JSON.stringify(newReg))
} }
getSummaryButton.innerText = "Get daily updates at 7am" getSummaryButton.innerText = "Get daily updates at 7am"
} else { } else {