github.com/xzl8028/xenia-server@v0.0.0-20190809101854-18450a97da63/tests/plugin_tests/test_get_direct_channel_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  	dm1, err := p.API.GetDirectChannel("{{.BasicUser.Id}}", "{{.BasicUser2.Id}}")
    46  	if err != nil {
    47  		return nil, err.Error()
    48  	}
    49  	if isEmpty(dm1) {
    50  		return nil, "dm1 is empty"
    51  	}
    52  
    53  	dm2, err := p.API.GetDirectChannel("{{.BasicUser.Id}}", "{{.BasicUser.Id}}")
    54  	if err != nil {
    55  		return nil, err.Error()
    56  	}
    57  	if isEmpty(dm2) {
    58  		return nil, "dm2 is empty"
    59  	}
    60  
    61  	dm3, err := p.API.GetDirectChannel("{{.BasicUser.Id}}", model.NewId())
    62  	if err == nil {
    63  		return nil, "Expected to get error while fetching incorrect channel"
    64  	}
    65  	if !isEmpty(dm3) {
    66  		return nil, "dm3 is NOT empty"
    67  	}
    68  	return nil, ""
    69  }
    70  
    71  func main() {
    72  	plugin.ClientMain(&MyPlugin{})
    73  }