-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquestion.go
130 lines (107 loc) · 2.77 KB
/
question.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
package main
import (
"encoding/json"
"fmt"
"strconv"
bolt "go.etcd.io/bbolt"
)
type Question struct {
ID uint64 // question unique identifier
TeamID string // slack team identifier
Channel string // slack channel identifier
Message string // question message text
Users []string // involved users
Cron string // crontab expression of the question
CurrentInstance string // timestamp of the latest instance
IsActive bool // whether this question is active
}
func (q *Question) Save() error {
return App.db.Update(func(tx *bolt.Tx) error {
questionsBucket := tx.Bucket([]byte("questions"))
if q.ID == 0 {
id, err := questionsBucket.NextSequence()
if err != nil {
return err
}
q.ID = id
}
data, err := json.Marshal(q)
if err != nil {
return err
}
return questionsBucket.Put([]byte(strconv.FormatUint(q.ID, 10)), data)
})
}
func (q *Question) Delete() error {
return App.db.Update(func(tx *bolt.Tx) error {
instanceBucket := tx.Bucket([]byte("messages"))
instances := [][]byte{}
instanceBucket.ForEach(func(k, v []byte) error {
var qi QuestionInstance
err := json.Unmarshal(v, &qi)
if err != nil {
return err
}
if qi.QuestionID == q.ID {
instances = append(instances, k)
}
return nil
})
for _, instance := range instances {
err := instanceBucket.Delete(instance)
if err != nil {
return err
}
}
questionsBucket := tx.Bucket([]byte("questions"))
return questionsBucket.Delete([]byte(strconv.FormatUint(q.ID, 10)))
})
}
func (q *Question) Instance() (QuestionInstance, error) {
var instance QuestionInstance
err := App.db.View(func(tx *bolt.Tx) error {
data := tx.Bucket([]byte("messages")).Get([]byte(fmt.Sprintf("%s:%s", q.Channel, q.CurrentInstance)))
if data == nil {
return nil
}
err := json.Unmarshal(data, &instance)
return err
})
instance.Question = q
return instance, err
}
func (q *Question) NewInstance() error {
qi := QuestionInstance{
Question: q,
QuestionID: q.ID,
Responses: make(map[string]bool),
}
for _, user := range q.Users {
qi.Responses[user] = false
}
err := qi.PostMessage()
if err != nil {
return fmt.Errorf("failed posting message: %w", err)
}
err = qi.Save()
if err != nil {
return fmt.Errorf("failed saving question instance: %w", err)
}
q.CurrentInstance = qi.Timestamp
err = q.Save()
if err != nil {
return fmt.Errorf("failed saving question: %w", err)
}
return nil
}
func LoadQuestion(id uint64) (Question, error) {
var q Question
err := App.db.View(func(tx *bolt.Tx) error {
data := tx.Bucket([]byte("questions")).Get([]byte(strconv.FormatUint(id, 10)))
if data == nil {
return nil
}
return json.Unmarshal(data, &q)
})
return q, err
}