github.com/adacta-ru/mattermost-server/v6@v6.0.0/app/plugin_api_tests/test_set_profile_image_plugin/main.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package main 5 6 import ( 7 "bytes" 8 "fmt" 9 "image" 10 "image/color" 11 "image/png" 12 13 "github.com/adacta-ru/mattermost-server/v6/app/plugin_api_tests" 14 "github.com/adacta-ru/mattermost-server/v6/model" 15 "github.com/adacta-ru/mattermost-server/v6/plugin" 16 ) 17 18 type MyPlugin struct { 19 plugin.MattermostPlugin 20 configuration plugin_api_tests.BasicConfig 21 } 22 23 func (p *MyPlugin) OnConfigurationChange() error { 24 if err := p.API.LoadPluginConfiguration(&p.configuration); err != nil { 25 return err 26 } 27 return nil 28 } 29 30 func (p *MyPlugin) MessageWillBePosted(c *plugin.Context, post *model.Post) (*model.Post, string) { 31 32 // Create an 128 x 128 image 33 img := image.NewRGBA(image.Rect(0, 0, 128, 128)) 34 // Draw a red dot at (2, 3) 35 img.Set(2, 3, color.RGBA{255, 0, 0, 255}) 36 buf := new(bytes.Buffer) 37 if err := png.Encode(buf, img); err != nil { 38 return nil, err.Error() 39 } 40 41 dataBytes := buf.Bytes() 42 43 // Set the user profile image 44 if err := p.API.SetProfileImage(p.configuration.BasicUserId, dataBytes); err != nil { 45 return nil, err.Error() 46 } 47 48 // Get the user profile image to check 49 imageProfile, err := p.API.GetProfileImage(p.configuration.BasicUserId) 50 if err != nil { 51 return nil, err.Error() 52 } 53 if plugin_api_tests.IsEmpty(imageProfile) { 54 return nil, "profile image is empty" 55 } 56 57 colorful := color.NRGBA{255, 0, 0, 255} 58 byteReader := bytes.NewReader(imageProfile) 59 img2, _, err2 := image.Decode(byteReader) 60 if err2 != nil { 61 return nil, err.Error() 62 } 63 if img2.At(2, 3) != colorful { 64 return nil, fmt.Sprintf("color mismatch %v != %v", img2.At(2, 3), colorful) 65 } 66 return nil, "OK" 67 } 68 69 func main() { 70 plugin.ClientMain(&MyPlugin{}) 71 }