-
Notifications
You must be signed in to change notification settings - Fork 33
/
maki.js
697 lines (631 loc) · 16.7 KB
/
maki.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
var config = require('./config');
var Maki = require('./lib/Maki');
var maki = new Maki( config );
var Passport = require('maki-passport-local');
var passport = new Passport({
resource: 'Person'
});
maki.use( passport );
maki.use(require('maki-client-level'));
maki.use(require('maki-client-polymer'));
maki.use(require('maki-client-markdown'));
maki.use(require('maki-client-search'));
var AuthSlack = require('maki-auth-slack');
var authSlack = new AuthSlack({
resource: 'Person',
slack: config.auth.slack
});
maki.use(authSlack);
var CMS = require('maki-cms-local');
var cms = new CMS({
public: true,
icon: 'book',
name: 'Doc',
description: 'Resources and reference materials.',
source: __dirname + '/docs',
components: {
query: 'maki-api-index',
get: 'maki-doc-view'
},
base: '/docs',
path: '/docs',
view: process.env.PWD + '/views/page'
});
var guides = new CMS({
name: 'Guide',
base: '/guides',
path: '/source/guides',
view: process.env.PWD + '/views/page',
components: {
masthead: 'maki-page-header',
get: 'maki-page-view'
}
});
var examples = new CMS({
name: 'Example',
base: '/examples',
path: '/source/examples',
icon: 'idea',
public: true,
view: process.env.PWD + '/views/page',
components: {
masthead: 'maki-page-header',
query: 'maki-example-showcase',
get: 'maki-page-view',
}
});
var tutorials = new CMS({
name: 'Tutorial',
base: '/tutorials',
path: '/source/tutorials',
view: process.env.PWD + '/views/page'
});
var snippets = new CMS({
name: 'Snippet',
base: '/snippets',
path: '/source/snippets',
view: process.env.PWD + '/views/page'
});
var developers = new CMS({
name: 'Developer',
base: '/developers',
path: '/source/developers',
view: process.env.PWD + '/views/page'
});
var Auth = require('maki-auth-simple');
var auth = new Auth({
resource: 'Person'
});
maki.use(cms);
maki.use(guides);
maki.use(examples);
maki.use(tutorials);
maki.use(snippets);
maki.use(developers);
maki.use(auth);
// TODO: create relative time system that uses absolute values for static events
// but relative time for recurring events
// i.e., an `end` time of 86400000 would be an event that consistently lasts one
// full day from a relative `start` time of 0, with a `recurrence` value of any
// enumerable value.
var Event = maki.define('Event', {
icon: 'calendar',
description: 'A range of time',
attributes: {
name: { type: String },
start: { type: Date },
end: { type: Date },
// TODO: consider creating a "Recurrence" object that also stores behavior
recurrence: { type: String , enum: ['daily', 'weekly', 'monthly', 'yearly'] }
}
})
var Topic = maki.define('Topic', {
public: false,
icon: 'comment',
handle: 'Conversations',
masthead: '/img/sunrise.jpg',
description: 'Topics being discussed by the Maki community.',
components: {
query: 'maki-topic-browser',
get: 'maki-chat-interface'
},
attributes: {
id: { type: String , max: 80 , required: true },
name: { type: String , max: 80 , required: true , slug: true },
description: { type: String },
topic: { type: String },
created: { type: Date , default: Date.now },
creator: { type: String , ref: 'Person' },
authors: [ { type: String , ref: 'Person' } ],
subscribers: [ { type: String , ref: 'Person' } ],
// TODO: write migration scripts
//people: [ { type: String , ref: 'Person' } ],
stats: {
subscribers: { type: Number , default: 0 },
messages: { type: Number , default: 0 },
},
links: {
slack: { type: String , max: 40 }
}
},
requires: {
'Person': {
filter: function() {
var topic = this;
return { id: { $in: topic.people } }
},
sort: '-id'
},
'Message': {
filter: function() {
return { topic: this.id };
},
sort: '-created',
populate: 'author'
},
'channelsToJoin': {
resource: 'Topic',
filter: function() {
var defaults = ['community', 'projects', 'learning'];
return { $or: [
{ id: this.id },
{ id: { $in: defaults } }
] };
},
map: function(item) {
return item.id;
}
}
},
params: {
query: {
limit: 1000
}
},
fields: {
description: 'description',
}
});
// TODO: change Maki internals to do this automatically
// this is a major change, standardizing on `id` as the local identifier field
// which gets used in all URIs as the path indicator for this resource, as
// opposed to `slug`, which has been used historically.
Topic.pre('create', function(next, done) {
var topic = this;
var speakingurl = require('speakingurl');
topic.id = speakingurl(topic.name);
next();
});
var Message = maki.define('Message', {
icon: 'speech',
description: 'Messages about the Topics under discussion.',
public: false,
components: {
'query': 'maki-chat-interface'
},
attributes: {
// TODO: make this a special field at the Fabric layer, and make it part of
// the API. Remove from schema definition, potentially don't even have
// access to it within application scope. External anyway, right?
'@context': { type: String },
id: { type: String , max: 80 , required: true , slug: true , id: true },
topic: { type: String , ref: 'Topic' },
parent: { type: String , ref: 'Message' },
author: { type: String , ref: 'Person' },
content: { type: String },
created: { type: Date , default: Date.now },
reactions: {},
links: {
slack: { type: String , max: 40 }
},
stats: {
reactions: { type: Number , default: 0 }
}
},
params: {
query: {
limit: 1000,
sort: '-stats.reactions'
}
},
requires: {
'parent': {
resource: 'Message',
single: true,
filter: function() {
var localMessage = this;
return { id: localMessage.parent };
},
populate: 'author'
},
'replies': {
resource: 'Message',
filter: function() {
var localMessage = this;
return { parent: localMessage.id };
},
populate: 'author'
}
},
handlers: {
html: {
'create': function(req, res) {
var message = this;
if (message.topic) {
return res.status(302).redirect('/topics/' + message.topic);
} else {
return res.status(303).redirect('/messages/' + message.id );;
}
}
}
}
});
Topic.pre('create', populateCreator);
Topic.pre('update', populateCreator);
Message.pre('create', function assignHash (next, done) {
var crypto = require('crypto');
var message = this;
if (message.id) return next();
var now = Date.now() * 1000;
var key = [message.author, now].join(':');
var hash = crypto.createHash('sha256').update(key).digest('hex');
message.id = hash;
return next();
});
//Message.pre('create', inferSlackContext);
Message.pre('create', populateChannel);
Message.pre('create', publishToSlack);
Message.pre('create', reduceChannel);
Message.pre('create', populateAuthor);
Message.pre('update', populateAuthor);
Message.post('create', calculateTopicStats);
function populateAuthor (next, done) {
var message = this;
Person.get({
$or: [
{ 'links.slack': message.author },
{ 'id': message.author }
]
}, function(err, person) {
if (err) console.error(err);
if (!person) {
return done('No such author: ' + message.author);
}
message.author = person.id;
next();
});
}
function populateCreator (next, done) {
var topic = this;
Person.get({
$or: [
{ 'links.slack': topic.creator },
{ 'id': topic.creator }
]
}, function(err, person) {
if (err) console.error(err);
if (!person) {
return done('No such person: ' + topic.creator);
}
topic.creator = person.id;
next();
});
}
function populateChannel (next, done) {
var message = this;
Topic.get({
$or: [
{ 'links.slack': message.topic },
{ 'id': message.topic }
]
}, function(err, topic) {
if (err) console.error(err);
if (!topic) {
console.warn('No topic found, strange behavior ahead...');
//return done('No such topic: ' + message.topic);
}
message.topic = topic;
next();
});
}
function reduceChannel (next, done) {
var message = this;
if (message.topic && message.topic.id) {
message.topic = message.topic.id;
}
next();
}
var Invitation = maki.define('Invitation', {
public: false,
components: {
masthead: 'maki-invitation-splash',
query: 'maki-invitation-manager',
get: 'maki-invitation-view',
},
attributes: {
id: { type: String , required: true , slug: true },
from: { type: String , max: 240 , authorize: 'user' },
user: { type: String , max: 240 , ref: 'Person' },
email: { type: String , required: true , max: 240 },
avatar: { type: String },
topics: [ { type: String } ],
message: { type: String },
created: { type: Date , default: Date.now },
status: { type: String , enum: ['created', 'sent', 'accepted'] , default: 'created' },
stats: {
reminders: { type: Number , default: 0 },
people: { type: Number , default: 1 }
}
},
requires: {
Topic: {
query: {},
sort: 'id'
}
},
});
Invitation.pre('create', function(next, done) {
var invitation = this;
if (invitation.email) {
invitation.avatar = require('crypto').createHash('md5').update(invitation.email).digest('hex');
invitation.id = invitation.avatar;
}
Invitation.get({ email: invitation.email }, function(err, user) {
if (user) {
return Invitation.patch({ id: user.id }, [
{ op: 'replace', path: '/stats/reminders', value: ++user.stats.reminders }
], function(err) {
if (err) console.error(err);
done(null, user);
});
} else {
next();
}
});
});
Invitation.post('get', function(done) {
var invitation = this;
if (invitation.email) {
invitation.avatar = require('crypto').createHash('md5').update(invitation.email).digest('hex');
invitation.id = invitation.avatar;
}
done();
});
var Reminder = maki.define('Reminder', {
public: false,
attributes: {
invitation: { type: String , required: true },
created: { type: Date , default: Date.now }
}
});
function useInternalID (next, done) {
var subject = this;
subject.id = subject._id;
next();
}
function calculateTopicStats (done) {
var message = this;
console.log('post create message:', message);
var query = {
topic: message.topic
};
console.log('query:', query);
Message.Model.count(query, function(err, count) {
console.log('count callback:', err, count);
Topic.patch({ id: message.topic }, [
{ op: 'replace', path: '/stats/messages', value: count }
], function(err, num) {
console.log('patch applied,', err, num);
done(err);
});
});
}
function calculateInvitationStats (done) {
var reminder = this;
console.log('post create reminder:', reminder);
var query = {
invitation: reminder.invitation
};
console.log('query:', query);
Reminder.Model.count(query, function(err, count) {
console.log('count callback:', err, count);
Invitation.patch({ id: reminder.invitation }, [
{ op: 'replace', path: '/stats/reminders', value: count }
], function(err, num) {
console.log('patch applied,', err, num);
done(err);
});
});
}
function inferSlackContext (next, done) {
var message = this;
Person.get({
id: message.author
}, function(err, person) {
if (err) console.error(err);
if (person && person.tokens && person.tokens.slack) {
message['@context'] = person.tokens.slack;
}
next();
});
}
function publishToSlack (next, done) {
var message = this;
var rest = require('restler');
var crypto = require('crypto');
if (!message['@context']) return next();
if (!message['topic']) return next();
var doc = {
//token: config.slack.token, // TODO: replace with user token...
token: message['@context'],
// that will probably require a global context, passed with every request.
// this is likely necessary in the long run.
channel: message.topic.links.slack,
text: message.content,
as_user: true
};
rest.post('https://slack.com/api/chat.postMessage', {
data: doc
}).on('complete', function(data) {
var key = [data.message.channel, data.message.user, data.message.ts].join(':');
message.id = crypto.createHash('sha256').update(key).digest('hex');
next();
});
}
Reminder.post('create', calculateInvitationStats);
/*Reminder.post('create', function(done) {
var reminder = this;
// TODO: update internal APIs to use a channel here
Invitation.patch({ id: reminder.invitation }, [
{ op: 'add', path: '/stats/transactions', value: 1 }
], function(err, num) {
console.log('REMINDER POST CREATE:', err, num);
});
});*/
maki.define('Release', {
public: false,
icon: 'tag',
description: 'Officially tagged releases of the Maki library.',
//source: 'https://api.github.com/repos/martindale/maki/releases',
attributes: {
name: { type: String , max: 80 },
tag: { type: String , max: 80 },
created: { type: Date },
published: { type: Date },
notes: { type: String , render: 'markdown' }
},
map: function( release ) {
return {
name: release.name,
tag: release.tag_name,
notes: release.body,
published: new Date( release.published_at )
};
}
});
maki.define('Plugin', {
public: false,
handle: 'Extensions',
icon: 'puzzle',
description: 'Modules that extend the default Maki behaviors.',
attributes: {
name: { type: String , max: 80 },
description: { type: String },
version: { type: String , max: 10 },
coverage: { type: Number , default: 0 },
},
});
maki.define('Index', {
public: false,
name: 'Index',
templates: {
query: 'splash'
},
components: {
masthead: 'maki-pitch',
query: 'maki-splash',
get: 'maki-splash'
},
routes: {
query: '/'
},
static: true,
//internal: true
});
var Profile = maki.define('Profile', {
public: false,
attributes: {
id: { type: String , required: true },
service: { type: String , required: true },
created: { type: Date , default: Date.now },
data: {}
}
});
var Person = maki.define('Person', {
icon: 'users',
handle: 'Community',
description: 'The list of people working on Maki, including all extended members of the community.',
masthead: null,
// TODO: auto-infer
components: {
masthead: 'maki-community-welcome',
query: 'maki-community',
get: 'maki-profile'
},
attributes: {
username: { type: String , max: 80 , required: true , slug: true , id: true },
name: {
family: { type: String , max: 80 },
given: { type: String , max: 80 }
},
bio: { type: String },
hash: { type: String , restricted: true },
salt: { type: String , restricted: true },
email: { type: String , max: 80 , restricted: true },
created: { type: Date , default: Date.now },
image: {
original: { type: String , max: 1024 },
avatar: { type: String , max: 1024 },
},
links: {
slack: { type: String , max: 40 }
},
tokens: {
slack: { type: String }
},
profiles: [ { type: String } ],
stats: {
messages: { type: Number , default: 0 },
invitations: { type: Number , default: 0 }
},
status: { type: String , enum: ['away', 'active'] }
},
auth: {
'patch': ['admin', function(done) {
var person = this;
return false;
}]
},
params: {
query: {
limit: 1000
}
},
fields: {
image: 'image',
description: 'bio'
},
requires: {
'Message': {
filter: function() {
return { author: this.id };
},
sort: '-created',
populate: 'author'
},
'Topic': {
query: {},
sort: 'id'
},
'topicMemberships': {
resource: 'Topic',
filter: function() {
return { people: this.id };
}
}
}
});
Person.pre('create', function(next, done) {
var person = this;
person._id = person.id;
next();
});
Person.pre('update', function(next, done) {
var person = this;
console.log('person:pre:update', person);
person._id = person.id;
next();
});
Person.post('get', function(done) {
var person = this;
if (!person.name) person.name = {};
person.name.display = person.username;
done();
});
var Entity = maki.define('Entity', {
public: false,
attributes: {
coordinates: {
x: { type: Number , default: 0 },
y: { type: Number , default: 0 },
z: { type: Number , default: 0 }
}
}
});
/*var Analytics = require('maki-analytics');
var analytics = new Analytics({ id: 'UA-57746323-2' });
maki.use( analytics ).serve(['http']).start();*/
maki.start(function() {
//console.log('routes:', maki.routes);
});
module.exports = maki;