|
|
@@ -2,6 +2,7 @@ module.exports = function(crowi, app) {
|
|
|
'use strict';
|
|
|
|
|
|
var debug = require('debug')('growi:routes:admin')
|
|
|
+ , logger = require('@alias/logger')('growi:routes:admin')
|
|
|
, fs = require('fs')
|
|
|
, models = crowi.models
|
|
|
, Page = models.Page
|
|
|
@@ -11,6 +12,9 @@ module.exports = function(crowi, app) {
|
|
|
, UserGroup = models.UserGroup
|
|
|
, UserGroupRelation = models.UserGroupRelation
|
|
|
, Config = models.Config
|
|
|
+ , GlobalNotificationSetting = models.GlobalNotificationSetting
|
|
|
+ , GlobalNotificationMailSetting = models.GlobalNotificationMailSetting
|
|
|
+ , GlobalNotificationSlackSetting = models.GlobalNotificationSlackSetting
|
|
|
, PluginUtils = require('../plugins/plugin-utils')
|
|
|
, pluginUtils = new PluginUtils()
|
|
|
, ApiResponse = require('../util/apiResponse')
|
|
|
@@ -188,13 +192,12 @@ module.exports = function(crowi, app) {
|
|
|
|
|
|
// app.get('/admin/notification' , admin.notification.index);
|
|
|
actions.notification = {};
|
|
|
- actions.notification.index = function(req, res) {
|
|
|
- var config = crowi.getConfig();
|
|
|
- var UpdatePost = crowi.model('UpdatePost');
|
|
|
- var slackSetting = Config.setupCofigFormData('notification', config);
|
|
|
- var hasSlackIwhUrl = Config.hasSlackIwhUrl(config);
|
|
|
- var hasSlackToken = Config.hasSlackToken(config);
|
|
|
- var slack = crowi.slack;
|
|
|
+ actions.notification.index = async(req, res) => {
|
|
|
+ const config = crowi.getConfig();
|
|
|
+ const UpdatePost = crowi.model('UpdatePost');
|
|
|
+ let slackSetting = Config.setupCofigFormData('notification', config);
|
|
|
+ const hasSlackIwhUrl = Config.hasSlackIwhUrl(config);
|
|
|
+ const hasSlackToken = Config.hasSlackToken(config);
|
|
|
|
|
|
if (!Config.hasSlackIwhUrl(req.config)) {
|
|
|
slackSetting['slack:incomingWebhookUrl'] = '';
|
|
|
@@ -205,14 +208,15 @@ module.exports = function(crowi, app) {
|
|
|
req.session.slackSetting = null;
|
|
|
}
|
|
|
|
|
|
- UpdatePost.findAll()
|
|
|
- .then(function(settings) {
|
|
|
- return res.render('admin/notification', {
|
|
|
- settings,
|
|
|
- slackSetting,
|
|
|
- hasSlackIwhUrl,
|
|
|
- hasSlackToken,
|
|
|
- });
|
|
|
+ const globalNotifications = await GlobalNotificationSetting.findAll();
|
|
|
+ const userNotifications = await UpdatePost.findAll();
|
|
|
+
|
|
|
+ return res.render('admin/notification', {
|
|
|
+ userNotifications,
|
|
|
+ slackSetting,
|
|
|
+ hasSlackIwhUrl,
|
|
|
+ hasSlackToken,
|
|
|
+ globalNotifications,
|
|
|
});
|
|
|
};
|
|
|
|
|
|
@@ -310,6 +314,84 @@ module.exports = function(crowi, app) {
|
|
|
});
|
|
|
};
|
|
|
|
|
|
+ actions.globalNotification = {};
|
|
|
+ actions.globalNotification.detail = async(req, res) => {
|
|
|
+ const notificationSettingId = req.params.id;
|
|
|
+ let renderVars = {};
|
|
|
+
|
|
|
+ if (notificationSettingId) {
|
|
|
+ try {
|
|
|
+ renderVars.setting = await GlobalNotificationSetting.findOne({_id: notificationSettingId});
|
|
|
+ }
|
|
|
+ catch (err) {
|
|
|
+ logger.error(`Error in finding a global notification setting with {_id: ${notificationSettingId}}`);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return res.render('admin/global-notification-detail', renderVars);
|
|
|
+ };
|
|
|
+
|
|
|
+ actions.globalNotification.create = (req, res) => {
|
|
|
+ const form = req.form.notificationGlobal;
|
|
|
+ let setting;
|
|
|
+
|
|
|
+ switch (form.notifyToType) {
|
|
|
+ case 'mail':
|
|
|
+ setting = new GlobalNotificationMailSetting(crowi);
|
|
|
+ setting.toEmail = form.toEmail;
|
|
|
+ break;
|
|
|
+ // case '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 setting = await GlobalNotificationSetting.findOne({_id: form.id});
|
|
|
+
|
|
|
+ switch (form.notifyToType) {
|
|
|
+ case 'mail':
|
|
|
+ setting.toEmail = form.toEmail;
|
|
|
+ break;
|
|
|
+ // case 'slack':
|
|
|
+ // 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.triggerPath = form.triggerPath;
|
|
|
+ setting.triggerEvents = getNotificationEvents(form);
|
|
|
+ setting.save();
|
|
|
+
|
|
|
+ return res.redirect('/admin/notification#global-notification');
|
|
|
+ };
|
|
|
+
|
|
|
+ const getNotificationEvents = (form) => {
|
|
|
+ let triggerEvents = [];
|
|
|
+ const triggerEventKeys = Object.keys(form).filter(key => key.match(/^triggerEvent/));
|
|
|
+ triggerEventKeys.forEach(key => {
|
|
|
+ if (form[key]) {
|
|
|
+ triggerEvents.push(form[key]);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ return triggerEvents;
|
|
|
+ };
|
|
|
+
|
|
|
actions.search.buildIndex = function(req, res) {
|
|
|
var search = crowi.getSearcher();
|
|
|
if (!search) {
|
|
|
@@ -1083,6 +1165,37 @@ 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.disable(id);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ await GlobalNotificationSetting.enable(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ return res.json(ApiResponse.success());
|
|
|
+ }
|
|
|
+ catch (err) {
|
|
|
+ return res.json(ApiResponse.error());
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ actions.api.removeGlobalNotification = async(req, res) => {
|
|
|
+ const id = req.query.id;
|
|
|
+
|
|
|
+ try {
|
|
|
+ await GlobalNotificationSetting.findOneAndRemove({_id: id});
|
|
|
+ return res.json(ApiResponse.success());
|
|
|
+ }
|
|
|
+ catch (err) {
|
|
|
+ return res.json(ApiResponse.error());
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
/**
|
|
|
* save settings, update config cache, and response json
|
|
|
*
|