notification.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. module.exports = function(crowi) {
  2. var debug = require('debug')('crowi:models:notification')
  3. , mongoose = require('mongoose')
  4. , ObjectId = mongoose.Schema.Types.ObjectId
  5. ;
  6. // TODO: slack 以外の対応
  7. notificationSchema = new mongoose.Schema({
  8. pathPattern: { type: String, required: true },
  9. patternPrefix: { type: String, required: true },
  10. patternPrefix2: { type: String, required: true },
  11. channel: { type: String, required: true },
  12. provider: { type: String, required: true },
  13. creator: { type: ObjectId, ref: 'User', index: true },
  14. createdAt: { type: Date, default: Date.now }
  15. });
  16. function createPrefixesbyPathPattern (pathPattern)
  17. {
  18. return ['*', '*'];
  19. }
  20. notificationSchema.statics.findSettingsByPath = function(path)
  21. {
  22. var Notification = this;
  23. return new Promise(function(resolve, reject) {
  24. });
  25. };
  26. notificationSchema.statics.findAll = function(offset)
  27. {
  28. var Notification = this;
  29. var offset = offset || 0;
  30. return new Promise(function(resolve, reject) {
  31. Notification
  32. .find()
  33. .sort({'createdAt': 1})
  34. .populate('creator')
  35. .exec(function(err, data) {
  36. if (err) {
  37. return reject(err);
  38. }
  39. if (data.length < 1) {
  40. return resolve([]);
  41. }
  42. return resolve(data);
  43. });
  44. });
  45. };
  46. notificationSchema.statics.create = function(pathPatter, channel, user)
  47. {
  48. var Notification = this;
  49. var provider = 'slack'; // now slack only
  50. var notif = new Notification;
  51. notif.pathPattern = pathPattern;
  52. notif.channel = Notification.nomalizeChannelName(channel);
  53. notif.provider = provider;
  54. notif.creator = user;
  55. notif.createdAt = Date.now();
  56. return new Promise(function(resolve, reject) {
  57. notif.save(function(err, data) {
  58. if (err) {
  59. debug('Error on saving notification.', err);
  60. return reject(err);
  61. }
  62. debug('notification saved.', data);
  63. return resolve(data);
  64. });
  65. });
  66. };
  67. notificationSchema.statics.remove = function(id)
  68. {
  69. var Notification = this;
  70. return new Promise(function(resolve, reject) {
  71. Notification.findOneAndRemove({_id: id}, function(err, data) {
  72. if (err) {
  73. debug('Notification.findOneAndRemove failed', err);
  74. return reject(err);
  75. }
  76. return resolve(data);
  77. });
  78. });
  79. };
  80. return mongoose.model('Attachment', attachmentSchema);
  81. };