From b1bd303b0e8d75a88b69adf02348dfdcac8e336c Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Tue, 3 Sep 2024 20:12:28 +0300 Subject: [PATCH] client: register capabilities on connect --- pkg/connector/client.go | 6 ++++++ pkg/signalmeow/provisioning.go | 29 ++++++++++++++++++++++++++--- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/pkg/connector/client.go b/pkg/connector/client.go index 56ab59ac..ebd2defc 100644 --- a/pkg/connector/client.go +++ b/pkg/connector/client.go @@ -240,6 +240,12 @@ func (s *SignalClient) Disconnect() { } func (s *SignalClient) tryConnect(ctx context.Context, retryCount int) { + err := s.Client.RegisterCapabilities(ctx) + if err != nil { + zerolog.Ctx(ctx).Err(err).Msg("Failed to register capabilities") + } else { + zerolog.Ctx(ctx).Debug().Msg("Successfully registered capabilities") + } ch, err := s.Client.StartReceiveLoops(ctx) if err != nil { zerolog.Ctx(ctx).Err(err).Msg("Failed to start receive loops") diff --git a/pkg/signalmeow/provisioning.go b/pkg/signalmeow/provisioning.go index f3abb2ca..160bdec5 100644 --- a/pkg/signalmeow/provisioning.go +++ b/pkg/signalmeow/provisioning.go @@ -330,6 +330,31 @@ func continueProvisioning(ctx context.Context, ws *websocket.Conn, provisioningC return provisioningMessage, err } +var signalCapabilities = map[string]any{ + "deleteSync": true, +} + +var signalCapabilitiesBody = exerrors.Must(json.Marshal(signalCapabilities)) + +func (cli *Client) RegisterCapabilities(ctx context.Context) error { + username, password := cli.Store.BasicAuthCreds() + resp, err := web.SendHTTPRequest(ctx, http.MethodPut, "/v1/devices/capabilities", &web.HTTPReqOpt{ + Body: signalCapabilitiesBody, + Username: &username, + Password: &password, + ContentType: web.ContentTypeJSON, + }) + if resp != nil { + _ = resp.Body.Close() + } + if err != nil { + return err + } else if resp.StatusCode >= 400 { + return fmt.Errorf("unexpected status code %d", resp.StatusCode) + } + return nil +} + func confirmDevice( ctx context.Context, username string, @@ -383,9 +408,7 @@ func confirmDevice( "name": encryptedDeviceName, "registrationId": aciRegistrationID, "pniRegistrationId": pniRegistrationID, - "capabilities": map[string]any{ - "deleteSync": true, - }, + "capabilities": signalCapabilities, }, "aciSignedPreKey": aciSignedPreKeyJson, "pniSignedPreKey": pniSignedPreKeyJson,