-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathactivate_hooks.go
115 lines (92 loc) · 3 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"fmt"
"time"
"github.com/pkg/errors"
"github.com/mattermost/mattermost/server/public/pluginapi"
"github.com/mattermost/mattermost/server/public/pluginapi/cluster"
)
// 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 p.client == nil {
p.client = pluginapi.NewClient(p.API, p.Driver)
}
if err := p.checkRequiredServerConfiguration(); err != nil {
return errors.Wrap(err, "server configuration is not compatible")
}
if err := p.OnConfigurationChange(); err != nil {
return err
}
p.initializeAPI()
configuration := p.getConfiguration()
if err := p.registerCommands(); err != nil {
return errors.Wrap(err, "failed to register commands")
}
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")
}
}
job, cronErr := cluster.Schedule(
p.API,
"BackgroundJob",
cluster.MakeWaitForRoundedInterval(15*time.Minute),
p.BackgroundJob,
)
if cronErr != nil {
return errors.Wrap(cronErr, "failed to schedule background job")
}
p.backgroundJob = job
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()
if p.backgroundJob != nil {
if err := p.backgroundJob.Close(); err != nil {
p.API.LogError("Failed to close background job", "err", err)
}
}
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")
}
}
return nil
}
func (p *Plugin) checkRequiredServerConfiguration() error {
config := p.client.Configuration.GetConfig()
if config.ServiceSettings.EnableGifPicker == nil || !*config.ServiceSettings.EnableGifPicker {
return errors.New("ServiceSettings.EnableGifPicker must be enabled")
}
if config.FileSettings.EnablePublicLink == nil || !*config.FileSettings.EnablePublicLink {
return errors.New("FileSettings.EnablePublicLink must be enabled")
}
return nil
}