[MM-15811] Adds fallback mechanism for elasticsearch queries (#11065)

* [MM-15811] Adds fallback mechanism for elasticsearch queries

* Rename config helpers
Этот коммит содержится в:
Miguel de la Cruz
2019-06-18 14:43:08 +01:00
коммит произвёл GitHub
родитель 4c075e45f4
Коммит 406dcb1311
4 изменённых файлов: 307 добавлений и 210 удалений

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

@@ -107,8 +107,7 @@ func (a *App) JoinDefaultChannels(teamId string, user *model.User, shouldBeAdmin
} }
} }
esInterface := a.Elasticsearch if a.IsESIndexingEnabled() {
if esInterface != nil && *a.Config().ElasticsearchSettings.EnableIndexing {
a.Srv.Go(func() { a.Srv.Go(func() {
if err = a.indexUser(user); err != nil { if err = a.indexUser(user); err != nil {
mlog.Error("Encountered error indexing user", mlog.String("user_id", user.Id), mlog.Err(err)) mlog.Error("Encountered error indexing user", mlog.String("user_id", user.Id), mlog.Err(err))
@@ -185,8 +184,7 @@ func (a *App) CreateChannelWithUser(channel *model.Channel, userId string) (*mod
message.Add("team_id", channel.TeamId) message.Add("team_id", channel.TeamId)
a.Publish(message) a.Publish(message)
esInterface := a.Elasticsearch if a.IsESIndexingEnabled() {
if esInterface != nil && *a.Config().ElasticsearchSettings.EnableIndexing {
a.Srv.Go(func() { a.Srv.Go(func() {
if err := a.indexUser(user); err != nil { if err := a.indexUser(user); err != nil {
mlog.Error("Encountered error indexing user", mlog.String("user_id", user.Id), mlog.Err(err)) mlog.Error("Encountered error indexing user", mlog.String("user_id", user.Id), mlog.Err(err))
@@ -261,11 +259,10 @@ func (a *App) CreateChannel(channel *model.Channel, addMember bool) (*model.Chan
}) })
} }
esInterface := a.Elasticsearch if a.IsESIndexingEnabled() {
if esInterface != nil && *a.Config().ElasticsearchSettings.EnableIndexing { if sc.Type == model.CHANNEL_OPEN {
if sc.Type == "O" {
a.Srv.Go(func() { a.Srv.Go(func() {
if err := esInterface.IndexChannel(sc); err != nil { if err := a.Elasticsearch.IndexChannel(sc); err != nil {
mlog.Error("Encountered error indexing channel", mlog.String("channel_id", sc.Id), mlog.Err(err)) mlog.Error("Encountered error indexing channel", mlog.String("channel_id", sc.Id), mlog.Err(err))
} }
}) })
@@ -309,8 +306,7 @@ func (a *App) GetOrCreateDirectChannel(userId, otherUserId string) (*model.Chann
}) })
} }
esInterface := a.Elasticsearch if a.IsESIndexingEnabled() {
if esInterface != nil && *a.Config().ElasticsearchSettings.EnableIndexing {
a.Srv.Go(func() { a.Srv.Go(func() {
for _, id := range []string{userId, otherUserId} { for _, id := range []string{userId, otherUserId} {
if err := a.indexUserFromId(id); err != nil { if err := a.indexUserFromId(id); err != nil {
@@ -419,8 +415,7 @@ func (a *App) CreateGroupChannel(userIds []string, creatorId string) (*model.Cha
message.Add("teammate_ids", model.ArrayToJson(userIds)) message.Add("teammate_ids", model.ArrayToJson(userIds))
a.Publish(message) a.Publish(message)
esInterface := a.Elasticsearch if a.IsESIndexingEnabled() {
if esInterface != nil && *a.Config().ElasticsearchSettings.EnableIndexing {
a.Srv.Go(func() { a.Srv.Go(func() {
for _, id := range userIds { for _, id := range userIds {
if err := a.indexUserFromId(id); err != nil { if err := a.indexUserFromId(id); err != nil {
@@ -517,10 +512,9 @@ func (a *App) UpdateChannel(channel *model.Channel) (*model.Channel, *model.AppE
messageWs.Add("channel", channel.ToJson()) messageWs.Add("channel", channel.ToJson())
a.Publish(messageWs) a.Publish(messageWs)
esInterface := a.Elasticsearch if a.IsESIndexingEnabled() && channel.Type == model.CHANNEL_OPEN {
if esInterface != nil && *a.Config().ElasticsearchSettings.EnableIndexing && channel.Type == "O" {
a.Srv.Go(func() { a.Srv.Go(func() {
if err := esInterface.IndexChannel(channel); err != nil { if err := a.Elasticsearch.IndexChannel(channel); err != nil {
mlog.Error("Encountered error indexing channel", mlog.String("channel_id", channel.Id), mlog.Err(err)) mlog.Error("Encountered error indexing channel", mlog.String("channel_id", channel.Id), mlog.Err(err))
} }
}) })
@@ -1003,8 +997,7 @@ func (a *App) AddChannelMember(userId string, channel *model.Channel, userReques
}) })
} }
esInterface := a.Elasticsearch if a.IsESIndexingEnabled() {
if esInterface != nil && *a.Config().ElasticsearchSettings.EnableIndexing {
a.Srv.Go(func() { a.Srv.Go(func() {
if err := a.indexUser(user); err != nil { if err := a.indexUser(user); err != nil {
mlog.Error("Encountered error indexing user", mlog.String("user_id", user.Id), mlog.Err(err)) mlog.Error("Encountered error indexing user", mlog.String("user_id", user.Id), mlog.Err(err))
@@ -1413,8 +1406,7 @@ func (a *App) JoinChannel(channel *model.Channel, userId string) *model.AppError
}) })
} }
esInterface := a.Elasticsearch if a.IsESIndexingEnabled() {
if esInterface != nil && *a.Config().ElasticsearchSettings.EnableIndexing {
a.Srv.Go(func() { a.Srv.Go(func() {
if err := a.indexUser(user); err != nil { if err := a.indexUser(user); err != nil {
mlog.Error("Encountered error indexing user", mlog.String("user_id", user.Id), mlog.Err(err)) mlog.Error("Encountered error indexing user", mlog.String("user_id", user.Id), mlog.Err(err))
@@ -1649,8 +1641,7 @@ func (a *App) removeUserFromChannel(userIdToRemove string, removerUserId string,
}) })
} }
esInterface := a.Elasticsearch if a.IsESIndexingEnabled() {
if esInterface != nil && *a.Config().ElasticsearchSettings.EnableIndexing {
a.Srv.Go(func() { a.Srv.Go(func() {
if err := a.indexUserFromId(userIdToRemove); err != nil { if err := a.indexUserFromId(userIdToRemove); err != nil {
mlog.Error("Encountered error indexing user", mlog.String("user_id", userIdToRemove), mlog.Err(err)) mlog.Error("Encountered error indexing user", mlog.String("user_id", userIdToRemove), mlog.Err(err))
@@ -1745,13 +1736,7 @@ func (a *App) UpdateChannelLastViewedAt(channelIds []string, userId string) *mod
return nil return nil
} }
func (a *App) AutocompleteChannels(teamId string, term string) (*model.ChannelList, *model.AppError) { func (a *App) esAutocompleteChannels(teamId, term string, includeDeleted bool) (*model.ChannelList, *model.AppError) {
includeDeleted := *a.Config().TeamSettings.ExperimentalViewArchivedChannels
term = strings.TrimSpace(term)
esInterface := a.Elasticsearch
license := a.License()
if esInterface != nil && *a.Config().ElasticsearchSettings.EnableAutocomplete && license != nil && *license.Features.Elasticsearch {
channelIds, err := a.Elasticsearch.SearchChannels(teamId, term) channelIds, err := a.Elasticsearch.SearchChannels(teamId, term)
if err != nil { if err != nil {
return nil, err return nil, err
@@ -1772,9 +1757,29 @@ func (a *App) AutocompleteChannels(teamId string, term string) (*model.ChannelLi
} }
return &channelList, nil return &channelList, nil
}
func (a *App) AutocompleteChannels(teamId string, term string) (*model.ChannelList, *model.AppError) {
includeDeleted := *a.Config().TeamSettings.ExperimentalViewArchivedChannels
var channelList *model.ChannelList
var err *model.AppError
term = strings.TrimSpace(term)
if a.IsESAutocompletionEnabled() {
channelList, err = a.esAutocompleteChannels(teamId, term, includeDeleted)
if err != nil {
mlog.Error("Encountered error on AutocompleteChannels through Elasticsearch. Falling back to default autocompletion.", mlog.Err(err))
}
} }
return a.Srv.Store.Channel().AutocompleteInTeam(teamId, term, includeDeleted) if !a.IsESAutocompletionEnabled() || err != nil {
channelList, err = a.Srv.Store.Channel().AutocompleteInTeam(teamId, term, includeDeleted)
if err != nil {
return nil, err
}
}
return channelList, nil
} }
func (a *App) AutocompleteChannelsForSearch(teamId string, userId string, term string) (*model.ChannelList, *model.AppError) { func (a *App) AutocompleteChannelsForSearch(teamId string, userId string, term string) (*model.ChannelList, *model.AppError) {
@@ -1927,8 +1932,7 @@ func (a *App) PermanentDeleteChannel(channel *model.Channel) *model.AppError {
return result.Err return result.Err
} }
esInterface := a.Elasticsearch if a.IsESIndexingEnabled() {
if esInterface != nil && *a.Config().ElasticsearchSettings.EnableIndexing {
a.Srv.Go(func() { a.Srv.Go(func() {
for _, user := range channelUsers.Data.(map[string]*model.User) { for _, user := range channelUsers.Data.(map[string]*model.User) {
if err := a.indexUser(user); err != nil { if err := a.indexUser(user); err != nil {
@@ -1936,9 +1940,9 @@ func (a *App) PermanentDeleteChannel(channel *model.Channel) *model.AppError {
} }
} }
}) })
if channel.Type == "O" { if channel.Type == model.CHANNEL_OPEN {
a.Srv.Go(func() { a.Srv.Go(func() {
if err := esInterface.DeleteChannel(channel); err != nil { if err := a.Elasticsearch.DeleteChannel(channel); err != nil {
mlog.Error("Encountered error deleting channel", mlog.String("channel_id", channel.Id), mlog.Err(err)) mlog.Error("Encountered error deleting channel", mlog.String("channel_id", channel.Id), mlog.Err(err))
} }
}) })

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

@@ -399,3 +399,19 @@ func (a *App) SaveConfig(newCfg *model.Config, sendConfigChangeClusterMessage bo
return nil return nil
} }
func (a *App) IsESIndexingEnabled() bool {
return a.Elasticsearch != nil && *a.Config().ElasticsearchSettings.EnableIndexing
}
func (a *App) IsESSearchEnabled() bool {
esInterface := a.Elasticsearch
license := a.License()
return esInterface != nil && *a.Config().ElasticsearchSettings.EnableSearching && license != nil && *license.Features.Elasticsearch
}
func (a *App) IsESAutocompletionEnabled() bool {
esInterface := a.Elasticsearch
license := a.License()
return esInterface != nil && *a.Config().ElasticsearchSettings.EnableAutocomplete && license != nil && *license.Features.Elasticsearch
}

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

@@ -274,10 +274,9 @@ func (a *App) CreatePost(post *model.Post, channel *model.Channel, triggerWebhoo
}) })
} }
esInterface := a.Elasticsearch if a.IsESIndexingEnabled() {
if esInterface != nil && *a.Config().ElasticsearchSettings.EnableIndexing {
a.Srv.Go(func() { a.Srv.Go(func() {
if err = esInterface.IndexPost(rpost, channel.TeamId); err != nil { if err = a.Elasticsearch.IndexPost(rpost, channel.TeamId); err != nil {
mlog.Error("Encountered error indexing post", mlog.String("post_id", post.Id), mlog.Err(err)) mlog.Error("Encountered error indexing post", mlog.String("post_id", post.Id), mlog.Err(err))
} }
}) })
@@ -551,15 +550,14 @@ func (a *App) UpdatePost(post *model.Post, safeUpdate bool) (*model.Post, *model
}) })
} }
esInterface := a.Elasticsearch if a.IsESIndexingEnabled() {
if esInterface != nil && *a.Config().ElasticsearchSettings.EnableIndexing {
a.Srv.Go(func() { a.Srv.Go(func() {
rchannel := <-a.Srv.Store.Channel().GetForPost(rpost.Id) rchannel := <-a.Srv.Store.Channel().GetForPost(rpost.Id)
if rchannel.Err != nil { if rchannel.Err != nil {
mlog.Error(fmt.Sprintf("Couldn't get channel %v for post %v for Elasticsearch indexing.", rpost.ChannelId, rpost.Id)) mlog.Error(fmt.Sprintf("Couldn't get channel %v for post %v for Elasticsearch indexing.", rpost.ChannelId, rpost.Id))
return return
} }
if err := esInterface.IndexPost(rpost, rchannel.Data.(*model.Channel).TeamId); err != nil { if err := a.Elasticsearch.IndexPost(rpost, rchannel.Data.(*model.Channel).TeamId); err != nil {
mlog.Error("Encountered error indexing post", mlog.String("post_id", post.Id), mlog.Err(err)) mlog.Error("Encountered error indexing post", mlog.String("post_id", post.Id), mlog.Err(err))
} }
}) })
@@ -708,10 +706,9 @@ func (a *App) DeletePost(postId, deleteByID string) (*model.Post, *model.AppErro
a.DeleteFlaggedPosts(post.Id) a.DeleteFlaggedPosts(post.Id)
}) })
esInterface := a.Elasticsearch if a.IsESIndexingEnabled() {
if esInterface != nil && *a.Config().ElasticsearchSettings.EnableIndexing {
a.Srv.Go(func() { a.Srv.Go(func() {
if err := esInterface.DeletePost(post); err != nil { if err := a.Elasticsearch.DeletePost(post); err != nil {
mlog.Error("Encountered error deleting post", mlog.String("post_id", post.Id), mlog.Err(err)) mlog.Error("Encountered error deleting post", mlog.String("post_id", post.Id), mlog.Err(err))
} }
}) })
@@ -811,14 +808,9 @@ func (a *App) SearchPostsInTeam(teamId string, paramsList []*model.SearchParams)
}) })
} }
func (a *App) SearchPostsInTeamForUser(terms string, userId string, teamId string, isOrSearch bool, includeDeletedChannels bool, timeZoneOffset int, page, perPage int) (*model.PostSearchResults, *model.AppError) { func (a *App) esSearchPostsInTeamForUser(paramsList []*model.SearchParams, userId, teamId string, isOrSearch, includeDeletedChannels bool, page, perPage int) (*model.PostSearchResults, *model.AppError) {
paramsList := model.ParseSearchParams(strings.TrimSpace(terms), timeZoneOffset)
includeDeleted := includeDeletedChannels && *a.Config().TeamSettings.ExperimentalViewArchivedChannels
esInterface := a.Elasticsearch
license := a.License()
if esInterface != nil && *a.Config().ElasticsearchSettings.EnableSearching && license != nil && *license.Features.Elasticsearch {
finalParamsList := []*model.SearchParams{} finalParamsList := []*model.SearchParams{}
includeDeleted := includeDeletedChannels && *a.Config().TeamSettings.ExperimentalViewArchivedChannels
for _, params := range paramsList { for _, params := range paramsList {
params.OrTerms = isOrSearch params.OrTerms = isOrSearch
@@ -880,6 +872,18 @@ func (a *App) SearchPostsInTeamForUser(terms string, userId string, teamId strin
} }
return model.MakePostSearchResults(postList, matches), nil return model.MakePostSearchResults(postList, matches), nil
}
func (a *App) SearchPostsInTeamForUser(terms string, userId string, teamId string, isOrSearch bool, includeDeletedChannels bool, timeZoneOffset int, page, perPage int) (*model.PostSearchResults, *model.AppError) {
var postSearchResults *model.PostSearchResults
var err *model.AppError
paramsList := model.ParseSearchParams(strings.TrimSpace(terms), timeZoneOffset)
if a.IsESSearchEnabled() {
postSearchResults, err = a.esSearchPostsInTeamForUser(paramsList, userId, teamId, isOrSearch, includeDeletedChannels, page, perPage)
if err != nil {
mlog.Error("Encountered error on SearchPostsInTeamForUser through Elasticsearch. Falling back to default search.", mlog.Err(err))
}
} }
if !*a.Config().ServiceSettings.EnablePostSearch { if !*a.Config().ServiceSettings.EnablePostSearch {
@@ -891,6 +895,8 @@ func (a *App) SearchPostsInTeamForUser(terms string, userId string, teamId strin
return model.MakePostSearchResults(model.NewPostList(), nil), nil return model.MakePostSearchResults(model.NewPostList(), nil), nil
} }
if !a.IsESSearchEnabled() || err != nil {
includeDeleted := includeDeletedChannels && *a.Config().TeamSettings.ExperimentalViewArchivedChannels
posts, err := a.searchPostsInTeam(teamId, userId, paramsList, func(params *model.SearchParams) { posts, err := a.searchPostsInTeam(teamId, userId, paramsList, func(params *model.SearchParams) {
params.IncludeDeletedChannels = includeDeleted params.IncludeDeletedChannels = includeDeleted
params.OrTerms = isOrSearch params.OrTerms = isOrSearch
@@ -908,7 +914,11 @@ func (a *App) SearchPostsInTeamForUser(terms string, userId string, teamId strin
if err != nil { if err != nil {
return nil, err return nil, err
} }
return model.MakePostSearchResults(posts, nil), nil
postSearchResults = model.MakePostSearchResults(posts, nil)
}
return postSearchResults, nil
} }
func (a *App) GetFileInfosForPostWithMigration(postId string) ([]*model.FileInfo, *model.AppError) { func (a *App) GetFileInfosForPostWithMigration(postId string) ([]*model.FileInfo, *model.AppError) {

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

@@ -280,8 +280,7 @@ func (a *App) createUserOrGuest(user *model.User, guest bool) (*model.User, *mod
}) })
} }
esInterface := a.Elasticsearch if a.IsESIndexingEnabled() {
if esInterface != nil && *a.Config().ElasticsearchSettings.EnableIndexing {
a.Srv.Go(func() { a.Srv.Go(func() {
if err := a.indexUser(user); err != nil { if err := a.indexUser(user); err != nil {
mlog.Error("Encountered error indexing user", mlog.String("user_id", user.Id), mlog.Err(err)) mlog.Error("Encountered error indexing user", mlog.String("user_id", user.Id), mlog.Err(err))
@@ -1160,8 +1159,7 @@ func (a *App) UpdateUser(user *model.User, sendNotifications bool) (*model.User,
a.InvalidateCacheForUser(user.Id) a.InvalidateCacheForUser(user.Id)
esInterface := a.Elasticsearch if a.IsESIndexingEnabled() {
if esInterface != nil && *a.Config().ElasticsearchSettings.EnableIndexing {
a.Srv.Go(func() { a.Srv.Go(func() {
if err := a.indexUser(user); err != nil { if err := a.indexUser(user); err != nil {
mlog.Error("Encountered error indexing user", mlog.String("user_id", user.Id), mlog.Err(err)) mlog.Error("Encountered error indexing user", mlog.String("user_id", user.Id), mlog.Err(err))
@@ -1516,8 +1514,7 @@ func (a *App) PermanentDeleteUser(user *model.User) *model.AppError {
mlog.Warn(fmt.Sprintf("Permanently deleted account %v id=%v", user.Email, user.Id), mlog.String("user_id", user.Id)) mlog.Warn(fmt.Sprintf("Permanently deleted account %v id=%v", user.Email, user.Id), mlog.String("user_id", user.Id))
esInterface := a.Elasticsearch if a.IsESIndexingEnabled() {
if esInterface != nil && *a.Config().ElasticsearchSettings.EnableIndexing {
a.Srv.Go(func() { a.Srv.Go(func() {
if err := a.Elasticsearch.DeleteUser(user); err != nil { if err := a.Elasticsearch.DeleteUser(user); err != nil {
mlog.Error("Encountered error deleting user", mlog.String("user_id", user.Id), mlog.Err(err)) mlog.Error("Encountered error deleting user", mlog.String("user_id", user.Id), mlog.Err(err))
@@ -1709,14 +1706,7 @@ func (a *App) SearchUsersNotInChannel(teamId string, channelId string, term stri
return users, nil return users, nil
} }
func (a *App) SearchUsersInTeam(teamId string, term string, options *model.UserSearchOptions) ([]*model.User, *model.AppError) { func (a *App) esSearchUsersInTeam(teamId, term string, options *model.UserSearchOptions) ([]*model.User, *model.AppError) {
var result store.StoreResult
term = strings.TrimSpace(term)
esInterface := a.Elasticsearch
license := a.License()
if esInterface != nil && *a.Config().ElasticsearchSettings.EnableAutocomplete && license != nil && *license.Features.Elasticsearch {
listOfAllowedChannels, err := a.GetViewUsersRestrictionsForTeam(a.Session.UserId, teamId) listOfAllowedChannels, err := a.GetViewUsersRestrictionsForTeam(a.Session.UserId, teamId)
if err != nil { if err != nil {
return nil, err return nil, err
@@ -1730,14 +1720,11 @@ func (a *App) SearchUsersInTeam(teamId string, term string, options *model.UserS
return nil, err return nil, err
} }
result = <-a.Srv.Store.User().GetProfileByIds(usersIds, false, nil) result := <-a.Srv.Store.User().GetProfileByIds(usersIds, false, nil)
} else {
result = <-a.Srv.Store.User().Search(teamId, term, options)
}
if result.Err != nil { if result.Err != nil {
return nil, result.Err return nil, result.Err
} }
users := result.Data.([]*model.User) users := result.Data.([]*model.User)
for _, user := range users { for _, user := range users {
@@ -1747,6 +1734,34 @@ func (a *App) SearchUsersInTeam(teamId string, term string, options *model.UserS
return users, nil return users, nil
} }
func (a *App) SearchUsersInTeam(teamId, term string, options *model.UserSearchOptions) ([]*model.User, *model.AppError) {
var users []*model.User
var err *model.AppError
term = strings.TrimSpace(term)
if a.IsESAutocompletionEnabled() {
users, err = a.esSearchUsersInTeam(teamId, term, options)
if err != nil {
mlog.Error("Encountered error on SearchUsersInTeam through Elasticsearch. Falling back to default search.", mlog.Err(err))
}
}
if !a.IsESAutocompletionEnabled() || err != nil {
result := <-a.Srv.Store.User().Search(teamId, term, options)
if result.Err != nil {
return nil, result.Err
}
users = result.Data.([]*model.User)
for _, user := range users {
a.SanitizeProfile(user, options.IsAdmin)
}
}
return users, nil
}
func (a *App) SearchUsersNotInTeam(notInTeamId string, term string, options *model.UserSearchOptions) ([]*model.User, *model.AppError) { func (a *App) SearchUsersNotInTeam(notInTeamId string, term string, options *model.UserSearchOptions) ([]*model.User, *model.AppError) {
term = strings.TrimSpace(term) term = strings.TrimSpace(term)
result := <-a.Srv.Store.User().SearchNotInTeam(notInTeamId, term, options) result := <-a.Srv.Store.User().SearchNotInTeam(notInTeamId, term, options)
@@ -1777,14 +1792,7 @@ func (a *App) SearchUsersWithoutTeam(term string, options *model.UserSearchOptio
return users, nil return users, nil
} }
func (a *App) AutocompleteUsersInChannel(teamId string, channelId string, term string, options *model.UserSearchOptions) (*model.UserAutocompleteInChannel, *model.AppError) { func (a *App) esAutocompleteUsersInChannel(teamId, channelId, term string, options *model.UserSearchOptions) (*model.UserAutocompleteInChannel, *model.AppError) {
var uchan, nuchan store.StoreChannel
term = strings.TrimSpace(term)
esInterface := a.Elasticsearch
license := a.License()
if esInterface != nil && *a.Config().ElasticsearchSettings.EnableAutocomplete && license != nil && *license.Features.Elasticsearch {
listOfAllowedChannels, err := a.getListOfAllowedChannelsForTeam(teamId, options.ViewRestrictions) listOfAllowedChannels, err := a.getListOfAllowedChannelsForTeam(teamId, options.ViewRestrictions)
if err != nil { if err != nil {
return nil, err return nil, err
@@ -1802,13 +1810,8 @@ func (a *App) AutocompleteUsersInChannel(teamId string, channelId string, term s
if err != nil { if err != nil {
return nil, err return nil, err
} }
uchan = a.Srv.Store.User().GetProfileByIds(uchanIds, false, nil) uchan := a.Srv.Store.User().GetProfileByIds(uchanIds, false, nil)
nuchan = a.Srv.Store.User().GetProfileByIds(nuchanIds, false, nil) nuchan := a.Srv.Store.User().GetProfileByIds(nuchanIds, false, nil)
} else {
uchan = a.Srv.Store.User().SearchInChannel(channelId, term, options)
nuchan = a.Srv.Store.User().SearchNotInChannel(teamId, channelId, term, options)
}
autocomplete := &model.UserAutocompleteInChannel{} autocomplete := &model.UserAutocompleteInChannel{}
result := <-uchan result := <-uchan
@@ -1838,15 +1841,52 @@ func (a *App) AutocompleteUsersInChannel(teamId string, channelId string, term s
return autocomplete, nil return autocomplete, nil
} }
func (a *App) AutocompleteUsersInTeam(teamId string, term string, options *model.UserSearchOptions) (*model.UserAutocompleteInTeam, *model.AppError) { func (a *App) AutocompleteUsersInChannel(teamId string, channelId string, term string, options *model.UserSearchOptions) (*model.UserAutocompleteInChannel, *model.AppError) {
autocomplete := &model.UserAutocompleteInTeam{} var autocomplete *model.UserAutocompleteInChannel
var result store.StoreResult var err *model.AppError
term = strings.TrimSpace(term) term = strings.TrimSpace(term)
esInterface := a.Elasticsearch if a.IsESAutocompletionEnabled() {
license := a.License() autocomplete, err = a.esAutocompleteUsersInChannel(teamId, channelId, term, options)
if esInterface != nil && *a.Config().ElasticsearchSettings.EnableAutocomplete && license != nil && *license.Features.Elasticsearch { if err != nil {
mlog.Error("Encountered error on AutocompleteUsersInChannel through Elasticsearch. Falling back to default autocompletion.", mlog.Err(err))
}
}
if !a.IsESAutocompletionEnabled() || err != nil {
autocomplete = &model.UserAutocompleteInChannel{}
uchan := a.Srv.Store.User().SearchInChannel(channelId, term, options)
nuchan := a.Srv.Store.User().SearchNotInChannel(teamId, channelId, term, options)
result := <-uchan
if result.Err != nil {
return nil, result.Err
}
users := result.Data.([]*model.User)
for _, user := range users {
a.SanitizeProfile(user, options.IsAdmin)
}
autocomplete.InChannel = users
result = <-nuchan
if result.Err != nil {
return nil, result.Err
}
users = result.Data.([]*model.User)
for _, user := range users {
a.SanitizeProfile(user, options.IsAdmin)
}
autocomplete.OutOfChannel = users
}
return autocomplete, nil
}
func (a *App) esAutocompleteUsersInTeam(teamId, term string, options *model.UserSearchOptions) (*model.UserAutocompleteInTeam, *model.AppError) {
listOfAllowedChannels, err := a.getListOfAllowedChannelsForTeam(teamId, options.ViewRestrictions) listOfAllowedChannels, err := a.getListOfAllowedChannelsForTeam(teamId, options.ViewRestrictions)
if err != nil { if err != nil {
return nil, err return nil, err
@@ -1860,21 +1900,49 @@ func (a *App) AutocompleteUsersInTeam(teamId string, term string, options *model
return nil, err return nil, err
} }
result = <-a.Srv.Store.User().GetProfileByIds(usersIds, false, nil) result := <-a.Srv.Store.User().GetProfileByIds(usersIds, false, nil)
} else {
result = <-a.Srv.Store.User().Search(teamId, term, options)
}
if result.Err != nil { if result.Err != nil {
return nil, result.Err return nil, result.Err
} }
users := result.Data.([]*model.User)
users := result.Data.([]*model.User)
for _, user := range users {
a.SanitizeProfile(user, options.IsAdmin)
}
autocomplete := &model.UserAutocompleteInTeam{}
autocomplete.InTeam = users
return autocomplete, nil
}
func (a *App) AutocompleteUsersInTeam(teamId string, term string, options *model.UserSearchOptions) (*model.UserAutocompleteInTeam, *model.AppError) {
var autocomplete *model.UserAutocompleteInTeam
var err *model.AppError
term = strings.TrimSpace(term)
if a.IsESAutocompletionEnabled() {
autocomplete, err = a.esAutocompleteUsersInTeam(teamId, term, options)
if err != nil {
mlog.Error("Encountered error on AutocompleteUsersInTeam through Elasticsearch. Falling back to default autocompletion.", mlog.Err(err))
}
}
if !a.IsESAutocompletionEnabled() || err != nil {
autocomplete = &model.UserAutocompleteInTeam{}
result := <-a.Srv.Store.User().Search(teamId, term, options)
if result.Err != nil {
return nil, result.Err
}
users := result.Data.([]*model.User)
for _, user := range users { for _, user := range users {
a.SanitizeProfile(user, options.IsAdmin) a.SanitizeProfile(user, options.IsAdmin)
} }
autocomplete.InTeam = users autocomplete.InTeam = users
}
return autocomplete, nil return autocomplete, nil
} }
@@ -1922,8 +1990,7 @@ func (a *App) UpdateOAuthUserAttrs(userData io.Reader, user *model.User, provide
user = users.New user = users.New
a.InvalidateCacheForUser(user.Id) a.InvalidateCacheForUser(user.Id)
esInterface := a.Elasticsearch if a.IsESIndexingEnabled() {
if esInterface != nil && *a.Config().ElasticsearchSettings.EnableIndexing {
a.Srv.Go(func() { a.Srv.Go(func() {
if err := a.indexUser(user); err != nil { if err := a.indexUser(user); err != nil {
mlog.Error("Encountered error indexing user", mlog.String("user_id", user.Id), mlog.Err(err)) mlog.Error("Encountered error indexing user", mlog.String("user_id", user.Id), mlog.Err(err))