Skip to content

Commit 0650451

Browse files
authored
Merge pull request #772 from Kamaradas/master
New/changes endpoints for linking devices
2 parents 5714161 + 2e50769 commit 0650451

File tree

3 files changed

+205
-19
lines changed

3 files changed

+205
-19
lines changed

src/api/api.go

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,14 @@ type RemoteDeleteRequest struct {
220220
Timestamp int64 `json:"timestamp"`
221221
}
222222

223+
type DeleteLocalAccountDataRequest struct {
224+
IgnoreRegistered bool `json:"ignore_registered" example:"false"`
225+
}
226+
227+
type DeviceLinkUriResponse struct {
228+
DeviceLinkUri string `json:"device_link_uri"`
229+
}
230+
223231
type Api struct {
224232
signalClient *client.SignalClient
225233
wsMutex sync.Mutex
@@ -328,6 +336,43 @@ func (a *Api) UnregisterNumber(c *gin.Context) {
328336
c.Writer.WriteHeader(204)
329337
}
330338

339+
// @Summary Delete local account data
340+
// @Tags Devices
341+
// @Description Delete all local data for the specified account. Only use this after unregistering the account or after removing a linked device.
342+
// @Accept json
343+
// @Produce json
344+
// @Param number path string true "Registered Phone Number"
345+
// @Param data body DeleteLocalAccountDataRequest false "Cleanup options"
346+
// @Success 204
347+
// @Failure 400 {object} Error
348+
// @Router /v1/devices/{number}/local-data [delete]
349+
func (a *Api) DeleteLocalAccountData(c *gin.Context) {
350+
number, err := url.PathUnescape(c.Param("number"))
351+
if err != nil {
352+
c.JSON(400, Error{Msg: "Couldn't process request - malformed number"})
353+
return
354+
}
355+
if number == "" {
356+
c.JSON(400, Error{Msg: "Couldn't process request - number missing"})
357+
return
358+
}
359+
360+
req := DeleteLocalAccountDataRequest{}
361+
if c.Request.Body != nil && c.Request.ContentLength != 0 {
362+
if err := c.BindJSON(&req); err != nil {
363+
c.JSON(400, Error{Msg: "Couldn't process request - invalid request"})
364+
return
365+
}
366+
}
367+
368+
if err := a.signalClient.DeleteLocalAccountData(number, req.IgnoreRegistered); err != nil {
369+
c.JSON(400, Error{Msg: err.Error()})
370+
return
371+
}
372+
373+
c.Status(http.StatusNoContent)
374+
}
375+
331376
// @Summary Verify a registered phone number.
332377
// @Tags Devices
333378
// @Description Verify a registered phone number with the signal network.
@@ -1103,6 +1148,30 @@ func (a *Api) GetQrCodeLink(c *gin.Context) {
11031148
c.Data(200, "image/png", png)
11041149
}
11051150

1151+
// @Summary Get raw device link URI
1152+
// @Tags Devices
1153+
// @Description Generate the deviceLinkUri string for linking without scanning a QR code.
1154+
// @Produce json
1155+
// @Param device_name query string true "Device Name"
1156+
// @Success 200 {object} DeviceLinkUriResponse
1157+
// @Failure 400 {object} Error
1158+
// @Router /v1/qrcodelink/raw [get]
1159+
func (a *Api) GetQrCodeLinkUri(c *gin.Context) {
1160+
deviceName := c.Query("device_name")
1161+
if deviceName == "" {
1162+
c.JSON(400, Error{Msg: "Please provide a name for the device"})
1163+
return
1164+
}
1165+
1166+
deviceLinkUri, err := a.signalClient.GetDeviceLinkUri(deviceName)
1167+
if err != nil {
1168+
c.JSON(400, Error{Msg: err.Error()})
1169+
return
1170+
}
1171+
1172+
c.JSON(200, DeviceLinkUriResponse{DeviceLinkUri: deviceLinkUri})
1173+
}
1174+
11061175
// @Summary List all accounts
11071176
// @Tags Accounts
11081177
// @Description Lists all of the accounts linked or registered
@@ -1974,6 +2043,40 @@ func (a *Api) ListDevices(c *gin.Context) {
19742043
c.JSON(200, devices)
19752044
}
19762045

2046+
// @Summary Remove linked device
2047+
// @Tags Devices
2048+
// @Description Remove a linked device from the primary account.
2049+
// @Param number path string true "Registered Phone Number"
2050+
// @Param deviceId path int true "Device ID from listDevices"
2051+
// @Success 204
2052+
// @Failure 400 {object} Error
2053+
// @Router /v1/devices/{number}/{deviceId} [delete]
2054+
func (a *Api) RemoveDevice(c *gin.Context) {
2055+
number, err := url.PathUnescape(c.Param("number"))
2056+
if err != nil {
2057+
c.JSON(400, Error{Msg: "Couldn't process request - malformed number"})
2058+
return
2059+
}
2060+
if number == "" {
2061+
c.JSON(400, Error{Msg: "Couldn't process request - number missing"})
2062+
return
2063+
}
2064+
2065+
deviceIdStr := c.Param("deviceId")
2066+
deviceId, err := strconv.ParseInt(deviceIdStr, 10, 64)
2067+
if err != nil {
2068+
c.JSON(400, Error{Msg: "deviceId must be numeric"})
2069+
return
2070+
}
2071+
2072+
err = a.signalClient.RemoveDevice(number, deviceId)
2073+
if err != nil {
2074+
c.JSON(400, Error{Msg: err.Error()})
2075+
return
2076+
}
2077+
c.Status(http.StatusNoContent)
2078+
}
2079+
19772080
// @Summary Set account specific settings.
19782081
// @Tags General
19792082
// @Description Set account specific settings.

src/client/client.go

Lines changed: 99 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ type ListContactsResponse struct {
232232
}
233233

234234
type ListDevicesResponse struct {
235+
Id int64 `json:"id"`
235236
Name string `json:"name"`
236237
LastSeenTimestamp int64 `json:"last_seen_timestamp"`
237238
CreationTimestamp int64 `json:"creation_timestamp"`
@@ -783,6 +784,32 @@ func (s *SignalClient) UnregisterNumber(number string, deleteAccount bool, delet
783784
return err
784785
}
785786

787+
func (s *SignalClient) DeleteLocalAccountData(number string, ignoreRegistered bool) error {
788+
if s.signalCliMode == JsonRpc {
789+
type Request struct {
790+
IgnoreRegistered bool `json:"ignore-registered,omitempty"`
791+
}
792+
req := Request{}
793+
if ignoreRegistered {
794+
req.IgnoreRegistered = true
795+
}
796+
797+
jsonRpc2Client, err := s.getJsonRpc2Client()
798+
if err != nil {
799+
return err
800+
}
801+
_, err = jsonRpc2Client.getRaw("deleteLocalAccountData", &number, req)
802+
return err
803+
} else {
804+
cmd := []string{"--config", s.signalCliConfig, "-a", number, "deleteLocalAccountData"}
805+
if ignoreRegistered {
806+
cmd = append(cmd, "--ignore-registered")
807+
}
808+
_, err := s.cliClient.Execute(true, cmd, "")
809+
return err
810+
}
811+
}
812+
786813
func (s *SignalClient) VerifyRegisteredNumber(number string, token string, pin string) error {
787814
if s.signalCliMode == JsonRpc {
788815
type Request struct {
@@ -1463,25 +1490,7 @@ func (s *SignalClient) GetQrCodeLink(deviceName string, qrCodeVersion int) ([]by
14631490
return []byte{}, errors.New("Couldn't create QR code: " + err.Error())
14641491
}
14651492

1466-
go (func() {
1467-
type FinishRequest struct {
1468-
DeviceLinkUri string `json:"deviceLinkUri"`
1469-
DeviceName string `json:"deviceName"`
1470-
}
1471-
1472-
req := FinishRequest{
1473-
DeviceLinkUri: resp.DeviceLinkUri,
1474-
DeviceName: deviceName,
1475-
}
1476-
1477-
result, err := jsonRpc2Client.getRaw("finishLink", nil, &req)
1478-
if err != nil {
1479-
log.Debug("Error linking device: ", err.Error())
1480-
return
1481-
}
1482-
log.Debug("Linking device result: ", result)
1483-
s.signalCliApiConfig.Load(s.signalCliApiConfigPath)
1484-
})()
1493+
s.finishLinkAsync(jsonRpc2Client, deviceName, resp.DeviceLinkUri)
14851494

14861495
return png, nil
14871496
}
@@ -1506,6 +1515,57 @@ func (s *SignalClient) GetQrCodeLink(deviceName string, qrCodeVersion int) ([]by
15061515
return png, nil
15071516
}
15081517

1518+
func (s *SignalClient) GetDeviceLinkUri(deviceName string) (string, error) {
1519+
if s.signalCliMode == JsonRpc {
1520+
type StartResponse struct {
1521+
DeviceLinkUri string `json:"deviceLinkUri"`
1522+
}
1523+
jsonRpc2Client, err := s.getJsonRpc2Client()
1524+
if err != nil {
1525+
return "", err
1526+
}
1527+
1528+
raw, err := jsonRpc2Client.getRaw("startLink", nil, struct{}{})
1529+
if err != nil {
1530+
return "", errors.New("Couldn't start link: " + err.Error())
1531+
}
1532+
1533+
var resp StartResponse
1534+
if err := json.Unmarshal([]byte(raw), &resp); err != nil {
1535+
return "", errors.New("Couldn't parse startLink response: " + err.Error())
1536+
}
1537+
1538+
// Complete the linking handshake in the background, just like GetQrCodeLink does.
1539+
s.finishLinkAsync(jsonRpc2Client, deviceName, resp.DeviceLinkUri)
1540+
return resp.DeviceLinkUri, nil
1541+
}
1542+
1543+
cmd := []string{"--config", s.signalCliConfig, "link", "-n", deviceName}
1544+
deviceLinkUri, err := s.cliClient.Execute(false, cmd, "")
1545+
if err != nil {
1546+
return "", errors.New("Couldn't create link URI: " + err.Error())
1547+
}
1548+
return strings.TrimSpace(deviceLinkUri), nil
1549+
}
1550+
1551+
func (s *SignalClient) finishLinkAsync(jsonRpc2Client *JsonRpc2Client, deviceName string, deviceLinkUri string) {
1552+
type finishRequest struct {
1553+
DeviceLinkUri string `json:"deviceLinkUri"`
1554+
DeviceName string `json:"deviceName"`
1555+
}
1556+
1557+
go func() {
1558+
req := finishRequest{DeviceLinkUri: deviceLinkUri, DeviceName: deviceName}
1559+
result, err := jsonRpc2Client.getRaw("finishLink", nil, &req)
1560+
if err != nil {
1561+
log.Debug("Error linking device: ", err.Error())
1562+
return
1563+
}
1564+
log.Debug("Linking device result: ", result)
1565+
s.signalCliApiConfig.Load(s.signalCliApiConfigPath)
1566+
}()
1567+
}
1568+
15091569
func (s *SignalClient) GetAccounts() ([]string, error) {
15101570
accounts := make([]string, 0)
15111571
var rawData string
@@ -2285,6 +2345,7 @@ func (s *SignalClient) ListDevices(number string) ([]ListDevicesResponse, error)
22852345

22862346
for _, entry := range signalCliResp {
22872347
deviceEntry := ListDevicesResponse{
2348+
Id: entry.Id,
22882349
Name: entry.Name,
22892350
CreationTimestamp: entry.CreatedTimestamp,
22902351
LastSeenTimestamp: entry.LastSeenTimestamp,
@@ -2295,6 +2356,25 @@ func (s *SignalClient) ListDevices(number string) ([]ListDevicesResponse, error)
22952356
return resp, nil
22962357
}
22972358

2359+
func (s *SignalClient) RemoveDevice(number string, deviceId int64) error {
2360+
var err error
2361+
if s.signalCliMode == JsonRpc {
2362+
type Request struct {
2363+
DeviceId int64 `json:"deviceId"`
2364+
}
2365+
request := Request{DeviceId: deviceId}
2366+
jsonRpc2Client, err := s.getJsonRpc2Client()
2367+
if err != nil {
2368+
return err
2369+
}
2370+
_, err = jsonRpc2Client.getRaw("removeDevice", &number, request)
2371+
} else {
2372+
cmd := []string{"--config", s.signalCliConfig, "-a", number, "removeDevice", "--deviceId", strconv.FormatInt(deviceId, 10)}
2373+
_, err = s.cliClient.Execute(true, cmd, "")
2374+
}
2375+
return err
2376+
}
2377+
22982378
func (s *SignalClient) SetTrustMode(number string, trustMode utils.SignalCliTrustMode) error {
22992379
if s.signalCliMode == JsonRpc {
23002380
return errors.New("Not supported in json-rpc mode, use the environment variable JSON_RPC_TRUST_NEW_IDENTITIES instead!")

src/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ func main() {
230230
link := v1.Group("qrcodelink")
231231
{
232232
link.GET("", api.GetQrCodeLink)
233+
link.GET("/raw", api.GetQrCodeLinkUri)
233234
}
234235

235236
accounts := v1.Group("accounts")
@@ -247,6 +248,8 @@ func main() {
247248
{
248249
devices.POST(":number", api.AddDevice)
249250
devices.GET(":number", api.ListDevices)
251+
devices.DELETE(":number/:deviceId", api.RemoveDevice)
252+
devices.DELETE(":number/local-data", api.DeleteLocalAccountData)
250253
}
251254

252255
attachments := v1.Group("attachments")

0 commit comments

Comments
 (0)