updatePost.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // disable no-return-await for model functions
  2. /* eslint-disable no-return-await */
  3. /**
  4. * This is the setting for notify to 3rd party tool (like Slack).
  5. */
  6. module.exports = function(crowi) {
  7. const debug = require('debug')('growi:models:updatePost');
  8. const mongoose = require('mongoose');
  9. const ObjectId = mongoose.Schema.Types.ObjectId;
  10. // TODO: slack 以外の対応
  11. const updatePostSchema = new mongoose.Schema({
  12. pathPattern: { type: String, required: true },
  13. patternPrefix: { type: String, required: true },
  14. patternPrefix2: { type: String, required: true },
  15. channel: { type: String, required: true },
  16. provider: { type: String, required: true },
  17. creator: { type: ObjectId, ref: 'User', index: true },
  18. createdAt: { type: Date, default: Date.now },
  19. });
  20. updatePostSchema.statics.normalizeChannelName = function(channel) {
  21. return channel.replace(/(#|,)/g, '');
  22. };
  23. updatePostSchema.statics.createPrefixesByPathPattern = function(pathPattern) {
  24. const patternPrefix = ['*', '*'];
  25. // not begin with slash
  26. if (!pathPattern.match(/^\/.+/)) {
  27. return patternPrefix;
  28. }
  29. const pattern = pathPattern.split('/');
  30. pattern.shift();
  31. if (pattern[0] && pattern[0] !== '*') {
  32. patternPrefix[0] = pattern[0];
  33. }
  34. if (pattern[1] && pattern[1] !== '*') {
  35. patternPrefix[1] = pattern[1];
  36. }
  37. return patternPrefix;
  38. };
  39. updatePostSchema.statics.getRegExpByPattern = function(pattern) {
  40. let reg = pattern;
  41. if (!reg.match(/^\/.*/)) {
  42. reg = `/*${reg}*`;
  43. }
  44. reg = `^${reg}`;
  45. reg = reg.replace(/\//g, '\\/');
  46. reg = reg.replace(/(\*)/g, '.*');
  47. return new RegExp(reg);
  48. };
  49. updatePostSchema.statics.findSettingsByPath = function(path) {
  50. const UpdatePost = this;
  51. const prefixes = UpdatePost.createPrefixesByPathPattern(path);
  52. return new Promise(((resolve, reject) => {
  53. UpdatePost.find({
  54. $or: [
  55. { patternPrefix: prefixes[0], patternPrefix2: prefixes[1] },
  56. { patternPrefix: '*', patternPrefix2: '*' },
  57. { patternPrefix: prefixes[0], patternPrefix2: '*' },
  58. { patternPrefix: '*', patternPrefix2: prefixes[1] },
  59. ],
  60. })
  61. .then((settings) => {
  62. if (settings.length <= 0) {
  63. return resolve(settings);
  64. }
  65. // eslint-disable-next-line no-param-reassign
  66. settings = settings.filter((setting) => {
  67. const patternRegex = UpdatePost.getRegExpByPattern(setting.pathPattern);
  68. return patternRegex.test(path);
  69. });
  70. return resolve(settings);
  71. });
  72. }));
  73. };
  74. updatePostSchema.statics.findAll = function(offset) {
  75. const UpdatePost = this;
  76. // eslint-disable-next-line no-param-reassign
  77. offset = offset || 0;
  78. return new Promise(((resolve, reject) => {
  79. UpdatePost
  80. .find()
  81. .sort({ createdAt: 1 })
  82. .populate('creator')
  83. .exec((err, data) => {
  84. if (err) {
  85. return reject(err);
  86. }
  87. if (data.length < 1) {
  88. return resolve([]);
  89. }
  90. return resolve(data);
  91. });
  92. }));
  93. };
  94. updatePostSchema.statics.create = function(pathPattern, channel, user) {
  95. const UpdatePost = this;
  96. const provider = 'slack'; // now slack only
  97. const prefixes = UpdatePost.createPrefixesByPathPattern(pathPattern);
  98. const notif = new UpdatePost();
  99. notif.pathPattern = pathPattern;
  100. notif.patternPrefix = prefixes[0];
  101. notif.patternPrefix2 = prefixes[1];
  102. notif.channel = UpdatePost.normalizeChannelName(channel);
  103. notif.provider = provider;
  104. notif.creator = user;
  105. notif.createdAt = Date.now();
  106. return new Promise(((resolve, reject) => {
  107. notif.save((err, doc) => {
  108. if (err) {
  109. return reject(err);
  110. }
  111. return resolve(doc);
  112. });
  113. }));
  114. };
  115. updatePostSchema.statics.remove = function(id) {
  116. const UpdatePost = this;
  117. return new Promise(((resolve, reject) => {
  118. UpdatePost.findOneAndRemove({ _id: id }, (err, data) => {
  119. if (err) {
  120. debug('UpdatePost.findOneAndRemove failed', err);
  121. return reject(err);
  122. }
  123. return resolve(data);
  124. });
  125. }));
  126. };
  127. return mongoose.model('UpdatePost', updatePostSchema);
  128. };