github.com/xzl8028/xenia-server@v0.0.0-20190809101854-18450a97da63/tests/plugin_tests/test_set_profile_image_plugin/main.go (about) 1 // Copyright (c) 2015-present Xenia, 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 "reflect" 13 14 "github.com/xzl8028/xenia-server/model" 15 "github.com/xzl8028/xenia-server/plugin" 16 ) 17 18 type MyPlugin struct { 19 plugin.XeniaPlugin 20 } 21 22 func isEmpty(object interface{}) bool { 23 24 // get nil case out of the way 25 if object == nil { 26 return true 27 } 28 29 objValue := reflect.ValueOf(object) 30 31 switch objValue.Kind() { 32 // collection types are empty when they have no element 33 case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice: 34 return objValue.Len() == 0 35 // pointers are empty if nil or if the value they point to is empty 36 case reflect.Ptr: 37 if objValue.IsNil() { 38 return true 39 } 40 deref := objValue.Elem().Interface() 41 return isEmpty(deref) 42 // for all other types, compare against the zero value 43 default: 44 zero := reflect.Zero(objValue.Type()) 45 return reflect.DeepEqual(object, zero.Interface()) 46 } 47 } 48 49 func (p *MyPlugin) MessageWillBePosted(c *plugin.Context, post *model.Post) (*model.Post, string) { 50 51 // Create an 128 x 128 image 52 img := image.NewRGBA(image.Rect(0, 0, 128, 128)) 53 // Draw a red dot at (2, 3) 54 img.Set(2, 3, color.RGBA{255, 0, 0, 255}) 55 buf := new(bytes.Buffer) 56 if err := png.Encode(buf, img); err != nil { 57 return nil, err.Error() 58 } 59 60 dataBytes := buf.Bytes() 61 62 // Set the user profile image 63 if err := p.API.SetProfileImage("{{.BasicUser.Id}}", dataBytes); err != nil { 64 return nil, err.Error() 65 } 66 67 // Get the user profile image to check 68 imageProfile, err := p.API.GetProfileImage("{{.BasicUser.Id}}") 69 if err != nil { 70 return nil, err.Error() 71 } 72 if isEmpty(imageProfile) { 73 return nil, "profile image is empty" 74 } 75 76 colorful := color.NRGBA{255, 0, 0, 255} 77 byteReader := bytes.NewReader(imageProfile) 78 img2, _, err2 := image.Decode(byteReader) 79 if err2 != nil { 80 return nil, err.Error() 81 } 82 if img2.At(2, 3) != colorful { 83 return nil, fmt.Sprintf("color mismatch %v != %v", img2.At(2, 3), colorful) 84 } 85 return nil, "" 86 } 87 88 func main() { 89 plugin.ClientMain(&MyPlugin{}) 90 }