diff --git a/src/api/api.go b/src/api/api.go index d608f6ff..fd999f42 100644 --- a/src/api/api.go +++ b/src/api/api.go @@ -894,6 +894,23 @@ func (a *Api) GetQrCodeLink(c *gin.Context) { c.Data(200, "image/png", png) } +// @Summary List all accounts +// @Tags Accounts +// @Description Lists all of the accounts linked or registered +// @Produce json +// @Success 200 {object} []string +// @Failure 400 {object} Error +// @Router /v1/accounts [get] +func (a *Api) GetAccounts(c *gin.Context) { + devices, err := a.signalClient.GetAccounts() + if err != nil { + c.JSON(500, Error{Msg: "Couldn't get list of accounts: " + err.Error()}) + return + } + + c.JSON(200, devices) +} + // @Summary List all attachments. // @Tags Attachments // @Description List all downloaded attachments diff --git a/src/client/client.go b/src/client/client.go index f37f491a..3391beca 100644 --- a/src/client/client.go +++ b/src/client/client.go @@ -1076,6 +1076,45 @@ func (s *SignalClient) GetQrCodeLink(deviceName string, qrCodeVersion int) ([]by return png, nil } +func (s *SignalClient) GetAccounts() ([]string, error) { + accounts := make([]string, 0) + var rawData string + var err error + + if s.signalCliMode == JsonRpc { + jsonRpc2Client, err := s.getJsonRpc2Client() + if err != nil { + return accounts, err + } + rawData, err = jsonRpc2Client.getRaw("listAccounts", nil, nil) + if err != nil { + return accounts, err + } + + } else { + rawData, err = s.cliClient.Execute(true, []string{"--config", s.signalCliConfig, "--output", "json", "listAccounts"}, "") + if err != nil { + return accounts, err + } + } + + type Account struct { + Number string `json:"number"` + } + accountObjs := []Account{} + + err = json.Unmarshal([]byte(rawData), &accountObjs) + if err != nil { + return accounts, err + } + + for _, account := range accountObjs { + accounts = append(accounts, account.Number) + } + + return accounts, nil +} + func (s *SignalClient) GetAttachments() ([]string, error) { files := []string{} diff --git a/src/docs/docs.go b/src/docs/docs.go index 0bc3b046..5e4ad071 100644 --- a/src/docs/docs.go +++ b/src/docs/docs.go @@ -45,6 +45,35 @@ var doc = `{ } } }, + "/v1/accounts": { + "get": { + "description": "Lists all of the devices linked or registered", + "produces": [ + "application/json" + ], + "tags": [ + "Accounts" + ], + "summary": "List all accounts", + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "array", + "items": { + "type": "string" + } + } + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/api.Error" + } + } + } + } + }, "/v1/attachments": { "get": { "description": "List all downloaded attachments", @@ -1951,6 +1980,9 @@ var doc = `{ }, "description": { "type": "string" + }, + "name": { + "type": "string" } } }, @@ -2090,6 +2122,10 @@ var doc = `{ "description": "Register and link Devices.", "name": "Devices" }, + { + "description": "List registered and linked accounts", + "name": "Accounts" + }, { "description": "Create, List and Delete Signal Groups.", "name": "Groups" diff --git a/src/docs/swagger.json b/src/docs/swagger.json index ef70d4cc..1ddaa324 100644 --- a/src/docs/swagger.json +++ b/src/docs/swagger.json @@ -29,6 +29,35 @@ } } }, + "/v1/accounts": { + "get": { + "description": "Lists all of the devices linked or registered", + "produces": [ + "application/json" + ], + "tags": [ + "Accounts" + ], + "summary": "List all accounts", + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "array", + "items": { + "type": "string" + } + } + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/api.Error" + } + } + } + } + }, "/v1/attachments": { "get": { "description": "List all downloaded attachments", @@ -1935,6 +1964,9 @@ }, "description": { "type": "string" + }, + "name": { + "type": "string" } } }, @@ -2074,6 +2106,10 @@ "description": "Register and link Devices.", "name": "Devices" }, + { + "description": "List registered and linked accounts", + "name": "Accounts" + }, { "description": "Create, List and Delete Signal Groups.", "name": "Groups" diff --git a/src/docs/swagger.yaml b/src/docs/swagger.yaml index c78b56cd..b3f233e6 100644 --- a/src/docs/swagger.yaml +++ b/src/docs/swagger.yaml @@ -311,6 +311,25 @@ paths: summary: Lists general information about the API tags: - General + /v1/accounts: + get: + description: Lists all of the devices linked or registered + produces: + - application/json + responses: + "200": + description: OK + schema: + items: + type: string + type: array + "400": + description: Bad Request + schema: + $ref: '#/definitions/api.Error' + summary: List all accounts + tags: + - Accounts /v1/attachments: get: description: List all downloaded attachments @@ -1372,6 +1391,8 @@ tags: name: General - description: Register and link Devices. name: Devices +- description: List registered and linked accounts + name: Accounts - description: Create, List and Delete Signal Groups. name: Groups - description: Send and Receive Signal Messages. diff --git a/src/main.go b/src/main.go index 0d1e6a04..996fa859 100644 --- a/src/main.go +++ b/src/main.go @@ -28,6 +28,9 @@ import ( // @tag.name Devices // @tag.description Register and link Devices. +// @tag.name Accounts +// @tag.description List registered and linked accounts + // @tag.name Groups // @tag.description Create, List and Delete Signal Groups. @@ -198,6 +201,11 @@ func main() { link.GET("", api.GetQrCodeLink) } + accounts := v1.Group("accounts") + { + accounts.GET("", api.GetAccounts) + } + devices := v1.Group("devices") { devices.POST(":number", api.AddDevice)