From 40b20c3eafa25fe4346903a6297d8b96e5b4bd40 Mon Sep 17 00:00:00 2001 From: Hossein Ahmadian-Yazdi Date: Tue, 5 May 2020 06:00:59 -0400 Subject: [PATCH] [MM-24177] Adds props to post for front end to know if highlight or not (#14348) * Add props to post * Address PR comments * address PR comments * Fix styling problems * Address PR comments * address PR comments * Add PR suggestion * fix usage * fix error * remove err * fix golang lint --- app/post.go | 5 +++ app/post_test.go | 89 ++++++++++++++++++++++++++++++++++++++++++++++++ model/post.go | 3 ++ 3 files changed, 97 insertions(+) diff --git a/app/post.go b/app/post.go index 9d02450a4e..e325d17b0d 100644 --- a/app/post.go +++ b/app/post.go @@ -396,6 +396,11 @@ func (a *App) FillInPostProps(post *model.Post, channel *model.Channel) *model.A post.DelProp("channel_mentions") } + matched := model.AT_MENTION_PATTEN.MatchString(post.Message) + if a.License() != nil && *a.License().Features.LDAPGroups && matched && !a.HasPermissionToChannel(post.UserId, post.ChannelId, model.PERMISSION_USE_GROUP_MENTIONS) { + post.AddProp(model.POST_PROPS_GROUP_HIGHLIGHT_DISABLED, true) + } + return nil } diff --git a/app/post_test.go b/app/post_test.go index c675098b6c..e22c675923 100644 --- a/app/post_test.go +++ b/app/post_test.go @@ -1755,3 +1755,92 @@ func TestCountMentionsFromPost(t *testing.T) { assert.Equal(t, numPosts, count) }) } + +func TestFillInPostProps(t *testing.T) { + t.Run("should not add disable group highlight to post props for user with group mention permissions", func(t *testing.T) { + th := Setup(t).InitBasic() + defer th.TearDown() + th.App.SetLicense(model.NewTestLicense("ldap")) + + user1 := th.BasicUser + + channel := th.CreateChannel(th.BasicTeam) + + post1, err := th.App.CreatePost(&model.Post{ + UserId: user1.Id, + ChannelId: channel.Id, + Message: "test123123 @group1 @group2 blah blah blah", + }, channel, false) + require.Nil(t, err) + + err = th.App.FillInPostProps(post1, channel) + + assert.Nil(t, err) + assert.Equal(t, post1.Props, model.StringInterface{}) + }) + + t.Run("should not add disable group highlight to post props for app without license", func(t *testing.T) { + th := Setup(t).InitBasic() + defer th.TearDown() + + id := model.NewId() + guest := &model.User{ + Email: "success+" + id + "@simulator.amazonses.com", + Username: "un_" + id, + Nickname: "nn_" + id, + Password: "Password1", + EmailVerified: true, + } + guest, err := th.App.CreateGuest(guest) + require.Nil(t, err) + th.LinkUserToTeam(guest, th.BasicTeam) + + channel := th.CreateChannel(th.BasicTeam) + th.AddUserToChannel(guest, channel) + + post1, err := th.App.CreatePost(&model.Post{ + UserId: guest.Id, + ChannelId: channel.Id, + Message: "test123123 @group1 @group2 blah blah blah", + }, channel, false) + require.Nil(t, err) + + err = th.App.FillInPostProps(post1, channel) + + assert.Nil(t, err) + assert.Equal(t, post1.Props, model.StringInterface{}) + }) + + t.Run("should add disable group highlight to post props for guest user", func(t *testing.T) { + th := Setup(t).InitBasic() + defer th.TearDown() + th.App.SetLicense(model.NewTestLicense("ldap")) + + id := model.NewId() + guest := &model.User{ + Email: "success+" + id + "@simulator.amazonses.com", + Username: "un_" + id, + Nickname: "nn_" + id, + Password: "Password1", + EmailVerified: true, + } + guest, err := th.App.CreateGuest(guest) + require.Nil(t, err) + th.LinkUserToTeam(guest, th.BasicTeam) + + channel := th.CreateChannel(th.BasicTeam) + th.AddUserToChannel(guest, channel) + + post1, err := th.App.CreatePost(&model.Post{ + UserId: guest.Id, + ChannelId: channel.Id, + Message: "test123123 @group1 @group2 blah blah blah", + }, channel, false) + require.Nil(t, err) + + err = th.App.FillInPostProps(post1, channel) + + assert.Nil(t, err) + assert.Equal(t, post1.Props, model.StringInterface{"disable_group_highlight": true}) + }) +} diff --git a/model/post.go b/model/post.go index eaa58357d3..d932ff2c10 100644 --- a/model/post.go +++ b/model/post.go @@ -63,8 +63,11 @@ const ( POST_PROPS_OVERRIDE_ICON_EMOJI = "override_icon_emoji" POST_PROPS_MENTION_HIGHLIGHT_DISABLED = "mentionHighlightDisabled" + POST_PROPS_GROUP_HIGHLIGHT_DISABLED = "disable_group_highlight" ) +var AT_MENTION_PATTEN = regexp.MustCompile(`\B@`) + type Post struct { Id string `json:"id"` CreateAt int64 `json:"create_at"`