Skip to content

Commit

Permalink
Delegate adding a member to a group
Browse files Browse the repository at this point in the history
  • Loading branch information
nono committed Feb 22, 2024
1 parent 00c6c45 commit cc0faa3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
28 changes: 25 additions & 3 deletions model/sharing/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,14 @@ func UpdateGroups(inst *instance.Instance, msg job.ShareGroupMessage) error {
for _, added := range msg.GroupsAdded {
for idx, group := range s.Groups {
if group.ID == added {
if err := s.AddMemberToGroup(inst, idx, contact); err != nil {
errm = multierror.Append(errm, err)
if s.Owner {
if err := s.AddMemberToGroup(inst, idx, contact); err != nil {
errm = multierror.Append(errm, err)
}
} else {
if err := s.DelegateAddMemberToGroup(inst, idx, contact); err != nil {
errm = multierror.Append(errm, err)
}
}
}
}
Expand All @@ -136,7 +142,7 @@ func UpdateGroups(inst *instance.Instance, msg job.ShareGroupMessage) error {
return errm
}

// AddMemberToGroup adds a contact to a sharing via a group.
// AddMemberToGroup adds a contact to a sharing via a group (on the owner).
func (s *Sharing) AddMemberToGroup(inst *instance.Instance, groupIndex int, contact *contact.Contact) error {
readOnly := s.Groups[groupIndex].ReadOnly
m, err := buildMemberFromContact(contact, readOnly)
Expand Down Expand Up @@ -168,6 +174,22 @@ func (s *Sharing) AddMemberToGroup(inst *instance.Instance, groupIndex int, cont
return nil
}

// DelegateAddMemberToGroup adds a contact to a sharing via a group (on a recipient).
func (s *Sharing) DelegateAddMemberToGroup(inst *instance.Instance, groupIndex int, contact *contact.Contact) error {
readOnly := s.Groups[groupIndex].ReadOnly
m, err := buildMemberFromContact(contact, readOnly)
if err != nil {
return err
}
m.OnlyInGroups = true
m.Groups = []int{groupIndex}
api := &APIDelegateAddContacts{
sid: s.ID(),
members: []Member{m},
}
return s.SendDelegated(inst, api)
}

// RemoveMemberFromGroup removes a member of a group.
func (s *Sharing) RemoveMemberFromGroup(inst *instance.Instance, groupIndex int, contact *contact.Contact) error {
var email string
Expand Down
6 changes: 6 additions & 0 deletions model/sharing/member.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,12 @@ func (s *Sharing) DelegateAddContactsAndGroups(inst *instance.Instance, groupIDs
}
}

return s.SendDelegated(inst, api)
}

// SendDelegated calls the delegated endpoint on the sharer to adds
// contacts/groups.
func (s *Sharing) SendDelegated(inst *instance.Instance, api *APIDelegateAddContacts) error {
data, err := jsonapi.MarshalObject(api)
if err != nil {
return err
Expand Down

0 comments on commit cc0faa3

Please sign in to comment.