-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodel.js
329 lines (297 loc) · 8.81 KB
/
model.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
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
325
326
327
328
329
var newCollection = function (name) {
var c = new Meteor.Collection(name);
c.deny({
insert: function (userId) { return !userId; },
update: function (userId) { return !userId; },
remove: function (userId) { return !userId; },
fetch: []
});
return c;
};
// PUZZLES
Puzzles = newCollection('puzzles');
// schema:
// title: string
// tags: list of tags (which in HQ neded to be identifier-style)
// families: object
// family ID -> family value (strings)
// metadata: object
// metadata ID -> value
// spreadsheets: list of objects
// docId: string
// link: string
// embedLink: string
// relatedQueries: list of puzzle queries? [XXX]
if (Meteor.isServer) {
Jigsaw.publish('all-puzzles', function () {
return Puzzles.find();
});
} else {
Meteor.subscribe("all-puzzles");
// Meteor.autosubscribe(function () {
// var puzzleId = Session.get('route.puzzleId');
// if (puzzleId)
// Meteor.subscribe('puzzle', puzzleId);
// });
}
var tagStringToArray = function (tagString) {
var nonEmptyTags = _.filter(tagString.split(' '), _.identity);
var lowerCaseTags = _.map(nonEmptyTags, function (t) {
return t.toLowerCase();
});
return _.uniq(lowerCaseTags);
};
// used in newsfeeds, etc
var puzzleLink = function (puzzleId, text) {
return '<a href="/puzzle/' + puzzleId + '">' + _.escape(text) + '</a>';
};
// puzzle queries need sorts (which was weird in AE), tags, negative tags,
// metadata to show. families were like tags. also, by default we filter out
// 'deleted'
// METADATA for puzzles
PuzzleMetadata = newCollection('puzzleMetadata');
// schema:
// name: string
// url: bool
// showInSearch: bool
if (Meteor.isServer) {
Jigsaw.publish(null, function () {
return PuzzleMetadata.find();
});
// XXX add admin methods for PuzzleMetadata instead
PuzzleMetadata.allow({
insert: function () { return true; },
remove: function () { return true; },
update: function () { return true; }
});
// Initial data!
if (PuzzleMetadata.find().count() === 0) {
PuzzleMetadata.insert({name: "Puzzle URL", url: true, showInSearch: true});
PuzzleMetadata.insert({name: "Answer", showInSearch: true});
PuzzleMetadata.insert({name: "Wrong answers"});
}
} else {
Handlebars.registerHelper("allMetadata", function () {
// Want to sort in some consistent order; maybe should actually define a
// sort key or something later.
return PuzzleMetadata.find({}, {sort: ['name']});
});
Handlebars.registerHelper("metadataInSearch", function () {
// Want to sort in some consistent order; maybe should actually define a
// sort key or something later.
return PuzzleMetadata.find({showInSearch: true}, {sort: ['name']});
});
}
// FAMILIES of tags for puzzles (ie, popups)
Families = newCollection('families');
// schema:
// name: string
// values: array of string
// default: string (should be in values)
if (Meteor.isServer) {
Jigsaw.publish(null, function () {
return Families.find();
});
// XXX add admin methods for Families instead
Families.allow({
insert: function () { return true; },
remove: function () { return true; },
update: function () { return true; }
});
// Initial data!
if (Families.find().count() === 0) {
Families.insert({
name: "Status",
values: ["New", "Solved", "Needs Insight", "Needs Research"],
default: "New"
});
}
} else {
Handlebars.registerHelper("allFamilies", function () {
// Want to sort in some consistent order; maybe should actually define a
// sort key or something later.
return Families.find({}, {sort: ['name']});
});
}
// COMMENTS
Comments = newCollection('comments');
// schema:
// puzzleId
// created date
// updated date (optional)
// author
// text
// version
// priority: important, normal, useless
var COMMENT_PRIORITIES = ['important', 'normal', 'useless'];
if (Meteor.isServer) {
// XXX Need to make a supported way of calling this.
Comments._ensureIndex('puzzleId');
Jigsaw.publish('comments-by-puzzle', function (puzzleId) {
return Comments.find({puzzleId: puzzleId});
});
} else {
Meteor.autosubscribe(function () {
var puzzleId = JigsawRouter.currentPuzzleId();
if (puzzleId)
Meteor.subscribe('comments-by-puzzle', puzzleId);
});
}
var normalizeCommentText = function (text) {
if (!(/\S/.test(text)))
return null;
if (text.substr(text.length - 1) !== '\n')
return text + '\n';
return text;
};
// Comments can only be created via this function. Note that this will fail if
// called on the client outside of a stub. Returns true if a comment was
// created.
var createComment = function (puzzleId, text) {
text = normalizeCommentText(text);
if (text === null)
return false;
var author = Meteor.user().username;
if (!author)
return false;
if (!puzzleId)
return false;
Comments.insert({puzzleId: puzzleId,
created: +(new Date),
author: author,
originalAuthor: author,
text: text,
version: 1,
priority: 'normal'});
return true;
};
// UPLOADS
Uploads = newCollection('uploads');
// schema:
// puzzleId
// filepicker (the filepicker.io data)
// url (to filepicker.io)
// filename
// mimetype
// size
// isWritable
// key (in s3)
// created date
// author
if (Meteor.isServer) {
// XXX Need to make a supported way of calling this.
Uploads._ensureIndex('puzzleId');
Jigsaw.publish('uploads-by-puzzle', function (puzzleId) {
return Uploads.find({puzzleId: puzzleId});
});
} else {
Meteor.autosubscribe(function () {
var puzzleId = JigsawRouter.currentPuzzleId();
if (puzzleId)
Meteor.subscribe('uploads-by-puzzle', puzzleId);
});
}
// Comments can only be created via this function. Note that this will fail if
// called on the client outside of a stub. Returns true if a comment was
// created.
var createUpload = function (puzzleId, filepicker) {
var author = Meteor.user().username;
if (!author)
return false;
if (!puzzleId)
return false;
Uploads.insert({puzzleId: puzzleId,
created: +(new Date),
author: author,
filepicker: filepicker});
return true;
};
// Upload config.
if (Meteor.isServer) {
// does NOT use Jigsaw.publish
Meteor.publish("public-config", function () {
if (this.userId) {
this.added('PublicConfig', 'singleton',
Meteor.settings.publicConfig);
this.complete();
} else {
// leave incomplete until after login!
}
return null;
});
} else {
var publicConfigCollection = new Meteor.Collection("PublicConfig");
var getPublicConfig = function (name) {
var config = publicConfigCollection.findOne();
return config && config[name];
};
Meteor.subscribe("public-config", function () {
var filepickerKey = getPublicConfig("filepickerKey");
if (filepickerKey) {
filepicker.setKey(filepickerKey);
}
});
}
// BANNERS
// manually-set banners that go on every page
Banners = newCollection('banners');
// schema:
// content
// created (server-side timestamp)
if (Meteor.isServer) {
Jigsaw.publish(null, function () {
return Banners.find();
});
// You don't get to update banners, and insert is done via method to add
// server-side timestamp, but remove is OK.
Banners.allow({
remove: function () { return true; }
});
} else {
Handlebars.registerHelper("allBanners", function () {
return Banners.find({}, {sort: {created: -1}});
});
}
// NEWSFEED
// display recent things that happened
Newsfeed = newCollection('newsfeed');
// schema:
// htmlContent: *HTML* STRING
// created (server-side timestamp)
if (Meteor.isServer) {
Jigsaw.publish(null, function () {
return Newsfeed.find({}, {sort: {created: -1}, limit: 10});
});
} else {
Handlebars.registerHelper("newsfeed", function () {
return Newsfeed.find({}, {sort: {created: -1}});
});
}
// Newsfeeds can only be created via this function. Note that this will fail if
// called on the client outside of a stub.
var createNewsfeed = function (htmlContent) {
Newsfeed.insert({htmlContent: htmlContent, created: (+new Date)});
};
// HEADER LINKS
// display recent things that happened
HeaderLinks = newCollection('headerLinks');
// schema:
// text: string
// href: string
// created (server-side timestamp)
if (Meteor.isServer) {
Jigsaw.publish(null, function () {
return HeaderLinks.find();
});
// You don't get to update links, and insert is done via method to add
// server-side timestamp, but remove is OK.
HeaderLinks.allow({
remove: function () { return true; }
});
} else {
Handlebars.registerHelper("allHeaderLinks", function () {
return HeaderLinks.find({}, {sort: {created: 1}});
});
}
// CUSTOM CSS
// XXX