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