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