Merge pull request #176 from filipre/reactions

Reactions Endpoint Proposal
This commit is contained in:
Bernhard B
2021-11-06 14:54:13 +01:00
committed by GitHub
5 changed files with 684 additions and 356 deletions

View File

@@ -9,8 +9,8 @@ import (
"github.com/gabriel-vasile/mimetype" "github.com/gabriel-vasile/mimetype"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
log "github.com/sirupsen/logrus"
"github.com/bbernhard/signal-cli-rest-api/client" "github.com/bbernhard/signal-cli-rest-api/client"
utils "github.com/bbernhard/signal-cli-rest-api/utils" utils "github.com/bbernhard/signal-cli-rest-api/utils"
@@ -57,6 +57,13 @@ type VerifyNumberSettings struct {
Pin string `json:"pin"` Pin string `json:"pin"`
} }
type Reaction struct {
Recipient string `json:"recipient"`
Reaction string `json:"reaction"`
TargetAuthor string `json:"target_author"`
Timestamp int64 `json:"timestamp"`
}
type SendMessageV1 struct { type SendMessageV1 struct {
Number string `json:"number"` Number string `json:"number"`
Recipients []string `json:"recipients"` Recipients []string `json:"recipients"`
@@ -80,8 +87,6 @@ type Error struct {
Msg string `json:"error"` Msg string `json:"error"`
} }
type CreateGroupResponse struct { type CreateGroupResponse struct {
Id string `json:"id"` Id string `json:"id"`
} }
@@ -283,7 +288,6 @@ func (a *Api) SendV2(c *gin.Context) {
c.JSON(201, SendMessageResponse{Timestamp: strconv.FormatInt((*timestamps)[0].Timestamp, 10)}) c.JSON(201, SendMessageResponse{Timestamp: strconv.FormatInt((*timestamps)[0].Timestamp, 10)})
} }
func (a *Api) handleSignalReceive(ws *websocket.Conn, number string) { func (a *Api) handleSignalReceive(ws *websocket.Conn, number string) {
for { for {
data, err := a.signalClient.Receive(number, 0) data, err := a.signalClient.Receive(number, 0)
@@ -890,6 +894,97 @@ func (a *Api) QuitGroup(c *gin.Context) {
c.Status(http.StatusNoContent) c.Status(http.StatusNoContent)
} }
// @Summary Send a reaction.
// @Tags Reactions
// @Description React to a message
// @Accept json
// @Produce json
// @Success 204 {string} OK
// @Failure 400 {object} Error
// @Param data body Reaction true "Reaction"
// @Router /v1/reactions/{number} [post]
func (a *Api) SendReaction(c *gin.Context) {
var req Reaction
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 req.Recipient == "" {
c.JSON(400, Error{Msg: "Couldn't process request - recipient missing"})
return
}
if req.Reaction == "" {
c.JSON(400, Error{Msg: "Couldn't process request - reaction missing"})
return
}
if req.TargetAuthor == "" {
c.JSON(400, Error{Msg: "Couldn't process request - target_author missing"})
return
}
if req.Timestamp == 0 {
c.JSON(400, Error{Msg: "Couldn't process request - timestamp missing"})
return
}
err = a.signalClient.SendReaction(number, req.Recipient, req.Reaction, req.TargetAuthor, req.Timestamp, false)
if err != nil {
c.JSON(400, Error{Msg: err.Error()})
return
}
c.Status(http.StatusNoContent)
}
// @Summary Remove a reaction.
// @Tags Reactions
// @Description Remove a reaction
// @Accept json
// @Produce json
// @Success 204 {string} OK
// @Failure 400 {object} Error
// @Param data body Reaction true "Reaction"
// @Router /v1/reactions/{number} [delete]
func (a *Api) RemoveReaction(c *gin.Context) {
var req Reaction
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 req.Recipient == "" {
c.JSON(400, Error{Msg: "Couldn't process request - recipient missing"})
return
}
if req.TargetAuthor == "" {
c.JSON(400, Error{Msg: "Couldn't process request - target_author missing"})
return
}
if req.Timestamp == 0 {
c.JSON(400, Error{Msg: "Couldn't process request - timestamp missing"})
return
}
err = a.signalClient.SendReaction(number, req.Recipient, req.Reaction, req.TargetAuthor, req.Timestamp, true)
if err != nil {
c.JSON(400, Error{Msg: err.Error()})
return
}
c.Status(http.StatusNoContent)
}
// @Summary Show Typing Indicator. // @Summary Show Typing Indicator.
// @Tags Messages // @Tags Messages
// @Description Show Typing Indicator. // @Description Show Typing Indicator.

View File

@@ -14,9 +14,10 @@ import (
"strings" "strings"
"time" "time"
"github.com/cyphar/filepath-securejoin" securejoin "github.com/cyphar/filepath-securejoin"
"github.com/gabriel-vasile/mimetype" "github.com/gabriel-vasile/mimetype"
"github.com/h2non/filetype" "github.com/h2non/filetype"
//"github.com/sourcegraph/jsonrpc2"//"net/rpc/jsonrpc" //"github.com/sourcegraph/jsonrpc2"//"net/rpc/jsonrpc"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@@ -995,6 +996,69 @@ func (s *SignalClient) QuitGroup(number string, groupId string) error {
return err return err
} }
func (s *SignalClient) SendReaction(number string, recipient string, emoji string, target_author string, timestamp int64, remove bool) error {
// see https://github.com/AsamK/signal-cli/blob/master/man/signal-cli.1.adoc#sendreaction
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 remove && emoji == "" {
emoji = "👍" // emoji must not be empty to remove a reaction
}
if s.signalCliMode == JsonRpc {
type Request struct {
Recipient string `json:"recipient,omitempty"`
GroupId string `json:"group-id,omitempty"`
Emoji string `json:"emoji"`
TargetAuthor string `json:"target-author"`
Timestamp int64 `json:"target-timestamp"`
Remove bool `json:"remove,omitempty"`
}
request := Request{}
if !isGroup {
request.Recipient = recp
} else {
request.GroupId = recp
}
request.Emoji = emoji
request.TargetAuthor = target_author
request.Timestamp = timestamp
if remove {
request.Remove = remove
}
jsonRpc2Client, err := s.getJsonRpc2Client(number)
if err != nil {
return err
}
_, err = jsonRpc2Client.getRaw("sendReaction", request)
return err
}
cmd := []string{
"--config", s.signalCliConfig,
"-u", number,
"sendReaction",
}
if !isGroup {
cmd = append(cmd, recp)
} else {
cmd = append(cmd, []string{"-g", recp}...)
}
cmd = append(cmd, []string{"-e", emoji, "-a", target_author, "-t", strconv.FormatInt(timestamp, 10)}...)
if remove {
cmd = append(cmd, "-r")
}
_, err = runSignalCli(true, cmd, "", s.signalCliMode)
return err
}
func (s *SignalClient) SendStartTyping(number string, recipient string) error { func (s *SignalClient) SendStartTyping(number string, recipient string) error {
var err error var err error
recp := recipient recp := recipient

View File

@@ -679,6 +679,101 @@
} }
} }
}, },
"/v1/reactions/{number}": {
"post": {
"description": "React to a message.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Reactions"
],
"summary": "Send a reaction.",
"parameters": [
{
"description": "Reactions",
"name": "data",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/api.Reaction"
}
}
],
"responses": {
"204": {
"description": "No Content",
"schema": {
"type": "string"
}
},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/api.Error"
}
}
}
},
"delete": {
"description": "Delete a reaction.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Reactions"
],
"summary": "Delete a reaction.",
"parameters": [
{
"description": "Reactions",
"name": "data",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/api.Reaction"
}
}
],
"responses": {
"204": {
"description": "No Content",
"schema": {
"type": "string"
}
},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/api.Error"
}
}
}
}
},
"api.Reaction": {
"type": "object",
"properties": {
"recipient": {
"type": "string"
},
"reaction": {
"type": "string"
},
"target_author": {
"type": "string"
},
"timestamp": {
"type": "integer"
}
}
},
"/v1/receive/{number}": { "/v1/receive/{number}": {
"get": { "get": {
"description": "Receives Signal Messages from the Signal Network. If you are running the docker container in normal/native mode, this is a GET endpoint. In json-rpc mode this is a websocket endpoint.", "description": "Receives Signal Messages from the Signal Network. If you are running the docker container in normal/native mode, this is a GET endpoint. In json-rpc mode this is a websocket endpoint.",
@@ -1280,6 +1375,10 @@
{ {
"description": "List and Trust Identities.", "description": "List and Trust Identities.",
"name": "Identities" "name": "Identities"
},
{
"description": "React to messages.",
"name": "Reactions"
} }
] ]
} }

