Skip to content

Commit 2a9f98e

Browse files
committed
client: Implement Remote Push
1 parent 2563d54 commit 2a9f98e

File tree

3 files changed

+63
-4
lines changed

3 files changed

+63
-4
lines changed

pkg/connector/client.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ var (
4848
_ bridgev2.NetworkAPI = (*TwitterClient)(nil)
4949
_ bridgev2.ReactionHandlingNetworkAPI = (*TwitterClient)(nil)
5050
_ bridgev2.ReadReceiptHandlingNetworkAPI = (*TwitterClient)(nil)
51+
_ bridgev2.PushableNetworkAPI = (*TwitterClient)(nil)
5152
)
5253

5354
func NewTwitterClient(ctx context.Context, tc *TwitterConnector, login *bridgev2.UserLogin) *TwitterClient {
@@ -72,6 +73,39 @@ func NewTwitterClient(ctx context.Context, tc *TwitterConnector, login *bridgev2
7273
return twitClient
7374
}
7475

76+
var pushCfg = &bridgev2.PushConfig{
77+
Web: &bridgev2.WebPushConfig{VapidKey: "BF5oEo0xDUpgylKDTlsd8pZmxQA1leYINiY-rSscWYK_3tWAkz4VMbtf1MLE_Yyd6iII6o-e3Q9TCN5vZMzVMEs"},
78+
}
79+
80+
func (tc *TwitterClient) GetPushConfigs() *bridgev2.PushConfig {
81+
return pushCfg
82+
}
83+
84+
func (tc *TwitterClient) RegisterPushNotifications(ctx context.Context, pushType bridgev2.PushType, token string) error {
85+
if tc.client == nil {
86+
return bridgev2.ErrNotLoggedIn
87+
}
88+
switch pushType {
89+
case bridgev2.PushTypeWeb:
90+
meta := tc.userLogin.Metadata.(*UserLoginMetadata)
91+
if meta.PushKeys == nil {
92+
meta.GeneratePushKeys()
93+
err := tc.userLogin.Save(ctx)
94+
if err != nil {
95+
return fmt.Errorf("failed to save push key: %w", err)
96+
}
97+
}
98+
pc := twittermeow.WebPushConfig{
99+
Endpoint: token,
100+
Auth: meta.PushKeys.Auth,
101+
P256DH: meta.PushKeys.P256DH,
102+
}
103+
return tc.client.SetPushNotificationConfig(twittermeow.REGISTER, pc)
104+
default:
105+
return fmt.Errorf("unsupported push type: %v", pushType)
106+
}
107+
}
108+
75109
func (tc *TwitterClient) Connect(ctx context.Context) error {
76110
if tc.client == nil {
77111
tc.userLogin.BridgeState.Send(status.BridgeState{

pkg/connector/connector.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,6 @@ func (tc *TwitterConnector) GetCapabilities() *bridgev2.NetworkGeneralCapabiliti
7171
return &bridgev2.NetworkGeneralCapabilities{}
7272
}
7373

74-
type UserLoginMetadata struct {
75-
Cookies string
76-
}
77-
7874
func (tc *TwitterConnector) LoadUserLogin(ctx context.Context, login *bridgev2.UserLogin) error {
7975
twitClient := NewTwitterClient(ctx, tc, login)
8076

pkg/connector/dbmeta.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package connector
2+
3+
import (
4+
"crypto/ecdh"
5+
"crypto/rand"
6+
7+
"go.mau.fi/util/exerrors"
8+
"go.mau.fi/util/random"
9+
)
10+
11+
type UserLoginMetadata struct {
12+
Cookies string
13+
PushKeys *PushKeys `json:"push_keys,omitempty"`
14+
}
15+
16+
type PushKeys struct {
17+
P256DH []byte `json:"p256dh"`
18+
Auth []byte `json:"auth"`
19+
Private []byte `json:"private"`
20+
}
21+
22+
func (m *UserLoginMetadata) GeneratePushKeys() {
23+
privateKey := exerrors.Must(ecdh.P256().GenerateKey(rand.Reader))
24+
m.PushKeys = &PushKeys{
25+
P256DH: privateKey.Public().(*ecdh.PublicKey).Bytes(),
26+
Auth: random.Bytes(16),
27+
Private: privateKey.Bytes(),
28+
}
29+
}

0 commit comments

Comments
 (0)