added possibility to specify HTTP callback in qrcodelink endpoint

* 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.
This commit is contained in:
Bernhard B 2025-01-30 21:31:04 +01:00
parent a02b1ce8eb
commit 2d58f22c95
2 changed files with 16 additions and 2 deletions

View File

@ -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"})
@ -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

View File

@ -11,6 +11,8 @@ import (
"path/filepath"
"strconv"
"strings"
"net/http"
"bytes"
securejoin "github.com/cyphar/filepath-securejoin"
"github.com/h2non/filetype"
@ -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 {
@ -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