Skip to content

Commit e099e60

Browse files
committed
client: add support for registering push notifications
[skip cd]
1 parent e98d50f commit e099e60

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ require (
1111
github.com/rs/zerolog v1.33.0
1212
go.mau.fi/util v0.8.1-0.20240927174413-000d30f9a02a
1313
go.mau.fi/webp v0.1.0
14-
go.mau.fi/whatsmeow v0.0.0-20241001110941-382edde94d9f
14+
go.mau.fi/whatsmeow v0.0.0-20241001150013-71e7937b706a
1515
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0
1616
golang.org/x/image v0.20.0
1717
golang.org/x/net v0.29.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ go.mau.fi/util v0.8.1-0.20240927174413-000d30f9a02a h1:4TrWJ0ooHT9YssDBUgXNU8FiR
6969
go.mau.fi/util v0.8.1-0.20240927174413-000d30f9a02a/go.mod h1:1Ixb8HWoVbl3rT6nAX6nV4iMkzn7KU/KXwE0Rn5RmsQ=
7070
go.mau.fi/webp v0.1.0 h1:BHObH/DcFntT9KYun5pDr0Ot4eUZO8k2C7eP7vF4ueA=
7171
go.mau.fi/webp v0.1.0/go.mod h1:e42Z+VMFrUMS9cpEwGRIor+lQWO8oUAyPyMtcL+NMt8=
72-
go.mau.fi/whatsmeow v0.0.0-20241001110941-382edde94d9f h1:L3aOZEtq5XJcJO5+/mxO3MW7e2ofXNFkVT6McpcpF5k=
73-
go.mau.fi/whatsmeow v0.0.0-20241001110941-382edde94d9f/go.mod h1:UvaXcdb8y5Mryj2LSXAMw7u4/exnWJIXn8Gvpmf6ndI=
72+
go.mau.fi/whatsmeow v0.0.0-20241001150013-71e7937b706a h1:6f0HGXBZiGrQMrI8MF5u0zjrdsBXLzWAneikUOhbN3E=
73+
go.mau.fi/whatsmeow v0.0.0-20241001150013-71e7937b706a/go.mod h1:UvaXcdb8y5Mryj2LSXAMw7u4/exnWJIXn8Gvpmf6ndI=
7474
go.mau.fi/zeroconfig v0.1.3 h1:As9wYDKmktjmNZW5i1vn8zvJlmGKHeVxHVIBMXsm4kM=
7575
go.mau.fi/zeroconfig v0.1.3/go.mod h1:NcSJkf180JT+1IId76PcMuLTNa1CzsFFZ0nBygIQM70=
7676
golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A=

pkg/connector/client.go

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package connector
1818

1919
import (
2020
"context"
21+
"encoding/json"
2122
"fmt"
2223
"sync"
2324
"sync/atomic"
@@ -97,28 +98,40 @@ type WhatsAppClient struct {
9798
lastPhoneOfflineWarning time.Time
9899
}
99100

100-
var _ bridgev2.NetworkAPI = (*WhatsAppClient)(nil)
101+
var (
102+
_ bridgev2.NetworkAPI = (*WhatsAppClient)(nil)
103+
_ bridgev2.PushableNetworkAPI = (*WhatsAppClient)(nil)
104+
)
101105

102106
var pushCfg = &bridgev2.PushConfig{
103-
Web: &bridgev2.WebPushConfig{},
107+
// TODO web push config might have to be fetched from server
108+
//Web: &bridgev2.WebPushConfig{},
109+
FCM: &bridgev2.FCMPushConfig{SenderID: "293955441834"},
104110
}
105111

106112
func (wa *WhatsAppClient) GetPushConfigs() *bridgev2.PushConfig {
107-
// implement get application server key (to be added to whatsmeow)
108-
//pushCfg.Web.VapidKey = applicationServerKey
109113
return pushCfg
110114
}
111115

112-
func (wa *WhatsAppClient) RegisterPushNotifications(_ context.Context, pushType bridgev2.PushType, _ string) error {
116+
func (wa *WhatsAppClient) RegisterPushNotifications(ctx context.Context, pushType bridgev2.PushType, token string) error {
113117
if wa.Client == nil {
114118
return bridgev2.ErrNotLoggedIn
115119
}
116-
if pushType != bridgev2.PushTypeWeb {
117-
return fmt.Errorf("unsupported push type: %s", pushType)
120+
var pc whatsmeow.PushConfig
121+
switch pushType {
122+
case bridgev2.PushTypeFCM:
123+
pc = &whatsmeow.FCMPushConfig{Token: token}
124+
case bridgev2.PushTypeWeb:
125+
var webPC whatsmeow.WebPushConfig
126+
err := json.Unmarshal([]byte(token), &webPC)
127+
if err != nil {
128+
return err
129+
}
130+
pc = &webPC
131+
default:
132+
return fmt.Errorf("unsupported push type %s", pushType)
118133
}
119-
120-
//wa.Client.RegisterWebPush(ctx, token) (to be added to whatsmeow)
121-
return nil
134+
return wa.Client.RegisterForPushNotifications(ctx, pc)
122135
}
123136

124137
func (wa *WhatsAppClient) IsThisUser(_ context.Context, userID networkid.UserID) bool {

0 commit comments

Comments
 (0)