bookmark.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // disable no-return-await for model functions
  2. /* eslint-disable no-return-await */
  3. module.exports = function(crowi) {
  4. const debug = require('debug')('growi:models:bookmark');
  5. const mongoose = require('mongoose');
  6. const ObjectId = mongoose.Schema.Types.ObjectId;
  7. const bookmarkEvent = crowi.event('bookmark');
  8. let bookmarkSchema = null;
  9. bookmarkSchema = new mongoose.Schema({
  10. page: { type: ObjectId, ref: 'Page', index: true },
  11. user: { type: ObjectId, ref: 'User', index: true },
  12. createdAt: { type: Date, default: Date.now },
  13. });
  14. bookmarkSchema.index({ page: 1, user: 1 }, { unique: true });
  15. bookmarkSchema.statics.countByPageId = async function(pageId) {
  16. return await this.count({ page: pageId });
  17. };
  18. bookmarkSchema.statics.populatePage = async function(bookmarks) {
  19. const Bookmark = this;
  20. const User = crowi.model('User');
  21. return Bookmark.populate(bookmarks, [
  22. { path: 'page' },
  23. { path: 'lastUpdateUser', model: 'User', select: User.USER_PUBLIC_FIELDS },
  24. ]);
  25. };
  26. // bookmark チェック用
  27. bookmarkSchema.statics.findByPageIdAndUserId = function(pageId, userId) {
  28. const Bookmark = this;
  29. return new Promise(((resolve, reject) => {
  30. return Bookmark.findOne({ page: pageId, user: userId }, (err, doc) => {
  31. if (err) {
  32. return reject(err);
  33. }
  34. return resolve(doc);
  35. });
  36. }));
  37. };
  38. /**
  39. * option = {
  40. * limit: Int
  41. * offset: Int
  42. * requestUser: User
  43. * }
  44. */
  45. bookmarkSchema.statics.findByUser = function(user, option) {
  46. const Bookmark = this;
  47. const requestUser = option.requestUser || null;
  48. debug('Finding bookmark with requesting user:', requestUser);
  49. const limit = option.limit || 50;
  50. const offset = option.offset || 0;
  51. const populatePage = option.populatePage || false;
  52. return new Promise(((resolve, reject) => {
  53. Bookmark
  54. .find({ user: user._id })
  55. .sort({ createdAt: -1 })
  56. .skip(offset)
  57. .limit(limit)
  58. .exec((err, bookmarks) => {
  59. if (err) {
  60. return reject(err);
  61. }
  62. if (!populatePage) {
  63. return resolve(bookmarks);
  64. }
  65. return Bookmark.populatePage(bookmarks, requestUser).then(resolve);
  66. });
  67. }));
  68. };
  69. bookmarkSchema.statics.add = async function(page, user) {
  70. const Bookmark = this;
  71. const newBookmark = new Bookmark({ page, user, createdAt: Date.now() });
  72. try {
  73. const bookmark = await newBookmark.save();
  74. bookmarkEvent.emit('create', page._id);
  75. return bookmark;
  76. }
  77. catch (err) {
  78. if (err.code === 11000) {
  79. // duplicate key (dummy response of new object)
  80. return newBookmark;
  81. }
  82. debug('Bookmark.save failed', err);
  83. throw err;
  84. }
  85. };
  86. /**
  87. * Remove bookmark
  88. * used only when removing the page
  89. * @param {string} pageId
  90. */
  91. bookmarkSchema.statics.removeBookmarksByPageId = async function(pageId) {
  92. const Bookmark = this;
  93. try {
  94. const data = await Bookmark.remove({ page: pageId });
  95. bookmarkEvent.emit('delete', pageId);
  96. return data;
  97. }
  98. catch (err) {
  99. debug('Bookmark.remove failed (removeBookmarkByPage)', err);
  100. throw err;
  101. }
  102. };
  103. bookmarkSchema.statics.removeBookmark = async function(page, user) {
  104. const Bookmark = this;
  105. try {
  106. const data = await Bookmark.findOneAndRemove({ page, user });
  107. bookmarkEvent.emit('delete', page);
  108. return data;
  109. }
  110. catch (err) {
  111. debug('Bookmark.findOneAndRemove failed', err);
  112. throw err;
  113. }
  114. };
  115. return mongoose.model('Bookmark', bookmarkSchema);
  116. };