From 8a0ce92936e46119532d0b572afd667b9f19be67 Mon Sep 17 00:00:00 2001 From: Bernhard B Date: Sun, 13 Mar 2022 20:10:15 +0100 Subject: [PATCH] added "update contacts" endpoint see #228 --- src/api/api.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/client/client.go | 33 +++++++++++++++++++++++++++++++++ src/main.go | 5 +++++ 3 files changed, 82 insertions(+) diff --git a/src/api/api.go b/src/api/api.go index e9d2e25..9ea9ba4 100644 --- a/src/api/api.go +++ b/src/api/api.go @@ -28,6 +28,12 @@ const ( pingPeriod = (pongWait * 9) / 10 ) +type UpdateContactRequest struct { + Recipient string `json:"recipient"` + Name *string `json:"name"` + ExpirationInSeconds *int `json:"expiration_in_seconds"` +} + type GroupPermissions struct { AddMembers string `json:"add_members" enums:"only-admins,every-member"` EditGroup string `json:"edit_group" enums:"only-admins,every-member"` @@ -1156,3 +1162,41 @@ func (a *Api) SearchForNumbers(c *gin.Context) { c.JSON(200, searchResponse) } + + +// @Summary Updates the info associated to a number on the contact list. If the contact doesn’t exist yet, it will be added. +// @Tags Contacts +// @Description Updates the info associated to a number on the contact list. +// @Accept json +// @Produce json +// @Param number path string true "Registered Phone Number" +// @Success 204 +// @Param data body UpdateContactRequest true "Contact" +// @Failure 400 {object} Error +// @Router /v1/contacts [put] +func (a *Api) UpdateContact(c *gin.Context) { + number := c.Param("number") + if number == "" { + c.JSON(400, Error{Msg: "Couldn't process request - number missing"}) + return + } + + var req UpdateContactRequest + err := c.BindJSON(&req) + if err != nil { + c.JSON(400, Error{Msg: "Couldn't process request - invalid request"}) + return + } + + if req.Recipient == "" { + c.JSON(400, Error{Msg: "Couldn't process request - recipient missing"}) + return + } + + err = a.signalClient.UpdateContact(number, req.Recipient, req.Name, req.ExpirationInSeconds) + if err != nil { + c.JSON(400, Error{Msg: err.Error()}) + return + } + c.Status(http.StatusNoContent) +} diff --git a/src/client/client.go b/src/client/client.go index 5e0c688..e49e32b 100644 --- a/src/client/client.go +++ b/src/client/client.go @@ -1236,3 +1236,36 @@ func (s *SignalClient) SearchForNumbers(numbers []string) ([]SearchResultEntry, return searchResultEntries, err } + +func (s *SignalClient) UpdateContact(number string, recipient string, name *string, expirationInSeconds *int) error { + var err error + if s.signalCliMode == JsonRpc { + type Request struct { + Recipient string `json:"recipient"` + Name string `json:"name,omitempty"` + Expiration int `json:"expiration,omitempty"` + } + request := Request{Recipient: recipient} + if name != nil { + request.Name = *name + } + if expirationInSeconds != nil { + request.Expiration = *expirationInSeconds + } + jsonRpc2Client, err := s.getJsonRpc2Client(number) + if err != nil { + return err + } + _, err = jsonRpc2Client.getRaw("updateContact", request) + } else { + cmd := []string{"--config", s.signalCliConfig, "-a", number, "updateContact", recipient} + if name != nil { + cmd = append(cmd, []string{"-n", *name}...) + } + if expirationInSeconds != nil { + cmd = append(cmd, []string{"-e", strconv.Itoa(*expirationInSeconds)}...) + } + _, err = runSignalCli(true, cmd, "", s.signalCliMode) + } + return err +} diff --git a/src/main.go b/src/main.go index 8c448ef..a8b9958 100644 --- a/src/main.go +++ b/src/main.go @@ -227,6 +227,11 @@ func main() { { search.GET("", api.SearchForNumbers) } + + contacts := v1.Group("/contacts") + { + contacts.PUT(":number", api.UpdateContact) + } } v2 := router.Group("/v2")