Skip to content

Commit

Permalink
client: Implement Remote Push
Browse files Browse the repository at this point in the history
  • Loading branch information
purpshell committed Oct 10, 2024
1 parent 2563d54 commit 2a9f98e
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 4 deletions.
34 changes: 34 additions & 0 deletions pkg/connector/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ var (
_ bridgev2.NetworkAPI = (*TwitterClient)(nil)
_ bridgev2.ReactionHandlingNetworkAPI = (*TwitterClient)(nil)
_ bridgev2.ReadReceiptHandlingNetworkAPI = (*TwitterClient)(nil)
_ bridgev2.PushableNetworkAPI = (*TwitterClient)(nil)
)

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

var pushCfg = &bridgev2.PushConfig{
Web: &bridgev2.WebPushConfig{VapidKey: "BF5oEo0xDUpgylKDTlsd8pZmxQA1leYINiY-rSscWYK_3tWAkz4VMbtf1MLE_Yyd6iII6o-e3Q9TCN5vZMzVMEs"},
}

func (tc *TwitterClient) GetPushConfigs() *bridgev2.PushConfig {
return pushCfg
}

func (tc *TwitterClient) RegisterPushNotifications(ctx context.Context, pushType bridgev2.PushType, token string) error {
if tc.client == nil {
return bridgev2.ErrNotLoggedIn
}
switch pushType {
case bridgev2.PushTypeWeb:
meta := tc.userLogin.Metadata.(*UserLoginMetadata)
if meta.PushKeys == nil {
meta.GeneratePushKeys()
err := tc.userLogin.Save(ctx)
if err != nil {
return fmt.Errorf("failed to save push key: %w", err)
}
}
pc := twittermeow.WebPushConfig{
Endpoint: token,
Auth: meta.PushKeys.Auth,
P256DH: meta.PushKeys.P256DH,
}
return tc.client.SetPushNotificationConfig(twittermeow.REGISTER, pc)
default:
return fmt.Errorf("unsupported push type: %v", pushType)
}
}

func (tc *TwitterClient) Connect(ctx context.Context) error {
if tc.client == nil {
tc.userLogin.BridgeState.Send(status.BridgeState{
Expand Down
4 changes: 0 additions & 4 deletions pkg/connector/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,6 @@ func (tc *TwitterConnector) GetCapabilities() *bridgev2.NetworkGeneralCapabiliti
return &bridgev2.NetworkGeneralCapabilities{}
}

type UserLoginMetadata struct {
Cookies string
}

func (tc *TwitterConnector) LoadUserLogin(ctx context.Context, login *bridgev2.UserLogin) error {
twitClient := NewTwitterClient(ctx, tc, login)

Expand Down
29 changes: 29 additions & 0 deletions pkg/connector/dbmeta.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package connector

import (
"crypto/ecdh"
"crypto/rand"

"go.mau.fi/util/exerrors"
"go.mau.fi/util/random"
)

type UserLoginMetadata struct {
Cookies string
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(),
}
}

0 comments on commit 2a9f98e

Please sign in to comment.