diff --git a/src/api/api.go b/src/api/api.go index 7fc7295..811e584 100644 --- a/src/api/api.go +++ b/src/api/api.go @@ -1439,6 +1439,16 @@ func (a *Api) AddDevice(c *gin.Context) { c.Status(http.StatusNoContent) } +// @Summary Set account specific settings. +// @Tags General +// @Description Set account specific settings. +// @Accept json +// @Produce json +// @Param number path string true "Registered Phone Number" +// @Success 204 +// @Param data body TrustModeRequest true "Request" +// @Failure 400 {object} Error +// @Router /v1/configuration/{number}/settings [post] func (a *Api) SetTrustMode(c *gin.Context) { number := c.Param("number") if number == "" { @@ -1468,6 +1478,16 @@ func (a *Api) SetTrustMode(c *gin.Context) { c.Status(http.StatusNoContent) } +// @Summary List account specific settings. +// @Tags General +// @Description List account specific settings. +// @Accept json +// @Produce json +// @Param number path string true "Registered Phone Number" +// @Success 200 +// @Param data body TrustModeResponse true "Request" +// @Failure 400 {object} Error +// @Router /v1/configuration/{number}/settings [get] func (a *Api) GetTrustMode(c *gin.Context) { number := c.Param("number") if number == "" { diff --git a/src/client/client.go b/src/client/client.go index 2f42919..ba2969f 100644 --- a/src/client/client.go +++ b/src/client/client.go @@ -262,7 +262,7 @@ func (s *SignalClient) Init() error { tcpPortsNumberMapping := s.jsonRpc2ClientConfig.GetTcpPortsForNumbers() for number, tcpPort := range tcpPortsNumberMapping { - s.jsonRpc2Clients[number] = NewJsonRpc2Client(s.signalCliApiConfig) + s.jsonRpc2Clients[number] = NewJsonRpc2Client(s.signalCliApiConfig, number) err := s.jsonRpc2Clients[number].Dial("127.0.0.1:" + strconv.FormatInt(tcpPort, 10)) if err != nil { return err diff --git a/src/client/jsonrpc2.go b/src/client/jsonrpc2.go index 94e1ff5..64ccca3 100644 --- a/src/client/jsonrpc2.go +++ b/src/client/jsonrpc2.go @@ -10,6 +10,7 @@ import ( "github.com/bbernhard/signal-cli-rest-api/utils" uuid "github.com/gofrs/uuid" log "github.com/sirupsen/logrus" + "github.com/tidwall/sjson" ) type Error struct { @@ -35,11 +36,13 @@ type JsonRpc2Client struct { receivedMessages chan JsonRpc2ReceivedMessage lastTimeErrorMessageSent time.Time signalCliApiConfig *utils.SignalCliApiConfig + number string } -func NewJsonRpc2Client(signalCliApiConfig *utils.SignalCliApiConfig) *JsonRpc2Client { +func NewJsonRpc2Client(signalCliApiConfig *utils.SignalCliApiConfig, number string) *JsonRpc2Client { return &JsonRpc2Client{ signalCliApiConfig: signalCliApiConfig, + number: number, } } @@ -64,6 +67,16 @@ func (r *JsonRpc2Client) getRaw(command string, args interface{}) (string, error Params interface{} `json:"params,omitempty"` } + trustModeStr := "" + trustMode, err := r.signalCliApiConfig.GetTrustModeForNumber(r.number) + if err == nil { + trustModeStr, err = utils.TrustModeToString(trustMode) + if err != nil { + trustModeStr = "" + log.Error("Invalid trust mode: ", trustModeStr) + } + } + u, err := uuid.NewV4() if err != nil { return "", err @@ -79,6 +92,13 @@ func (r *JsonRpc2Client) getRaw(command string, args interface{}) (string, error return "", err } + if trustModeStr != "" { + fullCommandBytes, err = sjson.SetBytes(fullCommandBytes, "params.trustNewIdentities", trustModeStr) + if err != nil { + return "", err + } + } + log.Debug("full command: ", string(fullCommandBytes)) _, err = r.conn.Write([]byte(string(fullCommandBytes) + "\n")) diff --git a/src/docs/docs.go b/src/docs/docs.go index 7dad03e..91fcacc 100644 --- a/src/docs/docs.go +++ b/src/docs/docs.go @@ -209,6 +209,88 @@ var doc = `{ } } }, + "/v1/configuration/{number}/settings": { + "get": { + "description": "List account specific settings.", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "General" + ], + "summary": "List account specific settings.", + "parameters": [ + { + "type": "string", + "description": "Registered Phone Number", + "name": "number", + "in": "path", + "required": true + }, + { + "description": "Request", + "name": "data", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/api.TrustModeResponse" + } + } + ], + "responses": { + "200": {}, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/api.Error" + } + } + } + }, + "post": { + "description": "Set account specific settings.", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "General" + ], + "summary": "Set account specific settings.", + "parameters": [ + { + "type": "string", + "description": "Registered Phone Number", + "name": "number", + "in": "path", + "required": true + }, + { + "description": "Request", + "name": "data", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/api.TrustModeRequest" + } + } + ], + "responses": { + "204": {}, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/api.Error" + } + } + } + } + }, "/v1/contacts{number}": { "put": { "description": "Updates the info associated to a number on the contact list.", @@ -1658,6 +1740,22 @@ var doc = `{ } } }, + "api.TrustModeRequest": { + "type": "object", + "properties": { + "trust_mode": { + "type": "string" + } + } + }, + "api.TrustModeResponse": { + "type": "object", + "properties": { + "trust_mode": { + "type": "string" + } + } + }, "api.TypingIndicatorRequest": { "type": "object", "properties": { diff --git a/src/docs/swagger.json b/src/docs/swagger.json index 00bd4a3..84355ab 100644 --- a/src/docs/swagger.json +++ b/src/docs/swagger.json @@ -193,6 +193,88 @@ } } }, + "/v1/configuration/{number}/settings": { + "get": { + "description": "List account specific settings.", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "General" + ], + "summary": "List account specific settings.", + "parameters": [ + { + "type": "string", + "description": "Registered Phone Number", + "name": "number", + "in": "path", + "required": true + }, + { + "description": "Request", + "name": "data", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/api.TrustModeResponse" + } + } + ], + "responses": { + "200": {}, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/api.Error" + } + } + } + }, + "post": { + "description": "Set account specific settings.", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "General" + ], + "summary": "Set account specific settings.", + "parameters": [ + { + "type": "string", + "description": "Registered Phone Number", + "name": "number", + "in": "path", + "required": true + }, + { + "description": "Request", + "name": "data", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/api.TrustModeRequest" + } + } + ], + "responses": { + "204": {}, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/api.Error" + } + } + } + } + }, "/v1/contacts{number}": { "put": { "description": "Updates the info associated to a number on the contact list.", @@ -1642,6 +1724,22 @@ } } }, + "api.TrustModeRequest": { + "type": "object", + "properties": { + "trust_mode": { + "type": "string" + } + } + }, + "api.TrustModeResponse": { + "type": "object", + "properties": { + "trust_mode": { + "type": "string" + } + } + }, "api.TypingIndicatorRequest": { "type": "object", "properties": { diff --git a/src/docs/swagger.yaml b/src/docs/swagger.yaml index 5b5752f..b1c54ce 100644 --- a/src/docs/swagger.yaml +++ b/src/docs/swagger.yaml @@ -141,6 +141,16 @@ definitions: verified_safety_number: type: string type: object + api.TrustModeRequest: + properties: + trust_mode: + type: string + type: object + api.TrustModeResponse: + properties: + trust_mode: + type: string + type: object api.TypingIndicatorRequest: properties: recipient: @@ -356,6 +366,61 @@ paths: summary: Set the REST API configuration. tags: - General + /v1/configuration/{number}/settings: + get: + consumes: + - application/json + description: List account specific settings. + parameters: + - description: Registered Phone Number + in: path + name: number + required: true + type: string + - description: Request + in: body + name: data + required: true + schema: + $ref: '#/definitions/api.TrustModeResponse' + produces: + - application/json + responses: + "200": {} + "400": + description: Bad Request + schema: + $ref: '#/definitions/api.Error' + summary: List account specific settings. + tags: + - General + post: + consumes: + - application/json + description: Set account specific settings. + parameters: + - description: Registered Phone Number + in: path + name: number + required: true + type: string + - description: Request + in: body + name: data + required: true + schema: + $ref: '#/definitions/api.TrustModeRequest' + produces: + - application/json + responses: + "204": {} + "400": + description: Bad Request + schema: + $ref: '#/definitions/api.Error' + summary: Set account specific settings. + tags: + - General /v1/contacts{number}: put: consumes: diff --git a/src/go.mod b/src/go.mod index 56b5303..dab8360 100644 --- a/src/go.mod +++ b/src/go.mod @@ -20,6 +20,7 @@ require ( github.com/swaggo/files v0.0.0-20190704085106-630677cd5c14 github.com/swaggo/gin-swagger v1.2.0 github.com/swaggo/swag v1.6.7 + github.com/tidwall/sjson v1.2.4 // indirect github.com/urfave/cli/v2 v2.2.0 // indirect golang.org/x/net v0.0.0-20200625001655-4c5254603344 // indirect golang.org/x/text v0.3.3 // indirect diff --git a/src/go.sum b/src/go.sum index 236835e..7bf88df 100644 --- a/src/go.sum +++ b/src/go.sum @@ -128,6 +128,14 @@ github.com/swaggo/gin-swagger v1.2.0/go.mod h1:qlH2+W7zXGZkczuL+r2nEBR2JTT+/lX05 github.com/swaggo/swag v1.5.1/go.mod h1:1Bl9F/ZBpVWh22nY0zmYyASPO1lI/zIwRDrpZU+tv8Y= github.com/swaggo/swag v1.6.7 h1:e8GC2xDllJZr3omJkm9YfmK0Y56+rMO3cg0JBKNz09s= github.com/swaggo/swag v1.6.7/go.mod h1:xDhTyuFIujYiN3DKWC/H/83xcfHp+UE/IzWWampG7Zc= +github.com/tidwall/gjson v1.12.1 h1:ikuZsLdhr8Ws0IdROXUS1Gi4v9Z4pGqpX/CvJkxvfpo= +github.com/tidwall/gjson v1.12.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= +github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= +github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= +github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs= +github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= +github.com/tidwall/sjson v1.2.4 h1:cuiLzLnaMeBhRmEv00Lpk3tkYrcxpmbU81tAY4Dw0tc= +github.com/tidwall/sjson v1.2.4/go.mod h1:098SZ494YoMWPmMO6ct4dcFnqxwj9r/gF0Etp19pSNM= github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc= github.com/ugorji/go v1.1.5-pre/go.mod h1:FwP/aQVg39TXzItUBMwnWp9T9gPQnXw4Poh4/oBQZ/0= github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo= diff --git a/src/main.go b/src/main.go index f718854..48fe752 100644 --- a/src/main.go +++ b/src/main.go @@ -150,8 +150,8 @@ func main() { { configuration.GET("", api.GetConfiguration) configuration.POST("", api.SetConfiguration) - configuration.POST(":number/trustmode", api.SetTrustMode) - configuration.GET(":number/trustmode", api.GetTrustMode) + configuration.POST(":number/settings", api.SetTrustMode) + configuration.GET(":number/settings", api.GetTrustMode) } health := v1.Group("/health")