Этот коммит содержится в:
Shota Gvinepadze
2019-06-25 17:14:01 +04:00
коммит произвёл Jesús Espino
родитель d1b1b319cf
Коммит e9cb343ba7
5 изменённых файлов: 51 добавлений и 50 удалений

Просмотреть файл

@@ -1798,11 +1798,7 @@ func (a *App) SearchAllChannels(term string, opts model.ChannelSearchOpts) (*mod
term = strings.TrimSpace(term) term = strings.TrimSpace(term)
result := <-a.Srv.Store.Channel().SearchAllChannels(term, storeOpts) return a.Srv.Store.Channel().SearchAllChannels(term, storeOpts)
if result.Err != nil {
return nil, result.Err
}
return result.Data.(*model.ChannelListWithTeamData), nil
} }
func (a *App) SearchChannels(teamId string, term string) (*model.ChannelList, *model.AppError) { func (a *App) SearchChannels(teamId string, term string) (*model.ChannelList, *model.AppError) {

Просмотреть файл

@@ -2098,50 +2098,47 @@ func (s SqlChannelStore) SearchInTeam(teamId string, term string, includeDeleted
}) })
} }
func (s SqlChannelStore) SearchAllChannels(term string, opts store.ChannelSearchOpts) store.StoreChannel { func (s SqlChannelStore) SearchAllChannels(term string, opts store.ChannelSearchOpts) (*model.ChannelListWithTeamData, *model.AppError) {
return store.Do(func(result *store.StoreResult) { query := s.getQueryBuilder().
query := s.getQueryBuilder(). Select("c.*, t.DisplayName AS TeamDisplayName, t.Name AS TeamName, t.UpdateAt as TeamUpdateAt").
Select("c.*, t.DisplayName AS TeamDisplayName, t.Name AS TeamName, t.UpdateAt as TeamUpdateAt"). From("Channels AS c").
From("Channels AS c"). Join("Teams AS t ON t.Id = c.TeamId").
Join("Teams AS t ON t.Id = c.TeamId"). Where(sq.Eq{"c.Type": []string{model.CHANNEL_PRIVATE, model.CHANNEL_OPEN}}).
Where(sq.Eq{"c.Type": []string{model.CHANNEL_PRIVATE, model.CHANNEL_OPEN}}). OrderBy("c.DisplayName, t.DisplayName").
OrderBy("c.DisplayName, t.DisplayName"). Limit(uint64(100))
Limit(uint64(100))
if !opts.IncludeDeleted { if !opts.IncludeDeleted {
query = query.Where(sq.Eq{"c.DeleteAt": int(0)}) query = query.Where(sq.Eq{"c.DeleteAt": int(0)})
} }
likeClause, likeTerm := s.buildLIKEClause(term, "c.Name, c.DisplayName, c.Purpose") likeClause, likeTerm := s.buildLIKEClause(term, "c.Name, c.DisplayName, c.Purpose")
if len(likeTerm) > 0 { if len(likeTerm) > 0 {
likeClause = strings.ReplaceAll(likeClause, ":LikeTerm", "'"+likeTerm+"'") likeClause = strings.ReplaceAll(likeClause, ":LikeTerm", "'"+likeTerm+"'")
fulltextClause, fulltextTerm := s.buildFulltextClause(term, "c.Name, c.DisplayName, c.Purpose") fulltextClause, fulltextTerm := s.buildFulltextClause(term, "c.Name, c.DisplayName, c.Purpose")
fulltextClause = strings.ReplaceAll(fulltextClause, ":FulltextTerm", "'"+fulltextTerm+"'") fulltextClause = strings.ReplaceAll(fulltextClause, ":FulltextTerm", "'"+fulltextTerm+"'")
query = query.Where("(" + likeClause + " OR " + fulltextClause + ")") query = query.Where("(" + likeClause + " OR " + fulltextClause + ")")
} }
if len(opts.ExcludeChannelNames) > 0 { if len(opts.ExcludeChannelNames) > 0 {
query = query.Where(fmt.Sprintf("c.Name NOT IN ('%s')", strings.Join(opts.ExcludeChannelNames, "', '"))) query = query.Where(fmt.Sprintf("c.Name NOT IN ('%s')", strings.Join(opts.ExcludeChannelNames, "', '")))
} }
if len(opts.NotAssociatedToGroup) > 0 { if len(opts.NotAssociatedToGroup) > 0 {
query = query.Where("c.Id NOT IN (SELECT ChannelId FROM GroupChannels WHERE GroupChannels.GroupId = ? AND GroupChannels.DeleteAt = 0)", opts.NotAssociatedToGroup) query = query.Where("c.Id NOT IN (SELECT ChannelId FROM GroupChannels WHERE GroupChannels.GroupId = ? AND GroupChannels.DeleteAt = 0)", opts.NotAssociatedToGroup)
} }
queryString, args, err := query.ToSql() queryString, args, err := query.ToSql()
if err != nil { if err != nil {
result.Err = model.NewAppError("SqlChannelStore.SearchAllChannels", "store.sql.build_query.app_error", nil, err.Error(), http.StatusInternalServerError) return nil, model.NewAppError("SqlChannelStore.SearchAllChannels", "store.sql.build_query.app_error", nil, err.Error(), http.StatusInternalServerError)
return }
}
var channels model.ChannelListWithTeamData var channels model.ChannelListWithTeamData
if _, err := s.GetReplica().Select(&channels, queryString, args...); err != nil { if _, err := s.GetReplica().Select(&channels, queryString, args...); err != nil {
result.Err = model.NewAppError("SqlChannelStore.Search", "store.sql_channel.search.app_error", nil, "term="+term+", "+", "+err.Error(), http.StatusInternalServerError) return nil, model.NewAppError("SqlChannelStore.Search", "store.sql_channel.search.app_error", nil, "term="+term+", "+", "+err.Error(), http.StatusInternalServerError)
} }
result.Data = &channels return &channels, nil
})
} }
func (s SqlChannelStore) SearchMore(userId string, teamId string, term string) (*model.ChannelList, *model.AppError) { func (s SqlChannelStore) SearchMore(userId string, teamId string, term string) (*model.ChannelList, *model.AppError) {

Просмотреть файл

@@ -183,7 +183,7 @@ type ChannelStore interface {
GetMembersForUserWithPagination(teamId, userId string, page, perPage int) StoreChannel GetMembersForUserWithPagination(teamId, userId string, page, perPage int) StoreChannel
AutocompleteInTeam(teamId string, term string, includeDeleted bool) (*model.ChannelList, *model.AppError) AutocompleteInTeam(teamId string, term string, includeDeleted bool) (*model.ChannelList, *model.AppError)
AutocompleteInTeamForSearch(teamId string, userId string, term string, includeDeleted bool) StoreChannel AutocompleteInTeamForSearch(teamId string, userId string, term string, includeDeleted bool) StoreChannel
SearchAllChannels(term string, opts ChannelSearchOpts) StoreChannel SearchAllChannels(term string, opts ChannelSearchOpts) (*model.ChannelListWithTeamData, *model.AppError)
SearchInTeam(teamId string, term string, includeDeleted bool) (*model.ChannelList, *model.AppError) SearchInTeam(teamId string, term string, includeDeleted bool) (*model.ChannelList, *model.AppError)
SearchMore(userId string, teamId string, term string) (*model.ChannelList, *model.AppError) SearchMore(userId string, teamId string, term string) (*model.ChannelList, *model.AppError)
SearchGroupChannels(userId, term string) (*model.ChannelList, *model.AppError) SearchGroupChannels(userId, term string) (*model.ChannelList, *model.AppError)

Просмотреть файл

@@ -2547,9 +2547,8 @@ func testChannelStoreSearchAllChannels(t *testing.T, ss store.Store) {
for _, testCase := range testCases { for _, testCase := range testCases {
t.Run(testCase.Description, func(t *testing.T) { t.Run(testCase.Description, func(t *testing.T) {
result := <-ss.Channel().SearchAllChannels(testCase.Term, testCase.Opts) channels, err := ss.Channel().SearchAllChannels(testCase.Term, testCase.Opts)
require.Nil(t, result.Err) require.Nil(t, err)
channels := result.Data.(*model.ChannelListWithTeamData)
require.Equal(t, len(*testCase.ExpectedResults), len(*channels)) require.Equal(t, len(*testCase.ExpectedResults), len(*channels))
for i, expected := range *testCase.ExpectedResults { for i, expected := range *testCase.ExpectedResults {
require.Equal(t, (*channels)[i].Id, expected.Id) require.Equal(t, (*channels)[i].Id, expected.Id)

Просмотреть файл

@@ -1291,19 +1291,28 @@ func (_m *ChannelStore) SaveMember(member *model.ChannelMember) store.StoreChann
} }
// SearchAllChannels provides a mock function with given fields: term, opts // SearchAllChannels provides a mock function with given fields: term, opts
func (_m *ChannelStore) SearchAllChannels(term string, opts store.ChannelSearchOpts) store.StoreChannel { func (_m *ChannelStore) SearchAllChannels(term string, opts store.ChannelSearchOpts) (*model.ChannelListWithTeamData, *model.AppError) {
ret := _m.Called(term, opts) ret := _m.Called(term, opts)
var r0 store.StoreChannel var r0 *model.ChannelListWithTeamData
if rf, ok := ret.Get(0).(func(string, store.ChannelSearchOpts) store.StoreChannel); ok { if rf, ok := ret.Get(0).(func(string, store.ChannelSearchOpts) *model.ChannelListWithTeamData); ok {
r0 = rf(term, opts) r0 = rf(term, opts)
} else { } else {
if ret.Get(0) != nil { if ret.Get(0) != nil {
r0 = ret.Get(0).(store.StoreChannel) r0 = ret.Get(0).(*model.ChannelListWithTeamData)
} }
} }
return r0 var r1 *model.AppError
if rf, ok := ret.Get(1).(func(string, store.ChannelSearchOpts) *model.AppError); ok {
r1 = rf(term, opts)
} else {
if ret.Get(1) != nil {
r1 = ret.Get(1).(*model.AppError)
}
}
return r0, r1
} }
// SearchGroupChannels provides a mock function with given fields: userId, term // SearchGroupChannels provides a mock function with given fields: userId, term