-
Notifications
You must be signed in to change notification settings - Fork 6
/
admin.go
125 lines (112 loc) · 2.56 KB
/
admin.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
116
117
118
119
120
121
122
123
124
125
package admin
import (
"context"
"net/http"
"github.com/jirwin/quadlek/quadlek"
"github.com/slack-go/slack"
"go.uber.org/zap"
)
func shutdown(ctx context.Context, cmdChannel <-chan *quadlek.CommandMsg) {
for {
select {
case cmdMsg := <-cmdChannel:
cmdMsg.Command.Reply() <- &quadlek.CommandResp{
Text: "Shutting down...",
}
cmdMsg.Bot.Stop()
case <-ctx.Done():
zap.L().Info("Exiting quit command.")
return
}
}
}
func restartInteraction(ctx context.Context, interactionChannel <-chan *quadlek.InteractionMsg) {
for {
select {
case scMsg := <-interactionChannel:
callbackID := ""
switch scMsg.Interaction.Type {
case "view_submission":
callbackID = scMsg.Interaction.View.CallbackID
default:
callbackID = scMsg.Interaction.CallbackID
}
switch callbackID {
case "restart":
r := slack.ModalViewRequest{
Type: "modal",
Title: &slack.TextBlockObject{
Type: "plain_text",
Text: "Restart Quadlek",
},
Blocks: slack.Blocks{
BlockSet: []slack.Block{
&slack.SectionBlock{
Type: "section",
Text: &slack.TextBlockObject{
Type: "plain_text",
Text: "Are you sure you'd like to restart quadlek?",
},
},
},
},
Submit: &slack.TextBlockObject{
Type: "plain_text",
Text: "Confirm",
},
CallbackID: "restart-confirm-modal",
}
_, err := scMsg.Bot.OpenView(scMsg.Interaction.TriggerID, r)
if err != nil {
zap.L().Error("error opening view", zap.Error(err))
continue
}
case "restart-confirm-modal":
zap.L().Info("shutting down...")
scMsg.Bot.Stop()
}
case <-ctx.Done():
zap.L().Info("Exiting quit command.")
return
}
}
}
func healthCheck(ctx context.Context, whChan <-chan *quadlek.WebhookMsg) {
for {
select {
case whMsg := <-whChan:
// respond to webhook
whMsg.ResponseWriter.WriteHeader(http.StatusOK)
_, err := whMsg.ResponseWriter.Write([]byte{})
if err != nil {
continue
}
whMsg.Done <- true
case <-ctx.Done():
zap.L().Info("Exiting healthcheck ")
return
}
}
}
func Register(adminChannel string) quadlek.Plugin {
return quadlek.MakePlugin(
"admin",
[]quadlek.Command{
quadlek.MakeCommand("shutdown", shutdown),
},
nil,
nil,
[]quadlek.Webhook{
quadlek.MakeWebhook("healthCheck", healthCheck),
},
nil,
)
}
func RegisterInteraction() quadlek.InteractionPlugin {
return quadlek.MakeInteractionPlugin(
"restart-quadlek",
[]quadlek.Interaction{
quadlek.MakeInteraction("restart", restartInteraction),
},
)
}