|
|
@@ -399,6 +399,10 @@ module.exports = function(crowi, app) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ const limit = 50;
|
|
|
+ const offset = parseInt(req.query.offset) || 0;
|
|
|
+ await addRenderVarsForDescendants(renderVars, path, req.user, offset, limit);
|
|
|
+
|
|
|
return res.render(view, renderVars);
|
|
|
};
|
|
|
|
|
|
@@ -523,7 +527,7 @@ module.exports = function(crowi, app) {
|
|
|
// check page existence
|
|
|
const isExist = await Page.count({path: pagePath}) > 0;
|
|
|
if (isExist) {
|
|
|
- return res.json(ApiResponse.error('Page exists'));
|
|
|
+ return res.json(ApiResponse.error('Page exists', 'already_exists'));
|
|
|
}
|
|
|
|
|
|
const options = {grant, grantUserGroupId, socketClientId};
|
|
|
@@ -580,13 +584,13 @@ module.exports = function(crowi, app) {
|
|
|
// check page existence
|
|
|
const isExist = await Page.count({_id: pageId}) > 0;
|
|
|
if (!isExist) {
|
|
|
- return res.json(ApiResponse.error(`Page('${pageId}' does not exist`));
|
|
|
+ return res.json(ApiResponse.error(`Page('${pageId}' is not found or forbidden`, 'notfound_or_forbidden'));
|
|
|
}
|
|
|
|
|
|
// check revision
|
|
|
let page = await Page.findByIdAndViewer(pageId, req.user);
|
|
|
if (page != null && revisionId != null && !page.isUpdatable(revisionId)) {
|
|
|
- return res.json(ApiResponse.error('Posted param "revisionId" is outdated.'));
|
|
|
+ return res.json(ApiResponse.error('Posted param "revisionId" is outdated.', 'outdated'));
|
|
|
}
|
|
|
|
|
|
const options = {isSyncRevisionToHackmd, socketClientId};
|
|
|
@@ -597,11 +601,10 @@ module.exports = function(crowi, app) {
|
|
|
options.grantUserGroupId = grantUserGroupId;
|
|
|
}
|
|
|
|
|
|
- // store previous revision
|
|
|
- const previousRevision = page.revision;
|
|
|
-
|
|
|
try {
|
|
|
- page = await Page.updatePage(page, pageBody, req.user, options);
|
|
|
+ const Revision = crowi.model('Revision');
|
|
|
+ const previousRevision = await Revision.findById(revisionId);
|
|
|
+ page = await Page.updatePage(page, pageBody, previousRevision.body, req.user, options);
|
|
|
}
|
|
|
catch (err) {
|
|
|
logger.error('error on _api/pages.update', err);
|
|
|
@@ -622,6 +625,8 @@ module.exports = function(crowi, app) {
|
|
|
|
|
|
// user notification
|
|
|
if (isSlackEnabled && slackChannels != null) {
|
|
|
+ const Revision = crowi.model('Revision');
|
|
|
+ const previousRevision = await Revision.findById(page.revision);
|
|
|
await notifyToSlackByUser(page, req.user, slackChannels, 'update', previousRevision);
|
|
|
}
|
|
|
};
|
|
|
@@ -651,6 +656,11 @@ module.exports = function(crowi, app) {
|
|
|
else if (pagePath) {
|
|
|
page = await Page.findByPathAndViewer(pagePath, req.user);
|
|
|
}
|
|
|
+
|
|
|
+ if (page == null) {
|
|
|
+ throw new Error(`Page '${pageId || pagePath}' is not found or forbidden`, 'notfound_or_forbidden');
|
|
|
+ }
|
|
|
+
|
|
|
page.initLatestRevisionField();
|
|
|
|
|
|
// populate
|
|
|
@@ -719,6 +729,9 @@ module.exports = function(crowi, app) {
|
|
|
let page;
|
|
|
try {
|
|
|
page = await Page.findByIdAndViewer(pageId, req.user);
|
|
|
+ if (page == null) {
|
|
|
+ throw new Error(`Page '${pageId}' is not found or forbidden`);
|
|
|
+ }
|
|
|
page = await page.like(req.user);
|
|
|
}
|
|
|
catch (err) {
|
|
|
@@ -758,6 +771,9 @@ module.exports = function(crowi, app) {
|
|
|
let page;
|
|
|
try {
|
|
|
page = await Page.findByIdAndViewer(pageId, req.user);
|
|
|
+ if (page == null) {
|
|
|
+ throw new Error(`Page '${pageId}' is not found or forbidden`);
|
|
|
+ }
|
|
|
page = await page.unlike(req.user);
|
|
|
}
|
|
|
catch (err) {
|
|
|
@@ -822,7 +838,7 @@ module.exports = function(crowi, app) {
|
|
|
let page = await Page.findByIdAndViewer(pageId, req.user);
|
|
|
|
|
|
if (page == null) {
|
|
|
- return res.json(ApiResponse.error('The page does not exist.'));
|
|
|
+ return res.json(ApiResponse.error(`Page '${pageId}' is not found or forbidden`, 'notfound_or_forbidden'));
|
|
|
}
|
|
|
|
|
|
debug('Delete page', page._id, page.path);
|
|
|
@@ -838,7 +854,7 @@ module.exports = function(crowi, app) {
|
|
|
}
|
|
|
else {
|
|
|
if (!page.isUpdatable(previousRevision)) {
|
|
|
- throw new Error('Someone could update this page, so couldn\'t delete.');
|
|
|
+ return res.json(ApiResponse.error('Someone could update this page, so couldn\'t delete.', 'outdated'));
|
|
|
}
|
|
|
|
|
|
if (isRecursively) {
|
|
|
@@ -851,7 +867,7 @@ module.exports = function(crowi, app) {
|
|
|
}
|
|
|
catch (err) {
|
|
|
logger.error('Error occured while get setting', err);
|
|
|
- return res.json(ApiResponse.error('Failed to delete page.'));
|
|
|
+ return res.json(ApiResponse.error('Failed to delete page.', 'unknown'));
|
|
|
}
|
|
|
|
|
|
debug('Page deleted', page.path);
|
|
|
@@ -882,7 +898,7 @@ module.exports = function(crowi, app) {
|
|
|
try {
|
|
|
page = await Page.findByIdAndViewer(pageId, req.user);
|
|
|
if (page == null) {
|
|
|
- throw new Error('The page is not found or the user does not have permission');
|
|
|
+ throw new Error(`Page '${pageId}' is not found or forbidden`, 'notfound_or_forbidden');
|
|
|
}
|
|
|
|
|
|
if (isRecursively) {
|
|
|
@@ -923,27 +939,32 @@ module.exports = function(crowi, app) {
|
|
|
moveUnderTrees: req.body.move_trees || 0,
|
|
|
socketClientId: +req.body.socketClientId || undefined,
|
|
|
};
|
|
|
- const isRecursiveMove = req.body.move_recursively || 0;
|
|
|
+ const isRecursively = req.body.recursively || 0;
|
|
|
|
|
|
if (!Page.isCreatableName(newPagePath)) {
|
|
|
- return res.json(ApiResponse.error(`このページ名は作成できません (${newPagePath})`));
|
|
|
+ return res.json(ApiResponse.error(`Could not use the path '${newPagePath})'`, 'invalid_path'));
|
|
|
}
|
|
|
|
|
|
const isExist = await Page.count({ path: newPagePath }) > 0;
|
|
|
if (isExist) {
|
|
|
// if page found, cannot cannot rename to that path
|
|
|
- return res.json(ApiResponse.error('The page already exists'));
|
|
|
+ return res.json(ApiResponse.error(`'new_path=${newPagePath}' already exists`, 'already_exists'));
|
|
|
}
|
|
|
|
|
|
let page;
|
|
|
|
|
|
try {
|
|
|
- page = await Page.findById(pageId);
|
|
|
+ page = await Page.findByIdAndViewer(pageId, req.user);
|
|
|
+
|
|
|
+ if (page == null) {
|
|
|
+ return res.json(ApiResponse.error(`Page '${pageId}' is not found or forbidden`, 'notfound_or_forbidden'));
|
|
|
+ }
|
|
|
+
|
|
|
if (!page.isUpdatable(previousRevision)) {
|
|
|
- throw new Error('Someone could update this page, so couldn\'t delete.');
|
|
|
+ return res.json(ApiResponse.error('Someone could update this page, so couldn\'t delete.', 'outdated'));
|
|
|
}
|
|
|
|
|
|
- if (isRecursiveMove) {
|
|
|
+ if (isRecursively) {
|
|
|
page = await Page.renameRecursively(page, newPagePath, req.user, options);
|
|
|
}
|
|
|
else {
|
|
|
@@ -952,7 +973,7 @@ module.exports = function(crowi, app) {
|
|
|
}
|
|
|
catch (err) {
|
|
|
logger.error(err);
|
|
|
- return res.json(ApiResponse.error('Failed to update page.'));
|
|
|
+ return res.json(ApiResponse.error('Failed to update page.', 'unknown'));
|
|
|
}
|
|
|
|
|
|
const result = {};
|
|
|
@@ -974,18 +995,23 @@ module.exports = function(crowi, app) {
|
|
|
* @apiParam {String} page_id Page Id.
|
|
|
* @apiParam {String} new_path
|
|
|
*/
|
|
|
- api.duplicate = function(req, res) {
|
|
|
+ api.duplicate = async function(req, res) {
|
|
|
const pageId = req.body.page_id;
|
|
|
const newPagePath = Page.normalizePath(req.body.new_path);
|
|
|
|
|
|
- Page.findById(pageId)
|
|
|
- .then(function(pageData) {
|
|
|
- req.body.path = newPagePath;
|
|
|
- req.body.body = pageData.revision.body;
|
|
|
- req.body.grant = pageData.grant;
|
|
|
+ const page = await Page.findByIdAndViewer(pageId, req.user);
|
|
|
|
|
|
- return api.create(req, res);
|
|
|
- });
|
|
|
+ if (page == null) {
|
|
|
+ return res.json(ApiResponse.error(`Page '${pageId}' is not found or forbidden`, 'notfound_or_forbidden'));
|
|
|
+ }
|
|
|
+
|
|
|
+ await page.populateDataToShowRevision();
|
|
|
+
|
|
|
+ req.body.path = newPagePath;
|
|
|
+ req.body.body = page.revision.body;
|
|
|
+ req.body.grant = page.grant;
|
|
|
+
|
|
|
+ return api.create(req, res);
|
|
|
};
|
|
|
|
|
|
/**
|