github.com/adacta-ru/mattermost-server/v6@v6.0.0/app/plugin_api_tests/test_update_user_status_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 "fmt" 8 9 "github.com/adacta-ru/mattermost-server/v6/app/plugin_api_tests" 10 "github.com/adacta-ru/mattermost-server/v6/model" 11 "github.com/adacta-ru/mattermost-server/v6/plugin" 12 ) 13 14 type MyPlugin struct { 15 plugin.MattermostPlugin 16 configuration plugin_api_tests.BasicConfig 17 } 18 19 func (p *MyPlugin) OnConfigurationChange() error { 20 if err := p.API.LoadPluginConfiguration(&p.configuration); err != nil { 21 return err 22 } 23 return nil 24 } 25 26 func (p *MyPlugin) MessageWillBePosted(c *plugin.Context, post *model.Post) (*model.Post, string) { 27 uid := p.configuration.BasicUserId 28 29 statuses := []string{model.STATUS_ONLINE, model.STATUS_AWAY, model.STATUS_DND, model.STATUS_OFFLINE} 30 31 for _, s := range statuses { 32 status, err := p.API.UpdateUserStatus(uid, s) 33 if err != nil { 34 return nil, err.Error() 35 } 36 if status == nil { 37 return nil, "Status was expected, got nil" 38 } 39 if s != status.Status { 40 return nil, fmt.Sprintf("Invalid status returned: %v != %v", s, status.Status) 41 } 42 43 } 44 45 status, err := p.API.UpdateUserStatus(uid, "notrealstatus") 46 if err == nil { 47 return nil, "Expected to get an error while updating invalid user status" 48 } 49 if status != nil { 50 return nil, "Status was expected to be nil, got: " + status.Status 51 } 52 53 return nil, "OK" 54 } 55 56 func main() { 57 plugin.ClientMain(&MyPlugin{}) 58 }