Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR to include client expiration #172

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 87 additions & 38 deletions src/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (

"github.com/gabriel-vasile/mimetype"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"github.com/gorilla/websocket"
log "github.com/sirupsen/logrus"

"github.com/bbernhard/signal-cli-rest-api/client"
utils "github.com/bbernhard/signal-cli-rest-api/utils"
Expand All @@ -29,23 +29,23 @@ const (

type GroupPermissions struct {
AddMembers string `json:"add_members" enums:"only-admins,every-member"`
EditGroup string `json:"edit_group" enums:"only-admins,every-member"`
EditGroup string `json:"edit_group" enums:"only-admins,every-member"`
}

type CreateGroupRequest struct {
Name string `json:"name"`
Members []string `json:"members"`
Description string `json:"description"`
Permissions GroupPermissions `json:"permissions"`
GroupLinkState string `json:"group_link" enums:"disabled,enabled,enabled-with-approval"`
Name string `json:"name"`
Members []string `json:"members"`
Description string `json:"description"`
Permissions GroupPermissions `json:"permissions"`
GroupLinkState string `json:"group_link" enums:"disabled,enabled,enabled-with-approval"`
}

type LoggingConfiguration struct {
Level string `json:"Level"`
Level string `json:"Level"`
}

type Configuration struct {
Logging LoggingConfiguration `json:"logging"`
Logging LoggingConfiguration `json:"logging"`
}

type RegisterNumberRequest struct {
Expand Down Expand Up @@ -76,7 +76,11 @@ type Error struct {
Msg string `json:"error"`
}


type UpdateContactRequest struct {
Name string `json:"name"`
ExpirationTime int `json:"expiration_time"`
Recipient string `json:"recipient"`
}

type CreateGroupResponse struct {
Id string `json:"id"`
Expand All @@ -102,12 +106,12 @@ var connectionUpgrader = websocket.Upgrader{
}

type Api struct {
signalClient *client.SignalClient
signalClient *client.SignalClient
}

func NewApi(signalClient *client.SignalClient) *Api {
return &Api{
signalClient: signalClient,
signalClient: signalClient,
}
}

Expand Down Expand Up @@ -279,7 +283,6 @@ func (a *Api) SendV2(c *gin.Context) {
c.JSON(201, SendMessageResponse{Timestamp: strconv.FormatInt((*timestamps)[0].Timestamp, 10)})
}


func (a *Api) handleSignalReceive(ws *websocket.Conn, number string) {
for {
data, err := a.signalClient.Receive(number, 0)
Expand Down Expand Up @@ -400,7 +403,7 @@ func (a *Api) CreateGroup(c *gin.Context) {
}

if req.GroupLinkState != "" && !utils.StringInSlice(req.GroupLinkState, []string{"enabled", "enabled-with-approval", "disabled"}) {
c.JSON(400, Error{Msg: "Invalid group link provided - only 'enabled', 'enabled-with-approval' and 'disabled' allowed!" })
c.JSON(400, Error{Msg: "Invalid group link provided - only 'enabled', 'enabled-with-approval' and 'disabled' allowed!"})
return
}

Expand Down Expand Up @@ -465,6 +468,52 @@ func (a *Api) GetGroup(c *gin.Context) {
}
}

// @Summary Update contact.
// @Tags Contact
// @Description Update message expiration time for a user.
// @Accept json
// @Produce json
// @Success 201 {string} string "OK"
// @Failure 400 {object} Error
// @Param data body UpdateContactRequest true "Input Data"
// @Router /v1/contact/{number} [post]
func (a *Api) Contact(c *gin.Context) {
var req UpdateContactRequest
number := c.Param("number")
err := c.BindJSON(&req)
if err != nil {
c.JSON(400, gin.H{"error": "Couldn't process request - invalid request"})
log.Error(err.Error())
return
}

if len(number) == 0 {
c.JSON(400, gin.H{"error": "Couldn't process request - please provide the recipients number"})
return
}

if len(req.Name) == 0 {
c.JSON(400, gin.H{"error": "Couldn't process request - please provide the recipients name"})
return
}

if len(req.Recipient) == 0 {
c.JSON(400, gin.H{"error": "Couldn't process request - please provide the recipients number"})
return
}

if !(req.ExpirationTime >= 0) {
c.JSON(400, gin.H{"error": "Couldn't process request - please provide an expiry time in seconds"})
return
}

err = a.signalClient.UpdateContact(number, req.Recipient, req.Name, req.ExpirationTime)
if err != nil {
c.JSON(400, Error{Msg: err.Error()})
return
}
}

// @Summary Delete a Signal Group.
// @Tags Groups
// @Description Delete the specified Signal Group.
Expand Down Expand Up @@ -553,18 +602,18 @@ func (a *Api) RemoveAttachment(c *gin.Context) {
err := a.signalClient.RemoveAttachment(attachment)
if err != nil {
switch err.(type) {
case *client.InvalidNameError:
c.JSON(400, Error{Msg: err.Error()})
return
case *client.NotFoundError:
c.JSON(404, Error{Msg: err.Error()})
return
case *client.InternalError:
c.JSON(500, Error{Msg: err.Error()})
return
default:
c.JSON(500, Error{Msg: err.Error()})
return
case *client.InvalidNameError:
c.JSON(400, Error{Msg: err.Error()})
return
case *client.NotFoundError:
c.JSON(404, Error{Msg: err.Error()})
return
case *client.InternalError:
c.JSON(500, Error{Msg: err.Error()})
return
default:
c.JSON(500, Error{Msg: err.Error()})
return
}
}

Expand All @@ -585,18 +634,18 @@ func (a *Api) ServeAttachment(c *gin.Context) {
attachmentBytes, err := a.signalClient.GetAttachment(attachment)
if err != nil {
switch err.(type) {
case *client.InvalidNameError:
c.JSON(400, Error{Msg: err.Error()})
return
case *client.NotFoundError:
c.JSON(404, Error{Msg: err.Error()})
return
case *client.InternalError:
c.JSON(500, Error{Msg: err.Error()})
return
default:
c.JSON(500, Error{Msg: err.Error()})
return
case *client.InvalidNameError:
c.JSON(400, Error{Msg: err.Error()})
return
case *client.NotFoundError:
c.JSON(404, Error{Msg: err.Error()})
return
case *client.InternalError:
c.JSON(500, Error{Msg: err.Error()})
return
default:
c.JSON(500, Error{Msg: err.Error()})
return
}
}

Expand Down
14 changes: 11 additions & 3 deletions src/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
Expand All @@ -14,9 +15,10 @@ import (
"strings"
"time"

"github.com/cyphar/filepath-securejoin"
securejoin "github.com/cyphar/filepath-securejoin"
"github.com/gabriel-vasile/mimetype"
"github.com/h2non/filetype"

//"github.com/sourcegraph/jsonrpc2"//"net/rpc/jsonrpc"
log "github.com/sirupsen/logrus"

Expand Down Expand Up @@ -470,6 +472,7 @@ func (s *SignalClient) RegisterNumber(number string, useVoice bool, captcha stri
if s.signalCliMode == JsonRpc {
return errors.New(endpointNotSupportedInJsonRpcMode)
}

command := []string{"--config", s.signalCliConfig, "-u", number, "register"}

if useVoice {
Expand Down Expand Up @@ -613,8 +616,8 @@ func (s *SignalClient) CreateGroup(number string, name string, members []string,
}

type Response struct {
GroupId string `json:"groupId"`
Timestamp int64 `json:"timestamp"`
GroupId string `json:"groupId"`
Timestamp int64 `json:"timestamp"`
}
var resp Response
json.Unmarshal([]byte(rawData), &resp)
Expand Down Expand Up @@ -729,6 +732,11 @@ func (s *SignalClient) DeleteGroup(number string, groupId string) error {
return err
}

func (s *SignalClient) UpdateContact(number string, recipient string, name string, expiration_time int) error {
_, err := runSignalCli(true, []string{"--config", s.signalCliConfig, "-u", number, "updateContact", recipient, "-n", name, "-e", fmt.Sprintf("%v", expiration_time)}, "", s.signalCliMode)
return err
}

func (s *SignalClient) GetQrCodeLink(deviceName string) ([]byte, error) {
if s.signalCliMode == JsonRpc {
return []byte{}, errors.New(endpointNotSupportedInJsonRpcMode)
Expand Down
14 changes: 14 additions & 0 deletions src/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1064,6 +1064,20 @@ var doc = `{
}
}
},
"api.UpdateContactRequest": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"recipient": {
"type": "string"
},
"expiration_time": {
"type": "integer"
}
}
},
"api.TrustIdentityRequest": {
"type": "object",
"properties": {
Expand Down
47 changes: 47 additions & 0 deletions src/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,53 @@
}
}
},
"/v1/contact/{number}": {
"post": {
"description": "Update Contact",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Update Contact"
],
"summary": "Update contact expiration",
"parameters": [
{
"type": "string",
"description": "Registered Phone Number",
"name": "number",
"in": "path",
"required": true
},
{
"description": "Input Data",
"name": "data",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/api.UpdateContactRequest"
}
}
],
"responses": {
"201": {
"description": "OK",
"schema": {
"type": "string"
}
},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/api.Error"
}
}
}
}
},
"definitions": {
"api.Configuration": {
"type": "object",
Expand Down
11 changes: 11 additions & 0 deletions src/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,17 @@ definitions:
type: string
type: array
type: object
api.UpdateContactRequest:
properties:
number:
type: string
name:
type: string
expiration_time:
type: intteger
recipient:
type: string
type: object
api.TrustIdentityRequest:
properties:
verified_safety_number:
Expand Down
1 change: 1 addition & 0 deletions src/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpO
github.com/json-iterator/go v1.1.5/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/konsorten/go-windows-terminal-sequences v1.0.3 h1:CE8S1cTafDpPvMhIxNJKvHsGVBgn1xWYf1NbHQhywc8=
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
Expand Down
12 changes: 6 additions & 6 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ func main() {
}
}


jsonRpc2ClientConfigPathPath := *signalCliConfig + "/jsonrpc2.yml"
signalClient := client.NewSignalClient(*signalCliConfig, *attachmentTmpDir, *avatarTmpDir, signalCliMode, jsonRpc2ClientConfigPathPath)
err = signalClient.Init()
Expand Down Expand Up @@ -190,6 +189,10 @@ func main() {
identities.GET(":number", api.ListIdentities)
identities.PUT(":number/trust/:numbertotrust", api.TrustIdentity)
}
contacts := v1.Group("/contact")
{
contacts.POST(":number", api.Contact)
}
}

v2 := router.Group("/v2")
Expand Down Expand Up @@ -222,7 +225,7 @@ func main() {
filename := filepath.Base(path)
if strings.HasPrefix(filename, "+") && info.Mode().IsRegular() {
log.Debug("AUTO_RECEIVE_SCHEDULE: Calling receive for number ", filename)
resp, err := http.Get("http://127.0.0.1:" + port + "/v1/receive/"+filename)
resp, err := http.Get("http://127.0.0.1:" + port + "/v1/receive/" + filename)
if err != nil {
log.Error("AUTO_RECEIVE_SCHEDULE: Couldn't call receive for number ", filename, ": ", err.Error())
}
Expand All @@ -235,7 +238,7 @@ func main() {
}

type ReceiveResponse struct {
Error string `json:"error"`
Error string `json:"error"`
}
var receiveResponse ReceiveResponse
err = json.Unmarshal(jsonResp, &receiveResponse)
Expand All @@ -258,8 +261,5 @@ func main() {
c.Start()
}


router.Run()
}