diff options
author | Joram Wilander <jwawilander@gmail.com> | 2016-10-31 09:10:20 -0400 |
---|---|---|
committer | Christopher Speller <crspeller@gmail.com> | 2016-10-31 09:10:20 -0400 |
commit | 4887c9228cb90d6a9bfe57c7e21aa507958de4f3 (patch) | |
tree | 73fd963f7e8d22954a8ec60e8afce6807f72c7a1 /store | |
parent | 60a92f0a270bcef2ee8560bdd276aadd8fdd3697 (diff) | |
download | chat-4887c9228cb90d6a9bfe57c7e21aa507958de4f3.tar.gz chat-4887c9228cb90d6a9bfe57c7e21aa507958de4f3.tar.bz2 chat-4887c9228cb90d6a9bfe57c7e21aa507958de4f3.zip |
Fix user autocomplete with special characters (#4373)
Diffstat (limited to 'store')
-rw-r--r-- | store/sql_user_store.go | 25 | ||||
-rw-r--r-- | store/sql_user_store_test.go | 24 |
2 files changed, 47 insertions, 2 deletions
diff --git a/store/sql_user_store.go b/store/sql_user_store.go index c1fb9b28d..4f5e11d00 100644 --- a/store/sql_user_store.go +++ b/store/sql_user_store.go @@ -1196,15 +1196,36 @@ func (us SqlUserStore) SearchInChannel(channelId string, term string, searchType func (us SqlUserStore) performSearch(searchQuery string, term string, searchType string, parameters map[string]interface{}) StoreResult { result := StoreResult{} + // these chars have special meaning and can be treated as spaces + for _, c := range specialSearchChar { + term = strings.Replace(term, c, " ", -1) + } + if term == "" { searchQuery = strings.Replace(searchQuery, "SEARCH_CLAUSE", "", 1) } else if utils.Cfg.SqlSettings.DriverName == model.DATABASE_DRIVER_POSTGRES { - term = term + ":*" + splitTerm := strings.Fields(term) + for i, t := range strings.Fields(term) { + if i == len(splitTerm)-1 { + splitTerm[i] = t + ":*" + } else { + splitTerm[i] = t + ":* &" + } + } + + term = strings.Join(splitTerm, " ") + searchType = convertMySQLFullTextColumnsToPostgres(searchType) searchClause := fmt.Sprintf("AND (%s) @@ to_tsquery(:Term)", searchType) searchQuery = strings.Replace(searchQuery, "SEARCH_CLAUSE", searchClause, 1) } else if utils.Cfg.SqlSettings.DriverName == model.DATABASE_DRIVER_MYSQL { - term = term + "*" + splitTerm := strings.Fields(term) + for i, t := range strings.Fields(term) { + splitTerm[i] = "+" + t + "*" + } + + term = strings.Join(splitTerm, " ") + searchClause := fmt.Sprintf("AND MATCH(%s) AGAINST (:Term IN BOOLEAN MODE)", searchType) searchQuery = strings.Replace(searchQuery, "SEARCH_CLAUSE", searchClause, 1) } diff --git a/store/sql_user_store_test.go b/store/sql_user_store_test.go index 7ffb68a47..23c124cb7 100644 --- a/store/sql_user_store_test.go +++ b/store/sql_user_store_test.go @@ -937,8 +937,14 @@ func TestUserStoreSearch(t *testing.T) { u1.Email = model.NewId() Must(store.User().Save(u1)) + u2 := &model.User{} + u2.Username = "jim-bobby" + model.NewId() + u2.Email = model.NewId() + Must(store.User().Save(u2)) + tid := model.NewId() Must(store.Team().SaveMember(&model.TeamMember{TeamId: tid, UserId: u1.Id})) + Must(store.Team().SaveMember(&model.TeamMember{TeamId: tid, UserId: u2.Id})) if r1 := <-store.User().Search(tid, "jimb", USER_SEARCH_TYPE_USERNAME); r1.Err != nil { t.Fatal(r1.Err) @@ -974,6 +980,24 @@ func TestUserStoreSearch(t *testing.T) { } } + if r1 := <-store.User().Search("", "jim-bobb", USER_SEARCH_TYPE_USERNAME); r1.Err != nil { + t.Fatal(r1.Err) + } else { + profiles := r1.Data.([]*model.User) + found := false + for _, profile := range profiles { + t.Log(profile.Username) + if profile.Id == u2.Id { + found = true + break + } + } + + if !found { + t.Fatal("should have found user") + } + } + if r1 := <-store.User().Search(tid, "", USER_SEARCH_TYPE_USERNAME); r1.Err != nil { t.Fatal(r1.Err) } |