Files
mostlymatter/app/auto_channels.go
Christopher Speller a22f02c867 MM-27412 Fixing /test command. (#15155)
* Fixing /test command.

* Add error handling to channels command.

* Switch to using app instead of client in some places.

* Fix lint being confused.

* Join channels automatically.

Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
2020-08-06 16:18:47 -07:00

75 строки
1.9 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package app
import (
"github.com/mattermost/mattermost-server/v5/model"
"github.com/mattermost/mattermost-server/v5/utils"
)
type AutoChannelCreator struct {
a *App
userId string
team *model.Team
Fuzzy bool
DisplayNameLen utils.Range
DisplayNameCharset string
NameLen utils.Range
NameCharset string
ChannelType string
}
func NewAutoChannelCreator(a *App, team *model.Team, userId string) *AutoChannelCreator {
return &AutoChannelCreator{
a: a,
team: team,
userId: userId,
Fuzzy: false,
DisplayNameLen: CHANNEL_DISPLAY_NAME_LEN,
DisplayNameCharset: utils.ALPHANUMERIC,
NameLen: CHANNEL_NAME_LEN,
NameCharset: utils.LOWERCASE,
ChannelType: CHANNEL_TYPE,
}
}
func (cfg *AutoChannelCreator) createRandomChannel() (*model.Channel, error) {
var displayName string
if cfg.Fuzzy {
displayName = utils.FuzzName()
} else {
displayName = utils.RandomName(cfg.NameLen, cfg.NameCharset)
}
name := utils.RandomName(cfg.NameLen, cfg.NameCharset)
channel := &model.Channel{
TeamId: cfg.team.Id,
DisplayName: displayName,
Name: name,
Type: cfg.ChannelType,
CreatorId: cfg.userId,
}
channel, err := cfg.a.CreateChannel(channel, true)
if err != nil {
return nil, err
}
return channel, nil
}
func (cfg *AutoChannelCreator) CreateTestChannels(num utils.Range) ([]*model.Channel, error) {
numChannels := utils.RandIntFromRange(num)
channels := make([]*model.Channel, numChannels)
for i := 0; i < numChannels; i++ {
var err error
channels[i], err = cfg.createRandomChannel()
if err != nil {
return nil, err
}
}
return channels, nil
}