Some checks failed
Server CI / Postgres (shard 0) (push) Has been skipped
Server CI / Postgres (shard 3) (push) Has been skipped
BuildEnv Docker Image / build-image (push) Failing after 4s
BuildEnv Docker Image / build-image-fips (push) Failing after 4s
Migration Review & Release Notes / Generate migration review & release notes (push) Failing after 3s
Server CI / Build Environment (push) Failing after 3s
Server CI / Postgres (shard 1) (push) Has been skipped
Tools CI / check-style (mattermost-govet) (push) Failing after 4s
Server CI / Postgres (shard 2) (push) Has been skipped
Tools CI / check-style (mmgotool) (push) Failing after 3s
Server CI / Elasticsearch v8 Compatibility (push) Has been skipped
Server CI / OpenSearch v2 Compatibility (push) Has been skipped
Server CI / Postgres FIPS (shard 0) (push) Has been skipped
Tools CI / Test (mattermost-govet) (push) Failing after 3s
Server CI / Postgres FIPS (shard 1) (push) Has been skipped
Server CI / Postgres FIPS (shard 2) (push) Has been skipped
Tools CI / Test (mmgotool) (push) Failing after 3s
Server CI / Postgres FIPS (shard 3) (push) Has been skipped
Server CI / Run mmctl tests (push) Has been skipped
Server CI / Run mmctl tests (FIPS) (push) Has been skipped
Server CI / Merge Postgres Test Results (push) Has been skipped
Server CI / Merge Postgres FIPS Test Results (push) Has been skipped
Server CI / Check backport migrations (push) Has been skipped
Server CI / Check generated files (push) Has been skipped
Server CI / check-style (push) Has been skipped
Server CI / Vet API (push) Has been skipped
Server CI / Build mattermost server app (push) Has been skipped
Server CI / Server CI Complete (push) Failing after 2s
Server CI / Server CI Artifacts (push) Has been skipped
Server CI / Sentry Upload (push) Has been skipped
Server CI / Server CI Report (push) Failing after 5m1s
Server CI Nightly Race / Race Detector (push) Has been skipped
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Webapp CI Cache Warm / Warm Webapp node_modules Cache (push) Has been cancelled
E2E CI Cache Warm / Warm E2E npm Registry Cache (push) Has been cancelled
API / build (push) Has been cancelled
Web App CI / check-types (push) Has been cancelled
Web App CI / check-lint (push) Has been cancelled
Web App CI / check-i18n (push) Has been cancelled
Web App CI / check-external-links (push) Has been cancelled
Web App CI / test (platform) (push) Has been cancelled
Web App CI / test (mattermost-redux) (push) Has been cancelled
Web App CI / test (channels shard 1/4) (push) Has been cancelled
Web App CI / test (channels shard 2/4) (push) Has been cancelled
Web App CI / test (channels shard 3/4) (push) Has been cancelled
Web App CI / test (channels shard 4/4) (push) Has been cancelled
Web App CI / upload-coverage (push) Has been cancelled
Web App CI / build (push) Has been cancelled
YAML Lint / yamllint (push) Has been cancelled
Push mirrored docker images / cd/Push mirrored docker images (push) Has been cancelled
docs-cd / Build and deploy docs (push) Has been cancelled
Docs CI / build (push) Has been cancelled
E2E Cache Warm: Cypress deps / Warm Cypress deps cache (push) Has been cancelled
Scorecards supply-chain security / Scorecard analysis (push) Has been cancelled
Server CI Weekly / Postgres FIPS (push) Has been skipped
Server CI Weekly / Run mmctl tests (FIPS) (push) Has been skipped
Server CI Weekly / Postgres with binary parameters (push) Has been skipped
Server CI Weekly / Build Environment (push) Failing after 1s
Server CI Cache Warm / Warm Go Module and Build Cache (push) Failing after 2s
Server CI Nightly Race / Build Environment (push) Failing after 1s
171 lines
4.8 KiB
Go
171 lines
4.8 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
package app
|
|
|
|
import (
|
|
"strings"
|
|
"unicode"
|
|
"unicode/utf8"
|
|
)
|
|
|
|
// Have the compiler confirm *StandardMentionParser implements MentionParser
|
|
var _ MentionParser = &StandardMentionParser{}
|
|
|
|
type StandardMentionParser struct {
|
|
keywords MentionKeywords
|
|
|
|
// multibyteKeywords holds the subset of keywords containing a multibyte character. It's computed
|
|
// once here because isKeywordMultibyte needs it for every word of every post, and recomputing it
|
|
// there makes parsing cost the product of the number of keywords and the number of words.
|
|
multibyteKeywords []string
|
|
|
|
results *MentionResults
|
|
}
|
|
|
|
func makeStandardMentionParser(keywords MentionKeywords) *StandardMentionParser {
|
|
var multibyteKeywords []string
|
|
for keyword := range keywords {
|
|
if len(keyword) != utf8.RuneCountInString(keyword) {
|
|
multibyteKeywords = append(multibyteKeywords, keyword)
|
|
}
|
|
}
|
|
|
|
return &StandardMentionParser{
|
|
keywords: keywords,
|
|
multibyteKeywords: multibyteKeywords,
|
|
|
|
results: &MentionResults{},
|
|
}
|
|
}
|
|
|
|
// Processes text to filter mentioned users and other potential mentions
|
|
func (p *StandardMentionParser) ProcessText(text string) {
|
|
systemMentions := map[string]bool{"@here": true, "@channel": true, "@all": true}
|
|
|
|
for _, word := range strings.FieldsFunc(text, func(c rune) bool {
|
|
// Split on any whitespace or punctuation that can't be part of an at mention or emoji pattern
|
|
return !(c == ':' || c == '.' || c == '-' || c == '_' || c == '@' || unicode.IsLetter(c) || unicode.IsNumber(c))
|
|
}) {
|
|
// skip word with format ':word:' with an assumption that it is an emoji format only
|
|
if word[0] == ':' && word[len(word)-1] == ':' {
|
|
continue
|
|
}
|
|
|
|
word = strings.TrimLeft(word, ":.-_")
|
|
|
|
if p.checkForMention(word) {
|
|
continue
|
|
}
|
|
|
|
foundWithoutSuffix := false
|
|
wordWithoutSuffix := word
|
|
|
|
for wordWithoutSuffix != "" && strings.LastIndexAny(wordWithoutSuffix, ".-:_") == (len(wordWithoutSuffix)-1) {
|
|
wordWithoutSuffix = wordWithoutSuffix[0 : len(wordWithoutSuffix)-1]
|
|
|
|
if p.checkForMention(wordWithoutSuffix) {
|
|
foundWithoutSuffix = true
|
|
break
|
|
}
|
|
}
|
|
|
|
if foundWithoutSuffix {
|
|
continue
|
|
}
|
|
|
|
if _, ok := systemMentions[word]; !ok && strings.HasPrefix(word, "@") {
|
|
// No need to bother about unicode as we are looking for ASCII characters.
|
|
last := word[len(word)-1]
|
|
switch last {
|
|
// If the word is possibly at the end of a sentence, remove that character.
|
|
case '.', '-', ':':
|
|
word = word[:len(word)-1]
|
|
}
|
|
p.results.OtherPotentialMentions = append(p.results.OtherPotentialMentions, word[1:])
|
|
} else if strings.ContainsAny(word, ".-:") {
|
|
// This word contains a character that may be the end of a sentence, so split further
|
|
splitWords := strings.FieldsFunc(word, func(c rune) bool {
|
|
return c == '.' || c == '-' || c == ':'
|
|
})
|
|
|
|
for _, splitWord := range splitWords {
|
|
if p.checkForMention(splitWord) {
|
|
continue
|
|
}
|
|
if _, ok := systemMentions[splitWord]; !ok && strings.HasPrefix(splitWord, "@") {
|
|
p.results.OtherPotentialMentions = append(p.results.OtherPotentialMentions, splitWord[1:])
|
|
}
|
|
}
|
|
}
|
|
|
|
if ids, match := p.isKeywordMultibyte(word); match {
|
|
p.addMentions(ids, KeywordMention)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (p *StandardMentionParser) Results() *MentionResults {
|
|
return p.results
|
|
}
|
|
|
|
// checkForMention checks if there is a mention to a specific user or to the keywords here / channel / all
|
|
func (p *StandardMentionParser) checkForMention(word string) bool {
|
|
var mentionType MentionType
|
|
|
|
switch strings.ToLower(word) {
|
|
case "@here":
|
|
p.results.HereMentioned = true
|
|
mentionType = ChannelMention
|
|
case "@channel":
|
|
p.results.ChannelMentioned = true
|
|
mentionType = ChannelMention
|
|
case "@all":
|
|
p.results.AllMentioned = true
|
|
mentionType = ChannelMention
|
|
default:
|
|
mentionType = KeywordMention
|
|
}
|
|
|
|
if ids, match := p.keywords[strings.ToLower(word)]; match {
|
|
p.addMentions(ids, mentionType)
|
|
return true
|
|
}
|
|
|
|
// Case-sensitive check for first name
|
|
if ids, match := p.keywords[word]; match {
|
|
p.addMentions(ids, mentionType)
|
|
return true
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
func (p *StandardMentionParser) addMentions(ids []MentionableID, mentionType MentionType) {
|
|
for _, id := range ids {
|
|
if userID, ok := id.AsUserID(); ok {
|
|
p.results.addMention(userID, mentionType)
|
|
} else if groupID, ok := id.AsGroupID(); ok {
|
|
p.results.addGroupMention(groupID)
|
|
}
|
|
}
|
|
}
|
|
|
|
// isKeywordMultibyte checks if a word containing a multibyte character contains a multibyte keyword
|
|
func (p *StandardMentionParser) isKeywordMultibyte(word string) ([]MentionableID, bool) {
|
|
ids := []MentionableID{}
|
|
match := false
|
|
|
|
if len(p.multibyteKeywords) == 0 || len(word) == utf8.RuneCountInString(word) {
|
|
return ids, match
|
|
}
|
|
|
|
for _, key := range p.multibyteKeywords {
|
|
if strings.Contains(word, key) {
|
|
ids, match = p.keywords[key]
|
|
}
|
|
}
|
|
|
|
return ids, match
|
|
}
|