Skip to content

Commit

Permalink
added possibility to update the group description & the avatar
Browse files Browse the repository at this point in the history
see #417
  • Loading branch information
Bernhard B committed Oct 3, 2023
1 parent 2474238 commit b39980e
Show file tree
Hide file tree
Showing 5 changed files with 245 additions and 4 deletions.
16 changes: 15 additions & 1 deletion src/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ type CreateGroupRequest struct {
GroupLinkState string `json:"group_link" enums:"disabled,enabled,enabled-with-approval"`
}

type UpdateGroupRequest struct {
Base64Avatar *string `json:"base64_avatar"`
Description *string `json:"description"`
}

type ChangeGroupMembersRequest struct {
Members []string `json:"members"`
}
Expand Down Expand Up @@ -1259,6 +1264,7 @@ func (a *Api) QuitGroup(c *gin.Context) {
// @Failure 400 {object} Error
// @Param number path string true "Registered Phone Number"
// @Param groupid path string true "Group ID"
// @Param data body UpdateGroupRequest true "Input Data"
// @Router /v1/groups/{number}/{groupid} [put]
func (a *Api) UpdateGroup(c *gin.Context) {
number := c.Param("number")
Expand All @@ -1274,7 +1280,15 @@ func (a *Api) UpdateGroup(c *gin.Context) {
return
}

err = a.signalClient.UpdateGroup(number, internalGroupId)
var req UpdateGroupRequest
err = c.BindJSON(&req)
if err != nil {
c.JSON(400, Error{Msg: "Couldn't process request - invalid request"})
log.Error(err.Error())
return
}

err = a.signalClient.UpdateGroup(number, internalGroupId, req.Base64Avatar, req.Description)
if err != nil {
c.JSON(400, Error{Msg: err.Error()})
return
Expand Down
65 changes: 62 additions & 3 deletions src/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1282,21 +1282,80 @@ func (s *SignalClient) QuitGroup(number string, groupId string) error {
return err
}

func (s *SignalClient) UpdateGroup(number string, groupId string) error {
func (s *SignalClient) UpdateGroup(number string, groupId string, base64Avatar *string, groupDescription *string) error {
var err error
var avatarTmpPath string = ""
if base64Avatar != nil {
u, err := uuid.NewV4()
if err != nil {
return err
}

avatarBytes, err := base64.StdEncoding.DecodeString(*base64Avatar)
if err != nil {
return errors.New("Couldn't decode base64 encoded avatar: " + err.Error())
}

fType, err := filetype.Get(avatarBytes)
if err != nil {
return err
}

avatarTmpPath = s.avatarTmpDir + u.String() + "." + fType.Extension

f, err := os.Create(avatarTmpPath)
if err != nil {
return err
}
defer f.Close()

if _, err := f.Write(avatarBytes); err != nil {
cleanupTmpFiles([]string{avatarTmpPath})
return err
}
if err := f.Sync(); err != nil {
cleanupTmpFiles([]string{avatarTmpPath})
return err
}
f.Close()
}

if s.signalCliMode == JsonRpc {
type Request struct {
GroupId string `json:"groupId"`
GroupId string `json:"groupId"`
Avatar string `json:"avatar,omitempty"`
Description *string `json:"description,omitempty"`
}
request := Request{GroupId: groupId}

if base64Avatar != nil {
request.Avatar = avatarTmpPath
}

request.Description = groupDescription


jsonRpc2Client, err := s.getJsonRpc2Client(number)
if err != nil {
return err
}
_, err = jsonRpc2Client.getRaw("updateGroup", request)
} else {
_, err = s.cliClient.Execute(true, []string{"--config", s.signalCliConfig, "-a", number, "updateGroup", "-g", groupId}, "")
cmd := []string{"--config", s.signalCliConfig, "-a", number, "updateGroup", "-g", groupId}
if base64Avatar != nil {
cmd = append(cmd, []string{"-a", avatarTmpPath}...)
}

if groupDescription != nil {
cmd = append(cmd, []string{"-d", *groupDescription}...)
}
_, err = s.cliClient.Execute(true, cmd, "")
}

if avatarTmpPath != "" {
cleanupTmpFiles([]string{avatarTmpPath})
}

return err
}

Expand Down
63 changes: 63 additions & 0 deletions src/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,58 @@ var doc = `{
}
}
},
"put": {
"description": "Update the state of a Signal Group.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Groups"
],
"summary": "Update the state of a Signal Group.",
"parameters": [
{
"type": "string",
"description": "Registered Phone Number",
"name": "number",
"in": "path",
"required": true
},
{
"type": "string",
"description": "Group ID",
"name": "groupid",
"in": "path",
"required": true
},
{
"description": "Input Data",
"name": "data",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/api.UpdateGroupRequest"
}
}
],
"responses": {
"204": {
"description": "No Content",
"schema": {
"type": "string"
}
},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/api.Error"
}
}
}
},
"delete": {
"description": "Delete the specified Signal Group.",
"consumes": [
Expand Down Expand Up @@ -1854,6 +1906,17 @@ var doc = `{
}
}
},
"api.UpdateGroupRequest": {
"type": "object",
"properties": {
"base64_avatar": {
"type": "string"
},
"description": {
"type": "string"
}
}
},
"api.UpdateProfileRequest": {
"type": "object",
"properties": {
Expand Down
63 changes: 63 additions & 0 deletions src/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,58 @@
}
}
},
"put": {
"description": "Update the state of a Signal Group.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Groups"
],
"summary": "Update the state of a Signal Group.",
"parameters": [
{
"type": "string",
"description": "Registered Phone Number",
"name": "number",
"in": "path",
"required": true
},
{
"type": "string",
"description": "Group ID",
"name": "groupid",
"in": "path",
"required": true
},
{
"description": "Input Data",
"name": "data",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/api.UpdateGroupRequest"
}
}
],
"responses": {
"204": {
"description": "No Content",
"schema": {
"type": "string"
}
},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/api.Error"
}
}
}
},
"delete": {
"description": "Delete the specified Signal Group.",
"consumes": [
Expand Down Expand Up @@ -1838,6 +1890,17 @@
}
}
},
"api.UpdateGroupRequest": {
"type": "object",
"properties": {
"base64_avatar": {
"type": "string"
},
"description": {
"type": "string"
}
}
},
"api.UpdateProfileRequest": {
"type": "object",
"properties": {
Expand Down
42 changes: 42 additions & 0 deletions src/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,13 @@ definitions:
recipient:
type: string
type: object
api.UpdateGroupRequest:
properties:
base64_avatar:
type: string
description:
type: string
type: object
api.UpdateProfileRequest:
properties:
base64_avatar:
Expand Down Expand Up @@ -634,6 +641,41 @@ paths:
summary: List a Signal Group.
tags:
- Groups
put:
consumes:
- application/json
description: Update the state of a Signal Group.
parameters:
- description: Registered Phone Number
in: path
name: number
required: true
type: string
- description: Group ID
in: path
name: groupid
required: true
type: string
- description: Input Data
in: body
name: data
required: true
schema:
$ref: '#/definitions/api.UpdateGroupRequest'
produces:
- application/json
responses:
"204":
description: No Content
schema:
type: string
"400":
description: Bad Request
schema:
$ref: '#/definitions/api.Error'
summary: Update the state of a Signal Group.
tags:
- Groups
/v1/groups/{number}/{groupid}/admins:
delete:
consumes:
Expand Down

0 comments on commit b39980e

Please sign in to comment.