mirror of
https://github.com/aljazceru/signal-cli-rest-api.git
synced 2025-12-19 23:54:22 +01:00
run gofmt
This commit is contained in:
107
src/api/api.go
107
src/api/api.go
@@ -6,22 +6,22 @@ import (
|
|||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
"path/filepath"
|
|
||||||
"net/http"
|
|
||||||
"strconv"
|
|
||||||
"io/ioutil"
|
|
||||||
|
|
||||||
|
"github.com/cyphar/filepath-securejoin"
|
||||||
|
"github.com/gabriel-vasile/mimetype"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
uuid "github.com/gofrs/uuid"
|
uuid "github.com/gofrs/uuid"
|
||||||
"github.com/h2non/filetype"
|
"github.com/h2non/filetype"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
qrcode "github.com/skip2/go-qrcode"
|
qrcode "github.com/skip2/go-qrcode"
|
||||||
"github.com/gabriel-vasile/mimetype"
|
|
||||||
"github.com/cyphar/filepath-securejoin"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const signalCliV2GroupError = "Cannot create a V2 group as self does not have a versioned profile"
|
const signalCliV2GroupError = "Cannot create a V2 group as self does not have a versioned profile"
|
||||||
@@ -29,38 +29,38 @@ const signalCliV2GroupError = "Cannot create a V2 group as self does not have a
|
|||||||
const groupPrefix = "group."
|
const groupPrefix = "group."
|
||||||
|
|
||||||
type GroupEntry struct {
|
type GroupEntry struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Id string `json:"id"`
|
Id string `json:"id"`
|
||||||
InternalId string `json:"internal_id"`
|
InternalId string `json:"internal_id"`
|
||||||
Members []string `json:"members"`
|
Members []string `json:"members"`
|
||||||
Blocked bool `json:"blocked"`
|
Blocked bool `json:"blocked"`
|
||||||
PendingInvites []string `json:"pending_invites"`
|
PendingInvites []string `json:"pending_invites"`
|
||||||
PendingRequests []string `json:"pending_requests"`
|
PendingRequests []string `json:"pending_requests"`
|
||||||
InviteLink string `json:"invite_link"`
|
InviteLink string `json:"invite_link"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type SignalCliGroupEntry struct {
|
type SignalCliGroupEntry struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Id string `json:"id"`
|
Id string `json:"id"`
|
||||||
IsMember bool `json:"isMember"`
|
IsMember bool `json:"isMember"`
|
||||||
IsBlocked bool `json:"isBlocked"`
|
IsBlocked bool `json:"isBlocked"`
|
||||||
Members []string `json:"members"`
|
Members []string `json:"members"`
|
||||||
PendingMembers []string `json:"pendingMembers"`
|
PendingMembers []string `json:"pendingMembers"`
|
||||||
RequestingMembers []string `json:"requestingMembers"`
|
RequestingMembers []string `json:"requestingMembers"`
|
||||||
GroupInviteLink string `json:"groupInviteLink"`
|
GroupInviteLink string `json:"groupInviteLink"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type IdentityEntry struct {
|
type IdentityEntry struct {
|
||||||
Number string `json:"number"`
|
Number string `json:"number"`
|
||||||
Status string `json:"status"`
|
Status string `json:"status"`
|
||||||
Fingerprint string `json:"fingerprint"`
|
Fingerprint string `json:"fingerprint"`
|
||||||
Added string `json:"added"`
|
Added string `json:"added"`
|
||||||
SafetyNumber string `json:"safety_number"`
|
SafetyNumber string `json:"safety_number"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type RegisterNumberRequest struct {
|
type RegisterNumberRequest struct {
|
||||||
UseVoice bool `json:"use_voice"`
|
UseVoice bool `json:"use_voice"`
|
||||||
Captcha string `json:"captcha"`
|
Captcha string `json:"captcha"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type VerifyNumberSettings struct {
|
type VerifyNumberSettings struct {
|
||||||
@@ -96,7 +96,7 @@ type CreateGroup struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type UpdateProfileRequest struct {
|
type UpdateProfileRequest struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Base64Avatar string `json:"base64_avatar"`
|
Base64Avatar string `json:"base64_avatar"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -233,9 +233,9 @@ func parseWhitespaceDelimitedKeyValueStringList(in string, keys []string) []map[
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
idx := strings.Index(temp, " " + key + ": ")
|
idx := strings.Index(temp, " "+key+": ")
|
||||||
pair := temp[:idx]
|
pair := temp[:idx]
|
||||||
value := strings.TrimPrefix(pair, key + ": ")
|
value := strings.TrimPrefix(pair, key+": ")
|
||||||
temp = strings.TrimLeft(temp[idx:], " "+key+": ")
|
temp = strings.TrimLeft(temp[idx:], " "+key+": ")
|
||||||
|
|
||||||
m[keys[i-1]] = value
|
m[keys[i-1]] = value
|
||||||
@@ -259,8 +259,8 @@ func getGroups(number string, signalCliConfig string) ([]GroupEntry, error) {
|
|||||||
|
|
||||||
err = json.Unmarshal([]byte(out), &signalCliGroupEntries)
|
err = json.Unmarshal([]byte(out), &signalCliGroupEntries)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return groupEntries, err
|
return groupEntries, err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, signalCliGroupEntry := range signalCliGroupEntries {
|
for _, signalCliGroupEntry := range signalCliGroupEntries {
|
||||||
var groupEntry GroupEntry
|
var groupEntry GroupEntry
|
||||||
@@ -326,14 +326,14 @@ func runSignalCli(wait bool, args []string, stdin string) (string, error) {
|
|||||||
type Api struct {
|
type Api struct {
|
||||||
signalCliConfig string
|
signalCliConfig string
|
||||||
attachmentTmpDir string
|
attachmentTmpDir string
|
||||||
avatarTmpDir string
|
avatarTmpDir string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewApi(signalCliConfig string, attachmentTmpDir string, avatarTmpDir string) *Api {
|
func NewApi(signalCliConfig string, attachmentTmpDir string, avatarTmpDir string) *Api {
|
||||||
return &Api{
|
return &Api{
|
||||||
signalCliConfig: signalCliConfig,
|
signalCliConfig: signalCliConfig,
|
||||||
attachmentTmpDir: attachmentTmpDir,
|
attachmentTmpDir: attachmentTmpDir,
|
||||||
avatarTmpDir: avatarTmpDir,
|
avatarTmpDir: avatarTmpDir,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -737,18 +737,18 @@ func (a *Api) GetQrCodeLink(c *gin.Context) {
|
|||||||
// @Router /v1/attachments [get]
|
// @Router /v1/attachments [get]
|
||||||
func (a *Api) GetAttachments(c *gin.Context) {
|
func (a *Api) GetAttachments(c *gin.Context) {
|
||||||
files := []string{}
|
files := []string{}
|
||||||
err := filepath.Walk(a.signalCliConfig + "/attachments/", func(path string, info os.FileInfo, err error) error {
|
err := filepath.Walk(a.signalCliConfig+"/attachments/", func(path string, info os.FileInfo, err error) error {
|
||||||
if info.IsDir() {
|
if info.IsDir() {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
files = append(files, filepath.Base(path))
|
files = append(files, filepath.Base(path))
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(500, Error{Msg: "Couldn't get list of attachments: " + err.Error()})
|
c.JSON(500, Error{Msg: "Couldn't get list of attachments: " + err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(200, files)
|
c.JSON(200, files)
|
||||||
}
|
}
|
||||||
@@ -763,7 +763,7 @@ func (a *Api) GetAttachments(c *gin.Context) {
|
|||||||
// @Router /v1/attachments/{attachment} [delete]
|
// @Router /v1/attachments/{attachment} [delete]
|
||||||
func (a *Api) RemoveAttachment(c *gin.Context) {
|
func (a *Api) RemoveAttachment(c *gin.Context) {
|
||||||
attachment := c.Param("attachment")
|
attachment := c.Param("attachment")
|
||||||
path, err := securejoin.SecureJoin(a.signalCliConfig + "/attachments/", attachment)
|
path, err := securejoin.SecureJoin(a.signalCliConfig+"/attachments/", attachment)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(400, Error{Msg: "Please provide a valid attachment name"})
|
c.JSON(400, Error{Msg: "Please provide a valid attachment name"})
|
||||||
return
|
return
|
||||||
@@ -783,7 +783,7 @@ func (a *Api) RemoveAttachment(c *gin.Context) {
|
|||||||
|
|
||||||
// @Summary Serve Attachment.
|
// @Summary Serve Attachment.
|
||||||
// @Tags Attachments
|
// @Tags Attachments
|
||||||
// @Description Serve the attachment with the given id
|
// @Description Serve the attachment with the given id
|
||||||
// @Produce json
|
// @Produce json
|
||||||
// @Success 200 {string} OK
|
// @Success 200 {string} OK
|
||||||
// @Failure 400 {object} Error
|
// @Failure 400 {object} Error
|
||||||
@@ -791,7 +791,7 @@ func (a *Api) RemoveAttachment(c *gin.Context) {
|
|||||||
// @Router /v1/attachments/{attachment} [get]
|
// @Router /v1/attachments/{attachment} [get]
|
||||||
func (a *Api) ServeAttachment(c *gin.Context) {
|
func (a *Api) ServeAttachment(c *gin.Context) {
|
||||||
attachment := c.Param("attachment")
|
attachment := c.Param("attachment")
|
||||||
path, err := securejoin.SecureJoin(a.signalCliConfig + "/attachments/", attachment)
|
path, err := securejoin.SecureJoin(a.signalCliConfig+"/attachments/", attachment)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(400, Error{Msg: "Please provide a valid attachment name"})
|
c.JSON(400, Error{Msg: "Please provide a valid attachment name"})
|
||||||
return
|
return
|
||||||
@@ -801,7 +801,7 @@ func (a *Api) ServeAttachment(c *gin.Context) {
|
|||||||
c.JSON(404, Error{Msg: "No attachment with that name found"})
|
c.JSON(404, Error{Msg: "No attachment with that name found"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
imgBytes, err := ioutil.ReadFile(path)
|
imgBytes, err := ioutil.ReadFile(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(500, Error{Msg: "Couldn't read attachment - please try again later"})
|
c.JSON(500, Error{Msg: "Couldn't read attachment - please try again later"})
|
||||||
@@ -825,7 +825,7 @@ func (a *Api) ServeAttachment(c *gin.Context) {
|
|||||||
|
|
||||||
// @Summary Update Profile.
|
// @Summary Update Profile.
|
||||||
// @Tags Profiles
|
// @Tags Profiles
|
||||||
// @Description Set your name and optional an avatar.
|
// @Description Set your name and optional an avatar.
|
||||||
// @Produce json
|
// @Produce json
|
||||||
// @Success 204 {string} OK
|
// @Success 204 {string} OK
|
||||||
// @Failure 400 {object} Error
|
// @Failure 400 {object} Error
|
||||||
@@ -853,7 +853,7 @@ func (a *Api) UpdateProfile(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
cmd := []string{"--config", a.signalCliConfig, "-u", number, "updateProfile", "--name", req.Name}
|
cmd := []string{"--config", a.signalCliConfig, "-u", number, "updateProfile", "--name", req.Name}
|
||||||
|
|
||||||
avatarTmpPaths := []string{}
|
avatarTmpPaths := []string{}
|
||||||
if req.Base64Avatar == "" {
|
if req.Base64Avatar == "" {
|
||||||
cmd = append(cmd, "--remove-avatar")
|
cmd = append(cmd, "--remove-avatar")
|
||||||
@@ -869,7 +869,7 @@ func (a *Api) UpdateProfile(c *gin.Context) {
|
|||||||
c.JSON(400, Error{Msg: "Couldn't decode base64 encoded avatar"})
|
c.JSON(400, Error{Msg: "Couldn't decode base64 encoded avatar"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fType, err := filetype.Get(avatarBytes)
|
fType, err := filetype.Get(avatarBytes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(400, Error{Msg: err.Error()})
|
c.JSON(400, Error{Msg: err.Error()})
|
||||||
@@ -923,7 +923,7 @@ func (a *Api) Health(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// @Summary List Identities
|
// @Summary List Identities
|
||||||
// @Tags Identities
|
// @Tags Identities
|
||||||
// @Description List all identities for the given number.
|
// @Description List all identities for the given number.
|
||||||
// @Produce json
|
// @Produce json
|
||||||
// @Success 200 {object} []IdentityEntry
|
// @Success 200 {object} []IdentityEntry
|
||||||
@@ -949,13 +949,12 @@ func (a *Api) ListIdentities(c *gin.Context) {
|
|||||||
numberAndTrustStatus := keyValuePair["NumberAndTrustStatus"]
|
numberAndTrustStatus := keyValuePair["NumberAndTrustStatus"]
|
||||||
numberAndTrustStatusSplitted := strings.Split(numberAndTrustStatus, ":")
|
numberAndTrustStatusSplitted := strings.Split(numberAndTrustStatus, ":")
|
||||||
|
|
||||||
|
|
||||||
identityEntry := IdentityEntry{Number: strings.Trim(numberAndTrustStatusSplitted[0], " "),
|
identityEntry := IdentityEntry{Number: strings.Trim(numberAndTrustStatusSplitted[0], " "),
|
||||||
Status: strings.Trim(numberAndTrustStatusSplitted[1], " "),
|
Status: strings.Trim(numberAndTrustStatusSplitted[1], " "),
|
||||||
Added: keyValuePair["Added"],
|
Added: keyValuePair["Added"],
|
||||||
Fingerprint: strings.Trim(keyValuePair["Fingerprint"], " "),
|
Fingerprint: strings.Trim(keyValuePair["Fingerprint"], " "),
|
||||||
SafetyNumber: strings.Trim(keyValuePair["Safety Number"], " "),
|
SafetyNumber: strings.Trim(keyValuePair["Safety Number"], " "),
|
||||||
}
|
}
|
||||||
identityEntries = append(identityEntries, identityEntry)
|
identityEntries = append(identityEntries, identityEntry)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user