bookmark.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* eslint-disable no-return-await */
  2. const debug = require('debug')('growi:models:bookmark');
  3. const mongoose = require('mongoose');
  4. const mongoosePaginate = require('mongoose-paginate-v2');
  5. const uniqueValidator = require('mongoose-unique-validator');
  6. const ObjectId = mongoose.Schema.Types.ObjectId;
  7. module.exports = function(crowi) {
  8. const bookmarkEvent = crowi.event('bookmark');
  9. let bookmarkSchema = null;
  10. bookmarkSchema = new mongoose.Schema({
  11. page: { type: ObjectId, ref: 'Page', index: true },
  12. user: { type: ObjectId, ref: 'User', index: true },
  13. }, {
  14. timestamps: { createdAt: true, updatedAt: false },
  15. });
  16. bookmarkSchema.index({ page: 1, user: 1 }, { unique: true });
  17. bookmarkSchema.plugin(mongoosePaginate);
  18. bookmarkSchema.plugin(uniqueValidator);
  19. bookmarkSchema.statics.countByPageId = async function(pageId) {
  20. return await this.count({ page: pageId });
  21. };
  22. /**
  23. * @return {object} key: page._id, value: bookmark count
  24. */
  25. bookmarkSchema.statics.getPageIdToCountMap = async function(pageIds) {
  26. const results = await this.aggregate()
  27. .match({ page: { $in: pageIds } })
  28. .group({ _id: '$page', count: { $sum: 1 } });
  29. // convert to map
  30. const idToCountMap = {};
  31. results.forEach((result) => {
  32. idToCountMap[result._id] = result.count;
  33. });
  34. return idToCountMap;
  35. };
  36. // bookmark チェック用
  37. bookmarkSchema.statics.findByPageIdAndUserId = function(pageId, userId) {
  38. const Bookmark = this;
  39. return new Promise(((resolve, reject) => {
  40. return Bookmark.findOne({ page: pageId, user: userId }, (err, doc) => {
  41. if (err) {
  42. return reject(err);
  43. }
  44. return resolve(doc);
  45. });
  46. }));
  47. };
  48. bookmarkSchema.statics.add = async function(page, user) {
  49. const Bookmark = this;
  50. const newBookmark = new Bookmark({ page, user });
  51. try {
  52. const bookmark = await newBookmark.save();
  53. bookmarkEvent.emit('create', page._id);
  54. return bookmark;
  55. }
  56. catch (err) {
  57. if (err.code === 11000) {
  58. // duplicate key (dummy response of new object)
  59. return newBookmark;
  60. }
  61. debug('Bookmark.save failed', err);
  62. throw err;
  63. }
  64. };
  65. /**
  66. * Remove bookmark
  67. * used only when removing the page
  68. * @param {string} pageId
  69. */
  70. bookmarkSchema.statics.removeBookmarksByPageId = async function(pageId) {
  71. const Bookmark = this;
  72. try {
  73. const data = await Bookmark.remove({ page: pageId });
  74. bookmarkEvent.emit('delete', pageId);
  75. return data;
  76. }
  77. catch (err) {
  78. debug('Bookmark.remove failed (removeBookmarkByPage)', err);
  79. throw err;
  80. }
  81. };
  82. bookmarkSchema.statics.removeBookmark = async function(pageId, user) {
  83. const Bookmark = this;
  84. try {
  85. const data = await Bookmark.findOneAndRemove({ page: pageId, user });
  86. bookmarkEvent.emit('delete', pageId);
  87. return data;
  88. }
  89. catch (err) {
  90. debug('Bookmark.findOneAndRemove failed', err);
  91. throw err;
  92. }
  93. };
  94. return mongoose.model('Bookmark', bookmarkSchema);
  95. };