From 72fc15ddf370acd836736ff3e3cd33c24c126a17 Mon Sep 17 00:00:00 2001 From: nickago Date: Thu, 13 Aug 2015 15:34:56 -0700 Subject: [PATCH 1/3] Added server parsing of wildcards --- store/sql_post_store.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/store/sql_post_store.go b/store/sql_post_store.go index 4ea28507b6..fdff27b828 100644 --- a/store/sql_post_store.go +++ b/store/sql_post_store.go @@ -5,9 +5,12 @@ package store import ( "fmt" + "regexp" + "strconv" + "strings" + "github.com/mattermost/platform/model" "github.com/mattermost/platform/utils" - "strings" ) type SqlPostStore struct { @@ -361,7 +364,7 @@ func (s SqlPostStore) getParentsPosts(channelId string, offset int, limit int) S var posts []*model.Post _, err := s.GetReplica().Select(&posts, - `SELECT + `SELECT q2.* FROM Posts q2 @@ -369,7 +372,7 @@ func (s SqlPostStore) getParentsPosts(channelId string, offset int, limit int) S (SELECT DISTINCT q3.RootId FROM - (SELECT + (SELECT RootId FROM Posts @@ -416,6 +419,12 @@ func (s SqlPostStore) Search(teamId string, userId string, terms string, isHasht // cannot escape it so we replace it. terms = strings.Replace(terms, "@", " ", -1) + // Parse text for wildcards + if wildcard, err := regexp.Compile("\\*($| )"); err == nil { + terms = wildcard.ReplaceAllLiteralString(terms, ":* ") + terms = strings.Replace(terms, " ", " ", -1) + } + var posts []*model.Post if utils.Cfg.SqlSettings.DriverName == "postgres" { From 824917b029826384129e504e83739fcc55541b4f Mon Sep 17 00:00:00 2001 From: nickago Date: Thu, 13 Aug 2015 15:52:52 -0700 Subject: [PATCH 2/3] Added wildcard highlighting and removed redundant whitespace parsing --- store/sql_post_store.go | 12 ++++++------ web/react/utils/utils.jsx | 18 +++++++++++++++--- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/store/sql_post_store.go b/store/sql_post_store.go index fdff27b828..e30691154d 100644 --- a/store/sql_post_store.go +++ b/store/sql_post_store.go @@ -419,15 +419,15 @@ func (s SqlPostStore) Search(teamId string, userId string, terms string, isHasht // cannot escape it so we replace it. terms = strings.Replace(terms, "@", " ", -1) - // Parse text for wildcards - if wildcard, err := regexp.Compile("\\*($| )"); err == nil { - terms = wildcard.ReplaceAllLiteralString(terms, ":* ") - terms = strings.Replace(terms, " ", " ", -1) - } - var posts []*model.Post if utils.Cfg.SqlSettings.DriverName == "postgres" { + + // Parse text for wildcards + if wildcard, err := regexp.Compile("\\*($| )"); err == nil { + terms = wildcard.ReplaceAllLiteralString(terms, ":* ") + } + searchQuery := fmt.Sprintf(`SELECT * FROM diff --git a/web/react/utils/utils.jsx b/web/react/utils/utils.jsx index f0cf17446d..66740d380a 100644 --- a/web/react/utils/utils.jsx +++ b/web/react/utils/utils.jsx @@ -457,9 +457,21 @@ module.exports.textToJsx = function(text, options) { var mentionRegex = /^(?:@)([a-z0-9_]+)$/gi; // looks loop invariant but a weird JS bug needs it to be redefined here var explicitMention = mentionRegex.exec(trimWord); - if ((trimWord.toLowerCase().indexOf(searchTerm) > -1 || word.toLowerCase().indexOf(searchTerm) > -1) && searchTerm != '') { - - highlightSearchClass = ' search-highlight'; + if (searchTerm !== '') { + let searchWords = searchTerm.split(' '); + for (let idx in searchWords) { + let searchWord = searchWords[idx]; + if (searchWord === word.toLowerCase() || searchWord === trimWord.toLowerCase()) { + highlightSearchClass = ' search-highlight'; + break; + } else if (searchWord.charAt(searchWord.length - 1) === '*') { + let searchWordPrefix = searchWord.slice(0,-1); + if (trimWord.toLowerCase().indexOf(searchWordPrefix) > -1 || word.toLowerCase().indexOf(searchWordPrefix) > -1) { + highlightSearchClass = ' search-highlight'; + break; + } + } + } } if (explicitMention && From b69221aa7fec759edb856e7b44ee24769b87486d Mon Sep 17 00:00:00 2001 From: nickago Date: Wed, 26 Aug 2015 06:40:41 -0700 Subject: [PATCH 3/3] Added unit tests --- store/sql_post_store.go | 1 - store/sql_post_store_test.go | 17 ++++++++++------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/store/sql_post_store.go b/store/sql_post_store.go index e30691154d..4a3e7e4ed8 100644 --- a/store/sql_post_store.go +++ b/store/sql_post_store.go @@ -6,7 +6,6 @@ package store import ( "fmt" "regexp" - "strconv" "strings" "github.com/mattermost/platform/model" diff --git a/store/sql_post_store_test.go b/store/sql_post_store_test.go index 8854fb5c40..7754b8275a 100644 --- a/store/sql_post_store_test.go +++ b/store/sql_post_store_test.go @@ -4,11 +4,11 @@ package store import ( - "github.com/mattermost/platform/model" - "github.com/mattermost/platform/utils" "strings" "testing" "time" + + "github.com/mattermost/platform/model" ) func TestPostStoreSave(t *testing.T) { @@ -547,11 +547,9 @@ func TestPostStoreSearch(t *testing.T) { t.Fatal("returned wrong serach result") } - if utils.Cfg.SqlSettings.DriverName == "mysql" { - r5 := (<-store.Post().Search(teamId, userId, "matter*", false)).Data.(*model.PostList) - if len(r5.Order) != 1 && r5.Order[0] != o1.Id { - t.Fatal("returned wrong serach result") - } + r5 := (<-store.Post().Search(teamId, userId, "matter*", false)).Data.(*model.PostList) + if len(r5.Order) != 1 && r5.Order[0] != o1.Id { + t.Fatal("returned wrong serach result") } r6 := (<-store.Post().Search(teamId, userId, "#hashtag", true)).Data.(*model.PostList) @@ -573,4 +571,9 @@ func TestPostStoreSearch(t *testing.T) { if len(r9.Order) != 2 { t.Fatal("returned wrong search result") } + + r10 := (<-store.Post().Search(teamId, userId, "matter* jer*", false)).Data.(*model.PostList) + if len(r10.Order) != 2 { + t.Fatal("returned wrong search result") + } }