|
@@ -22,19 +22,19 @@ module.exports = function(crowi, app) {
|
|
|
|
|
|
|
|
// register page events
|
|
// register page events
|
|
|
|
|
|
|
|
- var pageEvent = crowi.event('page');
|
|
|
|
|
|
|
+ const pageEvent = crowi.event('page');
|
|
|
pageEvent.on('update', function(page, user) {
|
|
pageEvent.on('update', function(page, user) {
|
|
|
crowi.getIo().sockets.emit('page edited', {page, user});
|
|
crowi.getIo().sockets.emit('page edited', {page, user});
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
function getPathFromRequest(req) {
|
|
function getPathFromRequest(req) {
|
|
|
- var path = '/' + (req.params[0] || '');
|
|
|
|
|
|
|
+ const path = '/' + (req.params[0] || '');
|
|
|
return path.replace(/\.md$/, '');
|
|
return path.replace(/\.md$/, '');
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
function isUserPage(path) {
|
|
function isUserPage(path) {
|
|
|
- if (path.match(/^\/user\/[^\/]+\/?$/)) {
|
|
|
|
|
|
|
+ if (path.match(/^\/user\/[^/]+\/?$/)) {
|
|
|
return true;
|
|
return true;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -43,9 +43,9 @@ module.exports = function(crowi, app) {
|
|
|
|
|
|
|
|
// TODO: total とかでちゃんと計算する
|
|
// TODO: total とかでちゃんと計算する
|
|
|
function generatePager(options) {
|
|
function generatePager(options) {
|
|
|
- var next = null,
|
|
|
|
|
- prev = null,
|
|
|
|
|
- offset = parseInt(options.offset, 10),
|
|
|
|
|
|
|
+ let next = null,
|
|
|
|
|
+ prev = null;
|
|
|
|
|
+ const offset = parseInt(options.offset, 10),
|
|
|
limit = parseInt(options.limit, 10),
|
|
limit = parseInt(options.limit, 10),
|
|
|
length = options.length || 0;
|
|
length = options.length || 0;
|
|
|
|
|
|
|
@@ -71,6 +71,26 @@ module.exports = function(crowi, app) {
|
|
|
};
|
|
};
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ // user notification
|
|
|
|
|
+ // TODO create '/service/user-notification' module
|
|
|
|
|
+ async function notifyToSlackByUser(page, user, slackChannels, updateOrCreate, previousRevision) {
|
|
|
|
|
+ await page.updateSlackChannel(slackChannels)
|
|
|
|
|
+ .catch(err => {
|
|
|
|
|
+ logger.error('Error occured in updating slack channels: ', err);
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ if (crowi.slack) {
|
|
|
|
|
+ const promises = slackChannels.split(',').map(function(chan) {
|
|
|
|
|
+ return crowi.slack.postPage(page, user, chan, updateOrCreate, previousRevision);
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ Promise.all(promises)
|
|
|
|
|
+ .catch(err => {
|
|
|
|
|
+ logger.error('Error occured in sending slack notification: ', err);
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* switch action by behaviorType
|
|
* switch action by behaviorType
|
|
|
*/
|
|
*/
|
|
@@ -146,26 +166,26 @@ module.exports = function(crowi, app) {
|
|
|
|
|
|
|
|
|
|
|
|
|
actions.pageListShow = function(req, res) {
|
|
actions.pageListShow = function(req, res) {
|
|
|
- var path = getPathFromRequest(req);
|
|
|
|
|
- var limit = 50;
|
|
|
|
|
- var offset = parseInt(req.query.offset) || 0;
|
|
|
|
|
- var SEENER_THRESHOLD = 10;
|
|
|
|
|
|
|
+ let path = getPathFromRequest(req);
|
|
|
|
|
+ const limit = 50;
|
|
|
|
|
+ const offset = parseInt(req.query.offset) || 0;
|
|
|
|
|
+ const SEENER_THRESHOLD = 10;
|
|
|
// add slash if root
|
|
// add slash if root
|
|
|
path = path + (path == '/' ? '' : '/');
|
|
path = path + (path == '/' ? '' : '/');
|
|
|
|
|
|
|
|
debug('Page list show', path);
|
|
debug('Page list show', path);
|
|
|
// index page
|
|
// index page
|
|
|
- var pagerOptions = {
|
|
|
|
|
|
|
+ const pagerOptions = {
|
|
|
offset: offset,
|
|
offset: offset,
|
|
|
limit: limit
|
|
limit: limit
|
|
|
};
|
|
};
|
|
|
- var queryOptions = {
|
|
|
|
|
|
|
+ const queryOptions = {
|
|
|
offset: offset,
|
|
offset: offset,
|
|
|
limit: limit + 1,
|
|
limit: limit + 1,
|
|
|
isPopulateRevisionBody: Config.isEnabledTimeline(config),
|
|
isPopulateRevisionBody: Config.isEnabledTimeline(config),
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
- var renderVars = {
|
|
|
|
|
|
|
+ const renderVars = {
|
|
|
page: null,
|
|
page: null,
|
|
|
path: path,
|
|
path: path,
|
|
|
isPortal: false,
|
|
isPortal: false,
|
|
@@ -180,8 +200,9 @@ module.exports = function(crowi, app) {
|
|
|
|
|
|
|
|
if (portalPage) {
|
|
if (portalPage) {
|
|
|
renderVars.revision = portalPage.revision;
|
|
renderVars.revision = portalPage.revision;
|
|
|
- renderVars.revisionHackmdSynced = portalPage.revisionHackmdSynced;
|
|
|
|
|
renderVars.pageIdOnHackmd = portalPage.pageIdOnHackmd;
|
|
renderVars.pageIdOnHackmd = portalPage.pageIdOnHackmd;
|
|
|
|
|
+ renderVars.revisionHackmdSynced = portalPage.revisionHackmdSynced;
|
|
|
|
|
+ renderVars.hasDraftOnHackmd = portalPage.hasDraftOnHackmd;
|
|
|
return Revision.findRevisionList(portalPage.path, {});
|
|
return Revision.findRevisionList(portalPage.path, {});
|
|
|
}
|
|
}
|
|
|
else {
|
|
else {
|
|
@@ -259,6 +280,7 @@ module.exports = function(crowi, app) {
|
|
|
pageRelatedGroup: null,
|
|
pageRelatedGroup: null,
|
|
|
template: null,
|
|
template: null,
|
|
|
revisionHackmdSynced: null,
|
|
revisionHackmdSynced: null,
|
|
|
|
|
+ hasDraftOnHackmd: false,
|
|
|
slack: '',
|
|
slack: '',
|
|
|
};
|
|
};
|
|
|
|
|
|
|
@@ -282,8 +304,9 @@ module.exports = function(crowi, app) {
|
|
|
renderVars.path = page.path;
|
|
renderVars.path = page.path;
|
|
|
renderVars.revision = page.revision;
|
|
renderVars.revision = page.revision;
|
|
|
renderVars.author = page.revision.author;
|
|
renderVars.author = page.revision.author;
|
|
|
- renderVars.revisionHackmdSynced = page.revisionHackmdSynced;
|
|
|
|
|
renderVars.pageIdOnHackmd = page.pageIdOnHackmd;
|
|
renderVars.pageIdOnHackmd = page.pageIdOnHackmd;
|
|
|
|
|
+ renderVars.revisionHackmdSynced = page.revisionHackmdSynced;
|
|
|
|
|
+ renderVars.hasDraftOnHackmd = page.hasDraftOnHackmd;
|
|
|
|
|
|
|
|
return Revision.findRevisionList(page.path, {})
|
|
return Revision.findRevisionList(page.path, {})
|
|
|
.then(function(tree) {
|
|
.then(function(tree) {
|
|
@@ -415,22 +438,22 @@ module.exports = function(crowi, app) {
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
actions.deletedPageListShow = function(req, res) {
|
|
actions.deletedPageListShow = function(req, res) {
|
|
|
- var path = '/trash' + getPathFromRequest(req);
|
|
|
|
|
- var limit = 50;
|
|
|
|
|
- var offset = parseInt(req.query.offset) || 0;
|
|
|
|
|
|
|
+ const path = '/trash' + getPathFromRequest(req);
|
|
|
|
|
+ const limit = 50;
|
|
|
|
|
+ const offset = parseInt(req.query.offset) || 0;
|
|
|
|
|
|
|
|
// index page
|
|
// index page
|
|
|
- var pagerOptions = {
|
|
|
|
|
|
|
+ const pagerOptions = {
|
|
|
offset: offset,
|
|
offset: offset,
|
|
|
limit: limit
|
|
limit: limit
|
|
|
};
|
|
};
|
|
|
- var queryOptions = {
|
|
|
|
|
|
|
+ const queryOptions = {
|
|
|
offset: offset,
|
|
offset: offset,
|
|
|
limit: limit + 1,
|
|
limit: limit + 1,
|
|
|
includeDeletedPage: true,
|
|
includeDeletedPage: true,
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
- var renderVars = {
|
|
|
|
|
|
|
+ const renderVars = {
|
|
|
page: null,
|
|
page: null,
|
|
|
path: path,
|
|
path: path,
|
|
|
pages: [],
|
|
pages: [],
|
|
@@ -455,8 +478,8 @@ module.exports = function(crowi, app) {
|
|
|
|
|
|
|
|
actions.search = function(req, res) {
|
|
actions.search = function(req, res) {
|
|
|
// spec: ?q=query&sort=sort_order&author=author_filter
|
|
// spec: ?q=query&sort=sort_order&author=author_filter
|
|
|
- var query = req.query.q;
|
|
|
|
|
- var search = require('../util/search')(crowi);
|
|
|
|
|
|
|
+ const query = req.query.q;
|
|
|
|
|
+ const search = require('../util/search')(crowi);
|
|
|
|
|
|
|
|
search.searchPageByKeyword(query)
|
|
search.searchPageByKeyword(query)
|
|
|
.then(function(pages) {
|
|
.then(function(pages) {
|
|
@@ -466,7 +489,7 @@ module.exports = function(crowi, app) {
|
|
|
return Promise.resolve([]);
|
|
return Promise.resolve([]);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- var ids = pages.hits.hits.map(function(page) {
|
|
|
|
|
|
|
+ const ids = pages.hits.hits.map(function(page) {
|
|
|
return page._id;
|
|
return page._id;
|
|
|
});
|
|
});
|
|
|
|
|
|
|
@@ -583,10 +606,10 @@ module.exports = function(crowi, app) {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
actions.pageShow = function(req, res) {
|
|
actions.pageShow = function(req, res) {
|
|
|
- var path = path || getPathFromRequest(req);
|
|
|
|
|
|
|
+ const path = getPathFromRequest(req);
|
|
|
|
|
|
|
|
// FIXME: せっかく getPathFromRequest になってるのにここが生 params[0] だとダサイ
|
|
// FIXME: せっかく getPathFromRequest になってるのにここが生 params[0] だとダサイ
|
|
|
- var isMarkdown = req.params[0].match(/.+\.md$/) || false;
|
|
|
|
|
|
|
+ const isMarkdown = req.params[0].match(/.+\.md$/) || false;
|
|
|
|
|
|
|
|
res.locals.path = path;
|
|
res.locals.path = path;
|
|
|
|
|
|
|
@@ -658,109 +681,14 @@ module.exports = function(crowi, app) {
|
|
|
});
|
|
});
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
- actions.pageEdit = function(req, res) {
|
|
|
|
|
-
|
|
|
|
|
- if (!req.form.isValid) {
|
|
|
|
|
- req.flash('dangerMessage', 'Request is invalid.');
|
|
|
|
|
- return res.redirect(req.headers.referer);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- var pageForm = req.form.pageForm;
|
|
|
|
|
- var path = pageForm.path;
|
|
|
|
|
- var body = pageForm.body;
|
|
|
|
|
- var currentRevision = pageForm.currentRevision;
|
|
|
|
|
- var grant = pageForm.grant;
|
|
|
|
|
- var grantUserGroupId = pageForm.grantUserGroupId;
|
|
|
|
|
-
|
|
|
|
|
- // TODO: make it pluggable
|
|
|
|
|
- var notify = pageForm.notify || {};
|
|
|
|
|
-
|
|
|
|
|
- debug('notify: ', notify);
|
|
|
|
|
-
|
|
|
|
|
- var redirectPath = pagePathUtil.encodePagePath(path);
|
|
|
|
|
- var pageData = {};
|
|
|
|
|
- var updateOrCreate;
|
|
|
|
|
- var previousRevision = false;
|
|
|
|
|
-
|
|
|
|
|
- // set to render
|
|
|
|
|
- res.locals.pageForm = pageForm;
|
|
|
|
|
-
|
|
|
|
|
- // 削除済みページはここで編集不可判定される
|
|
|
|
|
- if (!Page.isCreatableName(path)) {
|
|
|
|
|
- res.redirect(redirectPath);
|
|
|
|
|
- return ;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- var ignoreNotFound = true;
|
|
|
|
|
- Page.findPage(path, req.user, null, ignoreNotFound)
|
|
|
|
|
- .then(function(data) {
|
|
|
|
|
- pageData = data;
|
|
|
|
|
-
|
|
|
|
|
- if (data && !data.isUpdatable(currentRevision)) {
|
|
|
|
|
- debug('Conflict occured');
|
|
|
|
|
- req.flash('dangerMessage', 'Conflict occured');
|
|
|
|
|
- return res.redirect(req.headers.referer);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if (data) {
|
|
|
|
|
- previousRevision = data.revision;
|
|
|
|
|
- return Page.updatePage(data, body, req.user, { grant, grantUserGroupId })
|
|
|
|
|
- .then((page) => {
|
|
|
|
|
- // global notification
|
|
|
|
|
- globalNotificationService.notifyPageEdit(page);
|
|
|
|
|
- return page;
|
|
|
|
|
- });
|
|
|
|
|
- }
|
|
|
|
|
- else {
|
|
|
|
|
- // new page
|
|
|
|
|
- updateOrCreate = 'create';
|
|
|
|
|
- return Page.create(path, body, req.user, { grant, grantUserGroupId })
|
|
|
|
|
- .then((page) => {
|
|
|
|
|
- // global notification
|
|
|
|
|
- globalNotificationService.notifyPageCreate(page);
|
|
|
|
|
- return page;
|
|
|
|
|
- });
|
|
|
|
|
- }
|
|
|
|
|
- }).then(function(data) {
|
|
|
|
|
- // data is a saved page data with revision.
|
|
|
|
|
- pageData = data;
|
|
|
|
|
- if (!data) {
|
|
|
|
|
- throw new Error('Data not found');
|
|
|
|
|
- }
|
|
|
|
|
- // TODO: move to events
|
|
|
|
|
- if (notify.slack) {
|
|
|
|
|
- if (notify.slack.on && notify.slack.channel) {
|
|
|
|
|
- data.updateSlackChannel(notify.slack.channel)
|
|
|
|
|
- .catch(err => {
|
|
|
|
|
- logger.error('Error occured in updating slack channels: ', err);
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- if (crowi.slack) {
|
|
|
|
|
- const promises = notify.slack.channel.split(',').map(function(chan) {
|
|
|
|
|
- return crowi.slack.postPage(pageData, req.user, chan, updateOrCreate, previousRevision);
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- Promise.all(promises)
|
|
|
|
|
- .catch(err => {
|
|
|
|
|
- logger.error('Error occured in sending slack notification: ', err);
|
|
|
|
|
- });
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return res.redirect(redirectPath);
|
|
|
|
|
- });
|
|
|
|
|
- };
|
|
|
|
|
-
|
|
|
|
|
|
|
|
|
|
-
|
|
|
|
|
- var api = actions.api = {};
|
|
|
|
|
|
|
+ const api = actions.api = {};
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* redirector
|
|
* redirector
|
|
|
*/
|
|
*/
|
|
|
api.redirector = function(req, res) {
|
|
api.redirector = function(req, res) {
|
|
|
- var id = req.params.id;
|
|
|
|
|
|
|
+ const id = req.params.id;
|
|
|
|
|
|
|
|
Page.findPageById(id)
|
|
Page.findPageById(id)
|
|
|
.then(function(pageData) {
|
|
.then(function(pageData) {
|
|
@@ -787,13 +715,13 @@ module.exports = function(crowi, app) {
|
|
|
* @apiParam {String} user
|
|
* @apiParam {String} user
|
|
|
*/
|
|
*/
|
|
|
api.list = function(req, res) {
|
|
api.list = function(req, res) {
|
|
|
- var username = req.query.user || null;
|
|
|
|
|
- var path = req.query.path || null;
|
|
|
|
|
- var limit = 50;
|
|
|
|
|
- var offset = parseInt(req.query.offset) || 0;
|
|
|
|
|
|
|
+ const username = req.query.user || null;
|
|
|
|
|
+ const path = req.query.path || null;
|
|
|
|
|
+ const limit = 50;
|
|
|
|
|
+ const offset = parseInt(req.query.offset) || 0;
|
|
|
|
|
|
|
|
- var pagerOptions = { offset: offset, limit: limit };
|
|
|
|
|
- var queryOptions = { offset: offset, limit: limit + 1};
|
|
|
|
|
|
|
+ const pagerOptions = { offset: offset, limit: limit };
|
|
|
|
|
+ const queryOptions = { offset: offset, limit: limit + 1};
|
|
|
|
|
|
|
|
// Accepts only one of these
|
|
// Accepts only one of these
|
|
|
if (username === null && path === null) {
|
|
if (username === null && path === null) {
|
|
@@ -803,7 +731,7 @@ module.exports = function(crowi, app) {
|
|
|
return res.json(ApiResponse.error('Parameter user or path is required.'));
|
|
return res.json(ApiResponse.error('Parameter user or path is required.'));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- var pageFetcher;
|
|
|
|
|
|
|
+ let pageFetcher;
|
|
|
if (path === null) {
|
|
if (path === null) {
|
|
|
pageFetcher = User.findUserByUsername(username)
|
|
pageFetcher = User.findUserByUsername(username)
|
|
|
.then(function(user) {
|
|
.then(function(user) {
|
|
@@ -824,7 +752,7 @@ module.exports = function(crowi, app) {
|
|
|
}
|
|
}
|
|
|
pagerOptions.length = pages.length;
|
|
pagerOptions.length = pages.length;
|
|
|
|
|
|
|
|
- var result = {};
|
|
|
|
|
|
|
+ const result = {};
|
|
|
result.pages = pagePathUtil.encodePagesPath(pages);
|
|
result.pages = pagePathUtil.encodePagesPath(pages);
|
|
|
return res.json(ApiResponse.success(result));
|
|
return res.json(ApiResponse.success(result));
|
|
|
}).catch(function(err) {
|
|
}).catch(function(err) {
|
|
@@ -841,37 +769,48 @@ module.exports = function(crowi, app) {
|
|
|
* @apiParam {String} path
|
|
* @apiParam {String} path
|
|
|
* @apiParam {String} grant
|
|
* @apiParam {String} grant
|
|
|
*/
|
|
*/
|
|
|
- api.create = function(req, res) {
|
|
|
|
|
- var body = req.body.body || null;
|
|
|
|
|
- var pagePath = req.body.path || null;
|
|
|
|
|
- var grant = req.body.grant || null;
|
|
|
|
|
- var grantUserGroupId = req.body.grantUserGroupId || null;
|
|
|
|
|
|
|
+ api.create = async function(req, res) {
|
|
|
|
|
+ const body = req.body.body || null;
|
|
|
|
|
+ const pagePath = req.body.path || null;
|
|
|
|
|
+ const grant = req.body.grant || null;
|
|
|
|
|
+ const grantUserGroupId = req.body.grantUserGroupId || null;
|
|
|
|
|
+ const isSlackEnabled = !!req.body.isSlackEnabled; // cast to boolean
|
|
|
|
|
+ const slackChannels = req.body.slackChannels || null;
|
|
|
|
|
|
|
|
if (body === null || pagePath === null) {
|
|
if (body === null || pagePath === null) {
|
|
|
return res.json(ApiResponse.error('Parameters body and path are required.'));
|
|
return res.json(ApiResponse.error('Parameters body and path are required.'));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- var ignoreNotFound = true;
|
|
|
|
|
- Page.findPage(pagePath, req.user, null, ignoreNotFound)
|
|
|
|
|
- .then(function(data) {
|
|
|
|
|
- if (data !== null) {
|
|
|
|
|
- throw new Error('Page exists');
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ const ignoreNotFound = true;
|
|
|
|
|
+ const createdPage = await Page.findPage(pagePath, req.user, null, ignoreNotFound)
|
|
|
|
|
+ .then(function(data) {
|
|
|
|
|
+ if (data !== null) {
|
|
|
|
|
+ throw new Error('Page exists');
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- return Page.create(pagePath, body, req.user, { grant: grant, grantUserGroupId: grantUserGroupId});
|
|
|
|
|
- }).then(function(data) {
|
|
|
|
|
- if (!data) {
|
|
|
|
|
- throw new Error('Failed to create page.');
|
|
|
|
|
- }
|
|
|
|
|
- var result = { page: data.toObject() };
|
|
|
|
|
|
|
+ return Page.create(pagePath, body, req.user, { grant: grant, grantUserGroupId: grantUserGroupId});
|
|
|
|
|
+ })
|
|
|
|
|
+ .catch(function(err) {
|
|
|
|
|
+ return res.json(ApiResponse.error(err));
|
|
|
|
|
+ });
|
|
|
|
|
|
|
|
- result.page.lastUpdateUser = User.filterToPublicFields(data.lastUpdateUser);
|
|
|
|
|
- result.page.creator = User.filterToPublicFields(data.creator);
|
|
|
|
|
- return res.json(ApiResponse.success(result));
|
|
|
|
|
- }).catch(function(err) {
|
|
|
|
|
- return res.json(ApiResponse.error(err));
|
|
|
|
|
- });
|
|
|
|
|
|
|
+ const result = { page: createdPage.toObject() };
|
|
|
|
|
+ result.page.lastUpdateUser = User.filterToPublicFields(createdPage.lastUpdateUser);
|
|
|
|
|
+ result.page.creator = User.filterToPublicFields(createdPage.creator);
|
|
|
|
|
+ res.json(ApiResponse.success(result));
|
|
|
|
|
+
|
|
|
|
|
+ // global notification
|
|
|
|
|
+ try {
|
|
|
|
|
+ await globalNotificationService.notifyPageCreate(createdPage);
|
|
|
|
|
+ }
|
|
|
|
|
+ catch (err) {
|
|
|
|
|
+ logger.error(err);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|
+ // user notification
|
|
|
|
|
+ if (isSlackEnabled && slackChannels != null) {
|
|
|
|
|
+ await notifyToSlackByUser(createdPage, req.user, slackChannels, 'create', false);
|
|
|
|
|
+ }
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -888,41 +827,60 @@ module.exports = function(crowi, app) {
|
|
|
* - If revision_id is specified => update the page,
|
|
* - If revision_id is specified => update the page,
|
|
|
* - If revision_id is not specified => force update by the new contents.
|
|
* - If revision_id is not specified => force update by the new contents.
|
|
|
*/
|
|
*/
|
|
|
- api.update = function(req, res) {
|
|
|
|
|
- var pageBody = req.body.body || null;
|
|
|
|
|
- var pageId = req.body.page_id || null;
|
|
|
|
|
- var revisionId = req.body.revision_id || null;
|
|
|
|
|
- var grant = req.body.grant || null;
|
|
|
|
|
- var grantUserGroupId = req.body.grantUserGroupId || null;
|
|
|
|
|
|
|
+ api.update = async function(req, res) {
|
|
|
|
|
+ const pageBody = req.body.body || null;
|
|
|
|
|
+ const pageId = req.body.page_id || null;
|
|
|
|
|
+ const revisionId = req.body.revision_id || null;
|
|
|
|
|
+ const grant = req.body.grant || null;
|
|
|
|
|
+ const grantUserGroupId = req.body.grantUserGroupId || null;
|
|
|
|
|
+ const isSlackEnabled = !!req.body.isSlackEnabled; // cast to boolean
|
|
|
|
|
+ const slackChannels = req.body.slackChannels || null;
|
|
|
|
|
|
|
|
if (pageId === null || pageBody === null) {
|
|
if (pageId === null || pageBody === null) {
|
|
|
return res.json(ApiResponse.error('page_id and body are required.'));
|
|
return res.json(ApiResponse.error('page_id and body are required.'));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- Page.findPageByIdAndGrantedUser(pageId, req.user)
|
|
|
|
|
- .then(function(pageData) {
|
|
|
|
|
- if (pageData && revisionId !== null && !pageData.isUpdatable(revisionId)) {
|
|
|
|
|
- throw new Error('Revision error.');
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ let previousRevision = undefined;
|
|
|
|
|
+ let updatedPage = await Page.findPageByIdAndGrantedUser(pageId, req.user)
|
|
|
|
|
+ .then(function(pageData) {
|
|
|
|
|
+ if (pageData && revisionId !== null && !pageData.isUpdatable(revisionId)) {
|
|
|
|
|
+ throw new Error('Revision error.');
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- var grantOption = {};
|
|
|
|
|
- if (grant != null) {
|
|
|
|
|
- grantOption.grant = grant;
|
|
|
|
|
- }
|
|
|
|
|
- if (grantUserGroupId != null) {
|
|
|
|
|
- grantOption.grantUserGroupId = grantUserGroupId;
|
|
|
|
|
- }
|
|
|
|
|
- return Page.updatePage(pageData, pageBody, req.user, grantOption);
|
|
|
|
|
- }).then(function(pageData) {
|
|
|
|
|
- var result = {
|
|
|
|
|
- page: pageData.toObject(),
|
|
|
|
|
- };
|
|
|
|
|
- result.page.lastUpdateUser = User.filterToPublicFields(result.page.lastUpdateUser);
|
|
|
|
|
- return res.json(ApiResponse.success(result));
|
|
|
|
|
- }).catch(function(err) {
|
|
|
|
|
- debug('error on _api/pages.update', err);
|
|
|
|
|
- return res.json(ApiResponse.error(err));
|
|
|
|
|
- });
|
|
|
|
|
|
|
+ const grantOption = {};
|
|
|
|
|
+ if (grant != null) {
|
|
|
|
|
+ grantOption.grant = grant;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (grantUserGroupId != null) {
|
|
|
|
|
+ grantOption.grantUserGroupId = grantUserGroupId;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // store previous revision
|
|
|
|
|
+ previousRevision = pageData.revision;
|
|
|
|
|
+
|
|
|
|
|
+ return Page.updatePage(pageData, pageBody, req.user, grantOption);
|
|
|
|
|
+ })
|
|
|
|
|
+ .catch(function(err) {
|
|
|
|
|
+ debug('error on _api/pages.update', err);
|
|
|
|
|
+ res.json(ApiResponse.error(err));
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ const result = { page: updatedPage.toObject() };
|
|
|
|
|
+ result.page.lastUpdateUser = User.filterToPublicFields(updatedPage.lastUpdateUser);
|
|
|
|
|
+ res.json(ApiResponse.success(result));
|
|
|
|
|
+
|
|
|
|
|
+ // global notification
|
|
|
|
|
+ try {
|
|
|
|
|
+ await globalNotificationService.notifyPageEdit(updatedPage);
|
|
|
|
|
+ }
|
|
|
|
|
+ catch (err) {
|
|
|
|
|
+ logger.error(err);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // user notification
|
|
|
|
|
+ if (isSlackEnabled && slackChannels != null) {
|
|
|
|
|
+ await notifyToSlackByUser(updatedPage, req.user, slackChannels, 'update', previousRevision);
|
|
|
|
|
+ }
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -952,7 +910,7 @@ module.exports = function(crowi, app) {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
pageFinder.then(function(pageData) {
|
|
pageFinder.then(function(pageData) {
|
|
|
- var result = {};
|
|
|
|
|
|
|
+ const result = {};
|
|
|
result.page = pageData;
|
|
result.page = pageData;
|
|
|
|
|
|
|
|
return res.json(ApiResponse.success(result));
|
|
return res.json(ApiResponse.success(result));
|
|
@@ -969,7 +927,7 @@ module.exports = function(crowi, app) {
|
|
|
* @apiParam {String} page_id Page Id.
|
|
* @apiParam {String} page_id Page Id.
|
|
|
*/
|
|
*/
|
|
|
api.seen = function(req, res) {
|
|
api.seen = function(req, res) {
|
|
|
- var pageId = req.body.page_id;
|
|
|
|
|
|
|
+ const pageId = req.body.page_id;
|
|
|
if (!pageId) {
|
|
if (!pageId) {
|
|
|
return res.json(ApiResponse.error('page_id required'));
|
|
return res.json(ApiResponse.error('page_id required'));
|
|
|
}
|
|
}
|
|
@@ -978,7 +936,7 @@ module.exports = function(crowi, app) {
|
|
|
.then(function(page) {
|
|
.then(function(page) {
|
|
|
return page.seen(req.user);
|
|
return page.seen(req.user);
|
|
|
}).then(function(user) {
|
|
}).then(function(user) {
|
|
|
- var result = {};
|
|
|
|
|
|
|
+ const result = {};
|
|
|
result.seenUser = user;
|
|
result.seenUser = user;
|
|
|
|
|
|
|
|
return res.json(ApiResponse.success(result));
|
|
return res.json(ApiResponse.success(result));
|
|
@@ -996,14 +954,14 @@ module.exports = function(crowi, app) {
|
|
|
* @apiParam {String} page_id Page Id.
|
|
* @apiParam {String} page_id Page Id.
|
|
|
*/
|
|
*/
|
|
|
api.like = function(req, res) {
|
|
api.like = function(req, res) {
|
|
|
- var id = req.body.page_id;
|
|
|
|
|
|
|
+ const id = req.body.page_id;
|
|
|
|
|
|
|
|
Page.findPageByIdAndGrantedUser(id, req.user)
|
|
Page.findPageByIdAndGrantedUser(id, req.user)
|
|
|
.then(function(pageData) {
|
|
.then(function(pageData) {
|
|
|
return pageData.like(req.user);
|
|
return pageData.like(req.user);
|
|
|
})
|
|
})
|
|
|
.then(function(page) {
|
|
.then(function(page) {
|
|
|
- var result = {page: page};
|
|
|
|
|
|
|
+ const result = {page: page};
|
|
|
res.json(ApiResponse.success(result));
|
|
res.json(ApiResponse.success(result));
|
|
|
return page;
|
|
return page;
|
|
|
})
|
|
})
|
|
@@ -1025,13 +983,13 @@ module.exports = function(crowi, app) {
|
|
|
* @apiParam {String} page_id Page Id.
|
|
* @apiParam {String} page_id Page Id.
|
|
|
*/
|
|
*/
|
|
|
api.unlike = function(req, res) {
|
|
api.unlike = function(req, res) {
|
|
|
- var id = req.body.page_id;
|
|
|
|
|
|
|
+ const id = req.body.page_id;
|
|
|
|
|
|
|
|
Page.findPageByIdAndGrantedUser(id, req.user)
|
|
Page.findPageByIdAndGrantedUser(id, req.user)
|
|
|
.then(function(pageData) {
|
|
.then(function(pageData) {
|
|
|
return pageData.unlike(req.user);
|
|
return pageData.unlike(req.user);
|
|
|
}).then(function(data) {
|
|
}).then(function(data) {
|
|
|
- var result = {page: data};
|
|
|
|
|
|
|
+ const result = {page: data};
|
|
|
return res.json(ApiResponse.success(result));
|
|
return res.json(ApiResponse.success(result));
|
|
|
}).catch(function(err) {
|
|
}).catch(function(err) {
|
|
|
debug('Unlike failed', err);
|
|
debug('Unlike failed', err);
|
|
@@ -1047,8 +1005,8 @@ module.exports = function(crowi, app) {
|
|
|
* @apiParam {String} path
|
|
* @apiParam {String} path
|
|
|
*/
|
|
*/
|
|
|
api.getUpdatePost = function(req, res) {
|
|
api.getUpdatePost = function(req, res) {
|
|
|
- var path = req.query.path;
|
|
|
|
|
- var UpdatePost = crowi.model('UpdatePost');
|
|
|
|
|
|
|
+ const path = req.query.path;
|
|
|
|
|
+ const UpdatePost = crowi.model('UpdatePost');
|
|
|
|
|
|
|
|
if (!path) {
|
|
if (!path) {
|
|
|
return res.json(ApiResponse.error({}));
|
|
return res.json(ApiResponse.error({}));
|
|
@@ -1060,7 +1018,7 @@ module.exports = function(crowi, app) {
|
|
|
return e.channel;
|
|
return e.channel;
|
|
|
});
|
|
});
|
|
|
debug('Found updatePost data', data);
|
|
debug('Found updatePost data', data);
|
|
|
- var result = {updatePost: data};
|
|
|
|
|
|
|
+ const result = {updatePost: data};
|
|
|
return res.json(ApiResponse.success(result));
|
|
return res.json(ApiResponse.success(result));
|
|
|
}).catch(function(err) {
|
|
}).catch(function(err) {
|
|
|
debug('Error occured while get setting', err);
|
|
debug('Error occured while get setting', err);
|
|
@@ -1077,8 +1035,8 @@ module.exports = function(crowi, app) {
|
|
|
* @apiParam {String} revision_id
|
|
* @apiParam {String} revision_id
|
|
|
*/
|
|
*/
|
|
|
api.remove = function(req, res) {
|
|
api.remove = function(req, res) {
|
|
|
- var pageId = req.body.page_id;
|
|
|
|
|
- var previousRevision = req.body.revision_id || null;
|
|
|
|
|
|
|
+ const pageId = req.body.page_id;
|
|
|
|
|
+ const previousRevision = req.body.revision_id || null;
|
|
|
|
|
|
|
|
// get completely flag
|
|
// get completely flag
|
|
|
const isCompletely = (req.body.completely != null);
|
|
const isCompletely = (req.body.completely != null);
|
|
@@ -1113,7 +1071,7 @@ module.exports = function(crowi, app) {
|
|
|
})
|
|
})
|
|
|
.then(function(data) {
|
|
.then(function(data) {
|
|
|
debug('Page deleted', data.path);
|
|
debug('Page deleted', data.path);
|
|
|
- var result = {};
|
|
|
|
|
|
|
+ const result = {};
|
|
|
result.page = data;
|
|
result.page = data;
|
|
|
|
|
|
|
|
res.json(ApiResponse.success(result));
|
|
res.json(ApiResponse.success(result));
|
|
@@ -1137,7 +1095,7 @@ module.exports = function(crowi, app) {
|
|
|
* @apiParam {String} page_id Page Id.
|
|
* @apiParam {String} page_id Page Id.
|
|
|
*/
|
|
*/
|
|
|
api.revertRemove = function(req, res) {
|
|
api.revertRemove = function(req, res) {
|
|
|
- var pageId = req.body.page_id;
|
|
|
|
|
|
|
+ const pageId = req.body.page_id;
|
|
|
|
|
|
|
|
// get recursively flag
|
|
// get recursively flag
|
|
|
const isRecursively = (req.body.recursively !== undefined);
|
|
const isRecursively = (req.body.recursively !== undefined);
|
|
@@ -1153,7 +1111,7 @@ module.exports = function(crowi, app) {
|
|
|
}
|
|
}
|
|
|
}).then(function(data) {
|
|
}).then(function(data) {
|
|
|
debug('Complete to revert deleted page', data.path);
|
|
debug('Complete to revert deleted page', data.path);
|
|
|
- var result = {};
|
|
|
|
|
|
|
+ const result = {};
|
|
|
result.page = data;
|
|
result.page = data;
|
|
|
|
|
|
|
|
return res.json(ApiResponse.success(result));
|
|
return res.json(ApiResponse.success(result));
|
|
@@ -1175,15 +1133,14 @@ module.exports = function(crowi, app) {
|
|
|
* @apiParam {Bool} create_redirect
|
|
* @apiParam {Bool} create_redirect
|
|
|
*/
|
|
*/
|
|
|
api.rename = function(req, res) {
|
|
api.rename = function(req, res) {
|
|
|
- var pageId = req.body.page_id;
|
|
|
|
|
- var previousRevision = req.body.revision_id || null;
|
|
|
|
|
- var newPagePath = Page.normalizePath(req.body.new_path);
|
|
|
|
|
- var options = {
|
|
|
|
|
|
|
+ const pageId = req.body.page_id;
|
|
|
|
|
+ const previousRevision = req.body.revision_id || null;
|
|
|
|
|
+ const newPagePath = Page.normalizePath(req.body.new_path);
|
|
|
|
|
+ const options = {
|
|
|
createRedirectPage: req.body.create_redirect || 0,
|
|
createRedirectPage: req.body.create_redirect || 0,
|
|
|
moveUnderTrees: req.body.move_trees || 0,
|
|
moveUnderTrees: req.body.move_trees || 0,
|
|
|
};
|
|
};
|
|
|
- var isRecursiveMove = req.body.move_recursively || 0;
|
|
|
|
|
- var page = {};
|
|
|
|
|
|
|
+ const isRecursiveMove = req.body.move_recursively || 0;
|
|
|
|
|
|
|
|
if (!Page.isCreatableName(newPagePath)) {
|
|
if (!Page.isCreatableName(newPagePath)) {
|
|
|
return res.json(ApiResponse.error(`このページ名は作成できません (${newPagePath})`));
|
|
return res.json(ApiResponse.error(`このページ名は作成できません (${newPagePath})`));
|
|
@@ -1212,7 +1169,7 @@ module.exports = function(crowi, app) {
|
|
|
|
|
|
|
|
})
|
|
})
|
|
|
.then(function() {
|
|
.then(function() {
|
|
|
- var result = {};
|
|
|
|
|
|
|
+ const result = {};
|
|
|
result.page = page;
|
|
result.page = page;
|
|
|
|
|
|
|
|
return res.json(ApiResponse.success(result));
|
|
return res.json(ApiResponse.success(result));
|
|
@@ -1237,8 +1194,8 @@ module.exports = function(crowi, app) {
|
|
|
* @apiParam {String} new_path
|
|
* @apiParam {String} new_path
|
|
|
*/
|
|
*/
|
|
|
api.duplicate = function(req, res) {
|
|
api.duplicate = function(req, res) {
|
|
|
- var pageId = req.body.page_id;
|
|
|
|
|
- var newPagePath = Page.normalizePath(req.body.new_path);
|
|
|
|
|
|
|
+ const pageId = req.body.page_id;
|
|
|
|
|
+ const newPagePath = Page.normalizePath(req.body.new_path);
|
|
|
|
|
|
|
|
Page.findPageById(pageId)
|
|
Page.findPageById(pageId)
|
|
|
.then(function(pageData) {
|
|
.then(function(pageData) {
|
|
@@ -1259,7 +1216,7 @@ module.exports = function(crowi, app) {
|
|
|
* @apiParam {String} revision_id
|
|
* @apiParam {String} revision_id
|
|
|
*/
|
|
*/
|
|
|
api.unlink = function(req, res) {
|
|
api.unlink = function(req, res) {
|
|
|
- var pageId = req.body.page_id;
|
|
|
|
|
|
|
+ const pageId = req.body.page_id;
|
|
|
|
|
|
|
|
Page.findPageByIdAndGrantedUser(pageId, req.user)
|
|
Page.findPageByIdAndGrantedUser(pageId, req.user)
|
|
|
.then(function(pageData) {
|
|
.then(function(pageData) {
|
|
@@ -1269,7 +1226,7 @@ module.exports = function(crowi, app) {
|
|
|
.then(() => pageData);
|
|
.then(() => pageData);
|
|
|
}).then(function(data) {
|
|
}).then(function(data) {
|
|
|
debug('Redirect Page deleted', data.path);
|
|
debug('Redirect Page deleted', data.path);
|
|
|
- var result = {};
|
|
|
|
|
|
|
+ const result = {};
|
|
|
result.page = data;
|
|
result.page = data;
|
|
|
|
|
|
|
|
return res.json(ApiResponse.success(result));
|
|
return res.json(ApiResponse.success(result));
|