[feature] Push notifications (#3587)

* Update push subscription API model to be Mastodon 4.0 compatible

* Add webpush-go dependency

# Conflicts:
#	go.sum

* Single-row table for storing instance's VAPID key pair

* Generate VAPID key pair during startup

* Add VAPID public key to instance info API

* Return VAPID public key when registering an app

* Store Web Push subscriptions in DB

* Add Web Push sender (similar to email sender)

* Add no-op push senders to most processor tests

* Test Web Push notifications from workers

* Delete Web Push subscriptions when account is deleted

* Implement push subscription API

* Linter fixes

* Update Swagger

* Fix enum to int migration

* Fix GetVAPIDKeyPair

* Create web push subscriptions table with indexes

* Log Web Push server error messages

* Send instance URL as Web Push JWT subject

* Accept any 2xx code as a success

* Fix malformed VAPID sub claim

* Use packed notification flags

* Remove unused date columns

* Add notification type for update notifications

Not used yet

* Make GetVAPIDKeyPair idempotent

and remove PutVAPIDKeyPair

* Post-rebase fixes

* go mod tidy

* Special-case 400 errors other than 408/429

Most client errors should remove the subscription.

* Improve titles, trim body to reasonable length

* Disallow cleartext HTTP for Web Push servers

* Fix lint

* Remove redundant index on unique column

Also removes redundant unique and notnull tags on ID column since these are implied by pk

* Make realsender.go more readable

* Use Tobi's style for wrapping errors

* Restore treating all 5xx codes as temporary problems

* Always load target account settings

* Stub `policy` and `standard`

* webpush.Sender: take type converter as ctor param

* Move webpush.MockSender and noopSender into testrig
This commit is contained in:
Vyr Cossont 2025-01-23 16:47:30 -08:00 committed by GitHub
commit 5b765d734e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
134 changed files with 21525 additions and 125 deletions

View file

@ -61,6 +61,8 @@ var testModels = []interface{}{
&gtsmodel.ThreadToStatus{},
&gtsmodel.User{},
&gtsmodel.UserMute{},
&gtsmodel.VAPIDKeyPair{},
&gtsmodel.WebPushSubscription{},
&gtsmodel.Emoji{},
&gtsmodel.Instance{},
&gtsmodel.Notification{},
@ -348,6 +350,12 @@ func StandardDBSetup(db db.DB, accounts map[string]*gtsmodel.Account) {
}
}
for _, v := range NewTestWebPushSubscriptions() {
if err := db.Put(ctx, v); err != nil {
log.Panic(nil, err)
}
}
for _, v := range NewTestInteractionRequests() {
if err := db.Put(ctx, v); err != nil {
log.Panic(ctx, err)
@ -368,6 +376,11 @@ func StandardDBSetup(db db.DB, accounts map[string]*gtsmodel.Account) {
log.Panic(ctx, err)
}
// Generates and stores a VAPID key pair as a side effect.
if _, err := db.GetVAPIDKeyPair(ctx); err != nil {
log.Panic(nil, err)
}
log.Debug(ctx, "testing db setup complete")
}

View file

@ -28,6 +28,7 @@ import (
"github.com/superseriousbusiness/gotosocial/internal/state"
"github.com/superseriousbusiness/gotosocial/internal/subscriptions"
"github.com/superseriousbusiness/gotosocial/internal/typeutils"
"github.com/superseriousbusiness/gotosocial/internal/webpush"
)
// NewTestProcessor returns a Processor suitable for testing purposes.
@ -37,6 +38,7 @@ func NewTestProcessor(
state *state.State,
federator *federation.Federator,
emailSender email.Sender,
webPushSender webpush.Sender,
mediaManager *media.Manager,
) *processing.Processor {
@ -53,6 +55,7 @@ func NewTestProcessor(
mediaManager,
state,
emailSender,
webPushSender,
visibility.NewFilter(state),
interaction.NewFilter(state),
)

View file

@ -2585,7 +2585,7 @@ func NewTestNotifications() map[string]*gtsmodel.Notification {
return map[string]*gtsmodel.Notification{
"local_account_1_like": {
ID: "01F8Q0ANPTWW10DAKTX7BRPBJP",
NotificationType: gtsmodel.NotificationFave,
NotificationType: gtsmodel.NotificationFavourite,
CreatedAt: TimeMustParse("2022-05-14T13:21:09+02:00"),
TargetAccountID: "01F8MH1H7YV1Z7D2C8K2730QBF",
OriginAccountID: "01F8MH17FWEB39HZJ76B6VXSKF",
@ -2594,7 +2594,7 @@ func NewTestNotifications() map[string]*gtsmodel.Notification {
},
"local_account_2_like": {
ID: "01GTS6PRPXJYZBPFFQ56PP0XR8",
NotificationType: gtsmodel.NotificationFave,
NotificationType: gtsmodel.NotificationFavourite,
CreatedAt: TimeMustParse("2022-01-13T12:45:01+02:00"),
TargetAccountID: "01F8MH17FWEB39HZJ76B6VXSKF",
OriginAccountID: "01F8MH5NBDF2MV7CTC4Q5128HF",
@ -2603,7 +2603,7 @@ func NewTestNotifications() map[string]*gtsmodel.Notification {
},
"new_signup": {
ID: "01HTM9TETMB3YQCBKZ7KD4KV02",
NotificationType: gtsmodel.NotificationSignup,
NotificationType: gtsmodel.NotificationAdminSignup,
CreatedAt: TimeMustParse("2022-06-04T13:12:00Z"),
TargetAccountID: "01F8MH17FWEB39HZJ76B6VXSKF",
OriginAccountID: "01F8MH0BBE4FHXPH513MBVFHB0",
@ -3586,6 +3586,34 @@ func NewTestUserMutes() map[string]*gtsmodel.UserMute {
return map[string]*gtsmodel.UserMute{}
}
func NewTestWebPushSubscriptions() map[string]*gtsmodel.WebPushSubscription {
return map[string]*gtsmodel.WebPushSubscription{
"local_account_1_token_1": {
ID: "01G65Z755AFWAKHE12NY0CQ9FH",
AccountID: "01F8MH1H7YV1Z7D2C8K2730QBF",
TokenID: "01F8MGTQW4DKTDF8SW5CT9HYGA",
Endpoint: "https://example.test/push",
Auth: "cgna/fzrYLDQyPf5hD7IsA==",
P256dh: "BMYVItYVOX+AHBdtA62Q0i6c+F7MV2Gia3aoDr8mvHkuPBNIOuTLDfmFcnBqoZcQk6BtLcIONbxhHpy2R+mYIUY=",
NotificationFlags: gtsmodel.WebPushSubscriptionNotificationFlagsFromSlice([]gtsmodel.NotificationType{
gtsmodel.NotificationFollow,
gtsmodel.NotificationFollowRequest,
gtsmodel.NotificationFavourite,
gtsmodel.NotificationMention,
gtsmodel.NotificationReblog,
gtsmodel.NotificationPoll,
gtsmodel.NotificationStatus,
gtsmodel.NotificationUpdate,
gtsmodel.NotificationAdminSignup,
gtsmodel.NotificationAdminReport,
gtsmodel.NotificationPendingFave,
gtsmodel.NotificationPendingReply,
gtsmodel.NotificationPendingReblog,
}),
},
}
}
func NewTestInteractionRequests() map[string]*gtsmodel.InteractionRequest {
return map[string]*gtsmodel.InteractionRequest{
"admin_account_reply_turtle": {

View file

@ -47,6 +47,7 @@ type TestStructs struct {
HTTPClient *MockHTTPClient
TypeConverter *typeutils.Converter
EmailSender email.Sender
WebPushSender *WebPushMockSender
TransportController transport.Controller
}
@ -83,6 +84,7 @@ func SetupTestStructs(
federator := NewTestFederator(&state, transportController, mediaManager)
oauthServer := NewTestOauthServer(db)
emailSender := NewEmailSender(rTemplatePath, nil)
webPushSender := NewWebPushMockSender()
common := common.New(
&state,
@ -101,6 +103,7 @@ func SetupTestStructs(
mediaManager,
&state,
emailSender,
webPushSender,
visFilter,
intFilter,
)
@ -117,6 +120,7 @@ func SetupTestStructs(
HTTPClient: httpClient,
TypeConverter: typeconverter,
EmailSender: emailSender,
WebPushSender: webPushSender,
TransportController: transportController,
}
}

View file

@ -84,6 +84,7 @@ func StartWorkers(state *state.State, processor *workers.Processor) {
state.Workers.Federator.Start(1)
state.Workers.Dereference.Start(1)
state.Workers.Processing.Start(1)
state.Workers.WebPush.Start(1)
}
func StopWorkers(state *state.State) {
@ -92,6 +93,7 @@ func StopWorkers(state *state.State) {
state.Workers.Federator.Stop()
state.Workers.Dereference.Stop()
state.Workers.Processing.Stop()
state.Workers.WebPush.Stop()
}
func StartTimelines(state *state.State, visFilter *visibility.Filter, converter *typeutils.Converter) {

65
testrig/webpush.go Normal file
View file

@ -0,0 +1,65 @@
// GoToSocial
// Copyright (C) GoToSocial Authors admin@gotosocial.org
// SPDX-License-Identifier: AGPL-3.0-or-later
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package testrig
import (
"context"
"github.com/superseriousbusiness/gotosocial/internal/filter/usermute"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/webpush"
)
// WebPushMockSender collects a map of notifications sent to each account ID.
type WebPushMockSender struct {
Sent map[string][]*gtsmodel.Notification
}
// NewWebPushMockSender creates a mock sender that can record sent Web Push notifications for test expectations.
func NewWebPushMockSender() *WebPushMockSender {
return &WebPushMockSender{
Sent: map[string][]*gtsmodel.Notification{},
}
}
func (m *WebPushMockSender) Send(
ctx context.Context,
notification *gtsmodel.Notification,
filters []*gtsmodel.Filter,
mutes *usermute.CompiledUserMuteList,
) error {
m.Sent[notification.TargetAccountID] = append(m.Sent[notification.TargetAccountID], notification)
return nil
}
// noopSender drops anything sent to it.
type noopWebPushSender struct{}
// NewNoopWebPushSender creates a no-op sender that does nothing.
func NewNoopWebPushSender() webpush.Sender {
return &noopWebPushSender{}
}
func (n *noopWebPushSender) Send(
ctx context.Context,
notification *gtsmodel.Notification,
filters []*gtsmodel.Filter,
mutes *usermute.CompiledUserMuteList,
) error {
return nil
}