From 4f3ebf0bfcf28b6633ac06238a488e86ee9ee5eb Mon Sep 17 00:00:00 2001 From: Bernhard B Date: Tue, 26 Sep 2023 22:30:43 +0200 Subject: [PATCH] fixed bug in create group endpoint link & permissions weren't correctly set see #410 --- src/api/api.go | 35 +++++++++++++++++++------------ src/client/client.go | 50 +++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 69 insertions(+), 16 deletions(-) diff --git a/src/api/api.go b/src/api/api.go index c1ba06d..58deb60 100644 --- a/src/api/api.go +++ b/src/api/api.go @@ -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 { diff --git a/src/client/client.go b/src/client/client.go index 9171bfc..7ffab02 100644 --- a/src/client/client.go +++ b/src/client/client.go @@ -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"` @@ -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