mirror of
https://github.com/aljazceru/signal-cli-rest-api.git
synced 2025-12-19 07:34:23 +01:00
Merge pull request #549 from tactilenews/expose_list_contacts_endpoint
Expose listContacts endpoint
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user