[MM-24526] Filter * characters from the search terms in DB (#14884)

Этот коммит содержится в:
Amarjeet Anand
2020-08-05 17:13:31 +05:30
коммит произвёл GitHub
родитель 7f64199a37
Коммит fb453d578f
4 изменённых файлов: 67 добавлений и 6 удалений

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

@@ -7,6 +7,7 @@ import (
"database/sql"
"strconv"
"strings"
"unicode"
"github.com/mattermost/gorp"
"github.com/mattermost/mattermost-server/v5/mlog"
@@ -52,3 +53,38 @@ func finalizeTransaction(transaction *gorp.Transaction) {
mlog.Error("Failed to rollback transaction", mlog.Err(err))
}
}
// removeNonAlphaNumericUnquotedTerms removes all unquoted words that only contain
// non-alphanumeric chars from given line
func removeNonAlphaNumericUnquotedTerms(line, separator string) string {
words := strings.Split(line, separator)
filteredResult := make([]string, 0, len(words))
for _, w := range words {
if isQuotedWord(w) || containsAlphaNumericChar(w) {
filteredResult = append(filteredResult, strings.TrimSpace(w))
}
}
return strings.Join(filteredResult, separator)
}
// containsAlphaNumericChar returns true in case any letter or digit is present, false otherwise
func containsAlphaNumericChar(s string) bool {
for _, r := range s {
if unicode.IsLetter(r) || unicode.IsDigit(r) {
return true
}
}
return false
}
// isQuotedWord return true if the input string is quoted, false otherwise. Ex :-
// "quoted string" - will return true
// unquoted string - will return false
func isQuotedWord(s string) bool {
if len(s) < 2 {
return false
}
return s[0] == '"' && s[len(s)-1] == '"'
}