forked from abuisman/libreboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cards.js
283 lines (262 loc) · 7.73 KB
/
cards.js
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
Cards = new Mongo.Collection('cards');
CardComments = new Mongo.Collection('card_comments');
// XXX To improve pub/sub performances a card document should included a
// de-normalized number of comments so we don't have to publish the whole list
// of comments just to display the number of them in the board view.
Cards.attachSchema(new SimpleSchema({
title: {
type: String
},
archived: {
type: Boolean
},
listId: {
type: String
},
// The system could work without this `boardId` information (we could deduce
// the board identifier from the card), but it would make the system more
// difficult to manage and less efficient.
boardId: {
type: String
},
coverId: {
type: String,
optional: true
},
createdAt: {
type: Date,
denyUpdate: true
},
dateLastActivity: {
type: Date
},
description: {
type: String,
optional: true
},
labelIds: {
type: [String],
optional: true
},
members: {
type: [String],
optional: true
},
// XXX Should probably be called `authorId`. Is it even needed since we have
// the `members` field?
userId: {
type: String
},
sort: {
type: Number,
decimal: true
}
}));
CardComments.attachSchema(new SimpleSchema({
boardId: {
type: String
},
cardId: {
type: String
},
// XXX Rename in `content`? `text` is a bit vague...
text: {
type: String
},
// XXX We probably don't need this information here, since we already have
// it in the associated comment creation activity
createdAt: {
type: Date,
denyUpdate: false
},
// XXX Should probably be called `authorId`
userId: {
type: String
}
}));
// ALLOWS
Cards.allow({
insert: function(userId, doc) {
return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
},
update: function(userId, doc) {
return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
},
remove: function(userId, doc) {
return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
},
fetch: ['boardId']
});
CardComments.allow({
insert: function(userId, doc) {
return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
},
update: function(userId, doc) {
return userId === doc.userId;
},
remove: function(userId, doc) {
return userId === doc.userId;
},
fetch: ['userId', 'boardId']
});
// HELPERS
Cards.helpers({
list: function() {
return Lists.findOne(this.listId);
},
board: function() {
return Boards.findOne(this.boardId);
},
labels: function() {
var self = this;
var boardLabels = self.board().labels;
var cardLabels = _.filter(boardLabels, function(label) {
return _.contains(self.labelIds, label._id);
});
return cardLabels;
},
user: function() {
return Users.findOne(this.userId);
},
activities: function() {
return Activities.find({ type: 'card', cardId: this._id }, { sort: { createdAt: -1 }});
},
comments: function() {
return CardComments.find({ cardId: this._id }, { sort: { createdAt: -1 }});
},
attachments: function() {
return Attachments.find({ cardId: this._id }, { sort: { uploadedAt: -1 }});
},
cover: function() {
return Attachments.findOne(this.coverId);
},
absoluteUrl: function() {
var board = this.board();
return Router.path("Card", { boardId: board._id, slug: board.slug, cardId: this._id });
},
rootUrl: function() {
return Meteor.absoluteUrl(this.absoluteUrl().replace('/', ''));
}
});
CardComments.helpers({
user: function() {
return Users.findOne(this.userId);
}
});
CardComments.hookOptions.after.update = { fetchPrevious: false };
Cards.before.insert(function(userId, doc) {
doc.createdAt = new Date();
doc.dateLastActivity = new Date();
// defaults
doc.archived = false;
// userId native set.
if (!doc.userId) doc.userId = userId;
});
CardComments.before.insert(function(userId, doc) {
doc.createdAt = new Date();
doc.userId = userId;
});
isServer(function() {
Cards.after.insert(function(userId, doc) {
Activities.insert({
type: 'card',
activityType: "createCard",
boardId: doc.boardId,
listId: doc.listId,
cardId: doc._id,
userId: userId
});
});
// New activity for card (un)archivage
Cards.after.update(function(userId, doc, fieldNames, modifier) {
if (_.contains(fieldNames, 'archived')) {
if (doc.archived) {
Activities.insert({
type: 'card',
activityType: "archivedCard",
boardId: doc.boardId,
listId: doc.listId,
cardId: doc._id,
userId: userId
});
} else {
Activities.insert({
type: 'card',
activityType: "restoredCard",
boardId: doc.boardId,
listId: doc.listId,
cardId: doc._id,
userId: userId
});
}
}
});
// New activity for card moves
Cards.after.update(function(userId, doc, fieldNames, modifier) {
var oldListId = this.previous.listId;
if (_.contains(fieldNames, "listId") && doc.listId !== oldListId) {
Activities.insert({
type: 'card',
activityType: "moveCard",
listId: doc.listId,
oldListId: oldListId,
boardId: doc.boardId,
cardId: doc._id,
userId: userId
});
}
});
// Add a new activity if we add or remove a member to the card
Cards.before.update(function(userId, doc, fieldNames, modifier) {
if (! _.contains(fieldNames, 'members'))
return;
// Say hello to the new member
if (modifier.$addToSet && modifier.$addToSet.members) {
var memberId = modifier.$addToSet.members;
if (!_.contains(doc.members, memberId)) {
Activities.insert({
type: 'card',
activityType: "joinMember",
boardId: doc.boardId,
cardId: doc._id,
userId: userId,
memberId: memberId
});
}
}
// Say goodbye to the former member
if (modifier.$pull && modifier.$pull.members) {
var memberId = modifier.$pull.members;
Activities.insert({
type: 'card',
activityType: "unjoinMember",
boardId: doc.boardId,
cardId: doc._id,
userId: userId,
memberId: memberId
});
}
});
// Remove all activities associated with a card if we remove the card
Cards.after.remove(function(userId, doc) {
Activities.remove({
cardId: doc._id
});
});
CardComments.after.insert(function(userId, doc) {
Activities.insert({
type: 'comment',
activityType: "addComment",
boardId: doc.boardId,
cardId: doc.cardId,
commentId: doc._id,
userId: userId
});
});
CardComments.after.remove(function(userId, doc) {
var activity = Activities.findOne({ commentId: doc._id });
if (activity) {
Activities.remove(activity._id);
}
});
});