[MM-38216] Add API endpoint and adapt search to allow multi-team search (#18371)
* Add API endpoint and adapt search to allow multi-team search * Refactor handler, refactor sql query to use squirrel, rename app and store functions and add tests * Fix lint * Fix search engines and remove unneeded comments * Fix test * Remove user from channel after test
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
8c62cf6e3d
Коммит
02a9ef3f82
15
api4/post.go
15
api4/post.go
@@ -27,7 +27,8 @@ func (api *API) InitPost() {
|
||||
|
||||
api.BaseRoutes.ChannelForUser.Handle("/posts/unread", api.APISessionRequired(getPostsForChannelAroundLastUnread)).Methods("GET")
|
||||
|
||||
api.BaseRoutes.Team.Handle("/posts/search", api.APISessionRequiredDisableWhenBusy(searchPosts)).Methods("POST")
|
||||
api.BaseRoutes.Team.Handle("/posts/search", api.APISessionRequiredDisableWhenBusy(searchPostsInTeam)).Methods("POST")
|
||||
api.BaseRoutes.Posts.Handle("/search", api.APISessionRequiredDisableWhenBusy(searchPostsInAllTeams)).Methods("POST")
|
||||
api.BaseRoutes.Post.Handle("", api.APISessionRequired(updatePost)).Methods("PUT")
|
||||
api.BaseRoutes.Post.Handle("/patch", api.APISessionRequired(patchPost)).Methods("PUT")
|
||||
api.BaseRoutes.PostForUser.Handle("/set_unread", api.APISessionRequired(setPostUnread)).Methods("POST")
|
||||
@@ -473,7 +474,7 @@ func getPostThread(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
func searchPosts(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
func searchPostsInTeam(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
c.RequireTeamId()
|
||||
if c.Err != nil {
|
||||
return
|
||||
@@ -484,6 +485,14 @@ func searchPosts(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
searchPosts(c, w, r, c.Params.TeamId)
|
||||
}
|
||||
|
||||
func searchPostsInAllTeams(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
searchPosts(c, w, r, "")
|
||||
}
|
||||
|
||||
func searchPosts(c *Context, w http.ResponseWriter, r *http.Request, teamId string) {
|
||||
var params model.SearchParameter
|
||||
if jsonErr := json.NewDecoder(r.Body).Decode(¶ms); jsonErr != nil {
|
||||
c.Err = model.NewAppError("searchPosts", "api.post.search_posts.invalid_body.app_error", nil, jsonErr.Error(), http.StatusBadRequest)
|
||||
@@ -523,7 +532,7 @@ func searchPosts(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
startTime := time.Now()
|
||||
|
||||
results, err := c.App.SearchPostsInTeamForUser(c.AppContext, terms, c.AppContext.Session().UserId, c.Params.TeamId, isOrSearch, includeDeletedChannels, timeZoneOffset, page, perPage)
|
||||
results, err := c.App.SearchPostsForUser(c.AppContext, terms, c.AppContext.Session().UserId, teamId, isOrSearch, includeDeletedChannels, timeZoneOffset, page, perPage)
|
||||
|
||||
elapsedTime := float64(time.Since(startTime)) / float64(time.Second)
|
||||
metrics := c.App.Metrics()
|
||||
|
||||
@@ -2201,6 +2201,11 @@ func TestSearchPosts(t *testing.T) {
|
||||
_ = th.CreateMessagePostWithClient(th.Client, archivedChannel, "#hashtag for post3")
|
||||
th.Client.DeleteChannel(archivedChannel.Id)
|
||||
|
||||
otherTeam := th.CreateTeam()
|
||||
channelInOtherTeam := th.CreateChannelWithClientAndTeam(th.Client, model.ChannelTypeOpen, otherTeam.Id)
|
||||
_ = th.AddUserToChannel(th.BasicUser, channelInOtherTeam)
|
||||
_ = th.CreateMessagePostWithClient(th.Client, channelInOtherTeam, "search for post 5")
|
||||
|
||||
terms := "search"
|
||||
isOrSearch := false
|
||||
timezoneOffset := 5
|
||||
@@ -2209,6 +2214,18 @@ func TestSearchPosts(t *testing.T) {
|
||||
IsOrSearch: &isOrSearch,
|
||||
TimeZoneOffset: &timezoneOffset,
|
||||
}
|
||||
allTeamsPosts, _, err := client.SearchPostsWithParams("", &searchParams)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, allTeamsPosts.Order, 4, "wrong search along multiple teams")
|
||||
|
||||
terms = "search"
|
||||
isOrSearch = false
|
||||
timezoneOffset = 5
|
||||
searchParams = model.SearchParameter{
|
||||
Terms: &terms,
|
||||
IsOrSearch: &isOrSearch,
|
||||
TimeZoneOffset: &timezoneOffset,
|
||||
}
|
||||
posts, _, err := client.SearchPostsWithParams(th.BasicTeam.Id, &searchParams)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, posts.Order, 3, "wrong search")
|
||||
|
||||
Ссылка в новой задаче
Block a user