diff --git a/api4/group_test.go b/api4/group_test.go index 24cfe6698d..726942328a 100644 --- a/api4/group_test.go +++ b/api4/group_test.go @@ -193,6 +193,7 @@ func TestLinkGroupChannel(t *testing.T) { groupTeam, response := th.Client.LinkGroupSyncable(g.Id, th.BasicChannel.Id, model.GroupSyncableTypeChannel, patch) assert.Equal(t, http.StatusCreated, response.StatusCode) + assert.Equal(t, th.BasicChannel.TeamId, groupTeam.TeamID) assert.NotNil(t, groupTeam) _, response = th.SystemAdminClient.UpdateChannelRoles(th.BasicChannel.Id, th.BasicUser.Id, "") @@ -623,6 +624,7 @@ func TestPatchGroupChannel(t *testing.T) { assert.Equal(t, g.Id, groupSyncable.GroupId) assert.Equal(t, th.BasicChannel.Id, groupSyncable.SyncableId) + assert.Equal(t, th.BasicChannel.TeamId, groupSyncable.TeamID) assert.Equal(t, model.GroupSyncableTypeChannel, groupSyncable.Type) patch.AutoAdd = model.NewBool(true) diff --git a/model/group_syncable.go b/model/group_syncable.go index 6a4d402371..eb3bdf0978 100644 --- a/model/group_syncable.go +++ b/model/group_syncable.go @@ -60,14 +60,14 @@ func (syncable *GroupSyncable) UnmarshalJSON(b []byte) error { if err != nil { return err } + var channelId string + var teamId string for key, value := range kvp { switch key { case "team_id": - syncable.SyncableId = value.(string) - syncable.Type = GroupSyncableTypeTeam + teamId = value.(string) case "channel_id": - syncable.SyncableId = value.(string) - syncable.Type = GroupSyncableTypeChannel + channelId = value.(string) case "group_id": syncable.GroupId = value.(string) case "auto_add": @@ -75,30 +75,40 @@ func (syncable *GroupSyncable) UnmarshalJSON(b []byte) error { default: } } + if channelId != "" { + syncable.TeamID = teamId + syncable.SyncableId = channelId + syncable.Type = GroupSyncableTypeChannel + } else { + syncable.SyncableId = teamId + syncable.Type = GroupSyncableTypeTeam + } return nil } func (syncable *GroupSyncable) MarshalJSON() ([]byte, error) { type Alias GroupSyncable - switch syncable.Type { case GroupSyncableTypeTeam: return json.Marshal(&struct { - TeamID string `json:"team_id"` - TeamDisplayName string `json:"team_display_name,omitempty"` - TeamType string `json:"team_type,omitempty"` + TeamID string `json:"team_id"` + TeamDisplayName string `json:"team_display_name,omitempty"` + TeamType string `json:"team_type,omitempty"` + Type GroupSyncableType `json:"type,omitempty"` *Alias }{ TeamDisplayName: syncable.TeamDisplayName, TeamType: syncable.TeamType, TeamID: syncable.SyncableId, + Type: syncable.Type, Alias: (*Alias)(syncable), }) case GroupSyncableTypeChannel: return json.Marshal(&struct { - ChannelID string `json:"channel_id"` - ChannelDisplayName string `json:"channel_display_name,omitempty"` - ChannelType string `json:"channel_type,omitempty"` + ChannelID string `json:"channel_id"` + ChannelDisplayName string `json:"channel_display_name,omitempty"` + ChannelType string `json:"channel_type,omitempty"` + Type GroupSyncableType `json:"type,omitempty"` TeamID string `json:"team_id,omitempty"` TeamDisplayName string `json:"team_display_name,omitempty"` @@ -109,6 +119,7 @@ func (syncable *GroupSyncable) MarshalJSON() ([]byte, error) { ChannelID: syncable.SyncableId, ChannelDisplayName: syncable.ChannelDisplayName, ChannelType: syncable.ChannelType, + Type: syncable.Type, TeamID: syncable.TeamID, TeamDisplayName: syncable.TeamDisplayName, diff --git a/store/sqlstore/group_store.go b/store/sqlstore/group_store.go index ad432270cb..d30a371b92 100644 --- a/store/sqlstore/group_store.go +++ b/store/sqlstore/group_store.go @@ -479,11 +479,13 @@ func (s *SqlGroupStore) CreateGroupSyncable(groupSyncable *model.GroupSyncable) insertErr = s.GetMaster().Insert(groupSyncableToGroupTeam(groupSyncable)) case model.GroupSyncableTypeChannel: - if _, err := s.Channel().Get(groupSyncable.SyncableId, false); err != nil { + var channel *model.Channel + channel, err := s.Channel().Get(groupSyncable.SyncableId, false) + if err != nil { return nil, err } - insertErr = s.GetMaster().Insert(groupSyncableToGroupChannel(groupSyncable)) + groupSyncable.TeamID = channel.TeamId default: return nil, fmt.Errorf("invalid GroupSyncableType: %s", groupSyncable.Type) } @@ -661,7 +663,16 @@ func (s *SqlGroupStore) UpdateGroupSyncable(groupSyncable *model.GroupSyncable) case model.GroupSyncableTypeTeam: _, err = s.GetMaster().Update(groupSyncableToGroupTeam(groupSyncable)) case model.GroupSyncableTypeChannel: + // We need to get the TeamId so redux can manage channels when teams are unlinked + var channel *model.Channel + channel, channelErr := s.Channel().Get(groupSyncable.SyncableId, false) + if channelErr != nil { + return nil, channelErr + } + _, err = s.GetMaster().Update(groupSyncableToGroupChannel(groupSyncable)) + + groupSyncable.TeamID = channel.TeamId default: return nil, fmt.Errorf("invalid GroupSyncableType: %s", groupSyncable.Type) }