2017-04-12 08:27:57 -04:00
|
|
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
2015-06-14 23:53:32 -08:00
|
|
|
// See License.txt for license information.
|
|
|
|
|
|
2017-03-13 09:23:16 -04:00
|
|
|
package app
|
2015-06-14 23:53:32 -08:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"io"
|
|
|
|
|
"os"
|
2018-03-21 14:27:14 -04:00
|
|
|
"path/filepath"
|
2017-09-06 17:12:54 -05:00
|
|
|
|
2017-09-06 23:05:10 -07:00
|
|
|
"github.com/mattermost/mattermost-server/model"
|
|
|
|
|
"github.com/mattermost/mattermost-server/utils"
|
2018-12-17 08:51:46 -08:00
|
|
|
"github.com/mattermost/mattermost-server/utils/fileutils"
|
2015-06-14 23:53:32 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type AutoPostCreator struct {
|
2018-05-16 13:43:22 -04:00
|
|
|
client *model.Client4
|
2015-06-14 23:53:32 -08:00
|
|
|
channelid string
|
|
|
|
|
Fuzzy bool
|
|
|
|
|
TextLength utils.Range
|
|
|
|
|
HasImage bool
|
|
|
|
|
ImageFilenames []string
|
|
|
|
|
Users []string
|
|
|
|
|
Mentions utils.Range
|
|
|
|
|
Tags utils.Range
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Automatic poster used for testing
|
2018-05-16 13:43:22 -04:00
|
|
|
func NewAutoPostCreator(client *model.Client4, channelid string) *AutoPostCreator {
|
2015-06-14 23:53:32 -08:00
|
|
|
return &AutoPostCreator{
|
|
|
|
|
client: client,
|
|
|
|
|
channelid: channelid,
|
|
|
|
|
Fuzzy: false,
|
2017-02-09 13:39:15 -08:00
|
|
|
TextLength: utils.Range{Begin: 100, End: 200},
|
2015-06-14 23:53:32 -08:00
|
|
|
HasImage: false,
|
|
|
|
|
ImageFilenames: TEST_IMAGE_FILENAMES,
|
|
|
|
|
Users: []string{},
|
2017-02-09 13:39:15 -08:00
|
|
|
Mentions: utils.Range{Begin: 0, End: 5},
|
|
|
|
|
Tags: utils.Range{Begin: 0, End: 7},
|
2015-06-14 23:53:32 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (cfg *AutoPostCreator) UploadTestFile() ([]string, bool) {
|
2017-02-09 13:39:15 -08:00
|
|
|
filename := cfg.ImageFilenames[utils.RandIntFromRange(utils.Range{Begin: 0, End: len(cfg.ImageFilenames) - 1})]
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2018-12-17 08:51:46 -08:00
|
|
|
path, _ := fileutils.FindDir("web/static/images")
|
2018-03-21 14:27:14 -04:00
|
|
|
file, err := os.Open(filepath.Join(path, filename))
|
2017-11-03 10:25:38 -05:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, false
|
|
|
|
|
}
|
2015-06-14 23:53:32 -08:00
|
|
|
defer file.Close()
|
|
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
data := &bytes.Buffer{}
|
|
|
|
|
_, err = io.Copy(data, file)
|
2015-06-14 23:53:32 -08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, false
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-16 13:43:22 -04:00
|
|
|
resp, appErr := cfg.client.UploadFile(data.Bytes(), cfg.channelid, filename)
|
2015-06-14 23:53:32 -08:00
|
|
|
if appErr != nil {
|
|
|
|
|
return nil, false
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
return []string{resp.FileInfos[0].Id}, true
|
2015-06-14 23:53:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (cfg *AutoPostCreator) CreateRandomPost() (*model.Post, bool) {
|
2016-09-30 11:06:30 -04:00
|
|
|
var fileIds []string
|
2015-06-14 23:53:32 -08:00
|
|
|
if cfg.HasImage {
|
|
|
|
|
var err1 bool
|
2016-09-30 11:06:30 -04:00
|
|
|
fileIds, err1 = cfg.UploadTestFile()
|
2017-10-30 11:57:24 -05:00
|
|
|
if !err1 {
|
2015-06-14 23:53:32 -08:00
|
|
|
return nil, false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var postText string
|
|
|
|
|
if cfg.Fuzzy {
|
|
|
|
|
postText = utils.FuzzPost()
|
|
|
|
|
} else {
|
|
|
|
|
postText = utils.RandomText(cfg.TextLength, cfg.Tags, cfg.Mentions, cfg.Users)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
post := &model.Post{
|
|
|
|
|
ChannelId: cfg.channelid,
|
|
|
|
|
Message: postText,
|
2016-09-30 11:06:30 -04:00
|
|
|
FileIds: fileIds}
|
2018-05-16 13:43:22 -04:00
|
|
|
rpost, err2 := cfg.client.CreatePost(post)
|
2015-06-14 23:53:32 -08:00
|
|
|
if err2 != nil {
|
|
|
|
|
return nil, false
|
|
|
|
|
}
|
2018-05-16 13:43:22 -04:00
|
|
|
return rpost, true
|
2015-06-14 23:53:32 -08:00
|
|
|
}
|