|
@@ -9,8 +9,6 @@ module.exports = function(crowi, app) {
|
|
|
const UserGroup = models.UserGroup;
|
|
const UserGroup = models.UserGroup;
|
|
|
const UserGroupRelation = models.UserGroupRelation;
|
|
const UserGroupRelation = models.UserGroupRelation;
|
|
|
const GlobalNotificationSetting = models.GlobalNotificationSetting;
|
|
const GlobalNotificationSetting = models.GlobalNotificationSetting;
|
|
|
- const GlobalNotificationMailSetting = models.GlobalNotificationMailSetting;
|
|
|
|
|
- const GlobalNotificationSlackSetting = models.GlobalNotificationSlackSetting; // eslint-disable-line no-unused-vars
|
|
|
|
|
|
|
|
|
|
const {
|
|
const {
|
|
|
configManager,
|
|
configManager,
|
|
@@ -162,49 +160,8 @@ module.exports = function(crowi, app) {
|
|
|
// app.get('/admin/notification' , admin.notification.index);
|
|
// app.get('/admin/notification' , admin.notification.index);
|
|
|
actions.notification = {};
|
|
actions.notification = {};
|
|
|
actions.notification.index = async(req, res) => {
|
|
actions.notification.index = async(req, res) => {
|
|
|
- const UpdatePost = crowi.model('UpdatePost');
|
|
|
|
|
- let slackSetting = configManager.getConfigByPrefix('notification', 'slack:');
|
|
|
|
|
- const hasSlackIwhUrl = !!configManager.getConfig('notification', 'slack:incomingWebhookUrl');
|
|
|
|
|
- const hasSlackToken = !!configManager.getConfig('notification', 'slack:token');
|
|
|
|
|
|
|
|
|
|
- if (!hasSlackIwhUrl) {
|
|
|
|
|
- slackSetting['slack:incomingWebhookUrl'] = '';
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if (req.session.slackSetting) {
|
|
|
|
|
- slackSetting = req.session.slackSetting;
|
|
|
|
|
- req.session.slackSetting = null;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- const globalNotifications = await GlobalNotificationSetting.findAll();
|
|
|
|
|
- const userNotifications = await UpdatePost.findAll();
|
|
|
|
|
-
|
|
|
|
|
- return res.render('admin/notification', {
|
|
|
|
|
- userNotifications,
|
|
|
|
|
- slackSetting,
|
|
|
|
|
- hasSlackIwhUrl,
|
|
|
|
|
- hasSlackToken,
|
|
|
|
|
- globalNotifications,
|
|
|
|
|
- });
|
|
|
|
|
- };
|
|
|
|
|
-
|
|
|
|
|
- // app.post('/admin/notification/slackSetting' , admin.notification.slackauth);
|
|
|
|
|
- actions.notification.slackSetting = async function(req, res) {
|
|
|
|
|
- const slackSetting = req.form.slackSetting;
|
|
|
|
|
-
|
|
|
|
|
- if (req.form.isValid) {
|
|
|
|
|
- await configManager.updateConfigsInTheSameNamespace('notification', slackSetting);
|
|
|
|
|
- req.flash('successMessage', ['Successfully Updated!']);
|
|
|
|
|
-
|
|
|
|
|
- // Re-setup
|
|
|
|
|
- crowi.setupSlack().then(() => {
|
|
|
|
|
- });
|
|
|
|
|
- }
|
|
|
|
|
- else {
|
|
|
|
|
- req.flash('errorMessage', req.form.errors);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return res.redirect('/admin/notification');
|
|
|
|
|
|
|
+ return res.render('admin/notification');
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
// app.get('/admin/notification/slackAuth' , admin.notification.slackauth);
|
|
// app.get('/admin/notification/slackAuth' , admin.notification.slackauth);
|
|
@@ -237,25 +194,6 @@ module.exports = function(crowi, app) {
|
|
|
});
|
|
});
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
- // app.post('/admin/notification/slackIwhSetting' , admin.notification.slackIwhSetting);
|
|
|
|
|
- actions.notification.slackIwhSetting = async function(req, res) {
|
|
|
|
|
- const slackIwhSetting = req.form.slackIwhSetting;
|
|
|
|
|
-
|
|
|
|
|
- if (req.form.isValid) {
|
|
|
|
|
- await configManager.updateConfigsInTheSameNamespace('notification', slackIwhSetting);
|
|
|
|
|
- req.flash('successMessage', ['Successfully Updated!']);
|
|
|
|
|
-
|
|
|
|
|
- // Re-setup
|
|
|
|
|
- crowi.setupSlack().then(() => {
|
|
|
|
|
- return res.redirect('/admin/notification#slack-incoming-webhooks');
|
|
|
|
|
- });
|
|
|
|
|
- }
|
|
|
|
|
- else {
|
|
|
|
|
- req.flash('errorMessage', req.form.errors);
|
|
|
|
|
- return res.redirect('/admin/notification#slack-incoming-webhooks');
|
|
|
|
|
- }
|
|
|
|
|
- };
|
|
|
|
|
-
|
|
|
|
|
// app.post('/admin/notification/slackSetting/disconnect' , admin.notification.disconnectFromSlack);
|
|
// app.post('/admin/notification/slackSetting/disconnect' , admin.notification.disconnectFromSlack);
|
|
|
actions.notification.disconnectFromSlack = async function(req, res) {
|
|
actions.notification.disconnectFromSlack = async function(req, res) {
|
|
|
await configManager.updateConfigsInTheSameNamespace('notification', { 'slack:token': '' });
|
|
await configManager.updateConfigsInTheSameNamespace('notification', { 'slack:token': '' });
|
|
@@ -281,100 +219,6 @@ module.exports = function(crowi, app) {
|
|
|
return res.render('admin/global-notification-detail', { globalNotification });
|
|
return res.render('admin/global-notification-detail', { globalNotification });
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
- actions.globalNotification.create = (req, res) => {
|
|
|
|
|
- const form = req.form.notificationGlobal;
|
|
|
|
|
- let setting;
|
|
|
|
|
-
|
|
|
|
|
- switch (form.notifyToType) {
|
|
|
|
|
- case GlobalNotificationSetting.TYPE.MAIL:
|
|
|
|
|
- setting = new GlobalNotificationMailSetting(crowi);
|
|
|
|
|
- setting.toEmail = form.toEmail;
|
|
|
|
|
- break;
|
|
|
|
|
- case GlobalNotificationSetting.TYPE.SLACK:
|
|
|
|
|
- setting = new GlobalNotificationSlackSetting(crowi);
|
|
|
|
|
- setting.slackChannels = form.slackChannels;
|
|
|
|
|
- break;
|
|
|
|
|
- default:
|
|
|
|
|
- logger.error('GlobalNotificationSetting Type Error: undefined type');
|
|
|
|
|
- req.flash('errorMessage', 'Error occurred in creating a new global notification setting: undefined notification type');
|
|
|
|
|
- return res.redirect('/admin/notification#global-notification');
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- setting.triggerPath = form.triggerPath;
|
|
|
|
|
- setting.triggerEvents = getNotificationEvents(form);
|
|
|
|
|
- setting.save();
|
|
|
|
|
-
|
|
|
|
|
- return res.redirect('/admin/notification#global-notification');
|
|
|
|
|
- };
|
|
|
|
|
-
|
|
|
|
|
- actions.globalNotification.update = async(req, res) => {
|
|
|
|
|
- const form = req.form.notificationGlobal;
|
|
|
|
|
-
|
|
|
|
|
- const models = {
|
|
|
|
|
- [GlobalNotificationSetting.TYPE.MAIL]: GlobalNotificationMailSetting,
|
|
|
|
|
- [GlobalNotificationSetting.TYPE.SLACK]: GlobalNotificationSlackSetting,
|
|
|
|
|
- };
|
|
|
|
|
-
|
|
|
|
|
- let setting = await GlobalNotificationSetting.findOne({ _id: form.id });
|
|
|
|
|
- setting = setting.toObject();
|
|
|
|
|
-
|
|
|
|
|
- // when switching from one type to another,
|
|
|
|
|
- // remove toEmail from slack setting and slackChannels from mail setting
|
|
|
|
|
- if (setting.__t !== form.notifyToType) {
|
|
|
|
|
- setting = models[setting.__t].hydrate(setting);
|
|
|
|
|
- setting.toEmail = undefined;
|
|
|
|
|
- setting.slackChannels = undefined;
|
|
|
|
|
- await setting.save();
|
|
|
|
|
- setting = setting.toObject();
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- switch (form.notifyToType) {
|
|
|
|
|
- case GlobalNotificationSetting.TYPE.MAIL:
|
|
|
|
|
- setting = GlobalNotificationMailSetting.hydrate(setting);
|
|
|
|
|
- setting.toEmail = form.toEmail;
|
|
|
|
|
- break;
|
|
|
|
|
- case GlobalNotificationSetting.TYPE.SLACK:
|
|
|
|
|
- setting = GlobalNotificationSlackSetting.hydrate(setting);
|
|
|
|
|
- setting.slackChannels = form.slackChannels;
|
|
|
|
|
- break;
|
|
|
|
|
- default:
|
|
|
|
|
- logger.error('GlobalNotificationSetting Type Error: undefined type');
|
|
|
|
|
- req.flash('errorMessage', 'Error occurred in updating the global notification setting: undefined notification type');
|
|
|
|
|
- return res.redirect('/admin/notification#global-notification');
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- setting.__t = form.notifyToType;
|
|
|
|
|
- setting.triggerPath = form.triggerPath;
|
|
|
|
|
- setting.triggerEvents = getNotificationEvents(form);
|
|
|
|
|
- await setting.save();
|
|
|
|
|
-
|
|
|
|
|
- return res.redirect('/admin/notification#global-notification');
|
|
|
|
|
- };
|
|
|
|
|
-
|
|
|
|
|
- actions.globalNotification.remove = async(req, res) => {
|
|
|
|
|
- const id = req.params.id;
|
|
|
|
|
-
|
|
|
|
|
- try {
|
|
|
|
|
- await GlobalNotificationSetting.findOneAndRemove({ _id: id });
|
|
|
|
|
- return res.redirect('/admin/notification#global-notification');
|
|
|
|
|
- }
|
|
|
|
|
- catch (err) {
|
|
|
|
|
- req.flash('errorMessage', 'Error in deleting global notification setting');
|
|
|
|
|
- return res.redirect('/admin/notification#global-notification');
|
|
|
|
|
- }
|
|
|
|
|
- };
|
|
|
|
|
-
|
|
|
|
|
- const getNotificationEvents = (form) => {
|
|
|
|
|
- const triggerEvents = [];
|
|
|
|
|
- const triggerEventKeys = Object.keys(form).filter((key) => { return key.match(/^triggerEvent/) });
|
|
|
|
|
- triggerEventKeys.forEach((key) => {
|
|
|
|
|
- if (form[key]) {
|
|
|
|
|
- triggerEvents.push(form[key]);
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
- return triggerEvents;
|
|
|
|
|
- };
|
|
|
|
|
-
|
|
|
|
|
actions.search = {};
|
|
actions.search = {};
|
|
|
actions.search.index = function(req, res) {
|
|
actions.search.index = function(req, res) {
|
|
|
const search = crowi.getSearcher();
|
|
const search = crowi.getSearcher();
|
|
@@ -800,23 +644,6 @@ module.exports = function(crowi, app) {
|
|
|
return res.json({ status: true });
|
|
return res.json({ status: true });
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
- // app.post('/_api/admin/notifications.remove' , admin.api.notificationRemove);
|
|
|
|
|
- actions.api.notificationRemove = function(req, res) {
|
|
|
|
|
- const UpdatePost = crowi.model('UpdatePost');
|
|
|
|
|
- const id = req.body.id;
|
|
|
|
|
-
|
|
|
|
|
- UpdatePost.remove(id)
|
|
|
|
|
- .then(() => {
|
|
|
|
|
- debug('Successfully remove updatePost');
|
|
|
|
|
-
|
|
|
|
|
- return res.json(ApiResponse.success({}));
|
|
|
|
|
- })
|
|
|
|
|
- .catch((err) => {
|
|
|
|
|
- debug('Failed to remove updatePost', err);
|
|
|
|
|
- return res.json(ApiResponse.error());
|
|
|
|
|
- });
|
|
|
|
|
- };
|
|
|
|
|
-
|
|
|
|
|
// app.get('/_api/admin/users.search' , admin.api.userSearch);
|
|
// app.get('/_api/admin/users.search' , admin.api.userSearch);
|
|
|
actions.api.usersSearch = function(req, res) {
|
|
actions.api.usersSearch = function(req, res) {
|
|
|
const User = crowi.model('User');
|
|
const User = crowi.model('User');
|
|
@@ -834,25 +661,6 @@ module.exports = function(crowi, app) {
|
|
|
});
|
|
});
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
- actions.api.toggleIsEnabledForGlobalNotification = async(req, res) => {
|
|
|
|
|
- const id = req.query.id;
|
|
|
|
|
- const isEnabled = (req.query.isEnabled === 'true');
|
|
|
|
|
-
|
|
|
|
|
- try {
|
|
|
|
|
- if (isEnabled) {
|
|
|
|
|
- await GlobalNotificationSetting.enable(id);
|
|
|
|
|
- }
|
|
|
|
|
- else {
|
|
|
|
|
- await GlobalNotificationSetting.disable(id);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return res.json(ApiResponse.success());
|
|
|
|
|
- }
|
|
|
|
|
- catch (err) {
|
|
|
|
|
- return res.json(ApiResponse.error());
|
|
|
|
|
- }
|
|
|
|
|
- };
|
|
|
|
|
-
|
|
|
|
|
/**
|
|
/**
|
|
|
* save esa settings, update config cache, and response json
|
|
* save esa settings, update config cache, and response json
|
|
|
*
|
|
*
|