| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267 |
- module.exports = function(crowi) {
- var debug = require('debug')('growi:models:page')
- , mongoose = require('mongoose')
- , escapeStringRegexp = require('escape-string-regexp')
- , ObjectId = mongoose.Schema.Types.ObjectId
- , GRANT_PUBLIC = 1
- , GRANT_RESTRICTED = 2
- , GRANT_SPECIFIED = 3
- , GRANT_OWNER = 4
- , GRANT_USER_GROUP = 5
- , PAGE_GRANT_ERROR = 1
- , STATUS_WIP = 'wip'
- , STATUS_PUBLISHED = 'published'
- , STATUS_DELETED = 'deleted'
- , STATUS_DEPRECATED = 'deprecated'
- , pageEvent = crowi.event('page')
- , pageSchema
- , Comment = crowi.model('Comment');
- function isPortalPath(path) {
- if (path.match(/.*\/$/)) {
- return true;
- }
- return false;
- }
- pageSchema = new mongoose.Schema({
- path: { type: String, required: true, index: true, unique: true },
- revision: { type: ObjectId, ref: 'Revision' },
- redirectTo: { type: String, index: true },
- status: { type: String, default: STATUS_PUBLISHED, index: true },
- grant: { type: Number, default: GRANT_PUBLIC, index: true },
- grantedUsers: [{ type: ObjectId, ref: 'User' }],
- creator: { type: ObjectId, ref: 'User', index: true },
- // lastUpdateUser: this schema is from 1.5.x (by deletion feature), and null is default.
- // the last update user on the screen is by revesion.author for B.C.
- lastUpdateUser: { type: ObjectId, ref: 'User', index: true },
- liker: [{ type: ObjectId, ref: 'User', index: true }],
- seenUsers: [{ type: ObjectId, ref: 'User', index: true }],
- commentCount: { type: Number, default: 0 },
- extended: {
- type: String,
- default: '{}',
- get: function(data) {
- try {
- return JSON.parse(data);
- }
- catch (e) {
- return data;
- }
- },
- set: function(data) {
- return JSON.stringify(data);
- }
- },
- createdAt: { type: Date, default: Date.now },
- updatedAt: Date
- }, {
- toJSON: {getters: true},
- toObject: {getters: true}
- });
- pageEvent.on('create', pageEvent.onCreate);
- pageEvent.on('update', pageEvent.onUpdate);
- pageSchema.methods.isWIP = function() {
- return this.status === STATUS_WIP;
- };
- pageSchema.methods.isPublished = function() {
- // null: this is for B.C.
- return this.status === null || this.status === STATUS_PUBLISHED;
- };
- pageSchema.methods.isDeleted = function() {
- return this.status === STATUS_DELETED;
- };
- pageSchema.methods.isDeprecated = function() {
- return this.status === STATUS_DEPRECATED;
- };
- pageSchema.methods.isPublic = function() {
- if (!this.grant || this.grant == GRANT_PUBLIC) {
- return true;
- }
- return false;
- };
- pageSchema.methods.isPortal = function() {
- return isPortalPath(this.path);
- };
- pageSchema.methods.isCreator = function(userData) {
- // ゲスト閲覧の場合は userData に false が入る
- if (!userData) {
- return false;
- }
- if (this.populated('creator') && this.creator._id.toString() === userData._id.toString()) {
- return true;
- }
- else if (this.creator.toString() === userData._id.toString()) {
- return true;
- }
- return false;
- };
- pageSchema.methods.isGrantedFor = function(userData) {
- if (this.isPublic() || this.isCreator(userData)) {
- return true;
- }
- if (this.grantedUsers.indexOf(userData._id) >= 0) {
- return true;
- }
- return false;
- };
- pageSchema.methods.isLatestRevision = function() {
- // populate されていなくて判断できない
- if (!this.latestRevision || !this.revision) {
- return true;
- }
- return (this.latestRevision == this.revision._id.toString());
- };
- pageSchema.methods.isUpdatable = function(previousRevision) {
- var revision = this.latestRevision || this.revision;
- if (revision != previousRevision) {
- return false;
- }
- return true;
- };
- pageSchema.methods.isLiked = function(userData) {
- return this.liker.some(function(likedUser) {
- return likedUser == userData._id.toString();
- });
- };
- pageSchema.methods.like = function(userData) {
- var self = this,
- Page = self;
- return new Promise(function(resolve, reject) {
- var added = self.liker.addToSet(userData._id);
- if (added.length > 0) {
- self.save(function(err, data) {
- if (err) {
- return reject(err);
- }
- debug('liker updated!', added);
- return resolve(data);
- });
- }
- else {
- debug('liker not updated');
- return reject(self);
- }
- });
- };
- pageSchema.methods.unlike = function(userData, callback) {
- var self = this,
- Page = self;
- return new Promise(function(resolve, reject) {
- var beforeCount = self.liker.length;
- self.liker.pull(userData._id);
- if (self.liker.length != beforeCount) {
- self.save(function(err, data) {
- if (err) {
- return reject(err);
- }
- return resolve(data);
- });
- }
- else {
- debug('liker not updated');
- return reject(self);
- }
- });
- };
- pageSchema.methods.isSeenUser = function(userData) {
- var self = this,
- Page = self;
- return this.seenUsers.some(function(seenUser) {
- return seenUser.equals(userData._id);
- });
- };
- pageSchema.methods.seen = function(userData) {
- var self = this,
- Page = self;
- if (this.isSeenUser(userData)) {
- debug('seenUsers not updated');
- return Promise.resolve(this);
- }
- return new Promise(function(resolve, reject) {
- if (!userData || !userData._id) {
- reject(new Error('User data is not valid'));
- }
- var added = self.seenUsers.addToSet(userData);
- self.save(function(err, data) {
- if (err) {
- return reject(err);
- }
- debug('seenUsers updated!', added);
- return resolve(self);
- });
- });
- };
- pageSchema.methods.getSlackChannel = function() {
- var extended = this.get('extended');
- if (!extended) {
- return '';
- }
- return extended.slack || '';
- };
- pageSchema.methods.updateSlackChannel = function(slackChannel) {
- var extended = this.extended;
- extended.slack = slackChannel;
- return this.updateExtended(extended);
- };
- pageSchema.methods.updateExtended = function(extended) {
- var page = this;
- page.extended = extended;
- return new Promise(function(resolve, reject) {
- return page.save(function(err, doc) {
- if (err) {
- return reject(err);
- }
- return resolve(doc);
- });
- });
- };
- pageSchema.statics.populatePageData = function(pageData, revisionId) {
- var Page = crowi.model('Page');
- var User = crowi.model('User');
- pageData.latestRevision = pageData.revision;
- if (revisionId) {
- pageData.revision = revisionId;
- }
- pageData.likerCount = pageData.liker.length || 0;
- pageData.seenUsersCount = pageData.seenUsers.length || 0;
- return new Promise(function(resolve, reject) {
- pageData.populate([
- {path: 'lastUpdateUser', model: 'User', select: User.USER_PUBLIC_FIELDS},
- {path: 'creator', model: 'User', select: User.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', select: User.USER_PUBLIC_FIELDS}, function(err, data) {
- if (err) {
- return reject(err);
- }
- return resolve(data);
- });
- });
- });
- };
- pageSchema.statics.populatePageListToAnyObjects = function(pageIdObjectArray) {
- var Page = this;
- var pageIdMappings = {};
- var pageIds = pageIdObjectArray.map(function(page, idx) {
- if (!page._id) {
- throw new Error('Pass the arg of populatePageListToAnyObjects() must have _id on each element.');
- }
- pageIdMappings[String(page._id)] = idx;
- return page._id;
- });
- return new Promise(function(resolve, reject) {
- Page.findListByPageIds(pageIds, {limit: 100}) // limit => if the pagIds is greater than 100, ignore
- .then(function(pages) {
- pages.forEach(function(page) {
- Object.assign(pageIdObjectArray[pageIdMappings[String(page._id)]], page._doc);
- });
- resolve(pageIdObjectArray);
- });
- });
- };
- pageSchema.statics.updateCommentCount = function(pageId) {
- var self = this;
- var Comment = crowi.model('Comment');
- return Comment.countCommentByPageId(pageId)
- .then(function(count) {
- self.update({_id: pageId}, {commentCount: count}, {}, function(err, data) {
- if (err) {
- debug('Update commentCount Error', err);
- throw err;
- }
- return data;
- });
- });
- };
- pageSchema.statics.hasPortalPage = function(path, user, revisionId) {
- var self = this;
- return new Promise(function(resolve, reject) {
- self.findPage(path, user, revisionId)
- .then(function(page) {
- resolve(page);
- }).catch(function(err) {
- resolve(null); // check only has portal page, through error
- });
- });
- };
- pageSchema.statics.getGrantLabels = function() {
- var grantLabels = {};
- grantLabels[GRANT_PUBLIC] = 'Public'; // 公開
- grantLabels[GRANT_RESTRICTED] = 'Anyone with the link'; // リンクを知っている人のみ
- //grantLabels[GRANT_SPECIFIED] = 'Specified users only'; // 特定ユーザーのみ
- grantLabels[GRANT_USER_GROUP] = 'Only inside the group'; // 特定グループのみ
- grantLabels[GRANT_OWNER] = 'Just me'; // 自分のみ
- return grantLabels;
- };
- pageSchema.statics.normalizePath = function(path) {
- if (!path.match(/^\//)) {
- path = '/' + path;
- }
- path = path.replace(/\/\s+?/g, '/').replace(/\s+\//g, '/');
- return path;
- };
- pageSchema.statics.getUserPagePath = function(user) {
- return '/user/' + user.username;
- };
- pageSchema.statics.getDeletedPageName = function(path) {
- if (path.match('\/')) {
- path = path.substr(1);
- }
- return '/trash/' + path;
- };
- pageSchema.statics.getRevertDeletedPageName = function(path) {
- return path.replace('\/trash', '');
- };
- pageSchema.statics.isDeletableName = function(path) {
- var notDeletable = [
- /^\/user\/[^\/]+$/, // user page
- ];
- for (var i = 0; i < notDeletable.length; i++) {
- var pattern = notDeletable[i];
- if (path.match(pattern)) {
- return false;
- }
- }
- return true;
- };
- pageSchema.statics.isCreatableName = function(name) {
- var forbiddenPages = [
- /\^|\$|\*|\+|\#/,
- /^\/_.*/, // /_api/* and so on
- /^\/\-\/.*/,
- /^\/_r\/.*/,
- /^\/?https?:\/\/.+$/, // avoid miss in renaming
- /\/{2,}/, // avoid miss in renaming
- /\s+\/\s+/, // avoid miss in renaming
- /.+\/edit$/,
- /.+\.md$/,
- /^\/(installer|register|login|logout|admin|me|files|trash|paste|comments)(\/.*|$)/,
- ];
- var isCreatable = true;
- forbiddenPages.forEach(function(page) {
- var pageNameReg = new RegExp(page);
- if (name.match(pageNameReg)) {
- isCreatable = false;
- return ;
- }
- });
- return isCreatable;
- };
- pageSchema.statics.fixToCreatableName = function(path) {
- return path
- .replace(/\/\//g, '/')
- ;
- };
- pageSchema.statics.updateRevision = function(pageId, revisionId, cb) {
- this.update({_id: pageId}, {revision: revisionId}, {}, function(err, data) {
- cb(err, data);
- });
- };
- pageSchema.statics.findUpdatedList = function(offset, limit, cb) {
- this
- .find({})
- .sort({updatedAt: -1})
- .skip(offset)
- .limit(limit)
- .exec(function(err, data) {
- cb(err, data);
- });
- };
- pageSchema.statics.findPageById = function(id) {
- var Page = this;
- return new Promise(function(resolve, reject) {
- Page.findOne({_id: id}, function(err, pageData) {
- if (err) {
- return reject(err);
- }
- if (pageData == null) {
- return reject(new Error('Page not found'));
- }
- return Page.populatePageData(pageData, null).then(resolve);
- });
- });
- };
- pageSchema.statics.findPageByIdAndGrantedUser = function(id, userData) {
- var Page = this;
- var PageGroupRelation = crowi.model('PageGroupRelation');
- var pageData = null;
- return new Promise(function(resolve, reject) {
- Page.findPageById(id)
- .then(function(result) {
- pageData = result;
- if (userData && !pageData.isGrantedFor(userData)) {
- return PageGroupRelation.isExistsGrantedGroupForPageAndUser(pageData, userData);
- }
- else {
- return true;
- }
- }).then((checkResult) => {
- if (checkResult) {
- return resolve(pageData);
- }
- else {
- return reject(new Error('Page is not granted for the user')); //PAGE_GRANT_ERROR, null);
- }
- }).catch(function(err) {
- return reject(err);
- });
- });
- };
- // find page and check if granted user
- pageSchema.statics.findPage = function(path, userData, revisionId, ignoreNotFound) {
- var self = this;
- var PageGroupRelation = crowi.model('PageGroupRelation');
- return new Promise(function(resolve, reject) {
- self.findOne({path: path}, function(err, pageData) {
- if (err) {
- return reject(err);
- }
- 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)) {
- PageGroupRelation.isExistsGrantedGroupForPageAndUser(pageData, userData)
- .then(function(checkResult) {
- if (!checkResult) {
- return reject(new Error('Page is not granted for the user')); //PAGE_GRANT_ERROR, null);
- }
- else {
- // return resolve(pageData);
- self.populatePageData(pageData, revisionId || null).then(resolve).catch(reject);
- }
- })
- .catch(function(err) {
- return reject(err);
- });
- }
- else {
- self.populatePageData(pageData, revisionId || null).then(resolve).catch(reject);
- }
- });
- });
- };
- // find page by path
- pageSchema.statics.findPageByPath = function(path) {
- var Page = this;
- return new Promise(function(resolve, reject) {
- Page.findOne({path: path}, function(err, pageData) {
- if (err || pageData === null) {
- return reject(err);
- }
- return resolve(pageData);
- });
- });
- };
- pageSchema.statics.findListByPageIds = function(ids, options) {
- var Page = this;
- var User = crowi.model('User');
- var options = options || {}
- , limit = options.limit || 50
- , offset = options.skip || 0
- ;
- return new Promise(function(resolve, reject) {
- Page
- .find({ _id: { $in: ids }, grant: GRANT_PUBLIC })
- //.sort({createdAt: -1}) // TODO optionize
- .skip(offset)
- .limit(limit)
- .populate([
- {path: 'creator', model: 'User', select: User.USER_PUBLIC_FIELDS},
- {path: 'revision', model: 'Revision'},
- ])
- .exec(function(err, pages) {
- if (err) {
- return reject(err);
- }
- Page.populate(pages, {path: 'revision.author', model: 'User', select: User.USER_PUBLIC_FIELDS}, function(err, data) {
- if (err) {
- return reject(err);
- }
- return resolve(data);
- });
- });
- });
- };
- pageSchema.statics.findPageByRedirectTo = function(path) {
- var Page = this;
- return new Promise(function(resolve, reject) {
- Page.findOne({redirectTo: path}, function(err, pageData) {
- if (err || pageData === null) {
- return reject(err);
- }
- return resolve(pageData);
- });
- });
- };
- pageSchema.statics.findListByCreator = function(user, option, currentUser) {
- var Page = this;
- var User = crowi.model('User');
- var limit = option.limit || 50;
- var offset = option.offset || 0;
- var conditions = {
- creator: user._id,
- redirectTo: null,
- $or: [
- {status: null},
- {status: STATUS_PUBLISHED},
- ],
- };
- if (!user.equals(currentUser._id)) {
- conditions.grant = GRANT_PUBLIC;
- }
- return new Promise(function(resolve, reject) {
- Page
- .find(conditions)
- .sort({createdAt: -1})
- .skip(offset)
- .limit(limit)
- .populate('revision')
- .exec()
- .then(function(pages) {
- return Page.populate(pages, {path: 'revision.author', model: 'User', select: User.USER_PUBLIC_FIELDS}).then(resolve);
- });
- });
- };
- /**
- * Bulk get (for internal only)
- */
- pageSchema.statics.getStreamOfFindAll = function(options) {
- var Page = this
- , options = options || {}
- , publicOnly = options.publicOnly || true
- , criteria = {redirectTo: null, }
- ;
- if (publicOnly) {
- criteria.grant = GRANT_PUBLIC;
- }
- return this.find(criteria)
- .populate([
- {path: 'creator', model: 'User'},
- {path: 'revision', model: 'Revision'},
- ])
- .sort({updatedAt: -1})
- .cursor();
- };
- /**
- * find the page that is match with `path` and its descendants
- */
- pageSchema.statics.findListWithDescendants = function(path, userData, option) {
- var Page = this;
- // ignore other pages than descendants
- path = Page.addSlashOfEnd(path);
- // add option to escape the regex strings
- const combinedOption = Object.assign({isRegExpEscapedFromPath: true}, option);
- return Page.findListByStartWith(path, userData, combinedOption);
- };
- /**
- * find pages that start with `path`
- *
- * see the comment of `generateQueryToListByStartWith` function
- */
- pageSchema.statics.findListByStartWith = function(path, userData, option) {
- var Page = this;
- var User = crowi.model('User');
- if (!option) {
- option = {sort: 'updatedAt', desc: -1, offset: 0, limit: 50};
- }
- var opt = {
- sort: option.sort || 'updatedAt',
- desc: option.desc || -1,
- offset: option.offset || 0,
- limit: option.limit || 50
- };
- var sortOpt = {};
- sortOpt[opt.sort] = opt.desc;
- var isPopulateRevisionBody = option.isPopulateRevisionBody || false;
- return new Promise(function(resolve, reject) {
- var q = Page.generateQueryToListByStartWith(path, userData, option)
- .sort(sortOpt)
- .skip(opt.offset)
- .limit(opt.limit);
- // retrieve revision data
- if (isPopulateRevisionBody) {
- q = q.populate('revision');
- }
- else {
- q = q.populate('revision', '-body'); // exclude body
- }
- q.exec()
- .then(function(pages) {
- Page.populate(pages, {path: 'revision.author', model: 'User', select: User.USER_PUBLIC_FIELDS})
- .then(resolve)
- .catch(reject);
- });
- });
- };
- /**
- * generate the query to find the page that is match with `path` and its descendants
- */
- pageSchema.statics.generateQueryToListWithDescendants = function(path, userData, option) {
- var Page = this;
- // ignore other pages than descendants
- path = Page.addSlashOfEnd(path);
- // add option to escape the regex strings
- const combinedOption = Object.assign({isRegExpEscapedFromPath: true}, option);
- return Page.generateQueryToListByStartWith(path, userData, combinedOption);
- };
- /**
- * generate the query to find pages that start with `path`
- *
- * (GROWI) If 'isRegExpEscapedFromPath' is true, `path` should have `/` at the end
- * -> returns '{path}/*' and '{path}' self.
- * (Crowi) If 'isRegExpEscapedFromPath' is false and `path` has `/` at the end
- * -> returns '{path}*'
- * (Crowi) If 'isRegExpEscapedFromPath' is false and `path` doesn't have `/` at the end
- * -> returns '{path}*'
- *
- * *option*
- * - includeDeletedPage -- if true, search deleted pages (default: false)
- * - isRegExpEscapedFromPath -- if true, the regex strings included in `path` is escaped (default: false)
- */
- pageSchema.statics.generateQueryToListByStartWith = function(path, userData, option) {
- var Page = this;
- var pathCondition = [];
- var includeDeletedPage = option.includeDeletedPage || false;
- var isRegExpEscapedFromPath = option.isRegExpEscapedFromPath || false;
- /*
- * 1. add condition for finding the page completely match with `path` w/o last slash
- */
- let pathSlashOmitted = path;
- if (path.match(/\/$/)) {
- pathSlashOmitted = path.substr(0, path.length -1);
- pathCondition.push({path: pathSlashOmitted});
- }
- /*
- * 2. add decendants
- */
- var pattern = (isRegExpEscapedFromPath)
- ? escapeStringRegexp(path) // escape
- : pathSlashOmitted;
- var queryReg = new RegExp('^' + pattern);
- pathCondition.push({path: queryReg});
- var q = Page.find({
- 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},
- ], })
- .and({
- $or: pathCondition
- });
- if (!includeDeletedPage) {
- q.and({
- $or: [
- {status: null},
- {status: STATUS_PUBLISHED},
- ],
- });
- }
- return q;
- };
- pageSchema.statics.updatePageProperty = function(page, updateData) {
- var Page = this;
- return new Promise(function(resolve, reject) {
- // TODO foreach して save
- Page.update({_id: page._id}, {$set: updateData}, function(err, data) {
- if (err) {
- return reject(err);
- }
- return resolve(data);
- });
- });
- };
- pageSchema.statics.updateGrant = function(page, grant, userData, grantUserGroupId) {
- var Page = this;
- if (grant == GRANT_USER_GROUP && grantUserGroupId == null) {
- throw new Error('grant userGroupId is not specified');
- }
- return new Promise(function(resolve, reject) {
- page.grant = grant;
- if (grant == GRANT_PUBLIC || grant == GRANT_USER_GROUP) {
- page.grantedUsers = [];
- }
- else {
- page.grantedUsers = [];
- page.grantedUsers.push(userData._id);
- }
- page.save(function(err, data) {
- debug('Page.updateGrant, saved grantedUsers.', err, data);
- if (err) {
- return reject(err);
- }
- Page.updateGrantUserGroup(page, grant, grantUserGroupId, userData)
- .then(() => {
- return resolve(data);
- });
- });
- });
- };
- pageSchema.statics.updateGrantUserGroup = function(page, grant, grantUserGroupId, userData) {
- var UserGroupRelation = crowi.model('UserGroupRelation');
- var PageGroupRelation = crowi.model('PageGroupRelation');
- // グループの場合
- if (grant == GRANT_USER_GROUP) {
- debug('grant is usergroup', grantUserGroupId);
- return UserGroupRelation.findByGroupIdAndUser(grantUserGroupId, userData)
- .then((relation) => {
- if (relation == null) {
- return reject(new Error('no relations were exist for group and user.'));
- }
- return PageGroupRelation.findOrCreateRelationForPageAndGroup(page, relation.relatedGroup);
- })
- .catch((err) => {
- return reject(new Error('No UserGroup is exists. userGroupId : ', grantUserGroupId));
- });
- }
- else {
- return PageGroupRelation.removeAllByPage(page);
- }
- };
- // Instance method でいいのでは
- 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) {
- var isCreate = false;
- if (pageData.revision === undefined) {
- debug('pushRevision on Create');
- isCreate = true;
- }
- return new Promise(function(resolve, reject) {
- newRevision.save(function(err, newRevision) {
- if (err) {
- debug('Error on saving revision', err);
- return reject(err);
- }
- debug('Successfully saved new revision', newRevision);
- pageData.revision = newRevision;
- pageData.lastUpdateUser = user;
- 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);
- if (!isCreate) {
- debug('pushRevision on Update');
- }
- });
- });
- });
- };
- pageSchema.statics.create = function(path, body, user, options) {
- var Page = this
- , Revision = crowi.model('Revision')
- , format = options.format || 'markdown'
- , grant = options.grant || GRANT_PUBLIC
- , redirectTo = options.redirectTo || null
- , grantUserGroupId = options.grantUserGroupId || null;
- // force public
- if (isPortalPath(path)) {
- grant = GRANT_PUBLIC;
- }
- 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.lastUpdateUser = user;
- newPage.createdAt = Date.now();
- newPage.updatedAt = Date.now();
- newPage.redirectTo = redirectTo;
- newPage.grant = grant;
- newPage.status = STATUS_PUBLISHED;
- newPage.grantedUsers = [];
- newPage.grantedUsers.push(user);
- newPage.save(function(err, newPage) {
- if (err) {
- return reject(err);
- }
- if (newPage.grant == Page.GRANT_USER_GROUP && grantUserGroupId != null) {
- Page.updateGrantUserGroup(newPage, grant, grantUserGroupId, user)
- .catch((err) => {
- return reject(err);
- });
- }
- var newRevision = Revision.prepareRevision(newPage, body, user, {format: format});
- Page.pushRevision(newPage, newRevision, user).then(function(data) {
- resolve(data);
- pageEvent.emit('create', data, user);
- }).catch(function(err) {
- debug('Push Revision Error on create page', err);
- return reject(err);
- });
- });
- });
- });
- };
- pageSchema.statics.updatePage = function(pageData, body, user, options) {
- var Page = this
- , Revision = crowi.model('Revision')
- , grant = options.grant || null
- , grantUserGroupId = options.grantUserGroupId || null
- ;
- // update existing page
- var newRevision = Revision.prepareRevision(pageData, body, user);
- return new Promise(function(resolve, reject) {
- Page.pushRevision(pageData, newRevision, user)
- .then(function(revision) {
- if (grant != pageData.grant) {
- return Page.updateGrant(pageData, grant, user, grantUserGroupId).then(function(data) {
- debug('Page grant update:', data);
- resolve(data);
- pageEvent.emit('update', data, user);
- });
- }
- else {
- resolve(pageData);
- pageEvent.emit('update', pageData, user);
- }
- }).catch(function(err) {
- debug('Error on update', err);
- debug('Error on update', err.stack);
- });
- });
- };
- pageSchema.statics.deletePage = function(pageData, user, options) {
- var Page = this
- , newPath = Page.getDeletedPageName(pageData.path)
- ;
- if (Page.isDeletableName(pageData.path)) {
- return new Promise(function(resolve, reject) {
- Page.updatePageProperty(pageData, {status: STATUS_DELETED, lastUpdateUser: user})
- .then(function(data) {
- pageData.status = STATUS_DELETED;
- // ページ名が /trash/ 以下に存在する場合、おかしなことになる
- // が、 /trash 以下にページが有るのは、個別に作っていたケースのみ。
- // 一応しばらく前から uncreatable pages になっているのでこれでいいことにする
- debug('Deleted the page, and rename it', pageData.path, newPath);
- return Page.rename(pageData, newPath, user, {createRedirectPage: true});
- }).then(function(pageData) {
- resolve(pageData);
- }).catch(reject);
- });
- }
- else {
- return Promise.reject('Page is not deletable.');
- }
- };
- pageSchema.statics.deletePageRecursively = function(pageData, user, options) {
- var Page = this
- , path = pageData.path
- , options = options || {}
- ;
- return new Promise(function(resolve, reject) {
- Page
- .generateQueryToListWithDescendants(path, user, options)
- .then(function(pages) {
- Promise.all(pages.map(function(page) {
- return Page.deletePage(page, user, options);
- }))
- .then(function(data) {
- return resolve(pageData);
- });
- });
- });
- };
- pageSchema.statics.revertDeletedPage = function(pageData, user, options) {
- var Page = this
- , newPath = Page.getRevertDeletedPageName(pageData.path)
- ;
- // 削除時、元ページの path には必ず redirectTo 付きで、ページが作成される。
- // そのため、そいつは削除してOK
- // が、redirectTo ではないページが存在している場合それは何かがおかしい。(データ補正が必要)
- return new Promise(function(resolve, reject) {
- Page.findPageByPath(newPath)
- .then(function(originPageData) {
- if (originPageData.redirectTo !== pageData.path) {
- throw new Error('The new page of to revert is exists and the redirect path of the page is not the deleted page.');
- }
- return Page.completelyDeletePage(originPageData);
- }).then(function(done) {
- return Page.updatePageProperty(pageData, {status: STATUS_PUBLISHED, lastUpdateUser: user});
- }).then(function(done) {
- pageData.status = STATUS_PUBLISHED;
- debug('Revert deleted the page, and rename again it', pageData, newPath);
- return Page.rename(pageData, newPath, user, {});
- }).then(function(done) {
- pageData.path = newPath;
- resolve(pageData);
- }).catch(reject);
- });
- };
- pageSchema.statics.revertDeletedPageRecursively = function(pageData, user, options) {
- var Page = this
- , path = pageData.path
- , options = options || { includeDeletedPage: true}
- ;
- return new Promise(function(resolve, reject) {
- Page
- .generateQueryToListWithDescendants(path, user, options)
- .then(function(pages) {
- Promise.all(pages.map(function(page) {
- return Page.revertDeletedPage(page, user, options);
- }))
- .then(function(data) {
- return resolve(data[0]);
- });
- });
- });
- };
- /**
- * This is danger.
- */
- pageSchema.statics.completelyDeletePage = function(pageData, user, options) {
- // Delete Bookmarks, Attachments, Revisions, Pages and emit delete
- var Bookmark = crowi.model('Bookmark')
- , Attachment = crowi.model('Attachment')
- , Comment = crowi.model('Comment')
- , Revision = crowi.model('Revision')
- , Page = this
- , pageId = pageData._id
- ;
- debug('Completely delete', pageData.path);
- return new Promise(function(resolve, reject) {
- Bookmark.removeBookmarksByPageId(pageId)
- .then(function(done) {
- }).then(function(done) {
- return Attachment.removeAttachmentsByPageId(pageId);
- }).then(function(done) {
- return Comment.removeCommentsByPageId(pageId);
- }).then(function(done) {
- return Revision.removeRevisionsByPath(pageData.path);
- }).then(function(done) {
- return Page.removePageById(pageId);
- }).then(function(done) {
- return Page.removeRedirectOriginPageByPath(pageData.path);
- }).then(function(done) {
- pageEvent.emit('delete', pageData, user); // update as renamed page
- resolve(pageData);
- }).catch(reject);
- });
- };
- pageSchema.statics.completelyDeletePageRecursively = function(pageData, user, options) {
- // Delete Bookmarks, Attachments, Revisions, Pages and emit delete
- var Page = this
- , path = pageData.path
- , options = options || { includeDeletedPage: true }
- ;
- return new Promise(function(resolve, reject) {
- Page
- .generateQueryToListWithDescendants(path, user, options)
- .then(function(pages) {
- Promise.all(pages.map(function(page) {
- return Page.completelyDeletePage(page, user, options);
- }))
- .then(function(data) {
- return resolve(data[0]);
- });
- });
- });
- };
- pageSchema.statics.removePageById = function(pageId) {
- var Page = this;
- return new Promise(function(resolve, reject) {
- Page.remove({_id: pageId}, function(err, done) {
- debug('Remove phisiaclly, the page', pageId, err, done);
- if (err) {
- return reject(err);
- }
- resolve(done);
- });
- });
- };
- pageSchema.statics.removePageByPath = function(pagePath) {
- var Page = this;
- return Page.findPageByPath(pagePath)
- .then(function(pageData) {
- return Page.removePageById(pageData.id);
- });
- };
- /**
- * remove the page that is redirecting to specified `pagePath` recursively
- * ex: when
- * '/page1' redirects to '/page2' and
- * '/page2' redirects to '/page3'
- * and given '/page3',
- * '/page1' and '/page2' will be removed
- *
- * @param {string} pagePath
- */
- pageSchema.statics.removeRedirectOriginPageByPath = function(pagePath) {
- var Page = this;
- return Page.findPageByRedirectTo(pagePath)
- .then((redirectOriginPageData) => {
- // remove
- return Page.removePageById(redirectOriginPageData.id)
- // remove recursive
- .then(() => {
- return Page.removeRedirectOriginPageByPath(redirectOriginPageData.path);
- });
- })
- .catch((err) => {
- // do nothing if origin page doesn't exist
- return Promise.resolve();
- });
- };
- pageSchema.statics.rename = function(pageData, newPagePath, user, options) {
- var Page = this
- , Revision = crowi.model('Revision')
- , path = pageData.path
- , createRedirectPage = options.createRedirectPage || 0
- , moveUnderTrees = options.moveUnderTrees || 0;
- return new Promise(function(resolve, reject) {
- // pageData の path を変更
- Page.updatePageProperty(pageData, {updatedAt: Date.now(), path: newPagePath, lastUpdateUser: user})
- .then(function(data) {
- // reivisions の path を変更
- return Revision.updateRevisionListByPath(path, {path: newPagePath}, {});
- }).then(function(data) {
- pageData.path = newPagePath;
- if (createRedirectPage) {
- var body = 'redirect ' + newPagePath;
- Page.create(path, body, user, {redirectTo: newPagePath}).then(resolve).catch(reject);
- }
- else {
- resolve(data);
- }
- pageEvent.emit('update', pageData, user); // update as renamed page
- });
- });
- };
- pageSchema.statics.renameRecursively = function(pageData, newPagePathPrefix, user, options) {
- var Page = this
- , path = pageData.path
- , pathRegExp = new RegExp('^' + escapeStringRegexp(path), 'i');
- return new Promise(function(resolve, reject) {
- Page
- .generateQueryToListWithDescendants(path, user, options)
- .then(function(pages) {
- Promise.all(pages.map(function(page) {
- newPagePath = page.path.replace(pathRegExp, newPagePathPrefix);
- return Page.rename(page, newPagePath, user, options);
- }))
- .then(function() {
- pageData.path = newPagePathPrefix;
- return resolve();
- });
- });
- });
- };
- pageSchema.statics.getHistories = function() {
- // TODO
- return;
- };
- /**
- * return path that added slash to the end for specified path
- */
- pageSchema.statics.addSlashOfEnd = function(path) {
- let returnPath = path;
- if (!path.match(/\/$/)) {
- returnPath += '/';
- }
- return returnPath;
- };
- pageSchema.statics.GRANT_PUBLIC = GRANT_PUBLIC;
- pageSchema.statics.GRANT_RESTRICTED = GRANT_RESTRICTED;
- pageSchema.statics.GRANT_SPECIFIED = GRANT_SPECIFIED;
- pageSchema.statics.GRANT_OWNER = GRANT_OWNER;
- pageSchema.statics.PAGE_GRANT_ERROR = PAGE_GRANT_ERROR;
- return mongoose.model('Page', pageSchema);
- };
|