itizawa 6 лет назад
Родитель
Сommit
2b587030b4

+ 38 - 0
src/client/js/components/Admin/Notification/GlobalNotification.jsx

@@ -2,20 +2,58 @@ import React from 'react';
 import PropTypes from 'prop-types';
 import { withTranslation } from 'react-i18next';
 
+import loggerFactory from '@alias/logger';
+
 import { createSubscribedElement } from '../../UnstatedUtils';
+import { toastSuccess, toastError } from '../../../util/apiNotification';
 
 import AppContainer from '../../../services/AppContainer';
 import AdminNotificationContainer from '../../../services/AdminNotificationContainer';
 import GlobalNotificationList from './GlobalNotificationList';
 
+const logger = loggerFactory('growi:GlobalNotification');
+
 class GlobalNotification extends React.Component {
 
+  constructor() {
+    super();
+
+    this.onClickSubmit = this.onClickSubmit.bind(this);
+  }
+
+  async onClickSubmit() {
+    const { t, adminNotificationContainer } = this.props;
+
+    try {
+      await adminNotificationContainer.updateGlobalNotificationForPages();
+      toastSuccess(t('toaster.update_successed', { target: t('Notification Settings') }));
+    }
+    catch (err) {
+      toastError(err);
+      logger.error(err);
+    }
+  }
+
   render() {
     const { t, adminNotificationContainer } = this.props;
     const { globalNotifications } = adminNotificationContainer.state;
     return (
       <React.Fragment>
 
+        <h2 className="border-bottom mb-5">通知が有効になるページ</h2>
+
+        <div className="row my-3">
+          <div className="col-xs-offset-4 col-xs-5">
+            <button
+              type="button"
+              className="btn btn-primary"
+              onClick={this.onClickSubmit}
+              disabled={adminNotificationContainer.state.retrieveError}
+            >{t('Update')}
+            </button>
+          </div>
+        </div>
+
         <a href="/admin/global-notification/new">
           <p className="btn btn-default">{t('notification_setting.add_notification')}</p>
         </a>

+ 31 - 0
src/client/js/services/AdminNotificationContainer.js

@@ -24,6 +24,8 @@ export default class AdminNotificationContainer extends Container {
       isIncomingWebhookPrioritized: false,
       slackToken: '',
       userNotifications: [],
+      isNotificationOwnerPageEnabled: true,
+      isNotificationGroupPageEnabled: false,
       globalNotifications: [],
     };
 
@@ -49,6 +51,8 @@ export default class AdminNotificationContainer extends Container {
         isIncomingWebhookPrioritized: notificationParams.isIncomingWebhookPrioritized || false,
         slackToken: notificationParams.slackToken || '',
         userNotifications: notificationParams.userNotifications || [],
+        // isNotificationOwnerPageEnabled: notificationParams.isNotificationOwnerPageEnabled,
+        isNotificationGroupPageEnabled: notificationParams.isNotificationGroupPageEnabled,
         globalNotifications: notificationParams.globalNotifications || [],
       });
 
@@ -124,6 +128,33 @@ export default class AdminNotificationContainer extends Container {
     return deletedNotificaton;
   }
 
+  /**
+   * Switch isNotificationOwnerPageEnabled
+   */
+  switchIsNotificationOwnerPageEnabled() {
+    this.setState({ isNotificationOwnerPageEnabled: !this.state.isNotificationOwnerPageEnabled });
+  }
+
+  /**
+   * Switch isNotificationGroupPageEnabled
+   */
+  switchIsNotificationGroupPageEnabled() {
+    this.setState({ isNotificationGroupPageEnabled: !this.state.isNotificationGroupPageEnabled });
+  }
+
+  /**
+   * Update globalNotificationForPages
+   * @memberOf SlackAppConfiguration
+   */
+  async updateGlobalNotificationForPages() {
+    const response = await this.appContainer.apiv3.put('/notification-setting/notify-for-page-grant/', {
+      isNotificationOwnerPageEnabled: this.state.isNotificationOwnerPageEnabled,
+      isNotificationGroupPageEnabled: this.state.isNotificationGroupPageEnabled,
+    });
+
+    return response;
+  }
+
   /**
    * Delete global notification pattern
    */

+ 57 - 0
src/server/routes/apiv3/notification-setting.js

@@ -32,6 +32,10 @@ const validator = {
       return (req.body.notifyToType === 'slack') ? !!value : true;
     }),
   ],
+  notifyForPageGrant: [
+    body('isNotificationOwnerPageEnabled').isBoolean(),
+    body('isNotificationGroupPageEnabled').isBoolean(),
+  ],
 };
 
 /**
@@ -66,6 +70,15 @@ const validator = {
  *          channel:
  *            type: string
  *            description: slack channel name without '#'
+ *      NotifyForPageGrant:
+ *        type: object
+ *        properties:
+ *          isNotificationOwnerPageEnabled:
+ *            type: string
+ *            description: Whether to notify on owner page
+ *          isNotificationGroupPageEnabled:
+ *            type: string
+ *            description: Whether to notify on group page
  *      GlobalNotificationParams:
  *        type: object
  *        properties:
@@ -125,6 +138,8 @@ module.exports = (crowi) => {
       isIncomingWebhookPrioritized: await crowi.configManager.getConfig('notification', 'slack:isIncomingWebhookPrioritized'),
       slackToken: await crowi.configManager.getConfig('notification', 'slack:token'),
       userNotifications: await UpdatePost.findAll(),
+      isNotificationOwnerPageEnabled: await crowi.configManager.getConfig('notification', 'notification:owner-page:isEnabled'),
+      isNotificationGroupPageEnabled: await crowi.configManager.getConfig('notification', 'notification:group-page:isEnabled'),
       globalNotifications: await GlobalNotificationSetting.findAll(),
     };
     return res.apiv3({ notificationParams });
@@ -401,6 +416,48 @@ module.exports = (crowi) => {
 
   });
 
+
+  /**
+   * @swagger
+   *
+   *    /notification-setting/notify-for-page-grant:
+   *      put:
+   *        tags: [NotificationSetting]
+   *        description: Update settings for notify for page grant
+   *        requestBody:
+   *          required: true
+   *          content:
+   *            application/json:
+   *              schema:
+   *                $ref: '#/components/schemas/NotifyForPageGrant'
+   *        responses:
+   *          200:
+   *            description: Succeeded to settings for notify for page grant
+   *            content:
+   *              application/json:
+   *                schema:
+   *                  $ref: '#/components/schemas/NotifyForPageGrant'
+   */
+  router.put('/notify-for-page-grant', loginRequiredStrictly, adminRequired, csrf, validator.notifyForPageGrant, ApiV3FormValidator, async(req, res) => {
+
+    const requestParams = {
+      'notification:owner-page:isEnabled': req.body.isNotificationOwnerPageEnabled,
+      'notification:group-page:isEnabled': req.body.isNotificationGroupPageEnabled,
+    };
+    try {
+      await crowi.configManager.updateConfigsInTheSameNamespace('notification', requestParams);
+      const responseParams = {
+        isNotificationOwnerPageEnabled: await crowi.configManager.getConfig('notification', 'notification:owner-page:isEnabled'),
+        isNotificationGroupPageEnabled: await crowi.configManager.getConfig('notification', 'notification:group-page:isEnabled'),
+      };
+      return res.apiv3({ responseParams });
+    }
+    catch (err) {
+      const msg = 'Error occurred in updating notify for page grant';
+      logger.error('Error', err);
+      return res.apiv3Err(new ErrorV3(msg, 'update-notify-for-page-grant-failed'));
+    }
+  });
   /**
    * @swagger
    *