Skip to content

Commit

Permalink
fixed bug in create group endpoint
Browse files Browse the repository at this point in the history
link & permissions weren't correctly set

see #410
  • Loading branch information
Bernhard B committed Sep 26, 2023
1 parent f21925e commit 4f3ebf0
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 16 deletions.
35 changes: 22 additions & 13 deletions src/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,24 +546,33 @@ func (a *Api) CreateGroup(c *gin.Context) {
return
}

if req.Permissions.AddMembers != "" && !utils.StringInSlice(req.Permissions.AddMembers, []string{"every-member", "only-admins"}) {
c.JSON(400, Error{Msg: "Invalid add members permission provided - only 'every-member' and 'only-admins' allowed!"})
return
}
editGroupPermission := client.DefaultGroupPermission
addMembersPermission := client.DefaultGroupPermission
groupLinkState := client.DefaultGroupLinkState

if req.Permissions.EditGroup != "" && !utils.StringInSlice(req.Permissions.EditGroup, []string{"every-member", "only-admins"}) {
c.JSON(400, Error{Msg: "Invalid edit group permissions provided - only 'every-member' and 'only-admins' allowed!"})
return
if req.Permissions.AddMembers != "" {
if !utils.StringInSlice(req.Permissions.AddMembers, []string{"every-member", "only-admins"}) {
c.JSON(400, Error{Msg: "Invalid add members permission provided - only 'every-member' and 'only-admins' allowed!"})
return
}
addMembersPermission = addMembersPermission.FromString(req.Permissions.AddMembers)
}

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!"})
return
if req.Permissions.EditGroup != "" {
if !utils.StringInSlice(req.Permissions.EditGroup, []string{"every-member", "only-admins"}) {
c.JSON(400, Error{Msg: "Invalid edit group permissions provided - only 'every-member' and 'only-admins' allowed!"})
return
}
editGroupPermission = editGroupPermission.FromString(req.Permissions.EditGroup)
}

editGroupPermission := client.DefaultGroupPermission
addMembersPermission := client.DefaultGroupPermission
groupLinkState := client.DefaultGroupLinkState
if req.GroupLinkState != "" {
if !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!"})
return
}
groupLinkState = groupLinkState.FromString(req.GroupLinkState)
}

groupId, err := a.signalClient.CreateGroup(number, req.Name, req.Members, req.Description, editGroupPermission, addMembersPermission, groupLinkState)
if err != nil {
Expand Down
50 changes: 47 additions & 3 deletions src/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,37 @@ const (
)

func (g GroupPermission) String() string {
return []string{"", "default", "every-member", "only-admins"}[g]
return []string{"", "every-member", "only-admins"}[g]
}

func (g GroupPermission) FromString(input string) GroupPermission {
if input == "every-member" {
return EveryMember
}
if input == "only-admins" {
return OnlyAdmins
}
return DefaultGroupPermission
}

func (g GroupLinkState) String() string {
return []string{"", "enabled", "enabled-with-approval", "disabled"}[g]
}

func (g GroupLinkState) FromString(input string) GroupLinkState {
if input == "enabled" {
return Enabled
}
if input == "enabled-with-approval" {
return EnabledWithApproval
}
if input == "disabled" {
return Disabled
}

return DefaultGroupLinkState
}

type MessageMention struct {
Start int64 `json:"start"`
Length int64 `json:"length"`
Expand Down Expand Up @@ -662,11 +686,31 @@ func (s *SignalClient) CreateGroup(number string, name string, members []string,
var internalGroupId string
if s.signalCliMode == JsonRpc {
type Request struct {
Name string `json:"name"`
Members []string `json:"members"`
Name string `json:"name"`
Members []string `json:"members"`
Link string `json:"link,omitempty"`
Description string `json:"description,omitempty"`
EditGroupPermissions string `json:"setPermissionEditDetails,omitempty"`
AddMembersPermissions string `json:"setPermissionAddMember,omitempty"`
}
request := Request{Name: name, Members: members}

if groupLinkState != DefaultGroupLinkState {
request.Link = groupLinkState.String()
}

if description != "" {
request.Description = description
}

if editGroupPermission != DefaultGroupPermission {
request.EditGroupPermissions = editGroupPermission.String()
}

if addMembersPermission != DefaultGroupPermission {
request.AddMembersPermissions = addMembersPermission.String()
}

jsonRpc2Client, err := s.getJsonRpc2Client(number)
if err != nil {
return "", err
Expand Down

0 comments on commit 4f3ebf0

Please sign in to comment.