github.com/gigforks/mattermost-server@v4.9.1-0.20180619094218-800d97fa55d0+incompatible/app/auto_channels.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package app 5 6 import ( 7 "github.com/mattermost/mattermost-server/model" 8 "github.com/mattermost/mattermost-server/utils" 9 ) 10 11 type AutoChannelCreator struct { 12 client *model.Client 13 team *model.Team 14 Fuzzy bool 15 DisplayNameLen utils.Range 16 DisplayNameCharset string 17 NameLen utils.Range 18 NameCharset string 19 ChannelType string 20 } 21 22 func NewAutoChannelCreator(client *model.Client, team *model.Team) *AutoChannelCreator { 23 return &AutoChannelCreator{ 24 client: client, 25 team: team, 26 Fuzzy: false, 27 DisplayNameLen: CHANNEL_DISPLAY_NAME_LEN, 28 DisplayNameCharset: utils.ALPHANUMERIC, 29 NameLen: CHANNEL_NAME_LEN, 30 NameCharset: utils.LOWERCASE, 31 ChannelType: CHANNEL_TYPE, 32 } 33 } 34 35 func (cfg *AutoChannelCreator) createRandomChannel() (*model.Channel, bool) { 36 var displayName string 37 if cfg.Fuzzy { 38 displayName = utils.FuzzName() 39 } else { 40 displayName = utils.RandomName(cfg.NameLen, cfg.NameCharset) 41 } 42 name := utils.RandomName(cfg.NameLen, cfg.NameCharset) 43 44 channel := &model.Channel{ 45 TeamId: cfg.team.Id, 46 DisplayName: displayName, 47 Name: name, 48 Type: cfg.ChannelType} 49 50 println(cfg.client.GetTeamRoute()) 51 result, err := cfg.client.CreateChannel(channel) 52 if err != nil { 53 println(err.Error()) 54 println(err.DetailedError) 55 return nil, false 56 } 57 return result.Data.(*model.Channel), true 58 } 59 60 func (cfg *AutoChannelCreator) CreateTestChannels(num utils.Range) ([]*model.Channel, bool) { 61 numChannels := utils.RandIntFromRange(num) 62 channels := make([]*model.Channel, numChannels) 63 64 for i := 0; i < numChannels; i++ { 65 var err bool 66 channels[i], err = cfg.createRandomChannel() 67 if !err { 68 return channels, false 69 } 70 } 71 72 return channels, true 73 }