Skip to content

Expose listContacts endpoint #549

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading