web-push-startup-queries

This commit is contained in:
binwiederhier 2023-06-18 14:20:22 -04:00
parent 88c6b4adae
commit dc7dd836c6
8 changed files with 22 additions and 43 deletions

View file

@ -157,6 +157,7 @@ type Config struct {
WebPushPublicKey string
WebPushFile string
WebPushEmailAddress string
WebPushStartupQueries string
WebPushExpiryDuration time.Duration
WebPushExpiryWarningDuration time.Duration
}

View file

@ -158,7 +158,7 @@ func New(conf *Config) (*Server, error) {
}
var webPush *webPushStore
if conf.WebPushPublicKey != "" {
webPush, err = newWebPushStore(conf.WebPushFile)
webPush, err = newWebPushStore(conf.WebPushFile, conf.WebPushStartupQueries)
if err != nil {
return nil, err
}

View file

@ -150,18 +150,20 @@
# can enable background notifications in the web app. Once enabled, ntfy will forward published messages to the push
# endpoint, which will then forward it to the browser.
#
# You must configure all settings below to enable Web Push.
# You must configure web-push-public/private key, web-push-file, and web-push-email-address below to enable Web Push.
# Run "ntfy webpush keys" to generate the keys.
#
# - web-push-public-key is the generated VAPID public key, e.g. AA1234BBCCddvveekaabcdfqwertyuiopasdfghjklzxcvbnm1234567890
# - web-push-private-key is the generated VAPID private key, e.g. AA2BB1234567890abcdefzxcvbnm1234567890
# - web-push-file is a database file to keep track of browser subscription endpoints, e.g. `/var/cache/ntfy/webpush.db`
# - web-push-email-address is the admin email address send to the push provider, e.g. `sysadmin@example.com`
# - web-push-startup-queries is an optional list of queries to run on startup`
#
# web-push-public-key:
# web-push-private-key:
# web-push-file:
# web-push-email-address:
# web-push-startup-queries:
# If enabled, ntfy can perform voice calls via Twilio via the "X-Call" header.
#

View file

@ -431,41 +431,6 @@ func TestAccount_Delete_Not_Allowed(t *testing.T) {
require.Equal(t, 40026, toHTTPError(t, rr.Body.String()).Code)
}
func TestAccount_Delete_Success_WithWebPush(t *testing.T) {
conf := configureAuth(t, newTestConfigWithWebPush(t))
conf.EnableSignup = true
s := newTestServer(t, conf)
// Add account
rr := request(t, s, "POST", "/v1/account", `{"username":"phil", "password":"mypass"}`, nil)
require.Equal(t, 200, rr.Code)
// Add web push subscription
rr = request(t, s, "POST", "/v1/webpush", payloadForTopics(t, []string{"mytopic"}, testWebPushEndpoint), map[string]string{
"Authorization": util.BasicAuth("phil", "mypass"),
})
require.Equal(t, 200, rr.Code)
u, err := s.userManager.User("phil")
require.Nil(t, err)
subs, err := s.webPush.SubscriptionsForTopic("mytopic")
require.Nil(t, err)
require.Len(t, subs, 1)
require.Equal(t, u.ID, subs[0].UserID)
// Delete account
rr = request(t, s, "DELETE", "/v1/account", `{"password":"mypass"}`, map[string]string{
"Authorization": util.BasicAuth("phil", "mypass"),
})
require.Equal(t, 200, rr.Code)
// Make sure web push subscription was deleted
subs, err = s.webPush.SubscriptionsForTopic("mytopic")
require.Nil(t, err)
require.Len(t, subs, 0)
}
func TestAccount_Reservation_AddWithoutTierFails(t *testing.T) {
conf := newTestConfigWithAuthFile(t)
conf.EnableSignup = true

View file

@ -94,7 +94,7 @@ type webPushStore struct {
db *sql.DB
}
func newWebPushStore(filename string) (*webPushStore, error) {
func newWebPushStore(filename, startupQueries string) (*webPushStore, error) {
db, err := sql.Open("sqlite3", filename)
if err != nil {
return nil, err
@ -102,7 +102,7 @@ func newWebPushStore(filename string) (*webPushStore, error) {
if err := setupWebPushDB(db); err != nil {
return nil, err
}
if err := runWebPushStartupQueries(db); err != nil {
if err := runWebPushStartupQueries(db, startupQueries); err != nil {
return nil, err
}
return &webPushStore{
@ -129,9 +129,14 @@ func setupNewWebPushDB(db *sql.DB) error {
return nil
}
func runWebPushStartupQueries(db *sql.DB) error {
_, err := db.Exec(builtinStartupQueries)
return err
func runWebPushStartupQueries(db *sql.DB, startupQueries string) error {
if _, err := db.Exec(startupQueries); err != nil {
return err
}
if _, err := db.Exec(builtinStartupQueries); err != nil {
return err
}
return nil
}
// UpsertSubscription adds or updates Web Push subscriptions for the given topics and user ID. It always first deletes all

View file

@ -193,7 +193,7 @@ func TestWebPushStore_RemoveExpiredSubscriptions(t *testing.T) {
}
func newTestWebPushStore(t *testing.T) *webPushStore {
webPush, err := newWebPushStore(filepath.Join(t.TempDir(), "webpush.db"))
webPush, err := newWebPushStore(filepath.Join(t.TempDir(), "webpush.db"), "")
require.Nil(t, err)
return webPush
}