-
Notifications
You must be signed in to change notification settings - Fork 0
/
rules.go
324 lines (291 loc) · 9.72 KB
/
rules.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package libwekan
import (
"context"
"fmt"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)
type Rules []Rule
type RuleID string
type Rule struct {
ID RuleID `bson:"_id" json:"_id,omitempty"`
Title string `bson:"title" json:"title,omitempty"`
TriggerID *TriggerID `bson:"triggerId" json:"triggerId,omitempty"`
ActionID *ActionID `bson:"actionId" json:"actionId,omitempty"`
BoardID BoardID `bson:"boardId" json:"boardId,omitempty"`
CreatedAt time.Time `bson:"createdAt" json:"createdAt,omitempty"`
ModifiedAt time.Time `bson:"modifiedAt" json:"modifiedAt,omitempty"`
Action Action `bson:"-" json:"-"`
Trigger Trigger `bson:"-" json:"-"`
}
type TriggerID string
type Trigger struct {
ID TriggerID `bson:"_id" json:"_id,omitempty"`
ActivityType string `bson:"activityType" json:"activityType,omitempty"`
BoardID BoardID `bson:"boardId" json:"boardId,omitempty"`
LabelID BoardLabelID `bson:"labelId" json:"labelId,omitempty"`
Description string `bson:"desc" json:"desc,omitempty"`
UserID UserID `bson:"userId" json:"userId,omitempty"`
CreatedAt time.Time `bson:"createdAt" json:"createdAt,omitempty"`
ModifiedAt time.Time `bson:"modifiedAt" json:"modifiedAt,omitempty"`
}
type ActionID string
type Action struct {
ID ActionID `bson:"_id" json:"_id,omitempty"`
ActionType string `bson:"actionType" json:"actionType,omitempty"`
Username Username `bson:"username" json:"username,omitempty"`
BoardID BoardID `bson:"boardId" json:"boardId,omitempty"`
Description string `bson:"desc" json:"desc,omitempty"`
CreatedAt time.Time `bson:"createdAt" json:"createdAt,omitempty"`
ModifiedAt time.Time `bson:"modifiedAt" json:"modifiedAt,omitempty"`
}
func (ruleID RuleID) GetDocument(ctx context.Context, wekan *Wekan) (Rule, error) {
return wekan.SelectRuleFromID(ctx, ruleID)
}
func (ruleID RuleID) Check(ctx context.Context, wekan *Wekan) error {
_, err := wekan.SelectRuleFromID(ctx, ruleID)
return err
}
func (board Board) BuildTriggerAddedLabel(label BoardLabel) Trigger {
return Trigger{
ID: TriggerID(newId()),
ActivityType: "addedLabel",
BoardID: board.ID,
LabelID: label.ID,
Description: fmt.Sprintf("quand l'étiquette %s est ajoutée à la carte par *", label.Name),
UserID: "*",
CreatedAt: time.Now(),
ModifiedAt: time.Now(),
}
}
func (board Board) BuildTriggerRemovedLabel(label BoardLabel) Trigger {
return Trigger{
ID: TriggerID(newId()),
ActivityType: "removedLabel",
BoardID: board.ID,
LabelID: label.ID,
Description: fmt.Sprintf("quand l'étiquette %s est retirée de la carte par *", label.Name),
UserID: "*",
CreatedAt: time.Now(),
ModifiedAt: time.Now(),
}
}
func (board Board) BuildActionAddMember(username Username) Action {
return Action{
ID: ActionID(newId()),
ActionType: "addMember",
Username: username,
BoardID: board.ID,
Description: fmt.Sprintf("%s devient membre de la carte", username),
CreatedAt: time.Now(),
ModifiedAt: time.Now(),
}
}
func (board Board) BuildActionRemoveMember(username Username) Action {
return Action{
ID: ActionID(newId()),
ActionType: "removeMember",
Username: username,
BoardID: board.ID,
Description: fmt.Sprintf("%s est exclu de la carte", username),
CreatedAt: time.Now(),
ModifiedAt: time.Now(),
}
}
func (board Board) BuildRuleAddMember(user User, labelName BoardLabelName) Rule {
label := board.GetLabelByName(labelName)
if label == (BoardLabel{}) {
return Rule{}
}
if !board.UserIsActiveMember(user) {
return Rule{}
}
action := board.BuildActionAddMember(user.Username)
trigger := board.BuildTriggerAddedLabel(label)
rule := Rule{
ID: RuleID(newId()),
Title: fmt.Sprintf("Ajout %s (étiquette %s) ", user.Username, label.Name),
TriggerID: &trigger.ID,
ActionID: &action.ID,
BoardID: board.ID,
CreatedAt: time.Now(),
ModifiedAt: time.Now(),
Action: action,
Trigger: trigger,
}
return rule
}
func (board Board) BuildRuleRemoveMember(user User, labelName BoardLabelName) Rule {
label := board.GetLabelByName(labelName)
if label == (BoardLabel{}) {
return Rule{}
}
if !board.UserIsActiveMember(user) {
return Rule{}
}
action := board.BuildActionRemoveMember(user.Username)
trigger := board.BuildTriggerRemovedLabel(label)
rule := Rule{
ID: RuleID(newId()),
Title: fmt.Sprintf("Suppression %s (étiquette %s) ", user.Username, label.Name),
TriggerID: &trigger.ID,
ActionID: &action.ID,
BoardID: board.ID,
CreatedAt: time.Now(),
ModifiedAt: time.Now(),
Action: action,
Trigger: trigger,
}
return rule
}
func (rules Rules) selectUser(username Username) Rules {
return selectSlice(rules, func(rule Rule) bool { return rule.Action.Username == username })
}
func (rules Rules) SelectBoardLabelName(boardLabelID BoardLabelID) Rules {
return selectSlice(rules, func(rule Rule) bool { return rule.Trigger.LabelID == boardLabelID })
}
func (rules Rules) SelectRemoveMemberFromTaskforceRule() Rules {
return selectSlice(rules, func(rule Rule) bool {
return rule.Action.ActionType == "removeMember" && rule.Trigger.ActivityType == "removedLabel"
})
}
func (rules Rules) SelectAddMemberToTaskforceRule() Rules {
return selectSlice(rules, func(rule Rule) bool {
return rule.Action.ActionType == "addMember" && rule.Trigger.ActivityType == "addedLabel"
})
}
func (wekan *Wekan) InsertRule(ctx context.Context, rule Rule) error {
if err := wekan.AssertPrivileged(ctx); err != nil {
return err
}
if rule == (Rule{}) {
return InsertEmptyRuleError{}
}
err := wekan.InsertAction(ctx, rule.Action)
if err != nil {
return err
}
err = wekan.InsertTrigger(ctx, rule.Trigger)
if err != nil {
return err
}
_, err = wekan.db.Collection("rules").InsertOne(ctx, rule)
if err != nil {
return UnexpectedMongoError{err}
}
return nil
}
func (wekan *Wekan) InsertAction(ctx context.Context, action Action) error {
if err := wekan.AssertPrivileged(ctx); err != nil {
return err
}
_, err := wekan.db.Collection("actions").InsertOne(ctx, action)
if err != nil {
return UnexpectedMongoError{err}
}
return nil
}
func (wekan *Wekan) InsertTrigger(ctx context.Context, trigger Trigger) error {
if err := wekan.AssertPrivileged(ctx); err != nil {
return err
}
_, err := wekan.db.Collection("triggers").InsertOne(ctx, trigger)
if err != nil {
return UnexpectedMongoError{err}
}
return nil
}
func (wekan *Wekan) SelectRulesFromBoardID(ctx context.Context, boardID BoardID) (Rules, error) {
var malformedRules, rules Rules
cur, err := wekan.db.Collection("rules").Find(ctx, bson.M{"boardId": boardID})
if err != nil {
return nil, UnexpectedMongoError{err}
}
if err := cur.All(ctx, &malformedRules); err != nil {
return nil, UnexpectedMongoError{err}
}
for _, malformedRule := range malformedRules {
rule, err := wekan.SelectRuleFromID(ctx, malformedRule.ID)
if err != nil {
return Rules{}, err
}
rules = append(rules, rule)
}
return rules, nil
}
func (wekan *Wekan) SelectRuleFromID(ctx context.Context, ruleID RuleID) (Rule, error) {
var rule Rule
err := wekan.db.Collection("rules").FindOne(ctx, bson.M{"_id": ruleID}).Decode(&rule)
if err != nil {
if err == mongo.ErrNoDocuments {
return Rule{}, RuleNotFoundError{ruleID}
}
return Rule{}, UnexpectedMongoError{err}
}
if err := wekan.db.Collection("actions").FindOne(ctx, bson.M{"_id": rule.ActionID}).Decode(&rule.Action); err != nil {
if err != mongo.ErrNoDocuments {
return Rule{}, UnexpectedMongoError{err}
}
if rule.ActionID != nil {
return Rule{}, ActionNotFoundError{*rule.ActionID}
}
}
if err := wekan.db.Collection("triggers").FindOne(ctx, bson.M{"_id": rule.TriggerID}).Decode(&rule.Trigger); err != nil {
if err != mongo.ErrNoDocuments {
return Rule{}, UnexpectedMongoError{err}
}
if rule.TriggerID != nil {
return Rule{}, TriggerNotFoundError{*rule.TriggerID}
}
}
return rule, nil
}
func (wekan *Wekan) RemoveRuleWithID(ctx context.Context, ruleID RuleID) error {
if err := wekan.AssertPrivileged(ctx); err != nil {
return err
}
rule, err := ruleID.GetDocument(ctx, wekan)
if err != nil {
return err
}
_, err = wekan.db.Collection("actions").DeleteOne(ctx, bson.M{"_id": rule.Action.ID})
if err != nil {
return UnexpectedMongoError{err}
}
_, err = wekan.db.Collection("triggers").DeleteOne(ctx, bson.M{"_id": rule.Trigger.ID})
if err != nil {
return UnexpectedMongoError{err}
}
_, err = wekan.db.Collection("rules").DeleteOne(ctx, bson.M{"_id": rule.ID})
if err != nil {
return UnexpectedMongoError{err}
}
return nil
}
func (wekan *Wekan) EnsureRuleAddTaskforceMemberExists(ctx context.Context, user User, board Board, boardLabel BoardLabel) (bool, error) {
boardRules, err := wekan.SelectRulesFromBoardID(ctx, board.ID)
if err != nil {
return false, err
}
existingRules := boardRules.SelectBoardLabelName(boardLabel.ID).selectUser(user.Username).SelectAddMemberToTaskforceRule()
if len(existingRules) == 0 {
rule := board.BuildRuleAddMember(user, boardLabel.Name)
err = wekan.InsertRule(ctx, rule)
return err == nil, err
}
return false, nil
}
func (wekan *Wekan) EnsureRuleRemoveTaskforceMemberExists(ctx context.Context, user User, board Board, boardLabel BoardLabel) (bool, error) {
boardRules, err := wekan.SelectRulesFromBoardID(ctx, board.ID)
if err != nil {
return false, err
}
existingRules := boardRules.SelectBoardLabelName(boardLabel.ID).selectUser(user.Username).SelectRemoveMemberFromTaskforceRule()
if len(existingRules) == 0 {
rule := board.BuildRuleRemoveMember(user, boardLabel.Name)
err = wekan.InsertRule(ctx, rule)
return err == nil, err
}
return false, nil
}