From 5872bf9c2f9b81c7aad761d40a6970f6267f1424 Mon Sep 17 00:00:00 2001 From: Martin Kraft Date: Mon, 30 Jul 2018 15:06:08 -0400 Subject: Pr 9039 (#9187) * MM-11065: Allow to search and get archived channels from the API * Fixing more tests * Add some unit tests * Add includeDeleted parameter to session permissions check function * More test fixing * Adding archive channels list in channels search * Add restriction for archived channel edition * Reverting permissions checks modification * Changed the query parameter to include_deleted * Enable search archive channels as true by default * Adding tests for verify search on deleted channels * Allowing to override archive channels during the imports * Fixed test * Search in archive channels from the API must be explicitly requested * Removing includeDeleted parameter from GetChannelByName and GetChannelByNameForTeam * Back to ViewArchivedChannels config * Fixing tests * Reverting GetChannelByName parameter * Add include deleted parameter on GetChannel functions in plugins api * Fixing tests --- app/authorization.go | 2 +- app/channel.go | 53 ++++++++++++++++++++++++++++++++++++--------------- app/channel_test.go | 2 +- app/command_invite.go | 2 +- app/import.go | 4 ++-- app/import_test.go | 12 ++++++------ app/plugin_api.go | 8 ++++---- app/post.go | 8 +++++--- app/team.go | 2 +- app/user.go | 2 +- app/web_conn.go | 2 +- 11 files changed, 61 insertions(+), 36 deletions(-) (limited to 'app') diff --git a/app/authorization.go b/app/authorization.go index 3de50e27b..0955cb90c 100644 --- a/app/authorization.go +++ b/app/authorization.go @@ -37,7 +37,7 @@ func (a *App) SessionHasPermissionToChannel(session model.Session, channelId str return false } - cmc := a.Srv.Store.Channel().GetAllChannelMembersForUser(session.UserId, true) + cmc := a.Srv.Store.Channel().GetAllChannelMembersForUser(session.UserId, true, true) var channelRoles []string if cmcresult := <-cmc; cmcresult.Err == nil { diff --git a/app/channel.go b/app/channel.go index 619cc09eb..830dbb8b7 100644 --- a/app/channel.go +++ b/app/channel.go @@ -393,7 +393,7 @@ func (a *App) GetGroupChannel(userIds []string) (*model.Channel, *model.AppError return nil, model.NewAppError("GetGroupChannel", "api.channel.create_group.bad_user.app_error", nil, "user_ids="+model.ArrayToJson(userIds), http.StatusBadRequest) } - channel, err := a.GetChannelByName(model.GetGroupNameFromUserIds(userIds), "") + channel, err := a.GetChannelByName(model.GetGroupNameFromUserIds(userIds), "", true) if err != nil { return nil, err } @@ -1011,16 +1011,26 @@ func (a *App) GetChannel(channelId string) (*model.Channel, *model.AppError) { } } -func (a *App) GetChannelByName(channelName, teamId string) (*model.Channel, *model.AppError) { - if result := <-a.Srv.Store.Channel().GetByName(teamId, channelName, true); result.Err != nil && result.Err.Id == "store.sql_channel.get_by_name.missing.app_error" { +func (a *App) GetChannelByName(channelName, teamId string, includeDeleted bool) (*model.Channel, *model.AppError) { + var result store.StoreResult + + if includeDeleted { + result = <-a.Srv.Store.Channel().GetByNameIncludeDeleted(teamId, channelName, false) + } else { + result = <-a.Srv.Store.Channel().GetByName(teamId, channelName, false) + } + + if result.Err != nil && result.Err.Id == "store.sql_channel.get_by_name.missing.app_error" { result.Err.StatusCode = http.StatusNotFound return nil, result.Err - } else if result.Err != nil { + } + + if result.Err != nil { result.Err.StatusCode = http.StatusBadRequest return nil, result.Err - } else { - return result.Data.(*model.Channel), nil } + + return result.Data.(*model.Channel), nil } func (a *App) GetChannelsByNames(channelNames []string, teamId string) ([]*model.Channel, *model.AppError) { @@ -1035,7 +1045,7 @@ func (a *App) GetChannelsByNames(channelNames []string, teamId string) ([]*model } } -func (a *App) GetChannelByNameForTeamName(channelName, teamName string) (*model.Channel, *model.AppError) { +func (a *App) GetChannelByNameForTeamName(channelName, teamName string, includeDeleted bool) (*model.Channel, *model.AppError) { var team *model.Team if result := <-a.Srv.Store.Team().GetByName(teamName); result.Err != nil { @@ -1045,19 +1055,28 @@ func (a *App) GetChannelByNameForTeamName(channelName, teamName string) (*model. team = result.Data.(*model.Team) } - if result := <-a.Srv.Store.Channel().GetByName(team.Id, channelName, true); result.Err != nil && result.Err.Id == "store.sql_channel.get_by_name.missing.app_error" { + var result store.StoreResult + if includeDeleted { + result = <-a.Srv.Store.Channel().GetByNameIncludeDeleted(team.Id, channelName, false) + } else { + result = <-a.Srv.Store.Channel().GetByName(team.Id, channelName, false) + } + + if result.Err != nil && result.Err.Id == "store.sql_channel.get_by_name.missing.app_error" { result.Err.StatusCode = http.StatusNotFound return nil, result.Err - } else if result.Err != nil { + } + + if result.Err != nil { result.Err.StatusCode = http.StatusBadRequest return nil, result.Err - } else { - return result.Data.(*model.Channel), nil } + + return result.Data.(*model.Channel), nil } -func (a *App) GetChannelsForUser(teamId string, userId string) (*model.ChannelList, *model.AppError) { - if result := <-a.Srv.Store.Channel().GetChannels(teamId, userId); result.Err != nil { +func (a *App) GetChannelsForUser(teamId string, userId string, includeDeleted bool) (*model.ChannelList, *model.AppError) { + if result := <-a.Srv.Store.Channel().GetChannels(teamId, userId, includeDeleted); result.Err != nil { return nil, result.Err } else { return result.Data.(*model.ChannelList), nil @@ -1494,7 +1513,9 @@ func (a *App) UpdateChannelLastViewedAt(channelIds []string, userId string) *mod } func (a *App) AutocompleteChannels(teamId string, term string) (*model.ChannelList, *model.AppError) { - if result := <-a.Srv.Store.Channel().AutocompleteInTeam(teamId, term); result.Err != nil { + includeDeleted := *a.Config().TeamSettings.ViewArchivedChannels + + if result := <-a.Srv.Store.Channel().AutocompleteInTeam(teamId, term, includeDeleted); result.Err != nil { return nil, result.Err } else { return result.Data.(*model.ChannelList), nil @@ -1502,7 +1523,9 @@ func (a *App) AutocompleteChannels(teamId string, term string) (*model.ChannelLi } func (a *App) SearchChannels(teamId string, term string) (*model.ChannelList, *model.AppError) { - if result := <-a.Srv.Store.Channel().SearchInTeam(teamId, term); result.Err != nil { + includeDeleted := *a.Config().TeamSettings.ViewArchivedChannels + + if result := <-a.Srv.Store.Channel().SearchInTeam(teamId, term, includeDeleted); result.Err != nil { return nil, result.Err } else { return result.Data.(*model.ChannelList), nil diff --git a/app/channel_test.go b/app/channel_test.go index fa9520b52..ba5f3eb58 100644 --- a/app/channel_test.go +++ b/app/channel_test.go @@ -175,7 +175,7 @@ func TestJoinDefaultChannelsExperimentalDefaultChannels(t *testing.T) { th.App.JoinDefaultChannels(th.BasicTeam.Id, user, false, "") for _, channelName := range defaultChannelList { - channel, err := th.App.GetChannelByName(channelName, th.BasicTeam.Id) + channel, err := th.App.GetChannelByName(channelName, th.BasicTeam.Id, false) if err != nil { t.Errorf("Expected nil, got %s", err) diff --git a/app/command_invite.go b/app/command_invite.go index 54cf2da02..86cc5fdbb 100644 --- a/app/command_invite.go +++ b/app/command_invite.go @@ -59,7 +59,7 @@ func (me *InviteProvider) DoCommand(a *App, args *model.CommandArgs, message str if len(splitMessage) > 1 && splitMessage[1] != "" { targetChannelName := strings.TrimPrefix(strings.TrimSpace(splitMessage[1]), "~") - if channelToJoin, err = a.GetChannelByName(targetChannelName, args.TeamId); err != nil { + if channelToJoin, err = a.GetChannelByName(targetChannelName, args.TeamId, false); err != nil { return &model.CommandResponse{Text: args.T("api.command_invite.channel.error", map[string]interface{}{"Channel": targetChannelName}), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} } } else { diff --git a/app/import.go b/app/import.go index dc4b65396..50973d504 100644 --- a/app/import.go +++ b/app/import.go @@ -1116,7 +1116,7 @@ func (a *App) ImportUserTeams(user *model.User, data *[]UserTeamImportData) *mod a.UpdateTeamMemberSchemeRoles(team.Id, user.Id, isSchemeUser, isSchemeAdmin) } - if defaultChannel, err := a.GetChannelByName(model.DEFAULT_CHANNEL, team.Id); err != nil { + if defaultChannel, err := a.GetChannelByName(model.DEFAULT_CHANNEL, team.Id, true); err != nil { return err } else if _, err = a.addUserToChannel(user, defaultChannel, member); err != nil { return err @@ -1139,7 +1139,7 @@ func (a *App) ImportUserChannels(user *model.User, team *model.Team, teamMember // Loop through all channels. for _, cdata := range *data { - channel, err := a.GetChannelByName(*cdata.Name, team.Id) + channel, err := a.GetChannelByName(*cdata.Name, team.Id, true) if err != nil { return err } diff --git a/app/import_test.go b/app/import_test.go index f99e100f1..a8e5a65a8 100644 --- a/app/import_test.go +++ b/app/import_test.go @@ -1898,7 +1898,7 @@ func TestImportImportChannel(t *testing.T) { th.CheckChannelsCount(t, channelCount+1) // Get the Channel and check all the fields are correct. - if channel, err := th.App.GetChannelByName(*data.Name, team.Id); err != nil { + if channel, err := th.App.GetChannelByName(*data.Name, team.Id, false); err != nil { t.Fatalf("Failed to get channel from database.") } else { assert.Equal(t, *data.Name, channel.Name) @@ -1923,7 +1923,7 @@ func TestImportImportChannel(t *testing.T) { th.CheckChannelsCount(t, channelCount) // Get the Channel and check all the fields are correct. - if channel, err := th.App.GetChannelByName(*data.Name, team.Id); err != nil { + if channel, err := th.App.GetChannelByName(*data.Name, team.Id, false); err != nil { t.Fatalf("Failed to get channel from database.") } else { assert.Equal(t, *data.Name, channel.Name) @@ -2157,7 +2157,7 @@ func TestImportImportUser(t *testing.T) { DisplayName: ptrStr("Display Name"), Type: ptrStr("O"), }, false) - channel, err := th.App.GetChannelByName(channelName, team.Id) + channel, err := th.App.GetChannelByName(channelName, team.Id, false) if err != nil { t.Fatalf("Failed to get channel from database.") } @@ -2671,7 +2671,7 @@ func TestImportImportUser(t *testing.T) { if err := th.App.ImportChannel(channelData, false); err != nil { t.Fatalf("Import should have succeeded.") } - channel, err = th.App.GetChannelByName(*channelData.Name, team.Id) + channel, err = th.App.GetChannelByName(*channelData.Name, team.Id, false) if err != nil { t.Fatalf("Failed to get channel from database: %v", err.Error()) } @@ -2755,7 +2755,7 @@ func TestImportImportPost(t *testing.T) { DisplayName: ptrStr("Display Name"), Type: ptrStr("O"), }, false) - channel, err := th.App.GetChannelByName(channelName, team.Id) + channel, err := th.App.GetChannelByName(channelName, team.Id, false) if err != nil { t.Fatalf("Failed to get channel from database.") } @@ -3959,7 +3959,7 @@ func TestImportPostAndRepliesWithAttachments(t *testing.T) { DisplayName: ptrStr("Display Name"), Type: ptrStr("O"), }, false) - _, err = th.App.GetChannelByName(channelName, team.Id) + _, err = th.App.GetChannelByName(channelName, team.Id, false) if err != nil { t.Fatalf("Failed to get channel from database.") } diff --git a/app/plugin_api.go b/app/plugin_api.go index 414ce4d6e..66f17bdfb 100644 --- a/app/plugin_api.go +++ b/app/plugin_api.go @@ -195,12 +195,12 @@ func (api *PluginAPI) GetChannel(channelId string) (*model.Channel, *model.AppEr return api.app.GetChannel(channelId) } -func (api *PluginAPI) GetChannelByName(teamId, name string) (*model.Channel, *model.AppError) { - return api.app.GetChannelByName(name, teamId) +func (api *PluginAPI) GetChannelByName(teamId, name string, includeDeleted bool) (*model.Channel, *model.AppError) { + return api.app.GetChannelByName(name, teamId, includeDeleted) } -func (api *PluginAPI) GetChannelByNameForTeamName(teamName, channelName string) (*model.Channel, *model.AppError) { - return api.app.GetChannelByNameForTeamName(channelName, teamName) +func (api *PluginAPI) GetChannelByNameForTeamName(teamName, channelName string, includeDeleted bool) (*model.Channel, *model.AppError) { + return api.app.GetChannelByNameForTeamName(channelName, teamName, includeDeleted) } func (api *PluginAPI) GetDirectChannel(userId1, userId2 string) (*model.Channel, *model.AppError) { diff --git a/app/post.go b/app/post.go index 0344d8f0d..8c44436aa 100644 --- a/app/post.go +++ b/app/post.go @@ -638,8 +638,9 @@ func (a *App) DeletePostFiles(post *model.Post) { } } -func (a *App) SearchPostsInTeam(terms string, userId string, teamId string, isOrSearch bool) (*model.PostSearchResults, *model.AppError) { +func (a *App) SearchPostsInTeam(terms string, userId string, teamId string, isOrSearch bool, includeDeletedChannels bool) (*model.PostSearchResults, *model.AppError) { paramsList := model.ParseSearchParams(terms) + includeDeleted := includeDeletedChannels && *a.Config().TeamSettings.ViewArchivedChannels esInterface := a.Elasticsearch if license := a.License(); esInterface != nil && *a.Config().ElasticsearchSettings.EnableSearching && license != nil && *license.Features.Elasticsearch { @@ -651,7 +652,7 @@ func (a *App) SearchPostsInTeam(terms string, userId string, teamId string, isOr if params.Terms != "*" { // Convert channel names to channel IDs for idx, channelName := range params.InChannels { - if channel, err := a.GetChannelByName(channelName, teamId); err != nil { + if channel, err := a.GetChannelByName(channelName, teamId, includeDeleted); err != nil { mlog.Error(fmt.Sprint(err)) } else { params.InChannels[idx] = channel.Id @@ -677,7 +678,7 @@ func (a *App) SearchPostsInTeam(terms string, userId string, teamId string, isOr } // We only allow the user to search in channels they are a member of. - userChannels, err := a.GetChannelsForUser(teamId, userId) + userChannels, err := a.GetChannelsForUser(teamId, userId, includeDeleted) if err != nil { mlog.Error(fmt.Sprint(err)) return nil, err @@ -710,6 +711,7 @@ func (a *App) SearchPostsInTeam(terms string, userId string, teamId string, isOr channels := []store.StoreChannel{} for _, params := range paramsList { + params.IncludeDeletedChannels = includeDeleted params.OrTerms = isOrSearch // don't allow users to search for everything if params.Terms != "*" { diff --git a/app/team.go b/app/team.go index ff1037b7c..8d1331823 100644 --- a/app/team.go +++ b/app/team.go @@ -715,7 +715,7 @@ func (a *App) LeaveTeam(team *model.Team, user *model.User, requestorId string) var channelList *model.ChannelList - if result := <-a.Srv.Store.Channel().GetChannels(team.Id, user.Id); result.Err != nil { + if result := <-a.Srv.Store.Channel().GetChannels(team.Id, user.Id, true); result.Err != nil { if result.Err.Id == "store.sql_channel.get_channels.not_found.app_error" { channelList = &model.ChannelList{} } else { diff --git a/app/user.go b/app/user.go index b9a97b2a9..fa4f36ff1 100644 --- a/app/user.go +++ b/app/user.go @@ -901,7 +901,7 @@ func (a *App) UpdateActive(user *model.User, active bool) (*model.User, *model.A } for _, team := range teamsForUser { - channelsForUser, err := a.GetChannelsForUser(team.Id, user.Id) + channelsForUser, err := a.GetChannelsForUser(team.Id, user.Id, false) if err != nil { return nil, err } diff --git a/app/web_conn.go b/app/web_conn.go index 47fae24c3..1c314bb11 100644 --- a/app/web_conn.go +++ b/app/web_conn.go @@ -334,7 +334,7 @@ func (webCon *WebConn) ShouldSendEvent(msg *model.WebSocketEvent) bool { } if webCon.AllChannelMembers == nil { - if result := <-webCon.App.Srv.Store.Channel().GetAllChannelMembersForUser(webCon.UserId, true); result.Err != nil { + if result := <-webCon.App.Srv.Store.Channel().GetAllChannelMembersForUser(webCon.UserId, true, false); result.Err != nil { mlog.Error("webhub.shouldSendEvent: " + result.Err.Error()) return false } else { -- cgit v1.2.3-1-g7c22