summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorhmhealey <harrisonmhealey@gmail.com>2015-10-26 11:45:03 -0400
committerhmhealey <harrisonmhealey@gmail.com>2015-10-26 11:45:09 -0400
commit663bec814767fa9c92e7ab2c706c0fe4c0432cf1 (patch)
tree2d16013429e3e4facb690c6fb43f5cb53489323e
parent2383d5dd37d5ebf28c2576fd495a8a7f02f78901 (diff)
downloadchat-663bec814767fa9c92e7ab2c706c0fe4c0432cf1.tar.gz
chat-663bec814767fa9c92e7ab2c706c0fe4c0432cf1.tar.bz2
chat-663bec814767fa9c92e7ab2c706c0fe4c0432cf1.zip
Moved logic for searching for posts by multiple users/channels into the sql query
-rw-r--r--api/post_test.go3
-rw-r--r--model/search_params.go67
-rw-r--r--store/sql_post_store.go52
3 files changed, 73 insertions, 49 deletions
diff --git a/api/post_test.go b/api/post_test.go
index 3df622d84..e54e9ef0c 100644
--- a/api/post_test.go
+++ b/api/post_test.go
@@ -546,6 +546,9 @@ func TestSearchPostsFromUser(t *testing.T) {
Client.Must(Client.JoinChannel(channel1.Id))
Client.Must(Client.JoinChannel(channel2.Id))
+ // wait for the join/leave messages to be created for user3 since they're done asynchronously
+ time.Sleep(100 * time.Millisecond)
+
if result := Client.Must(Client.SearchPosts("from: " + user2.Username)).Data.(*model.PostList); len(result.Order) != 3 {
t.Fatalf("wrong number of posts returned %v", len(result.Order))
}
diff --git a/model/search_params.go b/model/search_params.go
index 6b665f5a0..144e8e461 100644
--- a/model/search_params.go
+++ b/model/search_params.go
@@ -8,10 +8,10 @@ import (
)
type SearchParams struct {
- Terms string
- IsHashtag bool
- InChannel string
- FromUser string
+ Terms string
+ IsHashtag bool
+ InChannels []string
+ FromUsers []string
}
var searchFlags = [...]string{"from", "channel", "in"}
@@ -106,45 +106,34 @@ func ParseSearchParams(text string) []*SearchParams {
}
}
- if len(inChannels) == 0 {
- inChannels = append(inChannels, "")
- }
- if len(fromUsers) == 0 {
- fromUsers = append(fromUsers, "")
- }
-
paramsList := []*SearchParams{}
- for _, inChannel := range inChannels {
- for _, fromUser := range fromUsers {
- if len(plainTerms) > 0 {
- paramsList = append(paramsList, &SearchParams{
- Terms: plainTerms,
- IsHashtag: false,
- InChannel: inChannel,
- FromUser: fromUser,
- })
- }
+ if len(plainTerms) > 0 {
+ paramsList = append(paramsList, &SearchParams{
+ Terms: plainTerms,
+ IsHashtag: false,
+ InChannels: inChannels,
+ FromUsers: fromUsers,
+ })
+ }
- if len(hashtagTerms) > 0 {
- paramsList = append(paramsList, &SearchParams{
- Terms: hashtagTerms,
- IsHashtag: true,
- InChannel: inChannel,
- FromUser: fromUser,
- })
- }
+ if len(hashtagTerms) > 0 {
+ paramsList = append(paramsList, &SearchParams{
+ Terms: hashtagTerms,
+ IsHashtag: true,
+ InChannels: inChannels,
+ FromUsers: fromUsers,
+ })
+ }
- // special case for when no terms are specified but we still have a filter
- if len(plainTerms) == 0 && len(hashtagTerms) == 0 {
- paramsList = append(paramsList, &SearchParams{
- Terms: "",
- IsHashtag: true,
- InChannel: inChannel,
- FromUser: fromUser,
- })
- }
- }
+ // special case for when no terms are specified but we still have a filter
+ if len(plainTerms) == 0 && len(hashtagTerms) == 0 {
+ paramsList = append(paramsList, &SearchParams{
+ Terms: "",
+ IsHashtag: true,
+ InChannels: inChannels,
+ FromUsers: fromUsers,
+ })
}
return paramsList
diff --git a/store/sql_post_store.go b/store/sql_post_store.go
index 6971de9d7..ea913ab6a 100644
--- a/store/sql_post_store.go
+++ b/store/sql_post_store.go
@@ -6,6 +6,7 @@ package store
import (
"fmt"
"regexp"
+ "strconv"
"strings"
"github.com/mattermost/platform/model"
@@ -413,10 +414,15 @@ func (s SqlPostStore) Search(teamId string, userId string, params *model.SearchP
go func() {
result := StoreResult{}
+ queryParams := map[string]interface{}{
+ "TeamId": teamId,
+ "UserId": userId,
+ }
+
termMap := map[string]bool{}
terms := params.Terms
- if terms == "" && params.InChannel == "" && params.FromUser == "" {
+ if terms == "" && len(params.InChannels) == 0 && len(params.FromUsers) == 0 {
result.Data = []*model.Post{}
storeChannel <- result
return
@@ -468,13 +474,45 @@ func (s SqlPostStore) Search(teamId string, userId string, params *model.SearchP
ORDER BY CreateAt DESC
LIMIT 100`
- if params.InChannel != "" {
+ if len(params.InChannels) > 1 {
+ inClause := ":InChannel0"
+ queryParams["InChannel0"] = params.InChannels[0]
+
+ for i := 1; i < len(params.InChannels); i++ {
+ paramName := "InChannel" + strconv.FormatInt(int64(i), 10)
+ inClause += ", :" + paramName
+ queryParams[paramName] = params.InChannels[i]
+ }
+
+ searchQuery = strings.Replace(searchQuery, "CHANNEL_FILTER", "AND Name IN ("+inClause+")", 1)
+ } else if len(params.InChannels) == 1 {
+ queryParams["InChannel"] = params.InChannels[0]
searchQuery = strings.Replace(searchQuery, "CHANNEL_FILTER", "AND Name = :InChannel", 1)
} else {
searchQuery = strings.Replace(searchQuery, "CHANNEL_FILTER", "", 1)
}
- if params.FromUser != "" {
+ if len(params.FromUsers) > 1 {
+ inClause := ":FromUser0"
+ queryParams["FromUser0"] = params.FromUsers[0]
+
+ for i := 1; i < len(params.FromUsers); i++ {
+ paramName := "FromUser" + strconv.FormatInt(int64(i), 10)
+ inClause += ", :" + paramName
+ queryParams[paramName] = params.FromUsers[i]
+ }
+
+ searchQuery = strings.Replace(searchQuery, "POST_FILTER", `
+ AND UserId IN (
+ SELECT
+ Id
+ FROM
+ Users
+ WHERE
+ TeamId = :TeamId
+ AND Username IN (`+inClause+`))`, 1)
+ } else if len(params.FromUsers) == 1 {
+ queryParams["FromUser"] = params.FromUsers[0]
searchQuery = strings.Replace(searchQuery, "POST_FILTER", `
AND UserId IN (
SELECT
@@ -506,13 +544,7 @@ func (s SqlPostStore) Search(teamId string, userId string, params *model.SearchP
searchQuery = strings.Replace(searchQuery, "SEARCH_CLAUSE", searchClause, 1)
}
- queryParams := map[string]interface{}{
- "TeamId": teamId,
- "UserId": userId,
- "Terms": terms,
- "InChannel": params.InChannel,
- "FromUser": params.FromUser,
- }
+ queryParams["Terms"] = terms
_, err := s.GetReplica().Select(&posts, searchQuery, queryParams)
if err != nil {