github.com/xzl8028/xenia-server@v0.0.0-20190809101854-18450a97da63/tests/plugin_tests/test_get_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  	"reflect"
     8  
     9  	"github.com/xzl8028/xenia-server/model"
    10  	"github.com/xzl8028/xenia-server/plugin"
    11  )
    12  
    13  type MyPlugin struct {
    14  	plugin.XeniaPlugin
    15  }
    16  
    17  func isEmpty(object interface{}) bool {
    18  
    19  	// get nil case out of the way
    20  	if object == nil {
    21  		return true
    22  	}
    23  
    24  	objValue := reflect.ValueOf(object)
    25  
    26  	switch objValue.Kind() {
    27  	// collection types are empty when they have no element
    28  	case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice:
    29  		return objValue.Len() == 0
    30  	// pointers are empty if nil or if the value they point to is empty
    31  	case reflect.Ptr:
    32  		if objValue.IsNil() {
    33  			return true
    34  		}
    35  		deref := objValue.Elem().Interface()
    36  		return isEmpty(deref)
    37  	// for all other types, compare against the zero value
    38  	default:
    39  		zero := reflect.Zero(objValue.Type())
    40  		return reflect.DeepEqual(object, zero.Interface())
    41  	}
    42  }
    43  
    44  func (p *MyPlugin) MessageWillBePosted(c *plugin.Context, post *model.Post) (*model.Post, string) {
    45  
    46  	// check existing user first
    47  	data, err := p.API.GetProfileImage("{{.BasicUser.Id}}")
    48  	if err != nil {
    49  		return nil, err.Error()
    50  	}
    51  	if isEmpty(data) {
    52  		return nil, "GetProfileImage return empty"
    53  	}
    54  
    55  	// then unknown user
    56  	data, err = p.API.GetProfileImage(model.NewId())
    57  	if err == nil || data != nil {
    58  		return nil, "GetProfileImage should've returned an error"
    59  	}
    60  	return nil, ""
    61  }
    62  
    63  func main() {
    64  	plugin.ClientMain(&MyPlugin{})
    65  }