Skip to content

Commit

Permalink
push: add proper support for web push
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Oct 2, 2024
1 parent 57f27cc commit 58d3367
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
22 changes: 14 additions & 8 deletions pkg/connector/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package connector

import (
"context"
"encoding/json"
"fmt"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -104,8 +103,8 @@ var (
)

var pushCfg = &bridgev2.PushConfig{
// TODO web push config might have to be fetched from server
//Web: &bridgev2.WebPushConfig{},
// TODO fetch this from server instead of hardcoding?
Web: &bridgev2.WebPushConfig{VapidKey: "BIt4eFAVqVxe4yOA5_VLbZTbOlV-2y1FYJ_R4RlxWoyYazAq4glIxI7fh_xLbob1SNv7ZtTWn9mmZCsk2YNXYeY"},
FCM: &bridgev2.FCMPushConfig{SenderID: "293955441834"},
}

Expand All @@ -122,12 +121,19 @@ func (wa *WhatsAppClient) RegisterPushNotifications(ctx context.Context, pushTyp
case bridgev2.PushTypeFCM:
pc = &whatsmeow.FCMPushConfig{Token: token}
case bridgev2.PushTypeWeb:
var webPC whatsmeow.WebPushConfig
err := json.Unmarshal([]byte(token), &webPC)
if err != nil {
return err
meta := wa.UserLogin.Metadata.(*waid.UserLoginMetadata)
if meta.PushKeys == nil {
meta.GeneratePushKeys()
err := wa.UserLogin.Save(ctx)
if err != nil {
return fmt.Errorf("failed to save push key: %w", err)
}
}
pc = &whatsmeow.WebPushConfig{
Endpoint: token,
Auth: meta.PushKeys.Auth,
P256DH: meta.PushKeys.P256DH,
}
pc = &webPC
default:
return fmt.Errorf("unsupported push type %s", pushType)
}
Expand Down
20 changes: 20 additions & 0 deletions pkg/waid/dbmeta.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@
package waid

import (
"crypto/ecdh"
"crypto/rand"
"encoding/json"

"go.mau.fi/util/exerrors"
"go.mau.fi/util/jsontime"
"go.mau.fi/util/random"
"go.mau.fi/whatsmeow/types"
)

Expand All @@ -28,6 +32,22 @@ type UserLoginMetadata struct {
PhoneLastSeen jsontime.Unix `json:"phone_last_seen"`
PhoneLastPinged jsontime.Unix `json:"phone_last_pinged"`
Timezone string `json:"timezone"`
PushKeys *PushKeys `json:"push_keys,omitempty"`
}

type PushKeys struct {
P256DH []byte `json:"p256dh"`
Auth []byte `json:"auth"`
Private []byte `json:"private"`
}

func (m *UserLoginMetadata) GeneratePushKeys() {
privateKey := exerrors.Must(ecdh.P256().GenerateKey(rand.Reader))
m.PushKeys = &PushKeys{
P256DH: privateKey.Public().(*ecdh.PublicKey).Bytes(),
Auth: random.Bytes(16),
Private: privateKey.Bytes(),
}
}

type MessageErrorType string
Expand Down

0 comments on commit 58d3367

Please sign in to comment.