-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_msg.go
200 lines (166 loc) · 5.26 KB
/
cmd_msg.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package main
import (
"fmt"
"github.com/alecthomas/kong"
"gorm.io/gorm"
"io"
"strconv"
)
type CmdMsg struct {
Send CmdMsgSend `cmd help:"Send a text message to a bagel"`
Read CmdMsgRead `cmd help:"Read messages from bagels"`
Feedback CmdMsgFeedback `cmd help:"Prompt bagels for their meeting status"`
Stats CmdMsgStats `cmd help:"Post stats update of a bagel log"`
}
type CmdMsgSend struct {
Text string `required help:"The text to send"`
Log string `help:"The id of the log to send a message to"`
Channel string `help:"The id of the slack channel/group to send a message to"`
}
func (cmd *CmdMsgSend) Run(ctx *kong.Context, db *gorm.DB, s *Slack) (err error) {
if cmd.Log != "" {
log, err := BagelLog_Fetch(db, cmd.Log)
if err != nil {
return err
}
var bagels []Bagel
db.Model(&log).Association("Bagels").Find(&bagels)
for _, bagel := range bagels {
conversation := bagel.SlackConversationID
if err = s.ChatPostMessage(conversation, cmd.Text, nil); err != nil {
_, err = io.WriteString(ctx.Stderr, err.Error())
return err
}
}
}
if cmd.Channel != "" {
if err = s.ChatPostMessage(cmd.Channel, cmd.Text, nil); err != nil {
_, err = io.WriteString(ctx.Stderr, err.Error())
return err
}
}
return nil
}
type CmdMsgRead struct {
Limit int `help:"The maximum number of messages to print" default:"10"`
Channel string `required help:"The id of the slack channel to read messages"`
}
func (cmd *CmdMsgRead) Run(ctx *kong.Context, db *gorm.DB, s *Slack) (err error) {
messages, err := s.ConversationsHistory(cmd.Channel, cmd.Limit)
if err != nil {
_, err = io.WriteString(ctx.Stderr, err.Error())
return err
}
for _, slackMessage := range messages {
var user User
db.Where("slack_id = ?", slackMessage.User).FirstOrInit(&user)
msg := fmt.Sprintf("%s: %s\n", user.Name, slackMessage.Text)
if _, err = io.WriteString(ctx.Stdout, msg); err != nil {
return err
}
}
return nil
}
type CmdMsgFeedback struct {
Log string `help:"The id of the log to ask feedback from"`
Bagel int `help:"The id of the bagel to ask feedback from"`
}
func (cmd *CmdMsgFeedback) Run(ctx *kong.Context, db *gorm.DB, s *Slack) (err error) {
text := "I'm here to keep track of your bagel chat. When you've planned/completed your bagel chat, don't forget to mark that you've done so."
if cmd.Log != "" {
log, err := BagelLog_Fetch(db, cmd.Log)
if err != nil {
_, err = io.WriteString(ctx.Stderr, err.Error())
return err
}
var bagels []Bagel
db.Model(&log).Association("Bagels").Find(&bagels)
for _, bagel := range bagels {
feedbackMsg := FeedbackMsg_Create(db)
db.Model(&bagel).Association("FeedbackMsgs").Append(&feedbackMsg)
blocks := SlackBlocks_FeedbackMsg(feedbackMsg, text)
conversation := bagel.SlackConversationID
if err = s.ChatPostMessage(conversation, text, blocks); err != nil {
_, err = io.WriteString(ctx.Stderr, err.Error())
return err
}
}
}
if cmd.Bagel != 0 {
var bagel Bagel
db.Where("id = ?", cmd.Bagel).First(&bagel)
if bagel.ID == 0 {
_, err = io.WriteString(ctx.Stderr, "no such bagel "+strconv.Itoa(cmd.Bagel))
return err
}
feedbackMsg := FeedbackMsg_Create(db)
db.Model(&bagel).Association("FeedbackMsgs").Append(&feedbackMsg)
blocks := SlackBlocks_FeedbackMsg(feedbackMsg, text)
conversation := bagel.SlackConversationID
if err = s.ChatPostMessage(conversation, text, blocks); err != nil {
_, err = io.WriteString(ctx.Stderr, err.Error())
return err
}
}
return nil
}
type CmdMsgStats struct {
Log string `required help:"The bagel log to tally feedback for"`
Channel string `required help:"The name/slackid of the slack channel to send the status update"`
}
func (cmd *CmdMsgStats) Run(ctx *kong.Context, db *gorm.DB, s *Slack) (err error) {
log, err := BagelLog_Fetch(db, cmd.Log)
if err != nil {
return err
}
var bagels []Bagel
db.Model(&log).Association("Bagels").Find(&bagels)
var firstBagelCompleted Bagel
var incomplete, planned, completed int
for _, bagel := range bagels {
var users []User
db.Model(&bagel).Association("Users").Find(&users)
userCount := 0
for _, user := range users {
if !user.IsBot {
userCount++
}
}
if bagel.IsCompleted {
completed += userCount
if firstBagelCompleted.ID == 0 {
firstBagelCompleted = bagel
} else if bagel.FeedbackDate < firstBagelCompleted.FeedbackDate {
firstBagelCompleted = bagel
}
} else if bagel.IsPlanned {
planned += userCount
} else {
incomplete += userCount
}
}
var firstGroupCompleted []string
if firstBagelCompleted.ID != 0 {
var firstGroupUsers []User
db.Model(&firstBagelCompleted).Association("Users").Find(&firstGroupUsers)
for _, user := range firstGroupUsers {
if !user.IsBot {
firstGroupCompleted = append(firstGroupCompleted, user.Name)
}
}
}
channel, err := s.FindChannel(cmd.Channel, cmd.Channel)
if err != nil {
return err
}
if channel == nil {
_, err = io.WriteString(ctx.Stderr, "no such channel "+cmd.Channel)
return err
}
blocks := SlackBlocks_FeedbackStatistics(completed, firstGroupCompleted, planned)
if err = s.ChatPostMessage(channel.ID, "Updated stats about bagel chats", blocks); err != nil {
_, err = io.WriteString(ctx.Stderr, err.Error())
return err
}
return nil
}