Added the SearchPostsInTeam method to the plugin API (#10106)

Этот коммит содержится в:
Andrew Braunstein
2019-02-12 22:41:32 -08:00
коммит произвёл Hanzei
родитель 87e36a3ecf
Коммит c08fda1337
11 изменённых файлов: 197 добавлений и 31 удалений

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

@@ -342,6 +342,14 @@ func (api *PluginAPI) SearchUsers(search *model.UserSearch) ([]*model.User, *mod
return api.app.SearchUsers(search, pluginSearchUsersOptions)
}
func (api *PluginAPI) SearchPostsInTeam(teamId string, paramsList []*model.SearchParams) ([]*model.Post, *model.AppError) {
postList, err := api.app.SearchPostsInTeam(teamId, paramsList)
if err != nil {
return nil, err
}
return postList.ToSlice(), nil
}
func (api *PluginAPI) AddChannelMember(channelId, userId string) (*model.ChannelMember, *model.AppError) {
// For now, don't allow overriding these via the plugin API.
userRequestorId := ""

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

@@ -693,6 +693,52 @@ func TestPluginAPISearchChannels(t *testing.T) {
})
}
func TestPluginAPISearchPostsInTeam(t *testing.T) {
th := Setup().InitBasic()
defer th.TearDown()
api := th.SetupPluginAPI()
testCases := []struct {
description string
teamId string
params []*model.SearchParams
expectedPostsLen int
}{
{
"nil params",
th.BasicTeam.Id,
nil,
0,
},
{
"empty params",
th.BasicTeam.Id,
[]*model.SearchParams{},
0,
},
{
"doesn't match any posts",
th.BasicTeam.Id,
model.ParseSearchParams("bad message", 0),
0,
},
{
"matched posts",
th.BasicTeam.Id,
model.ParseSearchParams(th.BasicPost.Message, 0),
1,
},
}
for _, testCase := range testCases {
t.Run(testCase.description, func(t *testing.T) {
posts, err := api.SearchPostsInTeam(testCase.teamId, testCase.params)
assert.Nil(t, err)
assert.Equal(t, testCase.expectedPostsLen, len(posts))
})
}
}
func TestPluginAPIGetChannelsForTeamForUser(t *testing.T) {
th := Setup().InitBasic()
defer th.TearDown()

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

@@ -743,7 +743,42 @@ func (a *App) parseAndFetchChannelIdByNameFromInFilter(channelName, userId, team
return channel, nil
}
func (a *App) SearchPostsInTeam(terms string, userId string, teamId string, isOrSearch bool, includeDeletedChannels bool, timeZoneOffset int, page, perPage int) (*model.PostSearchResults, *model.AppError) {
func (a *App) searchPostsInTeam(teamId string, userId string, paramsList []*model.SearchParams, modifierFun func(*model.SearchParams)) (*model.PostList, *model.AppError) {
channels := []store.StoreChannel{}
for _, params := range paramsList {
// Don't allow users to search for everything.
if params.Terms == "*" {
continue
}
modifierFun(params)
channels = append(channels, a.Srv.Store.Post().Search(teamId, userId, params))
}
posts := model.NewPostList()
for _, channel := range channels {
result := <-channel
if result.Err != nil {
return nil, result.Err
}
data := result.Data.(*model.PostList)
posts.Extend(data)
}
posts.SortByCreateAt()
return posts, nil
}
func (a *App) SearchPostsInTeam(teamId string, paramsList []*model.SearchParams) (*model.PostList, *model.AppError) {
if !*a.Config().ServiceSettings.EnablePostSearch {
return nil, model.NewAppError("SearchPostsInTeam", "store.sql_post.search.disabled", nil, fmt.Sprintf("teamId=%v", teamId), http.StatusNotImplemented)
}
return a.searchPostsInTeam(teamId, "", paramsList, func(params *model.SearchParams) {
params.SearchWithoutUserId = true
})
}
func (a *App) SearchPostsInTeamForUser(terms string, userId string, teamId string, isOrSearch bool, includeDeletedChannels bool, timeZoneOffset int, page, perPage int) (*model.PostSearchResults, *model.AppError) {
paramsList := model.ParseSearchParams(terms, timeZoneOffset)
includeDeleted := includeDeletedChannels && *a.Config().TeamSettings.ExperimentalViewArchivedChannels
@@ -815,7 +850,7 @@ func (a *App) SearchPostsInTeam(terms string, userId string, teamId string, isOr
}
if !*a.Config().ServiceSettings.EnablePostSearch {
return nil, model.NewAppError("SearchPostsInTeam", "store.sql_post.search.disabled", nil, fmt.Sprintf("teamId=%v userId=%v", teamId, userId), http.StatusNotImplemented)
return nil, model.NewAppError("SearchPostsInTeamForUser", "store.sql_post.search.disabled", nil, fmt.Sprintf("teamId=%v userId=%v", teamId, userId), http.StatusNotImplemented)
}
// Since we don't support paging we just return nothing for later pages
@@ -823,39 +858,23 @@ func (a *App) SearchPostsInTeam(terms string, userId string, teamId string, isOr
return model.MakePostSearchResults(model.NewPostList(), nil), nil
}
channels := []store.StoreChannel{}
for _, params := range paramsList {
posts, err := a.searchPostsInTeam(teamId, userId, paramsList, func(params *model.SearchParams) {
params.IncludeDeletedChannels = includeDeleted
params.OrTerms = isOrSearch
// don't allow users to search for everything
if params.Terms != "*" {
for idx, channelName := range params.InChannels {
if strings.HasPrefix(channelName, "@") {
channel, err := a.parseAndFetchChannelIdByNameFromInFilter(channelName, userId, teamId, includeDeletedChannels)
if err != nil {
mlog.Error(fmt.Sprint(err))
continue
}
params.InChannels[idx] = channel.Name
for idx, channelName := range params.InChannels {
if strings.HasPrefix(channelName, "@") {
channel, err := a.parseAndFetchChannelIdByNameFromInFilter(channelName, userId, teamId, includeDeletedChannels)
if err != nil {
mlog.Error(fmt.Sprint(err))
continue
}
params.InChannels[idx] = channel.Name
}
channels = append(channels, a.Srv.Store.Post().Search(teamId, userId, params))
}
})
if err != nil {
return nil, err
}
posts := model.NewPostList()
for _, channel := range channels {
result := <-channel
if result.Err != nil {
return nil, result.Err
}
data := result.Data.(*model.PostList)
posts.Extend(data)
}
posts.SortByCreateAt()
return model.MakePostSearchResults(posts, nil), nil
}