From b87fae646a624507f5b2c1270cae1d3585f589ac Mon Sep 17 00:00:00 2001 From: Chris Date: Tue, 28 Nov 2017 15:02:56 -0600 Subject: PLT-5458: If someone posts a channel link to channel_A that you don't belong to, it doesn't render properly (#7833) * add channel link hints to post props * optimization * update regex, add unit test * fix rebase issue --- model/post.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'model/post.go') diff --git a/model/post.go b/model/post.go index 8e4689eb7..b7b38e7ad 100644 --- a/model/post.go +++ b/model/post.go @@ -7,6 +7,7 @@ import ( "encoding/json" "io" "net/http" + "regexp" "strings" "unicode/utf8" ) @@ -294,6 +295,22 @@ func PostPatchFromJson(data io.Reader) *PostPatch { return &post } +var channelMentionRegexp = regexp.MustCompile(`\B~[a-zA-Z0-9\-_]+`) + +func (o *Post) ChannelMentions() (names []string) { + if strings.Contains(o.Message, "~") { + alreadyMentioned := make(map[string]bool) + for _, match := range channelMentionRegexp.FindAllString(o.Message, -1) { + name := match[1:] + if !alreadyMentioned[name] { + names = append(names, name) + alreadyMentioned[name] = true + } + } + } + return +} + func (r *PostActionIntegrationRequest) ToJson() string { b, err := json.Marshal(r) if err != nil { -- cgit v1.2.3-1-g7c22