View File

@@ -1,9 +1,9 @@
basePath: / "basePath: /
definitions: definitions:
api.Configuration: api.Configuration:
properties: properties:
logging: logging:
$ref: '#/definitions/api.LoggingConfiguration' $ref: "#/definitions/api.LoggingConfiguration"
type: object type: object
type: object type: object
api.CreateGroupRequest: api.CreateGroupRequest:
@@ -23,7 +23,7 @@ definitions:
name: name:
type: string type: string
permissions: permissions:
$ref: '#/definitions/api.GroupPermissions' $ref: "#/definitions/api.GroupPermissions"
type: object type: object
type: object type: object
api.CreateGroupResponse: api.CreateGroupResponse:
@@ -54,6 +54,17 @@ definitions:
Level: Level:
type: string type: string
type: object type: object
api.Reaction:
properties:
recipient:
type: string
reaction:
type: string
target_author:
type: string
timestamp:
type: integer
type: object
api.RegisterNumberRequest: api.RegisterNumberRequest:
properties: properties:
captcha: captcha:
@@ -184,7 +195,7 @@ paths:
"200": "200":
description: OK description: OK
schema: schema:
$ref: '#/definitions/client.About' $ref: "#/definitions/client.About"
summary: Lists general information about the API summary: Lists general information about the API
tags: tags:
- General - General
@@ -203,7 +214,7 @@ paths:
"400": "400":
description: Bad Request description: Bad Request
schema: schema:
$ref: '#/definitions/api.Error' $ref: "#/definitions/api.Error"
summary: List all attachments. summary: List all attachments.
tags: tags:
- Attachments - Attachments
@@ -226,7 +237,7 @@ paths:
"400": "400":
description: Bad Request description: Bad Request
schema: schema:
$ref: '#/definitions/api.Error' $ref: "#/definitions/api.Error"
summary: Remove attachment. summary: Remove attachment.
tags: tags:
- Attachments - Attachments
@@ -248,7 +259,7 @@ paths:
"400": "400":
description: Bad Request description: Bad Request
schema: schema:
$ref: '#/definitions/api.Error' $ref: "#/definitions/api.Error"
summary: Serve Attachment. summary: Serve Attachment.
tags: tags:
- Attachments - Attachments
@@ -263,11 +274,11 @@ paths:
"200": "200":
description: OK description: OK
schema: schema:
$ref: '#/definitions/api.Configuration' $ref: "#/definitions/api.Configuration"
"400": "400":
description: Bad Request description: Bad Request
schema: schema:
$ref: '#/definitions/api.Error' $ref: "#/definitions/api.Error"
summary: List the REST API configuration. summary: List the REST API configuration.
tags: tags:
- General - General
@@ -281,7 +292,7 @@ paths:
name: data name: data
required: true required: true
schema: schema:
$ref: '#/definitions/api.Configuration' $ref: "#/definitions/api.Configuration"
produces: produces:
- application/json - application/json
responses: responses:
@@ -292,7 +303,7 @@ paths:
"400": "400":
description: Bad Request description: Bad Request
schema: schema:
$ref: '#/definitions/api.Error' $ref: "#/definitions/api.Error"
summary: Set the REST API configuration. summary: Set the REST API configuration.
tags: tags:
- General - General
@@ -314,12 +325,12 @@ paths:
description: OK description: OK
schema: schema:
items: items:
$ref: '#/definitions/client.GroupEntry' $ref: "#/definitions/client.GroupEntry"
type: array type: array
"400": "400":
description: Bad Request description: Bad Request
schema: schema:
$ref: '#/definitions/api.Error' $ref: "#/definitions/api.Error"
summary: List all Signal Groups. summary: List all Signal Groups.
tags: tags:
- Groups - Groups
@@ -333,7 +344,7 @@ paths:
name: data name: data
required: true required: true
schema: schema:
$ref: '#/definitions/api.CreateGroupRequest' $ref: "#/definitions/api.CreateGroupRequest"
- description: Registered Phone Number - description: Registered Phone Number
in: path in: path
name: number name: number
@@ -345,11 +356,11 @@ paths:
"201": "201":
description: Created description: Created
schema: schema:
$ref: '#/definitions/api.CreateGroupResponse' $ref: "#/definitions/api.CreateGroupResponse"
"400": "400":
description: Bad Request description: Bad Request
schema: schema:
$ref: '#/definitions/api.Error' $ref: "#/definitions/api.Error"
summary: Create a new Signal Group. summary: Create a new Signal Group.
tags: tags:
- Groups - Groups
@@ -379,7 +390,7 @@ paths:
"400": "400":
description: Bad Request description: Bad Request
schema: schema:
$ref: '#/definitions/api.Error' $ref: "#/definitions/api.Error"
summary: Delete a Signal Group. summary: Delete a Signal Group.
tags: tags:
- Groups - Groups
@@ -404,11 +415,11 @@ paths:
"200": "200":
description: OK description: OK
schema: schema:
$ref: '#/definitions/client.GroupEntry' $ref: "#/definitions/client.GroupEntry"
"400": "400":
description: Bad Request description: Bad Request
schema: schema:
$ref: '#/definitions/api.Error' $ref: "#/definitions/api.Error"
summary: List a Signal Group. summary: List a Signal Group.
tags: tags:
- Groups - Groups
@@ -438,7 +449,7 @@ paths:
"400": "400":
description: Bad Request description: Bad Request
schema: schema:
$ref: '#/definitions/api.Error' $ref: "#/definitions/api.Error"
summary: Block a Signal Group. summary: Block a Signal Group.
tags: tags:
- Groups - Groups
@@ -468,7 +479,7 @@ paths:
"400": "400":
description: Bad Request description: Bad Request
schema: schema:
$ref: '#/definitions/api.Error' $ref: "#/definitions/api.Error"
summary: Join a Signal Group. summary: Join a Signal Group.
tags: tags:
- Groups - Groups
@@ -498,7 +509,7 @@ paths:
"400": "400":
description: Bad Request description: Bad Request
schema: schema:
$ref: '#/definitions/api.Error' $ref: "#/definitions/api.Error"
summary: Quit a Signal Group. summary: Quit a Signal Group.
tags: tags:
- Groups - Groups
@@ -531,7 +542,7 @@ paths:
description: OK description: OK
schema: schema:
items: items:
$ref: '#/definitions/client.IdentityEntry' $ref: "#/definitions/client.IdentityEntry"
type: array type: array
summary: List Identities summary: List Identities
tags: tags:
@@ -545,7 +556,7 @@ paths:
name: data name: data
required: true required: true
schema: schema:
$ref: '#/definitions/api.TrustIdentityRequest' $ref: "#/definitions/api.TrustIdentityRequest"
- description: Registered Phone Number - description: Registered Phone Number
in: path in: path
name: number name: number
@@ -575,7 +586,7 @@ paths:
name: data name: data
required: true required: true
schema: schema:
$ref: '#/definitions/api.UpdateProfileRequest' $ref: "#/definitions/api.UpdateProfileRequest"
- description: Registered Phone Number - description: Registered Phone Number
in: path in: path
name: number name: number
@@ -591,7 +602,7 @@ paths:
"400": "400":
description: Bad Request description: Bad Request
schema: schema:
$ref: '#/definitions/api.Error' $ref: "#/definitions/api.Error"
summary: Update Profile. summary: Update Profile.
tags: tags:
- Profiles - Profiles
@@ -614,10 +625,61 @@ paths:
"400": "400":
description: Bad Request description: Bad Request
schema: schema:
$ref: '#/definitions/api.Error' $ref: "#/definitions/api.Error"
summary: Link device and generate QR code. summary: Link device and generate QR code.
tags: tags:
- Devices - Devices
/v1/reactions/{number}:
post:
consumes:
- application/json
description: React to a message.
parameters:
- description: Reactions
in: body
name: data
required: true
schema:
$ref: '#/definitions/api.Reaction'
produces:
- application/json
responses:
"204":
description: No Content
schema:
type: string
"400":
description: Bad Request
schema:
$ref: '#/definitions/api.Error'
summary: Send a reaction.
tags:
- Reactions
delete:
consumes:
- application/json
description: Delete a reaction.
parameters:
- description: Reactions
in: body
name: data
required: true
schema:
$ref: '#/definitions/api.Reaction'
produces:
- application/json
responses:
"204":
description: No Content
schema:
type: string
"400":
description: Bad Request
schema:
$ref: '#/definitions/api.Error'
summary: Delete a reaction.
tags:
- Reactions
/v1/receive/{number}: /v1/receive/{number}:
get: get:
consumes: consumes:
@@ -629,7 +691,7 @@ paths:
name: number name: number
required: true required: true
type: string type: string
- description: 'Receive timeout in seconds (default: 1)' - description: "Receive timeout in seconds (default: 1)"
in: query in: query
name: timeout name: timeout
type: string type: string
@@ -645,7 +707,7 @@ paths:
"400": "400":
description: Bad Request description: Bad Request
schema: schema:
$ref: '#/definitions/api.Error' $ref: "#/definitions/api.Error"
summary: Receive Signal Messages. summary: Receive Signal Messages.
tags: tags:
- Messages - Messages
@@ -664,7 +726,7 @@ paths:
in: body in: body
name: data name: data
schema: schema:
$ref: '#/definitions/api.RegisterNumberRequest' $ref: "#/definitions/api.RegisterNumberRequest"
produces: produces:
- application/json - application/json
responses: responses:
@@ -672,7 +734,7 @@ paths:
"400": "400":
description: Bad Request description: Bad Request
schema: schema:
$ref: '#/definitions/api.Error' $ref: "#/definitions/api.Error"
summary: Register a phone number. summary: Register a phone number.
tags: tags:
- Devices - Devices
@@ -691,7 +753,7 @@ paths:
in: body in: body
name: data name: data
schema: schema:
$ref: '#/definitions/api.VerifyNumberSettings' $ref: "#/definitions/api.VerifyNumberSettings"
- description: Verification Code - description: Verification Code
in: path in: path
name: token name: token
@@ -707,7 +769,7 @@ paths:
"400": "400":
description: Bad Request description: Bad Request
schema: schema:
$ref: '#/definitions/api.Error' $ref: "#/definitions/api.Error"
summary: Verify a registered phone number. summary: Verify a registered phone number.
tags: tags:
- Devices - Devices
@@ -723,7 +785,7 @@ paths:
name: data name: data
required: true required: true
schema: schema:
$ref: '#/definitions/api.SendMessageV1' $ref: "#/definitions/api.SendMessageV1"
produces: produces:
- application/json - application/json
responses: responses:
@@ -734,7 +796,7 @@ paths:
"400": "400":
description: Bad Request description: Bad Request
schema: schema:
$ref: '#/definitions/api.Error' $ref: "#/definitions/api.Error"
summary: Send a signal message. summary: Send a signal message.
tags: tags:
- Messages - Messages
@@ -754,7 +816,7 @@ paths:
name: data name: data
required: true required: true
schema: schema:
$ref: '#/definitions/api.TypingIndicatorRequest' $ref: "#/definitions/api.TypingIndicatorRequest"
produces: produces:
- application/json - application/json
responses: responses:
@@ -765,7 +827,7 @@ paths:
"400": "400":
description: Bad Request description: Bad Request
schema: schema:
$ref: '#/definitions/api.Error' $ref: "#/definitions/api.Error"
summary: Hide Typing Indicator. summary: Hide Typing Indicator.
tags: tags:
- Messages - Messages
@@ -784,7 +846,7 @@ paths:
name: data name: data
required: true required: true
schema: schema:
$ref: '#/definitions/api.TypingIndicatorRequest' $ref: "#/definitions/api.TypingIndicatorRequest"
produces: produces:
- application/json - application/json
responses: responses:
@@ -795,7 +857,7 @@ paths:
"400": "400":
description: Bad Request description: Bad Request
schema: schema:
$ref: '#/definitions/api.Error' $ref: "#/definitions/api.Error"
summary: Show Typing Indicator. summary: Show Typing Indicator.
tags: tags:
- Messages - Messages
@@ -810,18 +872,18 @@ paths:
name: data name: data
required: true required: true
schema: schema:
$ref: '#/definitions/api.SendMessageV2' $ref: "#/definitions/api.SendMessageV2"
produces: produces:
- application/json - application/json
responses: responses:
"201": "201":
description: Created description: Created
schema: schema:
$ref: '#/definitions/api.SendMessageResponse' $ref: "#/definitions/api.SendMessageResponse"
"400": "400":
description: Bad Request description: Bad Request
schema: schema:
$ref: '#/definitions/api.Error' $ref: "#/definitions/api.Error"
summary: Send a signal message. summary: Send a signal message.
tags: tags:
- Messages - Messages
@@ -841,3 +903,6 @@ tags:
name: Profiles name: Profiles
- description: List and Trust Identities. - description: List and Trust Identities.
name: Identities name: Identities
- description: React to messages.
name: Reactions
"

