client: add support for registering push notifications

[skip cd]
This commit is contained in:
Tulir Asokan 2024-10-01 18:01:12 +03:00
parent e98d50f50b
commit e099e607fa
3 changed files with 26 additions and 13 deletions

2
go.mod
View file

@ -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

4
go.sum
View file

@ -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=

View file

@ -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 {