|
|
@@ -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
|
|
|
*/
|
|
|
@@ -849,6 +869,8 @@ module.exports = function(crowi, app) {
|
|
|
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) {
|
|
|
return res.json(ApiResponse.error('Parameters body and path are required.'));
|
|
|
@@ -863,20 +885,15 @@ module.exports = function(crowi, app) {
|
|
|
|
|
|
return Page.create(pagePath, body, req.user, { grant: grant, grantUserGroupId: grantUserGroupId});
|
|
|
})
|
|
|
- .then(function(data) {
|
|
|
- if (!data) {
|
|
|
- throw new Error('Failed to create page.');
|
|
|
- }
|
|
|
- const result = { page: data.toObject() };
|
|
|
-
|
|
|
- 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);
|
|
|
@@ -884,6 +901,11 @@ module.exports = function(crowi, app) {
|
|
|
catch (err) {
|
|
|
logger.error(err);
|
|
|
}
|
|
|
+
|
|
|
+ // user notification
|
|
|
+ if (isSlackEnabled && slackChannels != null) {
|
|
|
+ await notifyToSlackByUser(createdPage, req.user, slackChannels, 'create', false);
|
|
|
+ }
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
@@ -906,6 +928,8 @@ module.exports = function(crowi, app) {
|
|
|
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) {
|
|
|
return res.json(ApiResponse.error('page_id and body are required.'));
|
|
|
@@ -931,25 +955,26 @@ module.exports = function(crowi, app) {
|
|
|
|
|
|
return Page.updatePage(pageData, pageBody, req.user, grantOption);
|
|
|
})
|
|
|
- .then(function(pageData) {
|
|
|
- const result = { page: pageData.toObject() };
|
|
|
-
|
|
|
- result.page.lastUpdateUser = User.filterToPublicFields(result.page.lastUpdateUser);
|
|
|
- res.json(ApiResponse.success(result));
|
|
|
-
|
|
|
- return pageData;
|
|
|
- })
|
|
|
.catch(function(err) {
|
|
|
debug('error on _api/pages.update', err);
|
|
|
res.json(ApiResponse.error(err));
|
|
|
});
|
|
|
|
|
|
- // global notification
|
|
|
+ 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);
|
|
|
+ logger.error(err);
|
|
|
+ }
|
|
|
+
|
|
|
+ // user notification
|
|
|
+ if (isSlackEnabled && slackChannels != null) {
|
|
|
+ await notifyToSlackByUser(updatedPage, req.user, slackChannels, 'update', previousRevision);
|
|
|
}
|
|
|
};
|
|
|
|