[PLT-7362] Option to add user to channel if mentioned user is not currently in the channel (#7619)

* Option to add user to channel if mentioned user is not currently in the channel

* instead of link from server, just add component on client side to add channel member

* change implementation using post.props

* do clean up and add test

* sanitize post.props['add_channel_member'] on post creation

* move sanitize to app.CreatePost and also apply to app.UpdatePost
This commit is contained in:
Saturnino Abril
2017-10-21 01:38:26 +08:00
committed by GitHub
parent cbb18479e9
commit 18ee375860
7 changed files with 185 additions and 8 deletions

View File

@@ -24,7 +24,7 @@ func TestCreatePost(t *testing.T) {
defer th.TearDown()
Client := th.Client
post := &model.Post{ChannelId: th.BasicChannel.Id, Message: "#hashtag a" + model.NewId() + "a"}
post := &model.Post{ChannelId: th.BasicChannel.Id, Message: "#hashtag a" + model.NewId() + "a", Props: model.StringInterface{model.PROPS_ADD_CHANNEL_MEMBER: "no good"}}
rpost, resp := Client.CreatePost(post)
CheckNoError(t, resp)
CheckCreatedStatus(t, resp)
@@ -45,6 +45,10 @@ func TestCreatePost(t *testing.T) {
t.Fatal("newly created post shouldn't have EditAt set")
}
if rpost.Props[model.PROPS_ADD_CHANNEL_MEMBER] != nil {
t.Fatal("newly created post shouldn't have Props['add_channel_member'] set")
}
post.RootId = rpost.Id
post.ParentId = rpost.Id
_, resp = Client.CreatePost(post)
@@ -371,6 +375,73 @@ func TestCreatePostAll(t *testing.T) {
CheckForbiddenStatus(t, resp)
}
func TestCreatePostSendOutOfChannelMentions(t *testing.T) {
th := Setup().InitBasic().InitSystemAdmin()
defer th.TearDown()
Client := th.Client
WebSocketClient, err := th.CreateWebSocketClient()
if err != nil {
t.Fatal(err)
}
WebSocketClient.Listen()
inChannelUser := th.CreateUser()
th.LinkUserToTeam(inChannelUser, th.BasicTeam)
th.App.AddUserToChannel(inChannelUser, th.BasicChannel)
post1 := &model.Post{ChannelId: th.BasicChannel.Id, Message: "@" + inChannelUser.Username}
_, resp := Client.CreatePost(post1)
CheckNoError(t, resp)
CheckCreatedStatus(t, resp)
timeout := time.After(300 * time.Millisecond)
waiting := true
for waiting {
select {
case event := <-WebSocketClient.EventChannel:
if event.Event == model.WEBSOCKET_EVENT_EPHEMERAL_MESSAGE {
t.Fatal("should not have ephemeral message event")
}
case <-timeout:
waiting = false
}
}
outOfChannelUser := th.CreateUser()
th.LinkUserToTeam(outOfChannelUser, th.BasicTeam)
post2 := &model.Post{ChannelId: th.BasicChannel.Id, Message: "@" + outOfChannelUser.Username}
_, resp = Client.CreatePost(post2)
CheckNoError(t, resp)
CheckCreatedStatus(t, resp)
timeout = time.After(300 * time.Millisecond)
waiting = true
for waiting {
select {
case event := <-WebSocketClient.EventChannel:
if event.Event != model.WEBSOCKET_EVENT_EPHEMERAL_MESSAGE {
// Ignore any other events
continue
}
wpost := model.PostFromJson(strings.NewReader(event.Data["post"].(string)))
if acm, ok := wpost.Props[model.PROPS_ADD_CHANNEL_MEMBER].(map[string]interface{}); !ok {
t.Fatal("should have received ephemeral post with 'add_channel_member' in props")
} else {
if acm["post_id"] == nil || acm["user_ids"] == nil || acm["usernames"] == nil {
t.Fatal("should not be nil")
}
}
waiting = false
case <-timeout:
t.Fatal("timed out waiting for ephemeral message event")
}
}
}
func TestUpdatePost(t *testing.T) {
th := Setup().InitBasic().InitSystemAdmin()
defer th.TearDown()
@@ -421,6 +492,7 @@ func TestUpdatePost(t *testing.T) {
msg1 := "#hashtag a" + model.NewId() + " update post again"
rpost.Message = msg1
rpost.Props[model.PROPS_ADD_CHANNEL_MEMBER] = "no good"
rrupost, resp := Client.UpdatePost(rpost.Id, rpost)
CheckNoError(t, resp)
@@ -428,6 +500,10 @@ func TestUpdatePost(t *testing.T) {
t.Fatal("failed to updates")
}
if rrupost.Props[model.PROPS_ADD_CHANNEL_MEMBER] != nil {
t.Fatal("failed to sanitize Props['add_channel_member'], should be nil")
}
rpost2, err := th.App.CreatePost(&model.Post{ChannelId: channel.Id, Message: "zz" + model.NewId() + "a", Type: model.POST_JOIN_LEAVE, UserId: th.BasicUser.Id}, channel, false)
if err != nil {
t.Fatal(err)