View File

@@ -46,6 +46,9 @@ import (
// @tag.name Identities // @tag.name Identities
// @tag.description List and Trust Identities. // @tag.description List and Trust Identities.
// @tag.name Reactions
// @tag.description React to messages.
// @host 127.0.0.1:8080 // @host 127.0.0.1:8080
// @BasePath / // @BasePath /
func main() { func main() {
@@ -114,7 +117,6 @@ func main() {
} }
} }
jsonRpc2ClientConfigPathPath := *signalCliConfig + "/jsonrpc2.yml" jsonRpc2ClientConfigPathPath := *signalCliConfig + "/jsonrpc2.yml"
signalClient := client.NewSignalClient(*signalCliConfig, *attachmentTmpDir, *avatarTmpDir, signalCliMode, jsonRpc2ClientConfigPathPath) signalClient := client.NewSignalClient(*signalCliConfig, *attachmentTmpDir, *avatarTmpDir, signalCliMode, jsonRpc2ClientConfigPathPath)
err = signalClient.Init() err = signalClient.Init()
@@ -196,6 +198,12 @@ func main() {
typingIndicator.PUT(":number", api.SendStartTyping) typingIndicator.PUT(":number", api.SendStartTyping)
typingIndicator.DELETE(":number", api.SendStopTyping) typingIndicator.DELETE(":number", api.SendStopTyping)
} }
reactions := v1.Group("/reactions")
{
reactions.POST(":number", api.SendReaction)
reactions.DELETE(":number", api.RemoveReaction)
}
} }
v2 := router.Group("/v2") v2 := router.Group("/v2")
@@ -264,8 +272,5 @@ func main() {
c.Start() c.Start()
} }
router.Run() router.Run()
} }