Sotaro KARASAWA 10 anni fa
parent
commit
cec287be18

+ 1 - 0
lib/models/index.js

@@ -7,4 +7,5 @@ module.exports = {
   Bookmark: require('./bookmark'),
   Bookmark: require('./bookmark'),
   Comment: require('./comment'),
   Comment: require('./comment'),
   Attachment: require('./attachment'),
   Attachment: require('./attachment'),
+  Notification: require('./notification'),
 };
 };

+ 97 - 0
lib/models/notification.js

@@ -0,0 +1,97 @@
+module.exports = function(crowi) {
+  var debug = require('debug')('crowi:models:notification')
+    , mongoose = require('mongoose')
+    , ObjectId = mongoose.Schema.Types.ObjectId
+  ;
+
+  // TODO: slack 以外の対応
+  notificationSchema = new mongoose.Schema({
+    pathPattern: { type: String, required: true },
+    patternPrefix:  { type: String, required: true },
+    patternPrefix2: { type: String, required: true },
+    channel: { type: String, required: true },
+    provider: { type: String, required: true },
+    creator: { type: ObjectId, ref: 'User', index: true  },
+    createdAt: { type: Date, default: Date.now }
+  });
+
+  function createPrefixesbyPathPattern (pathPattern)
+  {
+    return ['*', '*'];
+  }
+
+  notificationSchema.statics.findSettingsByPath = function(path)
+  {
+    var Notification = this;
+
+    return new Promise(function(resolve, reject) {
+    });
+  };
+
+  notificationSchema.statics.findAll = function(offset)
+  {
+    var Notification = this;
+    var offset = offset || 0;
+
+    return new Promise(function(resolve, reject) {
+      Notification
+        .find()
+        .sort({'createdAt': 1})
+        .populate('creator')
+        .exec(function(err, data) {
+          if (err) {
+            return reject(err);
+          }
+
+          if (data.length < 1) {
+            return resolve([]);
+          }
+
+          return resolve(data);
+        });
+    });
+  };
+
+  notificationSchema.statics.create = function(pathPatter, channel, user)
+  {
+    var Notification = this;
+    var provider = 'slack'; // now slack only
+
+    var notif = new Notification;
+    notif.pathPattern = pathPattern;
+    notif.channel = Notification.nomalizeChannelName(channel);
+    notif.provider = provider;
+    notif.creator = user;
+    notif.createdAt = Date.now();
+
+    return new Promise(function(resolve, reject) {
+      notif.save(function(err, data) {
+        if (err) {
+          debug('Error on saving notification.', err);
+          return reject(err);
+        }
+        debug('notification saved.', data);
+        return resolve(data);
+      });
+    });
+  };
+
+  notificationSchema.statics.remove = function(id)
+  {
+    var Notification = this;
+
+    return new Promise(function(resolve, reject) {
+      Notification.findOneAndRemove({_id: id}, function(err, data) {
+        if (err) {
+          debug('Notification.findOneAndRemove failed', err);
+          return reject(err);
+        }
+
+        return resolve(data);
+      });
+    });
+  };
+
+  return mongoose.model('Attachment', attachmentSchema);
+};
+

+ 0 - 2
lib/routes/admin.js

@@ -72,7 +72,6 @@ module.exports = function(crowi, app) {
     var settingForm;
     var settingForm;
     settingForm = Config.setupCofigFormData('crowi', req.config);
     settingForm = Config.setupCofigFormData('crowi', req.config);
 
 
-    debug('settingForm', settingForm);
     return res.render('admin/app', {
     return res.render('admin/app', {
       settingForm: settingForm,
       settingForm: settingForm,
     });
     });
@@ -95,7 +94,6 @@ module.exports = function(crowi, app) {
       slackSetting['slack:clientId'] = '';
       slackSetting['slack:clientId'] = '';
       slackSetting['slack:clientSecret'] = '';
       slackSetting['slack:clientSecret'] = '';
     } else {
     } else {
-      debug('slack is:', slack);
       slackAuthUrl = slack.getAuthorizeURL();
       slackAuthUrl = slack.getAuthorizeURL();
     }
     }
 
 

+ 52 - 0
lib/views/admin/notification.html

@@ -84,6 +84,58 @@
         </a>
         </a>
         {% endif %}
         {% endif %}
       </div>
       </div>
+
+      <hr>
+
+      <h4>Default Notification Settings for Patterns</h4>
+
+      <table class="table table-bordered">
+        <thead>
+          <th>Pattern</th>
+          <th>Channel</th>
+          <th>Operation</th>
+        </thead>
+        <tbody>
+          <form id="slackNotificationForm">
+          <tr>
+            <td>
+              <input class="form-control" type="text" name="pathPattern" value="" placeholder="e.g. /projects/xxx/MTG/*">
+              <p class="help-block">
+                Path name of wiki. Pattern expression with <code>*</code> can be used.
+              </p>
+            </td>
+            <td>
+              <input class="form-control form-inline" type="text" name="channel" value="" placeholder="e.g. project-xxx">
+              <p class="help-block">
+                Slack channel name. Without <code>#</code>.
+              </p>
+            </td>
+            <td>
+              <input type="submit" value="Add" class="btn btn-primary">
+            </td>
+          </tr>
+          </form>
+
+          {% for notif in notifs %}
+          <tr>
+            <td>
+              {{ notif.pattern }}
+            </td>
+            <td>
+              {{ notif.channel }}
+            </td>
+            <td>
+              <form>
+                <input type="hidden" name="id" value="">
+                <input type="submit" value="Delete" class="btn btn-default">
+              </form>
+            </td>
+          </tr>
+          {% endfor %}
+        </tbody>
+      </table>
+
+
       {% endif %}
       {% endif %}
 
 
       {% if not hasSlackConfig %}
       {% if not hasSlackConfig %}