Skip to content

Commit

Permalink
get accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomek Osika committed Nov 12, 2023
1 parent c493655 commit 385a591
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 39 additions & 0 deletions src/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}

Expand Down
36 changes: 36 additions & 0 deletions src/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -1951,6 +1980,9 @@ var doc = `{
},
"description": {
"type": "string"
},
"name": {
"type": "string"
}
}
},
Expand Down Expand Up @@ -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"
Expand Down
36 changes: 36 additions & 0 deletions src/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -1935,6 +1964,9 @@
},
"description": {
"type": "string"
},
"name": {
"type": "string"
}
}
},
Expand Down Expand Up @@ -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"
Expand Down
21 changes: 21 additions & 0 deletions src/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 385a591

Please sign in to comment.