-
Notifications
You must be signed in to change notification settings - Fork 0
/
activities.go
281 lines (256 loc) · 9.04 KB
/
activities.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
package libwekan
import (
"context"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"time"
"go.mongodb.org/mongo-driver/bson"
)
type ActivityID string
type Activity struct {
ID ActivityID `bson:"_id" json:"_id,omitempty"`
UserID UserID `bson:"userId,omitempty" json:"userId,omitempty"`
Username Username `bson:"username,omitempty" json:"username,omitempty"`
Type string `bson:"type,omitempty" json:"type,omitempty"`
AssigneeID UserID `bson:"assigneeId,omitempty" json:"assigneeId,omitempty"`
MemberID UserID `bson:"memberId,omitempty" json:"memberId,omitempty"`
ActivityType string `bson:"activityType,omitempty" json:"activityType,omitempty"`
ActivityTypeID string `bson:"activityTypeId,omitempty" json:"activityTypeId,omitempty"`
BoardID BoardID `bson:"boardId,omitempty" json:"boardId,omitempty"`
BoardLabelID BoardLabelID `bson:"labelId,omitEmpty" json:"labelId,omitempty"`
CardTitle string `bson:"cardTitle,omitempty" json:"cardTitle,omitempty"`
ListID ListID `bson:"listId,omitempty" json:"listId,omitempty"`
OldListID ListID `bson:"oldListId,omitempty" json:"oldListId,omitempty"`
ListName string `bson:"listName,omitempty" json:"listName,omitempty"`
CardID CardID `bson:"cardId,omitempty" json:"cardId,omitempty"`
CommentID CommentID `bson:"commentId, omitempty"`
SwimlaneID SwimlaneID `bson:"swimlaneId,omitempty" json:"swimlaneId,omitempty"`
OldSwimlaneID SwimlaneID `bson:"oldSwimlaneId,omitempty" json:"oldSwimlaneId,omitempty"`
SwimlaneName string `bson:"swimlaneName,omitempty" json:"swimlaneName,omitempty"`
CreatedAt time.Time `bson:"createdAt" json:"createdAt,omitempty"`
ModifiedAt time.Time `bson:"modifiedAt" json:"modifiedAt,omitempty"`
}
func (activityID ActivityID) Check(ctx context.Context, wekan *Wekan) error {
_, err := wekan.GetActivityFromID(ctx, activityID)
return err
}
func (activityID ActivityID) GetDocument(ctx context.Context, wekan *Wekan) (Activity, error) {
return wekan.GetActivityFromID(ctx, activityID)
}
func (activity Activity) withIDandDates(t time.Time) (Activity, error) {
if activity.ID != "" {
return activity, AlreadySetActivityError{}
}
activity.ID = ActivityID(newId())
activity.CreatedAt = toMongoTime(t)
activity.ModifiedAt = toMongoTime(t)
return activity, nil
}
func newActivityCreateBoard(userID UserID, boardID BoardID) Activity {
return Activity{
BoardID: boardID,
ActivityTypeID: string(boardID),
UserID: userID,
ActivityType: "createBoard",
Type: "board",
}
}
func newActivityCreateSwimlane(userID UserID, boardID BoardID, swimlaneID SwimlaneID) Activity {
return Activity{
SwimlaneID: swimlaneID,
UserID: userID,
BoardID: boardID,
ActivityType: "createSwimlane",
Type: "list",
}
}
func newActivityAddBoardMember(userID UserID, memberID UserID, boardID BoardID) Activity {
return Activity{
UserID: userID,
MemberID: memberID,
BoardID: boardID,
ActivityType: "addBoardMember",
Type: "member",
}
}
func newActivityRemoveBoardMember(userID UserID, memberID UserID, boardID BoardID) Activity {
return Activity{
UserID: userID,
MemberID: memberID,
BoardID: boardID,
ActivityType: "removeBoardMember",
Type: "member",
}
}
func newActivityCardJoinMember(userID UserID, username Username, memberID UserID, boardID BoardID, listID ListID, cardID CardID, swimlaneID SwimlaneID) Activity {
return Activity{
UserID: userID,
Username: username,
MemberID: memberID,
BoardID: boardID,
CardID: cardID,
ListID: listID,
SwimlaneID: swimlaneID,
ActivityType: "joinMember",
}
}
func newActivityCardJoinAssignee(userID UserID, username Username, memberID UserID, boardID BoardID, listID ListID, cardID CardID, swimlaneID SwimlaneID) Activity {
return Activity{
UserID: userID,
Username: username,
AssigneeID: memberID,
BoardID: boardID,
CardID: cardID,
ListID: listID,
SwimlaneID: swimlaneID,
ActivityType: "joinAssignee",
}
}
func newActivityCardUnjoinMember(userID UserID, username Username, memberID UserID, boardID BoardID, listID ListID, cardID CardID, swimlaneID SwimlaneID) Activity {
return Activity{
UserID: userID,
Username: username,
MemberID: memberID,
BoardID: boardID,
CardID: cardID,
ListID: listID,
SwimlaneID: swimlaneID,
ActivityType: "unjoinMember",
}
}
func newActivityCardUnjoinAssignee(userID UserID, username Username, assigneeID UserID, boardID BoardID, listID ListID, cardID CardID, swimlaneID SwimlaneID) Activity {
return Activity{
UserID: userID,
Username: username,
AssigneeID: assigneeID,
BoardID: boardID,
CardID: cardID,
ListID: listID,
SwimlaneID: swimlaneID,
ActivityType: "unjoinAssignee",
}
}
func newActivityAddComment(userID UserID, boardID BoardID, cardID CardID, commentID CommentID, listID ListID, swimlaneID SwimlaneID) Activity {
return Activity{
UserID: userID,
BoardID: boardID,
CardID: cardID,
CommentID: commentID,
ListID: listID,
SwimlaneID: swimlaneID,
ActivityType: "addComment",
}
}
func newActivityAddedLabel(userID UserID, boardLabelID BoardLabelID, boardID BoardID, swimlaneID SwimlaneID) Activity {
return Activity{
UserID: userID,
BoardLabelID: boardLabelID,
ActivityType: "addedLabel",
BoardID: boardID,
SwimlaneID: swimlaneID,
}
}
func (wekan *Wekan) newActivityCreateCardFromCard(ctx context.Context, card Card) (Activity, error) {
list, err := wekan.GetListFromID(ctx, card.ListID)
if err != nil {
return Activity{}, err
}
swimlane, err := wekan.GetSwimlaneFromID(ctx, card.SwimlaneID)
if err != nil {
return Activity{}, err
}
return newActivityCreateCard(card.UserID, list, card, swimlane), nil
}
func newActivityCreateCard(userID UserID, list List, card Card, swimlane Swimlane) Activity {
return Activity{
UserID: userID,
ActivityType: "createCard",
BoardID: card.BoardID,
ListName: list.Title,
ListID: list.ID,
CardID: card.ID,
CardTitle: card.Title,
SwimlaneName: swimlane.Title,
SwimlaneID: swimlane.ID,
}
}
func (wekan *Wekan) newActivityMoveCardFromMovedCard(ctx context.Context, oldCard Card, userID UserID) (Activity, error) {
newCard, err := oldCard.ID.GetDocument(ctx, wekan)
if err != nil {
return Activity{}, err
}
newList, err := newCard.ListID.GetDocument(ctx, wekan)
if err != nil {
return Activity{}, err
}
swimlane, err := oldCard.SwimlaneID.GetDocument(ctx, wekan)
if err != nil {
return Activity{}, err
}
return newActivityMoveCard(userID, oldCard, newList, swimlane), nil
}
func newActivityMoveCard(userID UserID, oldCard Card, newList List, swimlane Swimlane) Activity {
return Activity{
UserID: userID,
ActivityType: "moveCard",
BoardID: oldCard.BoardID,
CardID: oldCard.ID,
CardTitle: oldCard.Title,
OldListID: oldCard.ListID,
ListID: newList.ID,
ListName: newList.Title,
SwimlaneID: swimlane.ID,
OldSwimlaneID: oldCard.SwimlaneID,
SwimlaneName: swimlane.Title,
}
}
func (wekan *Wekan) insertActivity(ctx context.Context, activity Activity) (Activity, error) {
insertable, err := activity.withIDandDates(time.Now())
if err != nil {
return Activity{}, err
}
_, err = wekan.db.Collection("activities").InsertOne(ctx, insertable)
if err != nil {
return Activity{}, UnexpectedMongoError{err}
}
return insertable, nil
}
func (wekan *Wekan) SelectActivitiesFromCardID(ctx context.Context, cardID CardID) ([]Activity, error) {
var activities []Activity
filter := bson.M{"cardId": cardID}
sort := options.Find().SetSort(bson.M{"createdAt": 1})
cur, err := wekan.db.Collection("activities").Find(ctx, filter, sort)
if err != nil {
return nil, UnexpectedMongoError{err}
}
err = cur.All(ctx, &activities)
if err != nil {
return nil, UnexpectedMongoError{err}
}
return activities, nil
}
func (wekan *Wekan) GetActivityFromID(ctx context.Context, activityID ActivityID) (Activity, error) {
var activity Activity
err := wekan.db.Collection("activities").FindOne(ctx, bson.M{"_id": activityID}).Decode(&activity)
if err != nil {
if err == mongo.ErrNoDocuments {
return Activity{}, ActivityNotFoundError{string(activityID)}
}
return Activity{}, UnexpectedMongoError{err}
}
return activity, nil
}
func (wekan *Wekan) SelectActivitiesFromQuery(ctx context.Context, query bson.M) ([]Activity, error) {
var activities []Activity
cur, err := wekan.db.Collection("activities").Find(ctx, query)
if err != nil {
return nil, UnexpectedMongoError{err}
}
if err := cur.All(ctx, &activities); err != nil {
return nil, UnexpectedMongoError{err}
}
return activities, nil
}
func (wekan *Wekan) SelectActivitiesFromBoardID(ctx context.Context, boardID BoardID) ([]Activity, error) {
return wekan.SelectActivitiesFromQuery(ctx, bson.M{"boardId": boardID})
}