diff options
author | Chris <ccbrown112@gmail.com> | 2017-11-28 15:02:56 -0600 |
---|---|---|
committer | Christopher Speller <crspeller@gmail.com> | 2017-11-28 13:02:56 -0800 |
commit | b87fae646a624507f5b2c1270cae1d3585f589ac (patch) | |
tree | d81b683cda11b08ed04d7e2431b04a84a715d851 /model | |
parent | 27ba68a7894d5204b8d75dc7353774977d62fa15 (diff) | |
download | chat-b87fae646a624507f5b2c1270cae1d3585f589ac.tar.gz chat-b87fae646a624507f5b2c1270cae1d3585f589ac.tar.bz2 chat-b87fae646a624507f5b2c1270cae1d3585f589ac.zip |
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
Diffstat (limited to 'model')
-rw-r--r-- | model/post.go | 17 | ||||
-rw-r--r-- | model/post_test.go | 7 |
2 files changed, 24 insertions, 0 deletions
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 { diff --git a/model/post_test.go b/model/post_test.go index 846c8c775..6a908887d 100644 --- a/model/post_test.go +++ b/model/post_test.go @@ -6,6 +6,8 @@ package model import ( "strings" "testing" + + "github.com/stretchr/testify/assert" ) func TestPostJson(t *testing.T) { @@ -124,6 +126,11 @@ func TestPostIsSystemMessage(t *testing.T) { } } +func TestPostChannelMentions(t *testing.T) { + post := Post{Message: "~a ~b ~b ~c/~d."} + assert.Equal(t, []string{"a", "b", "c", "d"}, post.ChannelMentions()) +} + func TestPostSanitizeProps(t *testing.T) { post1 := &Post{ Message: "test", |