github.com/jlevesy/mattermost-server@v5.3.2-0.20181003190404-7468f35cb0c8+incompatible/app/auto_posts.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  	"bytes"
     8  	"io"
     9  	"os"
    10  	"path/filepath"
    11  
    12  	"github.com/mattermost/mattermost-server/model"
    13  	"github.com/mattermost/mattermost-server/utils"
    14  )
    15  
    16  type AutoPostCreator struct {
    17  	client         *model.Client4
    18  	channelid      string
    19  	Fuzzy          bool
    20  	TextLength     utils.Range
    21  	HasImage       bool
    22  	ImageFilenames []string
    23  	Users          []string
    24  	Mentions       utils.Range
    25  	Tags           utils.Range
    26  }
    27  
    28  // Automatic poster used for testing
    29  func NewAutoPostCreator(client *model.Client4, channelid string) *AutoPostCreator {
    30  	return &AutoPostCreator{
    31  		client:         client,
    32  		channelid:      channelid,
    33  		Fuzzy:          false,
    34  		TextLength:     utils.Range{Begin: 100, End: 200},
    35  		HasImage:       false,
    36  		ImageFilenames: TEST_IMAGE_FILENAMES,
    37  		Users:          []string{},
    38  		Mentions:       utils.Range{Begin: 0, End: 5},
    39  		Tags:           utils.Range{Begin: 0, End: 7},
    40  	}
    41  }
    42  
    43  func (cfg *AutoPostCreator) UploadTestFile() ([]string, bool) {
    44  	filename := cfg.ImageFilenames[utils.RandIntFromRange(utils.Range{Begin: 0, End: len(cfg.ImageFilenames) - 1})]
    45  
    46  	path, _ := utils.FindDir("web/static/images")
    47  	file, err := os.Open(filepath.Join(path, filename))
    48  	if err != nil {
    49  		return nil, false
    50  	}
    51  	defer file.Close()
    52  
    53  	data := &bytes.Buffer{}
    54  	_, err = io.Copy(data, file)
    55  	if err != nil {
    56  		return nil, false
    57  	}
    58  
    59  	resp, appErr := cfg.client.UploadFile(data.Bytes(), cfg.channelid, filename)
    60  	if appErr != nil {
    61  		return nil, false
    62  	}
    63  
    64  	return []string{resp.FileInfos[0].Id}, true
    65  }
    66  
    67  func (cfg *AutoPostCreator) CreateRandomPost() (*model.Post, bool) {
    68  	var fileIds []string
    69  	if cfg.HasImage {
    70  		var err1 bool
    71  		fileIds, err1 = cfg.UploadTestFile()
    72  		if !err1 {
    73  			return nil, false
    74  		}
    75  	}
    76  
    77  	var postText string
    78  	if cfg.Fuzzy {
    79  		postText = utils.FuzzPost()
    80  	} else {
    81  		postText = utils.RandomText(cfg.TextLength, cfg.Tags, cfg.Mentions, cfg.Users)
    82  	}
    83  
    84  	post := &model.Post{
    85  		ChannelId: cfg.channelid,
    86  		Message:   postText,
    87  		FileIds:   fileIds}
    88  	rpost, err2 := cfg.client.CreatePost(post)
    89  	if err2 != nil {
    90  		return nil, false
    91  	}
    92  	return rpost, true
    93  }