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 /app | |
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 'app')
-rw-r--r-- | app/channel.go | 12 | ||||
-rw-r--r-- | app/post.go | 48 | ||||
-rw-r--r-- | app/post_test.go | 38 |
3 files changed, 98 insertions, 0 deletions
diff --git a/app/channel.go b/app/channel.go index 50067d42d..16c5dd084 100644 --- a/app/channel.go +++ b/app/channel.go @@ -757,6 +757,18 @@ func (a *App) GetChannelByName(channelName, teamId string) (*model.Channel, *mod } } +func (a *App) GetChannelsByNames(channelNames []string, teamId string) ([]*model.Channel, *model.AppError) { + if result := <-a.Srv.Store.Channel().GetByNames(teamId, channelNames, true); result.Err != nil && result.Err.Id == "store.sql_channel.get_by_name.missing.app_error" { + result.Err.StatusCode = http.StatusNotFound + return nil, result.Err + } else if result.Err != nil { + result.Err.StatusCode = http.StatusBadRequest + return nil, result.Err + } else { + return result.Data.([]*model.Channel), nil + } +} + func (a *App) GetChannelByNameForTeamName(channelName, teamName string) (*model.Channel, *model.AppError) { var team *model.Team diff --git a/app/post.go b/app/post.go index 00944ee3b..0bd3b654f 100644 --- a/app/post.go +++ b/app/post.go @@ -152,6 +152,10 @@ func (a *App) CreatePost(post *model.Post, channel *model.Channel, triggerWebhoo post.Hashtags, _ = model.ParseHashtags(post.Message) + if err := a.FillInPostProps(post, channel); err != nil { + return nil, err + } + var rpost *model.Post if result := <-a.Srv.Store.Post().Save(post); result.Err != nil { return nil, result.Err @@ -192,6 +196,46 @@ func (a *App) CreatePost(post *model.Post, channel *model.Channel, triggerWebhoo return rpost, nil } +// FillInPostProps should be invoked before saving posts to fill in properties such as +// channel_mentions. +// +// If channel is nil, FillInPostProps will look up the channel corresponding to the post. +func (a *App) FillInPostProps(post *model.Post, channel *model.Channel) *model.AppError { + channelMentions := post.ChannelMentions() + channelMentionsProp := make(map[string]interface{}) + + if len(channelMentions) > 0 { + if channel == nil { + result := <-a.Srv.Store.Channel().GetForPost(post.Id) + if result.Err == nil { + return model.NewAppError("FillInPostProps", "api.context.invalid_param.app_error", map[string]interface{}{"Name": "post.channel_id"}, result.Err.Error(), http.StatusBadRequest) + } + channel = result.Data.(*model.Channel) + } + + mentionedChannels, err := a.GetChannelsByNames(channelMentions, channel.TeamId) + if err != nil { + return err + } + + for _, mentioned := range mentionedChannels { + if mentioned.Type == model.CHANNEL_OPEN { + channelMentionsProp[mentioned.Name] = map[string]interface{}{ + "display_name": mentioned.DisplayName, + } + } + } + } + + if len(channelMentionsProp) > 0 { + post.AddProp("channel_mentions", channelMentionsProp) + } else if post.Props != nil { + delete(post.Props, "channel_mentions") + } + + return nil +} + func (a *App) handlePostEvents(post *model.Post, user *model.User, channel *model.Channel, triggerWebhooks bool, parentPostList *model.PostList) *model.AppError { var tchan store.StoreChannel if len(channel.TeamId) > 0 { @@ -329,6 +373,10 @@ func (a *App) UpdatePost(post *model.Post, safeUpdate bool) (*model.Post, *model newPost.Props = post.Props } + if err := a.FillInPostProps(post, nil); err != nil { + return nil, err + } + if result := <-a.Srv.Store.Post().Update(newPost, oldPost); result.Err != nil { return nil, result.Err } else { diff --git a/app/post_test.go b/app/post_test.go index e2e9a7261..3b7e8d039 100644 --- a/app/post_test.go +++ b/app/post_test.go @@ -138,3 +138,41 @@ func TestPostAction(t *testing.T) { err = th.App.DoPostAction(post.Id, attachments[0].Actions[0].Id, th.BasicUser.Id) require.Nil(t, err) } + +func TestPostChannelMentions(t *testing.T) { + th := Setup().InitBasic() + defer th.TearDown() + + channel := th.BasicChannel + user := th.BasicUser + + channelToMention, err := th.App.CreateChannel(&model.Channel{ + DisplayName: "Mention Test", + Name: "mention-test", + Type: model.CHANNEL_OPEN, + TeamId: th.BasicTeam.Id, + }, false) + if err != nil { + t.Fatal(err.Error()) + } + defer th.App.PermanentDeleteChannel(channelToMention) + + _, err = th.App.AddUserToChannel(user, channel) + require.Nil(t, err) + + post := &model.Post{ + Message: fmt.Sprintf("hello, ~%v!", channelToMention.Name), + ChannelId: channel.Id, + PendingPostId: model.NewId() + ":" + fmt.Sprint(model.GetMillis()), + UserId: user.Id, + CreateAt: 0, + } + + result, err := th.App.CreatePostAsUser(post) + require.Nil(t, err) + assert.Equal(t, map[string]interface{}{ + "mention-test": map[string]interface{}{ + "display_name": "Mention Test", + }, + }, result.Props["channel_mentions"]) +} |