MM-14675: Eliminates implicit GroupChannel/GroupTeam associations. (#13199)
* MM-14675: Upserts GroupTeam when GroupChannel is linked. MM-14675: Delete all associated GroupChannels when deleting a GroupTeam. MM-14675: Using replica DB where possible. MM-14675: Updates create method used in tests. * MM-14675: Removes unnecessary DB retrieval of GroupSyncable record.
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
0ffe199928
Коммит
75ffd0b69e
76
app/group.go
76
app/group.go
@@ -64,8 +64,42 @@ func (a *App) DeleteGroupMember(groupID string, userID string) (*model.GroupMemb
|
||||
return a.Srv.Store.Group().DeleteMember(groupID, userID)
|
||||
}
|
||||
|
||||
func (a *App) CreateGroupSyncable(groupSyncable *model.GroupSyncable) (*model.GroupSyncable, *model.AppError) {
|
||||
return a.Srv.Store.Group().CreateGroupSyncable(groupSyncable)
|
||||
func (a *App) UpsertGroupSyncable(groupSyncable *model.GroupSyncable) (*model.GroupSyncable, *model.AppError) {
|
||||
gs, err := a.Srv.Store.Group().GetGroupSyncable(groupSyncable.GroupId, groupSyncable.SyncableId, groupSyncable.Type)
|
||||
if err != nil && err.Id != "store.sql_group.no_rows" {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if gs == nil {
|
||||
gs, err = a.Srv.Store.Group().CreateGroupSyncable(groupSyncable)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
gs, err = a.Srv.Store.Group().UpdateGroupSyncable(groupSyncable)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
// if the type is channel, then upsert the associated GroupTeam [MM-14675]
|
||||
if gs.Type == model.GroupSyncableTypeChannel {
|
||||
channel, err := a.Srv.Store.Channel().Get(gs.SyncableId, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, err = a.UpsertGroupSyncable(&model.GroupSyncable{
|
||||
GroupId: gs.GroupId,
|
||||
SyncableId: channel.TeamId,
|
||||
Type: model.GroupSyncableTypeTeam,
|
||||
AutoAdd: gs.AutoAdd,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return gs, nil
|
||||
}
|
||||
|
||||
func (a *App) GetGroupSyncable(groupID string, syncableID string, syncableType model.GroupSyncableType) (*model.GroupSyncable, *model.AppError) {
|
||||
@@ -77,11 +111,45 @@ func (a *App) GetGroupSyncables(groupID string, syncableType model.GroupSyncable
|
||||
}
|
||||
|
||||
func (a *App) UpdateGroupSyncable(groupSyncable *model.GroupSyncable) (*model.GroupSyncable, *model.AppError) {
|
||||
return a.Srv.Store.Group().UpdateGroupSyncable(groupSyncable)
|
||||
var gs *model.GroupSyncable
|
||||
var err *model.AppError
|
||||
|
||||
if groupSyncable.DeleteAt == 0 {
|
||||
// updating a *deleted* GroupSyncable, so no need to ensure the GroupTeam is present (as done in the upsert)
|
||||
gs, err = a.Srv.Store.Group().UpdateGroupSyncable(groupSyncable)
|
||||
} else {
|
||||
// do an upsert to ensure that there's an associated GroupTeam
|
||||
gs, err = a.UpsertGroupSyncable(groupSyncable)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return gs, nil
|
||||
}
|
||||
|
||||
func (a *App) DeleteGroupSyncable(groupID string, syncableID string, syncableType model.GroupSyncableType) (*model.GroupSyncable, *model.AppError) {
|
||||
return a.Srv.Store.Group().DeleteGroupSyncable(groupID, syncableID, syncableType)
|
||||
gs, err := a.Srv.Store.Group().DeleteGroupSyncable(groupID, syncableID, syncableType)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// if a GroupTeam is being deleted delete all associated GroupChannels
|
||||
if gs.Type == model.GroupSyncableTypeTeam {
|
||||
allGroupChannels, err := a.Srv.Store.Group().GetAllGroupSyncablesByGroupId(gs.GroupId, model.GroupSyncableTypeChannel)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, groupChannel := range allGroupChannels {
|
||||
_, err = a.Srv.Store.Group().DeleteGroupSyncable(groupChannel.GroupId, groupChannel.SyncableId, groupChannel.Type)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return gs, nil
|
||||
}
|
||||
|
||||
func (a *App) TeamMembersToAdd(since int64) ([]*model.UserTeamIDPair, *model.AppError) {
|
||||
|
||||
@@ -131,19 +131,30 @@ func TestDeleteGroupMember(t *testing.T) {
|
||||
require.Nil(t, groupMember)
|
||||
}
|
||||
|
||||
func TestCreateGroupSyncable(t *testing.T) {
|
||||
func TestUpsertGroupSyncable(t *testing.T) {
|
||||
th := Setup(t).InitBasic()
|
||||
defer th.TearDown()
|
||||
group := th.CreateGroup()
|
||||
groupSyncable := model.NewGroupTeam(group.Id, th.BasicTeam.Id, false)
|
||||
|
||||
gs, err := th.App.CreateGroupSyncable(groupSyncable)
|
||||
gs, err := th.App.UpsertGroupSyncable(groupSyncable)
|
||||
require.Nil(t, err)
|
||||
require.NotNil(t, gs)
|
||||
|
||||
gs, err = th.App.CreateGroupSyncable(groupSyncable)
|
||||
require.NotNil(t, err)
|
||||
require.Nil(t, gs)
|
||||
// can update again without error
|
||||
gs, err = th.App.UpsertGroupSyncable(groupSyncable)
|
||||
require.Nil(t, err)
|
||||
require.NotNil(t, gs)
|
||||
|
||||
gs, err = th.App.DeleteGroupSyncable(gs.GroupId, gs.SyncableId, gs.Type)
|
||||
require.Nil(t, err)
|
||||
require.NotEqual(t, int64(0), gs.DeleteAt)
|
||||
|
||||
// Un-deleting works
|
||||
gs.DeleteAt = 0
|
||||
gs, err = th.App.UpsertGroupSyncable(gs)
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, int64(0), gs.DeleteAt)
|
||||
}
|
||||
|
||||
func TestGetGroupSyncable(t *testing.T) {
|
||||
@@ -152,7 +163,7 @@ func TestGetGroupSyncable(t *testing.T) {
|
||||
group := th.CreateGroup()
|
||||
groupSyncable := model.NewGroupTeam(group.Id, th.BasicTeam.Id, false)
|
||||
|
||||
gs, err := th.App.CreateGroupSyncable(groupSyncable)
|
||||
gs, err := th.App.UpsertGroupSyncable(groupSyncable)
|
||||
require.Nil(t, err)
|
||||
require.NotNil(t, gs)
|
||||
|
||||
@@ -169,7 +180,7 @@ func TestGetGroupSyncables(t *testing.T) {
|
||||
// Create a group team
|
||||
groupSyncable := model.NewGroupTeam(group.Id, th.BasicTeam.Id, false)
|
||||
|
||||
gs, err := th.App.CreateGroupSyncable(groupSyncable)
|
||||
gs, err := th.App.UpsertGroupSyncable(groupSyncable)
|
||||
require.Nil(t, err)
|
||||
require.NotNil(t, gs)
|
||||
|
||||
@@ -185,7 +196,7 @@ func TestDeleteGroupSyncable(t *testing.T) {
|
||||
group := th.CreateGroup()
|
||||
groupChannel := model.NewGroupChannel(group.Id, th.BasicChannel.Id, false)
|
||||
|
||||
gs, err := th.App.CreateGroupSyncable(groupChannel)
|
||||
gs, err := th.App.UpsertGroupSyncable(groupChannel)
|
||||
require.Nil(t, err)
|
||||
require.NotNil(t, gs)
|
||||
|
||||
@@ -211,7 +222,7 @@ func TestGetGroupsByChannel(t *testing.T) {
|
||||
Type: model.GroupSyncableTypeChannel,
|
||||
}
|
||||
|
||||
gs, err := th.App.CreateGroupSyncable(groupSyncable)
|
||||
gs, err := th.App.UpsertGroupSyncable(groupSyncable)
|
||||
require.Nil(t, err)
|
||||
require.NotNil(t, gs)
|
||||
|
||||
@@ -244,7 +255,7 @@ func TestGetGroupsByTeam(t *testing.T) {
|
||||
Type: model.GroupSyncableTypeTeam,
|
||||
}
|
||||
|
||||
gs, err := th.App.CreateGroupSyncable(groupSyncable)
|
||||
gs, err := th.App.UpsertGroupSyncable(groupSyncable)
|
||||
require.Nil(t, err)
|
||||
require.NotNil(t, gs)
|
||||
|
||||
|
||||
@@ -275,7 +275,7 @@ func TestFilterOutOfChannelMentions(t *testing.T) {
|
||||
constrainedChannel, appErr = th.App.UpdateChannel(constrainedChannel)
|
||||
require.Nil(t, appErr)
|
||||
|
||||
_, appErr = th.App.CreateGroupSyncable(&model.GroupSyncable{
|
||||
_, appErr = th.App.UpsertGroupSyncable(&model.GroupSyncable{
|
||||
GroupId: group.Id,
|
||||
Type: model.GroupSyncableTypeChannel,
|
||||
SyncableId: constrainedChannel.Id,
|
||||
|
||||
@@ -74,17 +74,17 @@ func TestCreateDefaultMemberships(t *testing.T) {
|
||||
t.Errorf("test group not created: %s", err.Error())
|
||||
}
|
||||
|
||||
_, err = th.App.CreateGroupSyncable(model.NewGroupChannel(gleeGroup.Id, practiceChannel.Id, true))
|
||||
_, err = th.App.UpsertGroupSyncable(model.NewGroupChannel(gleeGroup.Id, practiceChannel.Id, true))
|
||||
if err != nil {
|
||||
t.Errorf("test groupchannel not created: %s", err.Error())
|
||||
}
|
||||
|
||||
scienceTeamGroupSyncable, err := th.App.CreateGroupSyncable(model.NewGroupTeam(scienceGroup.Id, nerdsTeam.Id, false))
|
||||
scienceTeamGroupSyncable, err := th.App.UpsertGroupSyncable(model.NewGroupTeam(scienceGroup.Id, nerdsTeam.Id, false))
|
||||
if err != nil {
|
||||
t.Errorf("test groupteam not created: %s", err.Error())
|
||||
}
|
||||
|
||||
scienceChannelGroupSyncable, err := th.App.CreateGroupSyncable(model.NewGroupChannel(scienceGroup.Id, experimentsChannel.Id, false))
|
||||
scienceChannelGroupSyncable, err := th.App.UpsertGroupSyncable(model.NewGroupChannel(scienceGroup.Id, experimentsChannel.Id, false))
|
||||
if err != nil {
|
||||
t.Errorf("test groupchannel not created: %s", err.Error())
|
||||
}
|
||||
@@ -363,9 +363,9 @@ func TestDeleteGroupMemberships(t *testing.T) {
|
||||
require.True(t, *channel.GroupConstrained)
|
||||
|
||||
// create groupteam and groupchannel
|
||||
_, err = th.App.CreateGroupSyncable(model.NewGroupTeam(group.Id, team.Id, true))
|
||||
_, err = th.App.UpsertGroupSyncable(model.NewGroupTeam(group.Id, team.Id, true))
|
||||
require.Nil(t, err)
|
||||
_, err = th.App.CreateGroupSyncable(model.NewGroupChannel(group.Id, channel.Id, true))
|
||||
_, err = th.App.UpsertGroupSyncable(model.NewGroupChannel(group.Id, channel.Id, true))
|
||||
require.Nil(t, err)
|
||||
|
||||
// verify the member count
|
||||
|
||||
Ссылка в новой задаче
Block a user