-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathGroup.js
575 lines (509 loc) · 18.3 KB
/
Group.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
//
// Hatch.js is a CMS and social website building framework built in Node.js
// Copyright (C) 2013 Inventures Software Ltd
//
// This file is part of Hatch.js
//
// Hatch.js is free software: you can redistribute it and/or modify it under the terms of the
// GNU Affero General Public License as published by the Free Software Foundation, version 3
//
// Hatch.js is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU Affero General Public License for more details. You should have received a copy of the GNU
// General Public License along with Hatch.js. If not, see <http://www.gnu.org/licenses/>.
//
// Authors: Marcus Greenwood, Anatoliy Chakkaev and others
//
var path = require('path');
module.exports = function (compound, Group) {
var Page = compound.models.Page;
var Content = compound.models.Content;
var User = compound.models.User;
var _ = require('underscore');
var async = require('async');
Group.hasMany(Page, {as: 'pages', foreignKey: 'groupId'});
/**
* Before a group is saved, make sure the URLs are up to date.
*
* @param {JSON} data - data to save
* @param {Function} next - continuation function
*/
Group.beforeCreate = Group.beforeSave = function (next, data) {
if (data.id) {
Group.updatePageUrls(data, next);
} else {
data.createdAt = new Date();
next();
}
};
/**
* Find a group for the specfied URL.
*
* @param {String} url - URL to search for
* @param {Function} callback - callback function
*/
Group.findByUrl = function (url, callback) {
function find (url, callback) {
Group.findOne({ where: { pageUrls: url }}, function (err, group) {
if (!group) {
Content.findOne({ where: { url: url }}, function (err, post) {
if (post) {
return callback(err, null, post);
} else {
if (url.indexOf('/') > -1) {
url = url.substring(0, url.lastIndexOf('/'));
return find(url, callback);
} else {
return callback(err, group);
}
}
});
} else {
return callback(err, group);
}
});
}
find(url, callback);
};
/**
* Update a group's URLs by getting the updated list from the pages within.
*
* @param {Group} group - group to update
* @param {Function} callback - callback function
*/
Group.updatePageUrls = function (group, callback) {
Page.all({ where: { groupId: group.id }}, function (err, pages) {
pages = _.filter(pages, function (page) { return page !== null && page.url; });
group.pageUrls = _.pluck(pages, 'url');
callback();
});
};
/**
* Get the path part of the URL for this group's homepage.
*
* @return {String} - group path
*/
Group.getter.path = function () {
return this._url && this._url.replace(/[^\/]+/, '');
};
/**
* Get the value of a setting within a module in this group.
*
* @param {String} name - full dot-notation setting name - e.g.
* core-settings.settingName
* @return {String}
*/
Group.prototype.getSetting = function (name) {
var moduleName = name.split('.')[0];
var setting = name.split('.')[1];
var module = this.getModule(moduleName);
if (!module) {
return null;
}
var result = module.contract[setting];
if (!result) {
var defaultModule = compound.app.get(moduleName);
if(!defaultModule) {
return null;
}
result = defaultModule[setting];
}
return result;
};
/**
* Find special page by it's relative path
*
* @param {String} pathname - relative page path with no domain or group
* path, it could not start with '/'. for root page it just blank string.
*
* @returns Object or Page
*/
Group.prototype.matchSpecialPage = function (pathname) {
var group = this;
var fullPagePath = path.join(this.url.match(/^[^\/]+/)[0], pathname);
fullPagePath = fullPagePath.replace(/\/$/, '').split('?')[0];
var found = null;
this.pagesCache.forEach(function (page) {
if (page.type !== 'page') {
var sp = compound.hatch.page.get(page.type);
if (sp && sp.matchRoute) {
var p = sp.matchRoute(group, pathname);
if (p) {
found = page;
found.handler = sp.handler;
found.specialPageParams = p;
}
}
}
});
if (!found) {
var special = compound.hatch.page.match(this, pathname);
var page = special[0];
var params = special[1];
if (page && page.defaultPage) {
found = group.pages.build(page.defaultPage);
found.url = this.homepage.url + '/' + page.defaultPath;
found.type = found.type || special.type;
found.grid = found.grid || '02-two-columns';
found.handler = page.handler;
found.specialPageParams = params;
}
}
console.log(found)
return found;
};
/**
* Define the page for the current request context.
*
* @param {String} url - current url
* @param {HttpContext} c - http context
* @param {Function} callback - callback function
*/
Group.prototype.definePage = function definePage(url, c, callback) {
// remove trailing and leading slashes
url = url.replace(/^\/|\/$/g, '');
var group = this;
var path = url.split('?')[0];
var page = c.req.page || this.matchSpecialPage(path);
// special page out of this group (sp.defaultPage)
if (page && page.type !== 'page') {
var handler = page.handler;
c.req.specialPageParams = page.specialPageParams;
}
if (!page) {
callback(null, null);
} else if (page.renderHtml) {
gotPage(null, page);
} else if (page.id) {
Page.find(page.id, gotPage);
} else {
gotPage(null, page);
}
function gotPage(err, page) {
if (page && page.templateId) {
var found = group.getCachedPage(page.templateId);
if (found) {
page.mergeTemplate(new Page(found));
return callback(err, page);
}
}
// store the page in the request
c.req.page = page;
if (handler) {
handler(c, function() {
callback(err, page);
});
} else {
callback(err, page);
}
}
};
/**
* Get a cached page from this group's pagesCache.
*
* @param {Number} id - page.id
* @return {Page} - page object
*/
Group.prototype.getCachedPage = function getCachedPage(id) {
var found;
this.pagesCache.forEach(function (p) {
if (p.id == id) {
found = p;
}
});
return found;
};
/**
* Clone this group and save the new group to the database.
*
* @param {Object} params - clone parameters
* @param {Function} callback - continuation function
*/
Group.prototype.clone = function (params, callback) {
var oldGroup = this;
var newUrl = params.url;
var newName = params.name;
if (!newUrl || !newName) {
return callback(new Error('Name and URL required'));
}
var g = oldGroup.toObject();
var oldUrl = g.homepage.url;
delete g.id;
// quick fix homepage
// TODO: move to juggling
var hp = {};
for (var i in g.homepage) hp[i] = g.homepage[i];
g.homepage = hp;
g.url = newUrl;
g.homepage.url = newUrl;
g.pagesCache = [];
g.name = newName;
g.locale = oldGroup.locale;
// remove slash from the end of group url
newUrl = newUrl.replace(/\/$/, '');
var pages, group;
var pageIds = {};
Page.findOne({where: { url: newUrl }}, function (err, p) {
if (p) {
return callback(new Error('URL already taken'));
}
createGroup();
});
function createGroup() {
Group.create(g, function (err, gg) {
group = gg;
oldGroup.pages(function (err, ps) {
if (ps.length === 0) {
return callback(null, group);
}
pages = Page.tree(ps).map(function (page) {
var p = page.toObject();
p.url = p.url.replace(oldUrl, newUrl + '/');
p.url = p.url.replace('//', '/');
p.groupId = group.id;
return p;
});
createHomepage(
createTemplates.bind(null, createPages)
);
});
});
}
function createHomepage(done) {
var calledOnce = false;
pages.forEach(function (p) {
if (calledOnce) return
if (p.url === newUrl + '/') {
calledOnce = true;
p.url = newUrl;
var oldId = p.id;
delete p.id;
Page.create(p, function (err, page) {
pageIds[oldId] = page.id;
group.homepage.id = page.id;
group.save(done);
pages.forEach(function (p) {
if (p.parentId === oldId) {
p.parentId = page.id;
}
});
});
}
});
}
function createTemplates(done) {
var wait = 1;
pages.forEach(function (p) {
if (p.type === 'template') {
var oldTemplateId = p.id;
delete p.id;
delete p.parentId;
wait += 1;
Page.create(p, function (err, page) {
pageIds[oldTemplateId] = page.id;
pages.forEach(function (p) {
if (p.templateId === oldTemplateId) {
p.templateId = page.id;
}
});
ok();
});
}
});
ok();
function ok() {
if (--wait === 0) done();
}
}
function createPages() {
var p = pages.shift();
if (!p) {
Page.updateGroup(group.id, function (err, group) {
callback(null, group);
});
return;
}
var oldId = p.id;
delete p.id;
p.parentId = pageIds[p.parentId];
Page.create(p, function (err, page) {
if (oldId) {
pageIds[oldId] = page.id;
}
createPages();
});
}
};
/**
* Set the specified user as the owner of this group.
*
* @param {User} user - new owner
* @param {Function} callback - callback function
*/
Group.prototype.setOwner = function (user, callback) {
var membership = user.memberships.find(this.id, 'groupId');
if (!membership) {
membership = {
groupId: this.id,
joinedAt: new Date(),
role: 'owner',
state: 'accepted'
};
user.memberships.push(membership);
}
membership.role = 'owner';
membership.state = 'accepted';
user.save(callback);
}
// TODO: deprecate
Group.prototype.handle = function groupEventHandler(name, env, done) {
// group can handle some events
// depending on special pages added to group
// we need to iterate through special pages
var sph, pageId;
this.pagesCache.forEach(function (page) {
if (!page.type || page.type === 'page') {
return;
}
var sp = env.api.module.getSpecialPage(page.type);
if (sp.respondTo === name) {
sph = sp;
pageId = page.id;
}
});
// checking wich pages responds to that events
// if appropriated page was found
if (sph) {
Page.find(pageId, function (err, page) {
env.req.page = page;
env.next = function() {
env.res.send(env.res.html);
};
// run page's special handler
if (sph.handler) {
console.log('got special page for that event', name);
sph.handler(env, function () {
// and then pass controll to admin/page#show (using pubsub->page)
env.api.pubsub.emit('page', env);
});
} else {
env.api.pubsub.emit('page', env);
}
});
} else {
done(false);
}
};
/**
* creates a new group from a blank template
*
* @param {[params]} params [group creation parameters]
* @param {Function} done [continuation function]
*/
Group.createFromScratch = function (params, done) {
params.modules = params.modules || [
{ name: 'user', contract: { google: true, local: true }},
{ name: 'admin'},
{ name: 'core'},
{ name: 'stylesheet' },
{ name: 'core-widgets' },
{ name: 'content' }
];
Page.findOne({where: {url: params.url}}, function (err, p) {
if (p) return done(new Error('Group exists'));
Group.create(params, function (e, g) {
if (e) return done(e);
g.pages.create({
url: params.url,
title: params.name
}, function (err, page) {
g.homepage = page.toMinimalObject();
g.save(function (err, g) {
Page.updateGroup(g.id);
addAdminUser(g.id, params.url.split('/')[0]);
setTimeout(done.bind(null, err, g), 500);
});
});
});
});
function addAdminUser(groupId, host) {
User.create({
email: 'admin@' + host,
username: 'admin-' + host,
password: 'secr3t',
membership: [{groupId: groupId, role: 'owner', state: 'approved'}]
}, function (err, user) {
});
}
};
/**
* Update the URL for this group.
*
* @param {String} url - new URL
*/
Group.prototype.updateUrl = function(url, next) {
var group = this;
var oldUrl = group.homepage.url;
if (oldUrl.indexOf('/') === oldUrl.length) {
oldUrl = oldUrl.substring(0, oldUrl.length -1);
}
//fix the url
if(url.indexOf('http') > -1) url = url.substring(url.indexOf('//') + 2);
//validate the new url
Page.all({ where: { url: url }}, function(err, pages) {
if(pages.length > 0) return next(new Error('Sorry, the URL "' + url + '" is being used by another group.'));
//get the pages for the group
Page.all({ where: { groupId: group.id }}, function(err, pages) {
//loop through each page and update the url for each
async.forEach(pages, function(page, next) {
console.log('replace ' + oldUrl + ' with ' + url + ' for: ' + page.url);
page.url = page.url.replace(oldUrl, url);
page.url = page.url.replace('//', '/');
if (page.url.lastIndexOf('/') === page.url.length) {
page.url = page.url.substring(0, page.url.length - 1);
}
console.log('new url = ' + page.url);
page.save(next);
}), function(err, results) {
Page.updateGroup(group.id, function() {
return next(err);
});
};
});
});
};
/**
* Get the specified module for this group.
*
* @param {String} name - module name
* @return {Module} - module
*/
Group.prototype.getModule = function(name) {
var module = this.modules.find(name, 'name');
return module;
};
/**
* Save a custom profile field for this group.
*
* @param {Object} field - custom profile field object
* @param {Function} callback - callback function
*/
Group.prototype.saveCustomProfileField = function (field, callback) {
if (field.id) {
this.customProfileFields.remove(this.customProfileFields.find(field.id, 'id'));
}
this.customProfileFields.push(field);
this.save(callback);
};
/**
* Remove a custom profile field from this group.
*
* @param {Number} fieldId - id of the field to remove
* @param {Function} callback - callback function
*/
Group.prototype.removeCustomProfileField = function (fieldId, callback) {
this.customProfileFields.remove(this.customProfileFields.find(fieldId, 'id'));
this.save(callback);
};
};