Skip to content

Commit

Permalink
Merge branch 'typingindicator'
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernhard B committed Oct 30, 2021
2 parents 94d10b1 + 80805c0 commit ee6ef0b
Show file tree
Hide file tree
Showing 6 changed files with 435 additions and 0 deletions.
70 changes: 70 additions & 0 deletions src/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ type SendMessageV2 struct {
Base64Attachments []string `json:"base64_attachments"`
}

type TypingIndicatorRequest struct {
Recipient string `json:"recipient"`
}

type Error struct {
Msg string `json:"error"`
}
Expand Down Expand Up @@ -885,3 +889,69 @@ func (a *Api) QuitGroup(c *gin.Context) {
}
c.Status(http.StatusNoContent)
}

// @Summary Show Typing Indicator.
// @Tags Messages
// @Description Show Typing Indicator.
// @Accept json
// @Produce json
// @Success 201 {string} OK
// @Failure 400 {object} Error
// @Param number path string true "Registered Phone Number"
// @Param data body TypingIndicatorRequest true "Type"
// @Router /v1/typing-indicator/{number} [put]
func (a *Api) SendStartTyping(c *gin.Context) {
var req TypingIndicatorRequest
err := c.BindJSON(&req)
if err != nil {
c.JSON(400, Error{Msg: "Couldn't process request - invalid request"})
log.Error(err.Error())
return
}

number := c.Param("number")
if number == "" {
c.JSON(400, Error{Msg: "Couldn't process request - number missing"})
return
}

err = a.signalClient.SendStartTyping(number, req.Recipient)
if err != nil {
c.JSON(400, Error{Msg: err.Error()})
return
}
c.Status(http.StatusNoContent)
}

// @Summary Hide Typing Indicator.
// @Tags Messages
// @Description Hide Typing Indicator.
// @Accept json
// @Produce json
// @Success 201 {string} OK
// @Failure 400 {object} Error
// @Param number path string true "Registered Phone Number"
// @Param data body TypingIndicatorRequest true "Type"
// @Router /v1/typing-indicator/{number} [delete]
func (a *Api) SendStopTyping(c *gin.Context) {
var req TypingIndicatorRequest
err := c.BindJSON(&req)
if err != nil {
c.JSON(400, Error{Msg: "Couldn't process request - invalid request"})
log.Error(err.Error())
return
}

number := c.Param("number")
if number == "" {
c.JSON(400, Error{Msg: "Couldn't process request - number missing"})
return
}

err = a.signalClient.SendStopTyping(number, req.Recipient)
if err != nil {
c.JSON(400, Error{Msg: err.Error()})
return
}
c.Status(http.StatusNoContent)
}
85 changes: 85 additions & 0 deletions src/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -994,3 +994,88 @@ func (s *SignalClient) QuitGroup(number string, groupId string) error {
}
return err
}

func (s *SignalClient) SendStartTyping(number string, recipient string) error {
var err error
recp := recipient
isGroup := false
if strings.HasPrefix(recipient, groupPrefix) {
isGroup = true
recp, err = ConvertGroupIdToInternalGroupId(recipient)
if err != nil {
return errors.New("Invalid group id")
}
}

if s.signalCliMode == JsonRpc {
type Request struct {
Recipient string `json:"recipient,omitempty"`
GroupId string `json:"group-id,omitempty"`
}
request := Request{}
if !isGroup {
request.Recipient = recp
} else {
request.GroupId = recp
}

jsonRpc2Client, err := s.getJsonRpc2Client(number)
if err != nil {
return err
}
_, err = jsonRpc2Client.getRaw("sendTyping", request)
} else {
cmd := []string{"--config", s.signalCliConfig, "-u", number, "sendTyping"}
if !isGroup {
cmd = append(cmd, recp)
} else {
cmd = append(cmd, []string{"-g", recp}...)
}
_, err = runSignalCli(true, cmd, "", s.signalCliMode)
}

return err
}

func (s *SignalClient) SendStopTyping(number string, recipient string) error {
var err error
recp := recipient
isGroup := false
if strings.HasPrefix(recipient, groupPrefix) {
isGroup = true
recp, err = ConvertGroupIdToInternalGroupId(recipient)
if err != nil {
return errors.New("Invalid group id")
}
}

if s.signalCliMode == JsonRpc {
type Request struct {
Recipient string `json:"recipient,omitempty"`
GroupId string `json:"group-id,omitempty"`
Stop bool `json:"stop"`
}
request := Request{Stop: true}
if !isGroup {
request.Recipient = recp
} else {
request.GroupId = recp
}

jsonRpc2Client, err := s.getJsonRpc2Client(number)
if err != nil {
return err
}
_, err = jsonRpc2Client.getRaw("sendTyping", request)
} else {
cmd := []string{"--config", s.signalCliConfig, "-u", number, "sendTyping", "--stop"}
if !isGroup {
cmd = append(cmd, recp)
} else {
cmd = append(cmd, []string{"-g", recp}...)
}
_, err = runSignalCli(true, cmd, "", s.signalCliMode)
}

return err
}
103 changes: 103 additions & 0 deletions src/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,98 @@ var doc = `{
}
}
},
"/v1/typing-indicator/{number}": {
"put": {
"description": "Show Typing Indicator.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Messages"
],
"summary": "Show Typing Indicator.",
"parameters": [
{
"type": "string",
"description": "Registered Phone Number",
"name": "number",
"in": "path",
"required": true
},
{
"description": "Type",
"name": "data",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/api.TypingIndicatorRequest"
}
}
],
"responses": {
"201": {
"description": "Created",
"schema": {
"type": "string"
}
},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/api.Error"
}
}
}
},
"delete": {
"description": "Hide Typing Indicator.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Messages"
],
"summary": "Hide Typing Indicator.",
"parameters": [
{
"type": "string",
"description": "Registered Phone Number",
"name": "number",
"in": "path",
"required": true
},
{
"description": "Type",
"name": "data",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/api.TypingIndicatorRequest"
}
}
],
"responses": {
"201": {
"description": "Created",
"schema": {
"type": "string"
}
},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/api.Error"
}
}
}
}
},
"/v2/send": {
"post": {
"description": "Send a signal message",
Expand Down Expand Up @@ -1072,6 +1164,14 @@ var doc = `{
}
}
},
"api.TypingIndicatorRequest": {
"type": "object",
"properties": {
"recipient": {
"type": "string"
}
}
},
"api.UpdateProfileRequest": {
"type": "object",
"properties": {
Expand All @@ -1097,6 +1197,9 @@ var doc = `{
"build": {
"type": "integer"
},
"mode": {
"type": "string"
},
"versions": {
"type": "array",
"items": {
Expand Down
Loading

0 comments on commit ee6ef0b

Please sign in to comment.