Skip to content

Commit

Permalink
Merge pull request #549 from tactilenews/expose_list_contacts_endpoint
Browse files Browse the repository at this point in the history
Expose listContacts endpoint
  • Loading branch information
bbernhard authored Jun 18, 2024
2 parents 05225a2 + ee10b6b commit e7ebd01
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1971,3 +1971,28 @@ func (a *Api) AddStickerPack(c *gin.Context) {

c.Status(201)
}

// @Summary List Contacts
// @Tags Contacts
// @Description List all contacts for the given number.
// @Produce json
// @Success 200 {object} []client.ListContactsResponse
// @Param number path string true "Registered Phone Number"
// @Router /v1/contacts/{number} [get]
func (a *Api) ListContacts(c *gin.Context) {
number := c.Param("number")

if number == "" {
c.JSON(400, Error{Msg: "Couldn't process request - number missing"})
return
}

contacts, err := a.signalClient.ListContacts(number)

if err != nil {
c.JSON(400, Error{Msg: err.Error()})
return
}

c.JSON(200, contacts)
}
67 changes: 67 additions & 0 deletions src/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,17 @@ type ListInstalledStickerPacksResponse struct {
Author string `json:"author"`
}

type ListContactsResponse struct {
Number string `json:"number"`
Uuid string `json:"uuid"`
Name string `json:"name"`
ProfileName string `json:"profile_name"`
Username string `json:"username"`
Color string `json:"color"`
Blocked bool `json:"blocked"`
MessageExpiration string `json:"message_expiration"`
}

func cleanupTmpFiles(paths []string) {
for _, path := range paths {
os.Remove(path)
Expand Down Expand Up @@ -2112,3 +2123,59 @@ func (s *SignalClient) AddStickerPack(number string, packId string, packKey stri
return err
}
}

func (s *SignalClient) ListContacts(number string) ([]ListContactsResponse, error) {
type ListContactsSignlCliResponse struct {
Number string `json:"number"`
Uuid string `json:"uuid"`
Name string `json:"name"`
ProfileName string `json:"profileName"`
Username string `json:"username"`
Color string `json:"color"`
Blocked bool `json:"blocked"`
MessageExpiration string `json:"messageExpiration"`
}

resp := []ListContactsResponse{}

var err error
var rawData string

if s.signalCliMode == JsonRpc {
jsonRpc2Client, err := s.getJsonRpc2Client()
if err != nil {
return nil, err
}
rawData, err = jsonRpc2Client.getRaw("listContacts", &number, nil)
if err != nil {
return resp, err
}
} else {
cmd := []string{"--config", s.signalCliConfig, "-o", "json", "-a", number, "listContacts"}
rawData, err = s.cliClient.Execute(true, cmd, "")
if err != nil {
return resp, err
}
}

var signalCliResp []ListContactsSignlCliResponse
err = json.Unmarshal([]byte(rawData), &signalCliResp)
if err != nil {
return resp, errors.New("Couldn't process request - invalid signal-cli response")
}

for _, value := range signalCliResp {
resp = append(resp, ListContactsResponse{
Number: value.Number,
Uuid: value.Uuid,
Name: value.Name,
ProfileName: value.ProfileName,
Username: value.Username,
Color: value.Color,
Blocked: value.Blocked,
MessageExpiration: value.MessageExpiration,
})
}

return resp, nil
}
1 change: 1 addition & 0 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ func main() {

contacts := v1.Group("/contacts")
{
contacts.GET(":number", api.ListContacts)
contacts.PUT(":number", api.UpdateContact)
contacts.POST(":number/sync", api.SendContacts)
}
Expand Down

0 comments on commit e7ebd01

Please sign in to comment.