From e099e607fae070b2edb3788796c5977132dfd090 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Tue, 1 Oct 2024 18:01:12 +0300 Subject: [PATCH] client: add support for registering push notifications [skip cd] --- go.mod | 2 +- go.sum | 4 ++-- pkg/connector/client.go | 33 +++++++++++++++++++++++---------- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/go.mod b/go.mod index 9ff9ba69..6799240a 100644 --- a/go.mod +++ b/go.mod @@ -11,7 +11,7 @@ require ( github.com/rs/zerolog v1.33.0 go.mau.fi/util v0.8.1-0.20240927174413-000d30f9a02a go.mau.fi/webp v0.1.0 - go.mau.fi/whatsmeow v0.0.0-20241001110941-382edde94d9f + go.mau.fi/whatsmeow v0.0.0-20241001150013-71e7937b706a golang.org/x/exp v0.0.0-20240909161429-701f63a606c0 golang.org/x/image v0.20.0 golang.org/x/net v0.29.0 diff --git a/go.sum b/go.sum index 5620508d..5c7de717 100644 --- a/go.sum +++ b/go.sum @@ -69,8 +69,8 @@ go.mau.fi/util v0.8.1-0.20240927174413-000d30f9a02a h1:4TrWJ0ooHT9YssDBUgXNU8FiR go.mau.fi/util v0.8.1-0.20240927174413-000d30f9a02a/go.mod h1:1Ixb8HWoVbl3rT6nAX6nV4iMkzn7KU/KXwE0Rn5RmsQ= go.mau.fi/webp v0.1.0 h1:BHObH/DcFntT9KYun5pDr0Ot4eUZO8k2C7eP7vF4ueA= go.mau.fi/webp v0.1.0/go.mod h1:e42Z+VMFrUMS9cpEwGRIor+lQWO8oUAyPyMtcL+NMt8= -go.mau.fi/whatsmeow v0.0.0-20241001110941-382edde94d9f h1:L3aOZEtq5XJcJO5+/mxO3MW7e2ofXNFkVT6McpcpF5k= -go.mau.fi/whatsmeow v0.0.0-20241001110941-382edde94d9f/go.mod h1:UvaXcdb8y5Mryj2LSXAMw7u4/exnWJIXn8Gvpmf6ndI= +go.mau.fi/whatsmeow v0.0.0-20241001150013-71e7937b706a h1:6f0HGXBZiGrQMrI8MF5u0zjrdsBXLzWAneikUOhbN3E= +go.mau.fi/whatsmeow v0.0.0-20241001150013-71e7937b706a/go.mod h1:UvaXcdb8y5Mryj2LSXAMw7u4/exnWJIXn8Gvpmf6ndI= go.mau.fi/zeroconfig v0.1.3 h1:As9wYDKmktjmNZW5i1vn8zvJlmGKHeVxHVIBMXsm4kM= go.mau.fi/zeroconfig v0.1.3/go.mod h1:NcSJkf180JT+1IId76PcMuLTNa1CzsFFZ0nBygIQM70= golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A= diff --git a/pkg/connector/client.go b/pkg/connector/client.go index b3947167..6ce4fd76 100644 --- a/pkg/connector/client.go +++ b/pkg/connector/client.go @@ -18,6 +18,7 @@ package connector import ( "context" + "encoding/json" "fmt" "sync" "sync/atomic" @@ -97,28 +98,40 @@ type WhatsAppClient struct { lastPhoneOfflineWarning time.Time } -var _ bridgev2.NetworkAPI = (*WhatsAppClient)(nil) +var ( + _ bridgev2.NetworkAPI = (*WhatsAppClient)(nil) + _ bridgev2.PushableNetworkAPI = (*WhatsAppClient)(nil) +) var pushCfg = &bridgev2.PushConfig{ - Web: &bridgev2.WebPushConfig{}, + // TODO web push config might have to be fetched from server + //Web: &bridgev2.WebPushConfig{}, + FCM: &bridgev2.FCMPushConfig{SenderID: "293955441834"}, } func (wa *WhatsAppClient) GetPushConfigs() *bridgev2.PushConfig { - // implement get application server key (to be added to whatsmeow) - //pushCfg.Web.VapidKey = applicationServerKey return pushCfg } -func (wa *WhatsAppClient) RegisterPushNotifications(_ context.Context, pushType bridgev2.PushType, _ string) error { +func (wa *WhatsAppClient) RegisterPushNotifications(ctx context.Context, pushType bridgev2.PushType, token string) error { if wa.Client == nil { return bridgev2.ErrNotLoggedIn } - if pushType != bridgev2.PushTypeWeb { - return fmt.Errorf("unsupported push type: %s", pushType) + var pc whatsmeow.PushConfig + switch pushType { + 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 + } + pc = &webPC + default: + return fmt.Errorf("unsupported push type %s", pushType) } - - //wa.Client.RegisterWebPush(ctx, token) (to be added to whatsmeow) - return nil + return wa.Client.RegisterForPushNotifications(ctx, pc) } func (wa *WhatsAppClient) IsThisUser(_ context.Context, userID networkid.UserID) bool {