|
@@ -10,7 +10,7 @@ module.exports = function(crowi) {
|
|
|
, USER_PUBLIC_FIELDS = '_id fbId image googleId name username email status createdAt' // TODO: どこか別の場所へ...
|
|
, USER_PUBLIC_FIELDS = '_id fbId image googleId name username email status createdAt' // TODO: どこか別の場所へ...
|
|
|
, pageSchema;
|
|
, pageSchema;
|
|
|
|
|
|
|
|
- function populatePageData(pageData, revisionId, callback) {
|
|
|
|
|
|
|
+ function populatePageData(pageData, revisionId) {
|
|
|
var Page = crowi.model('Page');
|
|
var Page = crowi.model('Page');
|
|
|
|
|
|
|
|
pageData.latestRevision = pageData.revision;
|
|
pageData.latestRevision = pageData.revision;
|
|
@@ -20,13 +20,21 @@ module.exports = function(crowi) {
|
|
|
pageData.likerCount = pageData.liker.length || 0;
|
|
pageData.likerCount = pageData.liker.length || 0;
|
|
|
pageData.seenUsersCount = pageData.seenUsers.length || 0;
|
|
pageData.seenUsersCount = pageData.seenUsers.length || 0;
|
|
|
|
|
|
|
|
- pageData.populate([
|
|
|
|
|
- {path: 'creator', model: 'User', select: USER_PUBLIC_FIELDS},
|
|
|
|
|
- {path: 'revision', model: 'Revision'},
|
|
|
|
|
- {path: 'liker', options: { limit: 11 }},
|
|
|
|
|
- {path: 'seenUsers', options: { limit: 11 }},
|
|
|
|
|
- ], function (err, pageData) {
|
|
|
|
|
- Page.populate(pageData, {path: 'revision.author', model: 'User'}, callback);
|
|
|
|
|
|
|
+ return new Promise(function(resolve, reject) {
|
|
|
|
|
+ pageData.populate([
|
|
|
|
|
+ {path: 'creator', model: 'User', select: USER_PUBLIC_FIELDS},
|
|
|
|
|
+ {path: 'revision', model: 'Revision'},
|
|
|
|
|
+ {path: 'liker', options: { limit: 11 }},
|
|
|
|
|
+ {path: 'seenUsers', options: { limit: 11 }},
|
|
|
|
|
+ ], function (err, pageData) {
|
|
|
|
|
+ Page.populate(pageData, {path: 'revision.author', model: 'User'}, function(err, data) {
|
|
|
|
|
+ if (err) {
|
|
|
|
|
+ return reject(err);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return resolve(data);
|
|
|
|
|
+ });
|
|
|
|
|
+ });
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -52,6 +60,14 @@ module.exports = function(crowi) {
|
|
|
return false;
|
|
return false;
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
|
|
+ pageSchema.methods.isPortal = function() {
|
|
|
|
|
+ if (this.path.match(/.+\/$/)) {
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return false;
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
pageSchema.methods.isCreator = function(userData) {
|
|
pageSchema.methods.isCreator = function(userData) {
|
|
|
if (this.populated('creator') && this.creator._id.toString() === userData._id.toString()) {
|
|
if (this.populated('creator') && this.creator._id.toString() === userData._id.toString()) {
|
|
|
return true;
|
|
return true;
|
|
@@ -181,6 +197,18 @@ module.exports = function(crowi) {
|
|
|
});
|
|
});
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
|
|
+ pageSchema.statics.hasPortalPage = function (path, user) {
|
|
|
|
|
+ var self = this;
|
|
|
|
|
+ return new Promise(function(resolve, reject) {
|
|
|
|
|
+ self.findPage(path, user)
|
|
|
|
|
+ .then(function(page) {
|
|
|
|
|
+ resolve(page);
|
|
|
|
|
+ }).catch(function(err) {
|
|
|
|
|
+ resolve(null); // check only has portal page, through error
|
|
|
|
|
+ });
|
|
|
|
|
+ });
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
pageSchema.statics.getGrantLabels = function() {
|
|
pageSchema.statics.getGrantLabels = function() {
|
|
|
var grantLabels = {};
|
|
var grantLabels = {};
|
|
|
grantLabels[GRANT_PUBLIC] = '公開';
|
|
grantLabels[GRANT_PUBLIC] = '公開';
|
|
@@ -239,54 +267,72 @@ module.exports = function(crowi) {
|
|
|
});
|
|
});
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
- pageSchema.statics.findPageById = function(id, cb) {
|
|
|
|
|
|
|
+ pageSchema.statics.findPageById = function(id) {
|
|
|
var Page = this;
|
|
var Page = this;
|
|
|
|
|
|
|
|
- Page.findOne({_id: id}, function(err, pageData) {
|
|
|
|
|
- if (pageData === null) {
|
|
|
|
|
- return cb(new Error('Page Not Found'), null);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ return new Promise(function(resolve, reject) {
|
|
|
|
|
+ Page.findOne({_id: id}, function(err, pageData) {
|
|
|
|
|
+ if (err) {
|
|
|
|
|
+ return reject(err);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- return populatePageData(pageData, null, cb);
|
|
|
|
|
|
|
+ populatePageData(pageData, null).then(resolve).catch(reject);
|
|
|
|
|
+ });
|
|
|
});
|
|
});
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
- pageSchema.statics.findPageByIdAndGrantedUser = function(id, userData, cb) {
|
|
|
|
|
|
|
+ pageSchema.statics.findPageByIdAndGrantedUser = function(id, userData) {
|
|
|
var Page = this;
|
|
var Page = this;
|
|
|
|
|
|
|
|
- Page.findPageById(id, function(err, pageData) {
|
|
|
|
|
- if (pageData === null) {
|
|
|
|
|
- return cb(new Error('Page Not Found'), null);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if (userData && !pageData.isGrantedFor(userData)) {
|
|
|
|
|
- return cb(PAGE_GRANT_ERROR, null);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ return new Promise(function(resolve, reject) {
|
|
|
|
|
+ Page.findPageById(id)
|
|
|
|
|
+ .then(function(pageData) {
|
|
|
|
|
+ if (userData && !pageData.isGrantedFor(userData)) {
|
|
|
|
|
+ return reject(new Error('Page is not granted for the user')); //PAGE_GRANT_ERROR, null);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- return cb(null,pageData);
|
|
|
|
|
|
|
+ return resolve(pageData);
|
|
|
|
|
+ }).catch(function(err) {
|
|
|
|
|
+ return reject(err);
|
|
|
|
|
+ });
|
|
|
});
|
|
});
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
- pageSchema.statics.findPage = function(path, userData, revisionId, options, cb) {
|
|
|
|
|
- var Page = this;
|
|
|
|
|
|
|
+ // find page and check if granted user
|
|
|
|
|
+ pageSchema.statics.findPage = function(path, userData, revisionId, ignoreNotFound) {
|
|
|
|
|
+ var self = this;
|
|
|
|
|
|
|
|
- this.findOne({path: path}, function(err, pageData) {
|
|
|
|
|
- if (pageData === null) {
|
|
|
|
|
- return cb(new Error('Page Not Found'), null);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ return new Promise(function(resolve, reject) {
|
|
|
|
|
+ self.findOne({path: path}, function(err, pageData) {
|
|
|
|
|
+ if (err) {
|
|
|
|
|
+ return reject(err);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- if (!pageData.isGrantedFor(userData)) {
|
|
|
|
|
- return cb(PAGE_GRANT_ERROR, null);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if (pageData === null) {
|
|
|
|
|
+ if (ignoreNotFound) {
|
|
|
|
|
+ return resolve(null);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ var pageNotFoundError = new Error('Page Not Found')
|
|
|
|
|
+ pageNotFoundError.name = 'Crowi:Page:NotFound';
|
|
|
|
|
+ return reject(pageNotFoundError);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (!pageData.isGrantedFor(userData)) {
|
|
|
|
|
+ return reject(new Error('Page is not granted for the user')); //PAGE_GRANT_ERROR, null);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- return populatePageData(pageData, revisionId, cb);
|
|
|
|
|
|
|
+ populatePageData(pageData, revisionId || null).then(resolve).catch(reject);
|
|
|
|
|
+ });
|
|
|
});
|
|
});
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
- pageSchema.statics.findListByPageIds = function(ids, options, cb) {
|
|
|
|
|
|
|
+ pageSchema.statics.findListByPageIds = function(ids, options) {
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
- pageSchema.statics.findListByStartWith = function(path, userData, options, cb) {
|
|
|
|
|
|
|
+ pageSchema.statics.findListByStartWith = function(path, userData, options) {
|
|
|
|
|
+ var Page = this;
|
|
|
|
|
+
|
|
|
if (!options) {
|
|
if (!options) {
|
|
|
options = {sort: 'updatedAt', desc: -1, offset: 0, limit: 50};
|
|
options = {sort: 'updatedAt', desc: -1, offset: 0, limit: 50};
|
|
|
}
|
|
}
|
|
@@ -301,24 +347,30 @@ module.exports = function(crowi) {
|
|
|
var queryReg = new RegExp('^' + path);
|
|
var queryReg = new RegExp('^' + path);
|
|
|
var sliceOption = options.revisionSlice || {$slice: 1};
|
|
var sliceOption = options.revisionSlice || {$slice: 1};
|
|
|
|
|
|
|
|
- var q = this.find({
|
|
|
|
|
- path: queryReg,
|
|
|
|
|
- redirectTo: null,
|
|
|
|
|
- $or: [
|
|
|
|
|
- {grant: null},
|
|
|
|
|
- {grant: GRANT_PUBLIC},
|
|
|
|
|
- {grant: GRANT_RESTRICTED, grantedUsers: userData._id},
|
|
|
|
|
- {grant: GRANT_SPECIFIED, grantedUsers: userData._id},
|
|
|
|
|
- {grant: GRANT_OWNER, grantedUsers: userData._id},
|
|
|
|
|
- ],
|
|
|
|
|
- })
|
|
|
|
|
- .populate('revision')
|
|
|
|
|
- .sort(sortOpt)
|
|
|
|
|
- .skip(opt.offset)
|
|
|
|
|
- .limit(opt.limit);
|
|
|
|
|
-
|
|
|
|
|
- q.exec(function(err, data) {
|
|
|
|
|
- cb(err, data);
|
|
|
|
|
|
|
+ return new Promise(function(resolve, reject) {
|
|
|
|
|
+ var q = Page.find({
|
|
|
|
|
+ path: queryReg,
|
|
|
|
|
+ redirectTo: null,
|
|
|
|
|
+ $or: [
|
|
|
|
|
+ {grant: null},
|
|
|
|
|
+ {grant: GRANT_PUBLIC},
|
|
|
|
|
+ {grant: GRANT_RESTRICTED, grantedUsers: userData._id},
|
|
|
|
|
+ {grant: GRANT_SPECIFIED, grantedUsers: userData._id},
|
|
|
|
|
+ {grant: GRANT_OWNER, grantedUsers: userData._id},
|
|
|
|
|
+ ],
|
|
|
|
|
+ })
|
|
|
|
|
+ .populate('revision')
|
|
|
|
|
+ .sort(sortOpt)
|
|
|
|
|
+ .skip(opt.offset)
|
|
|
|
|
+ .limit(opt.limit);
|
|
|
|
|
+
|
|
|
|
|
+ q.exec(function(err, data) {
|
|
|
|
|
+ if (err) {
|
|
|
|
|
+ return reject(err);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ resolve(data);
|
|
|
|
|
+ });
|
|
|
});
|
|
});
|
|
|
};
|
|
};
|
|
|
|
|
|
|
@@ -329,83 +381,109 @@ module.exports = function(crowi) {
|
|
|
});
|
|
});
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
- pageSchema.statics.updateGrant = function(page, grant, userData, cb) {
|
|
|
|
|
- this.update({_id: page._id}, {$set: {grant: grant}}, function(err, data) {
|
|
|
|
|
- if (grant == GRANT_PUBLIC) {
|
|
|
|
|
- page.grantedUsers = [];
|
|
|
|
|
- } else {
|
|
|
|
|
- page.grantedUsers = [];
|
|
|
|
|
- page.grantedUsers.push(userData._id);
|
|
|
|
|
- }
|
|
|
|
|
- page.save(function(err, data) {
|
|
|
|
|
- return cb(err, data);
|
|
|
|
|
|
|
+ pageSchema.statics.updateGrant = function(page, grant, userData) {
|
|
|
|
|
+ var self = this;
|
|
|
|
|
+
|
|
|
|
|
+ return new Promise(function(resolve, reject) {
|
|
|
|
|
+ self.update({_id: page._id}, {$set: {grant: grant}}, function(err, data) {
|
|
|
|
|
+ if (err) {
|
|
|
|
|
+ return reject(err);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (grant == GRANT_PUBLIC) {
|
|
|
|
|
+ page.grantedUsers = [];
|
|
|
|
|
+ } else {
|
|
|
|
|
+ page.grantedUsers = [];
|
|
|
|
|
+ page.grantedUsers.push(userData._id);
|
|
|
|
|
+ }
|
|
|
|
|
+ page.save(function(err, data) {
|
|
|
|
|
+ if (err) {
|
|
|
|
|
+ return reject(err);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return resolve(data);
|
|
|
|
|
+ });
|
|
|
});
|
|
});
|
|
|
});
|
|
});
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
// Instance method でいいのでは
|
|
// Instance method でいいのでは
|
|
|
- pageSchema.statics.pushToGrantedUsers = function(page, userData, cb) {
|
|
|
|
|
- if (!page.grantedUsers || !Array.isArray(page.grantedUsers)) {
|
|
|
|
|
- page.grantedUsers = [];
|
|
|
|
|
- }
|
|
|
|
|
- page.grantedUsers.push(userData._id);
|
|
|
|
|
- page.save(function(err, data) {
|
|
|
|
|
- return cb(err, data);
|
|
|
|
|
|
|
+ pageSchema.statics.pushToGrantedUsers = function(page, userData) {
|
|
|
|
|
+
|
|
|
|
|
+ return new Promise(function(resolve, reject) {
|
|
|
|
|
+ if (!page.grantedUsers || !Array.isArray(page.grantedUsers)) {
|
|
|
|
|
+ page.grantedUsers = [];
|
|
|
|
|
+ }
|
|
|
|
|
+ page.grantedUsers.push(userData);
|
|
|
|
|
+ page.save(function(err, data) {
|
|
|
|
|
+ if (err) {
|
|
|
|
|
+ return reject(err);
|
|
|
|
|
+ }
|
|
|
|
|
+ return resolve(data);
|
|
|
|
|
+ });
|
|
|
});
|
|
});
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
- pageSchema.statics.pushRevision = function(pageData, newRevision, user, cb) {
|
|
|
|
|
- newRevision.save(function(err, newRevision) {
|
|
|
|
|
- if (err) {
|
|
|
|
|
- debug('Error on saving revision', err);
|
|
|
|
|
- return cb(err, null);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ pageSchema.statics.pushRevision = function(pageData, newRevision, user) {
|
|
|
|
|
|
|
|
- debug('Successfully saved new revision', newRevision);
|
|
|
|
|
- pageData.revision = newRevision._id;
|
|
|
|
|
- pageData.updatedAt = Date.now();
|
|
|
|
|
- pageData.save(function(err, data) {
|
|
|
|
|
|
|
+ return new Promise(function(resolve, reject) {
|
|
|
|
|
+ newRevision.save(function(err, newRevision) {
|
|
|
if (err) {
|
|
if (err) {
|
|
|
- debug('Error on save page data (after push revision)', err);
|
|
|
|
|
- cb(err, null);
|
|
|
|
|
- return;
|
|
|
|
|
|
|
+ debug('Error on saving revision', err);
|
|
|
|
|
+ return reject(err);
|
|
|
}
|
|
}
|
|
|
- cb(err, data);
|
|
|
|
|
|
|
+
|
|
|
|
|
+ debug('Successfully saved new revision', newRevision);
|
|
|
|
|
+ pageData.revision = newRevision;
|
|
|
|
|
+ pageData.updatedAt = Date.now();
|
|
|
|
|
+ pageData.save(function(err, data) {
|
|
|
|
|
+ if (err) {
|
|
|
|
|
+ // todo: remove new revision?
|
|
|
|
|
+ debug('Error on save page data (after push revision)', err);
|
|
|
|
|
+ return reject(err);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ resolve(data);
|
|
|
|
|
+ });
|
|
|
});
|
|
});
|
|
|
});
|
|
});
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
- pageSchema.statics.create = function(path, body, user, options, cb) {
|
|
|
|
|
|
|
+ pageSchema.statics.create = function(path, body, user, options) {
|
|
|
var Page = this
|
|
var Page = this
|
|
|
, Revision = crowi.model('Revision')
|
|
, Revision = crowi.model('Revision')
|
|
|
, format = options.format || 'markdown'
|
|
, format = options.format || 'markdown'
|
|
|
, grant = options.grant || GRANT_PUBLIC
|
|
, grant = options.grant || GRANT_PUBLIC
|
|
|
, redirectTo = options.redirectTo || null;
|
|
, redirectTo = options.redirectTo || null;
|
|
|
|
|
|
|
|
- this.findOne({path: path}, function(err, pageData) {
|
|
|
|
|
- if (pageData) {
|
|
|
|
|
- cb(new Error('Cannot create new page to existed path'), null);
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ return new Promise(function(resolve, reject) {
|
|
|
|
|
+ Page.findOne({path: path}, function(err, pageData) {
|
|
|
|
|
+ if (pageData) {
|
|
|
|
|
+ return reject(new Error('Cannot create new page to existed path'));
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- var newPage = new Page();
|
|
|
|
|
- newPage.path = path;
|
|
|
|
|
- newPage.creator = user;
|
|
|
|
|
- newPage.createdAt = Date.now();
|
|
|
|
|
- newPage.updatedAt = Date.now();
|
|
|
|
|
- newPage.redirectTo = redirectTo;
|
|
|
|
|
- newPage.grant = grant;
|
|
|
|
|
- newPage.grantedUsers = [];
|
|
|
|
|
- newPage.grantedUsers.push(user);
|
|
|
|
|
- newPage.save(function (err, newPage) {
|
|
|
|
|
-
|
|
|
|
|
- var newRevision = Revision.prepareRevision(newPage, body, user, {format: format});
|
|
|
|
|
- Page.pushRevision(newPage, newRevision, user, function(err, data) {
|
|
|
|
|
|
|
+ var newPage = new Page();
|
|
|
|
|
+ newPage.path = path;
|
|
|
|
|
+ newPage.creator = user;
|
|
|
|
|
+ newPage.createdAt = Date.now();
|
|
|
|
|
+ newPage.updatedAt = Date.now();
|
|
|
|
|
+ newPage.redirectTo = redirectTo;
|
|
|
|
|
+ newPage.grant = grant;
|
|
|
|
|
+ newPage.grantedUsers = [];
|
|
|
|
|
+ newPage.grantedUsers.push(user);
|
|
|
|
|
+
|
|
|
|
|
+ newPage.save(function (err, newPage) {
|
|
|
if (err) {
|
|
if (err) {
|
|
|
- console.log('Push Revision Error on create page', err);
|
|
|
|
|
|
|
+ return reject(err);
|
|
|
}
|
|
}
|
|
|
- cb(err, data);
|
|
|
|
|
- return;
|
|
|
|
|
|
|
+
|
|
|
|
|
+ var newRevision = Revision.prepareRevision(newPage, body, user, {format: format});
|
|
|
|
|
+ Page.pushRevision(newPage, newRevision, user).then(function(data) {
|
|
|
|
|
+ resolve(data);
|
|
|
|
|
+ }).catch(function(err) {
|
|
|
|
|
+ debug('Push Revision Error on create page', err);
|
|
|
|
|
+ return reject(err);
|
|
|
|
|
+ });
|
|
|
});
|
|
});
|
|
|
});
|
|
});
|
|
|
});
|
|
});
|