forked from mattermost/mattermost-plugin-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
activate_hooks.go
99 lines (79 loc) · 2.66 KB
/
activate_hooks.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"fmt"
"github.com/blang/semver"
"github.com/pkg/errors"
)
const minimumServerVersion = "5.11.0"
func (p *Plugin) checkServerVersion() error {
serverVersion, err := semver.Parse(p.API.GetServerVersion())
if err != nil {
return errors.Wrap(err, "failed to parse server version")
}
r := semver.MustParseRange(">=" + minimumServerVersion)
if !r(serverVersion) {
return fmt.Errorf("this plugin requires Mattermost v%s or later", minimumServerVersion)
}
return nil
}
// OnActivate is invoked when the plugin is activated.
//
// This demo implementation logs a message to the demo channel whenever the plugin is activated.
// It also creates a demo bot account
func (p *Plugin) OnActivate() error {
if err := p.checkServerVersion(); err != nil {
return err
}
configuration := p.getConfiguration()
teams, err := p.API.GetTeams()
if err != nil {
return errors.Wrap(err, "failed to query teams OnActivate")
}
for _, team := range teams {
_, ok := configuration.demoChannelIds[team.Id]
if !ok {
p.API.LogWarn("No demo channel id for team", "team", team.Id)
continue
}
msg := fmt.Sprintf("OnActivate: %s", manifest.Id)
if err := p.postPluginMessage(team.Id, msg); err != nil {
return errors.Wrap(err, "failed to post OnActivate message")
}
if err := p.registerCommand(team.Id); err != nil {
return errors.Wrap(err, "failed to register command")
}
}
return nil
}
// OnDeactivate is invoked when the plugin is deactivated. This is the plugin's last chance to use
// the API, and the plugin will be terminated shortly after this invocation.
//
// This demo implementation logs a message to the demo channel whenever the plugin is deactivated.
func (p *Plugin) OnDeactivate() error {
configuration := p.getConfiguration()
teams, err := p.API.GetTeams()
if err != nil {
return errors.Wrap(err, "failed to query teams OnDeactivate")
}
for _, team := range teams {
_, ok := configuration.demoChannelIds[team.Id]
if !ok {
p.API.LogWarn("No demo channel id for team", "team", team.Id)
continue
}
msg := fmt.Sprintf("OnDeactivate: %s", manifest.Id)
if err := p.postPluginMessage(team.Id, msg); err != nil {
return errors.Wrap(err, "failed to post OnDeactivate message")
}
if err := p.API.UnregisterCommand(team.Id, CommandTriggerPlugin); err != nil {
return errors.Wrap(err, "failed to unregister command")
}
if err := p.API.UnregisterCommand(team.Id, CommandTriggerEphemeral); err != nil {
return errors.Wrap(err, "failed to unregister command")
}
if err := p.API.UnregisterCommand(team.Id, CommandTriggerCrash); err != nil {
return errors.Wrap(err, "failed to unregister command")
}
}
return nil
}