[MM-54357] Recent Mentions is showing posts for other similar named users. (#25010)

* Handle double quotes in Postgres
* quote the username when performing the search
Этот коммит содержится в:
Vishal
2023-11-02 11:05:44 +05:30
коммит произвёл GitHub
родитель e0dd8e3d3e
Коммит dfb561a641
3 изменённых файлов: 146 добавлений и 32 удалений

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

@@ -24,6 +24,9 @@ import (
"github.com/mattermost/mattermost/server/v8/einterfaces"
)
// Regex to get quoted strings
var quotedStringsRegex = regexp.MustCompile(`("[^"]*")`)
type SqlPostStore struct {
*SqlStore
metrics einterfaces.MetricsInterface
@@ -2029,27 +2032,38 @@ func (s *SqlPostStore) search(teamId string, userId string, params *model.Search
excludedTerms = wildcard.ReplaceAllLiteralString(excludedTerms, ":* ")
}
excludeClause := ""
if excludedTerms != "" {
excludeClause = " & !(" + strings.Join(strings.Fields(excludedTerms), " | ") + ")"
}
var termsClause string
if params.OrTerms {
termsClause = "(" + strings.Join(strings.Fields(terms), " | ") + ")" + excludeClause
} else if strings.HasPrefix(terms, `"`) && strings.HasSuffix(terms, `"`) {
termsClause = "(" + strings.Join(strings.Fields(terms), " <-> ") + ")" + excludeClause
} else {
tsVectorSearchQuery := strings.Join(strings.Fields(terms), " & ")
if params.IsHashtag {
tsVectorSearchQuery = terms
// Replace spaces with to_tsquery symbols
replaceSpaces := func(input string, excludedInput bool) string {
if input == "" {
return input
}
termsClause = "(" + tsVectorSearchQuery + ")" + excludeClause
// Remove extra spaces
input = strings.Join(strings.Fields(input), " ")
// Replace spaces within quoted strings with '<->'
input = quotedStringsRegex.ReplaceAllStringFunc(input, func(match string) string {
return strings.Replace(match, " ", "<->", -1)
})
// Replace spaces outside of quoted substrings with '&' or '|'
replacer := "&"
if excludedInput || params.OrTerms {
replacer = "|"
}
input = strings.Replace(input, " ", replacer, -1)
return input
}
tsQueryClause := replaceSpaces(terms, false)
excludedClause := replaceSpaces(excludedTerms, true)
if excludedClause != "" {
tsQueryClause += " &!(" + excludedClause + ")"
}
searchClause := fmt.Sprintf("to_tsvector('%[1]s', %[2]s) @@ to_tsquery('%[1]s', ?)", s.pgDefaultTextSearchConfig, searchType)
baseQuery = baseQuery.Where(searchClause, termsClause)
baseQuery = baseQuery.Where(searchClause, tsQueryClause)
} else if s.DriverName() == model.DatabaseDriverMysql {
if searchType == "Message" {
terms, err = removeMysqlStopWordsFromTerms(terms)