| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960 |
- module.exports = function(crowi) {
- var debug = require('debug')('crowi:models:page')
- , mongoose = require('mongoose')
- , ObjectId = mongoose.Schema.Types.ObjectId
- , GRANT_PUBLIC = 1
- , GRANT_RESTRICTED = 2
- , GRANT_SPECIFIED = 3
- , GRANT_OWNER = 4
- , PAGE_GRANT_ERROR = 1
- , STATUS_WIP = 'wip'
- , STATUS_PUBLISHED = 'published'
- , STATUS_DELETED = 'deleted'
- , STATUS_DEPRECATED = 'deprecated'
- , pageEvent = crowi.event('page')
- , pageSchema;
- 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) {
- 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 (page, num)
- {
- var self = this;
- return new Promise(function(resolve, reject) {
- self.update({_id: page}, {commentCount: num}, {}, function(err, data) {
- if (err) {
- debug('Update commentCount Error', err);
- return reject(err);
- }
- return resolve(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_OWNER] = 'Just me'; // 自分のみ
- return grantLabels;
- };
- pageSchema.statics.normalizePath = function(path) {
- if (!path.match(/^\//)) {
- path = '/' + path;
- }
- 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\/.*/,
- /^\/user\/[^\/]+\/(bookmarks|comments|activities|pages|recent-create|recent-edit)/, // reserved
- /^\/?https?:\/\/.+$/, // 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;
- 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 resolve(pageData);
- }).catch(function(err) {
- return reject(err);
- });
- });
- };
- // find page and check if granted user
- pageSchema.statics.findPage = function(path, userData, revisionId, ignoreNotFound) {
- var self = this;
- 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)) {
- return reject(new Error('Page is not granted for the user')); //PAGE_GRANT_ERROR, null);
- }
- 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.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 };
- 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();
- };
- /**
- * findListByStartWith
- *
- * If `path` has `/` at the end, returns '{path}/*' and '{path}' self.
- * If `path` doesn't have `/` at the end, returns '{path}*'
- * e.g.
- */
- pageSchema.statics.findListByStartWith = function(path, userData, option) {
- var Page = this;
- var User = crowi.model('User');
- var pathCondition = [];
- var includeDeletedPage = option.includeDeletedPage || false
- 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 queryReg = new RegExp('^' + path);
- var sliceOption = option.revisionSlice || {$slice: 1};
- pathCondition.push({path: queryReg});
- if (path.match(/\/$/)) {
- debug('Page list by ending with /, so find also upper level page');
- pathCondition.push({path: path.substr(0, path.length -1)});
- }
- return new Promise(function(resolve, reject) {
- // FIXME: might be heavy
- 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},
- ],})
- .populate('revision')
- .and({
- $or: pathCondition
- })
- .sort(sortOpt)
- .skip(opt.offset)
- .limit(opt.limit);
- if (!includeDeletedPage) {
- q.and({
- $or: [
- {status: null},
- {status: STATUS_PUBLISHED},
- ],
- });
- }
- q.exec()
- .then(function(pages) {
- Page.populate(pages, {path: 'revision.author', model: 'User', select: User.USER_PUBLIC_FIELDS})
- .then(resolve)
- .catch(reject);
- })
- });
- };
- 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) {
- var Page = this;
- return new Promise(function(resolve, reject) {
- page.grant = grant;
- if (grant == GRANT_PUBLIC) {
- 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);
- }
- return resolve(data);
- });
- });
- };
- // 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;
- // 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);
- }
- 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
- ;
- // 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).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.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);
- });
- };
- /**
- * 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) {
- pageEvent.emit('delete', pageData, user); // update as renamed page
- resolve();
- }).catch(reject);
- });
- };
- 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.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.getHistories = function() {
- // TODO
- return;
- };
- 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);
- };
|