diff options
author | Daniel Schalla <daniel@schalla.me> | 2018-07-20 23:00:58 +0200 |
---|---|---|
committer | Harrison Healey <harrisonmhealey@gmail.com> | 2018-07-20 17:00:58 -0400 |
commit | 3539a9a60b24bd9c0c1360b17c8fe3e6ebf8cf3c (patch) | |
tree | d4913b503841b94b435d75febabb43a0e55c4ed3 /app | |
parent | 6104c37761deb8f06ea4af8838db12b8158318be (diff) | |
download | chat-3539a9a60b24bd9c0c1360b17c8fe3e6ebf8cf3c.tar.gz chat-3539a9a60b24bd9c0c1360b17c8fe3e6ebf8cf3c.tar.bz2 chat-3539a9a60b24bd9c0c1360b17c8fe3e6ebf8cf3c.zip |
Default Channel Functionality + Test Suite (#9068)
gofmt
Make skipping for non public channels default
Deduplication of Default Channels; Only post join to townsquare
Post join channel message for all custom default channels
Diffstat (limited to 'app')
-rw-r--r-- | app/channel.go | 109 | ||||
-rw-r--r-- | app/channel_test.go | 30 | ||||
-rw-r--r-- | app/diagnostics.go | 1 |
3 files changed, 86 insertions, 54 deletions
diff --git a/app/channel.go b/app/channel.go index ce183e6a9..619cc09eb 100644 --- a/app/channel.go +++ b/app/channel.go @@ -45,71 +45,72 @@ func (a *App) JoinDefaultChannels(teamId string, user *model.User, shouldBeAdmin } } - if result := <-a.Srv.Store.Channel().GetByName(teamId, "town-square", true); result.Err != nil { - err = result.Err - } else { - townSquare := result.Data.(*model.Channel) - - cm := &model.ChannelMember{ - ChannelId: townSquare.Id, - UserId: user.Id, - SchemeUser: true, - SchemeAdmin: shouldBeAdmin, - NotifyProps: model.GetDefaultChannelNotifyProps(), - } - - if cmResult := <-a.Srv.Store.Channel().SaveMember(cm); cmResult.Err != nil { - err = cmResult.Err - } - if result := <-a.Srv.Store.ChannelMemberHistory().LogJoinEvent(user.Id, townSquare.Id, model.GetMillis()); result.Err != nil { - mlog.Warn(fmt.Sprintf("Failed to update ChannelMemberHistory table %v", result.Err)) - } + defaultChannelList := []string{"town-square"} - if *a.Config().ServiceSettings.ExperimentalEnableDefaultChannelLeaveJoinMessages { - if requestor == nil { - if err := a.postJoinTeamMessage(user, townSquare); err != nil { - mlog.Error(fmt.Sprint("Failed to post join/leave message", err)) - } - } else { - if err := a.postAddToTeamMessage(requestor, user, townSquare, ""); err != nil { - mlog.Error(fmt.Sprint("Failed to post join/leave message", err)) - } + if len(a.Config().TeamSettings.ExperimentalDefaultChannels) == 0 { + defaultChannelList = append(defaultChannelList, "off-topic") + } else { + seenChannels := map[string]bool{} + for _, channelName := range a.Config().TeamSettings.ExperimentalDefaultChannels { + if seenChannels[channelName] != true { + defaultChannelList = append(defaultChannelList, channelName) + seenChannels[channelName] = true } } - - a.InvalidateCacheForChannelMembers(result.Data.(*model.Channel).Id) } - if result := <-a.Srv.Store.Channel().GetByName(teamId, "off-topic", true); result.Err != nil { - err = result.Err - } else if offTopic := result.Data.(*model.Channel); offTopic.Type == model.CHANNEL_OPEN { + for _, channelName := range defaultChannelList { + if result := <-a.Srv.Store.Channel().GetByName(teamId, channelName, true); result.Err != nil { + err = result.Err + } else { - cm := &model.ChannelMember{ - ChannelId: offTopic.Id, - UserId: user.Id, - SchemeUser: true, - SchemeAdmin: shouldBeAdmin, - NotifyProps: model.GetDefaultChannelNotifyProps(), - } + channel := result.Data.(*model.Channel) - if cmResult := <-a.Srv.Store.Channel().SaveMember(cm); cmResult.Err != nil { - err = cmResult.Err - } - if result := <-a.Srv.Store.ChannelMemberHistory().LogJoinEvent(user.Id, offTopic.Id, model.GetMillis()); result.Err != nil { - mlog.Warn(fmt.Sprintf("Failed to update ChannelMemberHistory table %v", result.Err)) - } + if channel.Type != model.CHANNEL_OPEN { + continue + } + + cm := &model.ChannelMember{ + ChannelId: channel.Id, + UserId: user.Id, + SchemeUser: true, + SchemeAdmin: shouldBeAdmin, + NotifyProps: model.GetDefaultChannelNotifyProps(), + } - if requestor == nil { - if err := a.postJoinChannelMessage(user, offTopic); err != nil { - mlog.Error(fmt.Sprint("Failed to post join/leave message", err)) + if cmResult := <-a.Srv.Store.Channel().SaveMember(cm); cmResult.Err != nil { + err = cmResult.Err } - } else { - if err := a.PostAddToChannelMessage(requestor, user, offTopic, ""); err != nil { - mlog.Error(fmt.Sprint("Failed to post join/leave message", err)) + if result := <-a.Srv.Store.ChannelMemberHistory().LogJoinEvent(user.Id, channel.Id, model.GetMillis()); result.Err != nil { + mlog.Warn(fmt.Sprintf("Failed to update ChannelMemberHistory table %v", result.Err)) } - } - a.InvalidateCacheForChannelMembers(result.Data.(*model.Channel).Id) + if *a.Config().ServiceSettings.ExperimentalEnableDefaultChannelLeaveJoinMessages { + if channel.Name == model.DEFAULT_CHANNEL { + if requestor == nil { + if err := a.postJoinTeamMessage(user, channel); err != nil { + mlog.Error(fmt.Sprint("Failed to post join/leave message", err)) + } + } else { + if err := a.postAddToTeamMessage(requestor, user, channel, ""); err != nil { + mlog.Error(fmt.Sprint("Failed to post join/leave message", err)) + } + } + } else { + if requestor == nil { + if err := a.postJoinChannelMessage(user, channel); err != nil { + mlog.Error(fmt.Sprint("Failed to post join/leave message", err)) + } + } else { + if err := a.PostAddToChannelMessage(requestor, user, channel, ""); err != nil { + mlog.Error(fmt.Sprint("Failed to post join/leave message", err)) + } + } + } + } + + a.InvalidateCacheForChannelMembers(result.Data.(*model.Channel).Id) + } } return err diff --git a/app/channel_test.go b/app/channel_test.go index 82c9e07ad..fa9520b52 100644 --- a/app/channel_test.go +++ b/app/channel_test.go @@ -163,6 +163,36 @@ func TestJoinDefaultChannelsCreatesChannelMemberHistoryRecordOffTopic(t *testing assert.True(t, found) } +func TestJoinDefaultChannelsExperimentalDefaultChannels(t *testing.T) { + th := Setup().InitBasic() + defer th.TearDown() + + basicChannel2 := th.CreateChannel(th.BasicTeam) + defaultChannelList := []string{th.BasicChannel.Name, basicChannel2.Name, basicChannel2.Name} + th.App.Config().TeamSettings.ExperimentalDefaultChannels = defaultChannelList + + user := th.CreateUser() + th.App.JoinDefaultChannels(th.BasicTeam.Id, user, false, "") + + for _, channelName := range defaultChannelList { + channel, err := th.App.GetChannelByName(channelName, th.BasicTeam.Id) + + if err != nil { + t.Errorf("Expected nil, got %s", err) + } + + member, err := th.App.GetChannelMember(channel.Id, user.Id) + + if member == nil { + t.Errorf("Expected member object, got nil") + } + + if err != nil { + t.Errorf("Expected nil object, got %s", err) + } + } +} + func TestCreateChannelPublicCreatesChannelMemberHistoryRecord(t *testing.T) { th := Setup().InitBasic() defer th.TearDown() diff --git a/app/diagnostics.go b/app/diagnostics.go index 72195b3d1..1aae97f0f 100644 --- a/app/diagnostics.go +++ b/app/diagnostics.go @@ -283,6 +283,7 @@ func (a *App) trackConfig() { "experimental_town_square_is_hidden_in_lhs": *cfg.TeamSettings.ExperimentalHideTownSquareinLHS, "experimental_town_square_is_read_only": *cfg.TeamSettings.ExperimentalTownSquareIsReadOnly, "experimental_primary_team": isDefault(*cfg.TeamSettings.ExperimentalPrimaryTeam, ""), + "experimental_default_channels": len(cfg.TeamSettings.ExperimentalDefaultChannels), }) a.SendDiagnostic(TRACK_CONFIG_CLIENT_REQ, map[string]interface{}{ |