bookmark.js 3.0 KB

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