Skip to content

Commit

Permalink
added possibility to specify HTTP callback in qrcodelink endpoint
Browse files Browse the repository at this point in the history
* with the 'callback_url' parameter it is possible to specify a HTTP
  endpoint, which gets called after the linking was successful. In the
  payload, the number which was registered is returned. This only works
  in the json-rpc mode.
  • Loading branch information
bbernhard committed Jan 30, 2025
1 parent a02b1ce commit 2d58f22
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,11 @@ func (a *Api) DeleteGroup(c *gin.Context) {
func (a *Api) GetQrCodeLink(c *gin.Context) {
deviceName := c.Query("device_name")
qrCodeVersion := c.Query("qrcode_version")
callbackUrl, err := url.PathUnescape(c.Query("callback_url"))
if err != nil {
c.JSON(400, Error{Msg: "Couldn't process request - invalid callback"})
return
}

if deviceName == "" {
c.JSON(400, Error{Msg: "Please provide a name for the device"})
Expand All @@ -1023,7 +1028,7 @@ func (a *Api) GetQrCodeLink(c *gin.Context) {
}
}

png, err := a.signalClient.GetQrCodeLink(deviceName, qrCodeVersionInt)
png, err := a.signalClient.GetQrCodeLink(deviceName, qrCodeVersionInt, callbackUrl)
if err != nil {
c.JSON(400, Error{Msg: err.Error()})
return
Expand Down
11 changes: 10 additions & 1 deletion src/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"path/filepath"
"strconv"
"strings"
"net/http"
"bytes"

securejoin "github.com/cyphar/filepath-securejoin"
"github.com/h2non/filetype"
Expand Down Expand Up @@ -1212,7 +1214,7 @@ func (s *SignalClient) DeleteGroup(number string, groupId string) error {
}
}

func (s *SignalClient) GetQrCodeLink(deviceName string, qrCodeVersion int) ([]byte, error) {
func (s *SignalClient) GetQrCodeLink(deviceName string, qrCodeVersion int, callbackUrl string) ([]byte, error) {
if s.signalCliMode == JsonRpc {
jsonRpc2Client, err := s.getJsonRpc2Client()
if err != nil {
Expand Down Expand Up @@ -1264,6 +1266,13 @@ func (s *SignalClient) GetQrCodeLink(deviceName string, qrCodeVersion int) ([]by
}
log.Debug("Linking device result: ", result)
s.signalCliApiConfig.Load(s.signalCliApiConfigPath)

if callbackUrl != "" {
_, err = http.Post(callbackUrl, "application/json", bytes.NewBuffer([]byte(result)))
if err != nil {
log.Error(err)
}
}
})()

return png, nil
Expand Down

0 comments on commit 2d58f22

Please sign in to comment.