All files / models bookmark.js

18.39% Statements 16/87
0% Branches 0/28
4.55% Functions 1/22
18.39% Lines 16/87

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 18815x 15x 15x 15x 15x 15x         15x   15x                                                 15x                           15x                                                                                               15x                           15x                                                     15x                                     15x                         15x                         15x    
module.exports = function(crowi) {
  const debug = require('debug')('crowi:models:bookmark')
  const mongoose = require('mongoose')
  const ObjectId = mongoose.Schema.Types.ObjectId
  const bookmarkEvent = crowi.event('Bookmark')
  const bookmarkSchema = new mongoose.Schema({
    page: { type: ObjectId, ref: 'Page', index: true },
    user: { type: ObjectId, ref: 'User', index: true },
    createdAt: { type: Date, default: Date.now() },
  })
  bookmarkSchema.index({ page: 1, user: 1 }, { unique: true })
 
  bookmarkSchema.statics.populatePage = function(bookmarks, requestUser) {
    var Bookmark = this
 
    requestUser = requestUser || null
 
    return Bookmark.populate(bookmarks, { path: 'page' })
      .then(function(bookmarks) {
        return Bookmark.populate(bookmarks, { path: 'page.revision', model: 'Revision' })
      })
      .then(function(bookmarks) {
        // hmm...
        bookmarks = bookmarks.filter(function(bookmark) {
          // requestUser を指定しない場合 public のみを返す
          if (requestUser === null) {
            return bookmark.page.isPublic()
          }
 
          return bookmark.page.isGrantedFor(requestUser)
        })
 
        return Bookmark.populate(bookmarks, { path: 'page.revision.author', model: 'User' })
      })
  }
 
  // bookmark チェック用
  bookmarkSchema.statics.findByPageIdAndUserId = function(pageId, userId) {
    var Bookmark = this
 
    return new Promise(function(resolve, reject) {
      return Bookmark.findOne({ page: pageId, user: userId }, function(err, doc) {
        if (err) {
          return reject(err)
        }
 
        return resolve(doc)
      })
    })
  }
 
  bookmarkSchema.statics.findByUserId = function(userId, option) {
    var Bookmark = this
 
    var limit = option.limit || 50
    var offset = option.offset || 0
 
    var finder = new Promise(function(resolve, reject) {
      Bookmark.find({ user: userId })
        .sort({ createdAt: -1 })
        .skip(offset)
        .limit(limit)
        .exec(function(err, bookmarks) {
          if (err) {
            return reject(err)
          }
 
          return Bookmark.populatePage(bookmarks).then(resolve)
        })
    })
 
    var counter = new Promise(function(resolve, reject) {
      Bookmark.count({ user: userId }).exec(function(err, count) {
        if (err) {
          return reject(err)
        }
 
        return resolve(count)
      })
    })
 
    return Promise.all([finder, counter])
      .then(function([bookmarks, count]) {
        return {
          meta: {
            total: count,
            limit: limit,
            offset: offset,
          },
          data: bookmarks,
        }
      })
      .catch(function(err) {
        debug('err', err)
        throw err
      })
  }
 
  // bookmark count
  bookmarkSchema.statics.countByPageId = async function(pageId) {
    const Bookmark = this
    const count = await Bookmark.count({ page: pageId })
 
    return count
  }
 
  /**
   * option = {
   *  limit: Int
   *  offset: Int
   *  requestUser: User
   * }
   */
  bookmarkSchema.statics.findByUser = function(user, option) {
    var Bookmark = this
    var requestUser = option.requestUser || null
 
    var limit = option.limit || 50
    var offset = option.offset || 0
    var populatePage = option.populatePage || false
 
    return new Promise(function(resolve, reject) {
      Bookmark.find({ user: user._id })
        .sort({ createdAt: -1 })
        .skip(offset)
        .limit(limit)
        .exec(function(err, bookmarks) {
          if (err) {
            return reject(err)
          }
 
          if (!populatePage) {
            return resolve(bookmarks)
          }
 
          return Bookmark.populatePage(bookmarks, requestUser).then(resolve)
        })
    })
  }
 
  bookmarkSchema.statics.add = async function(page, user) {
    const Bookmark = this
 
    const newBookmark = new Bookmark({ page, user, createdAt: Date.now() })
 
    try {
      const bookmark = await newBookmark.save()
      bookmarkEvent.emit('create', page._id)
      return bookmark
    } catch (err) {
      if (err.code === 11000) {
        // duplicate key (dummy response of new object)
        return newBookmark
      }
      debug('Bookmark.save failed', err)
      throw err
    }
  }
 
  bookmarkSchema.statics.removeBookmarksByPageId = async function(pageId) {
    const Bookmark = this
 
    try {
      const data = await Bookmark.remove({ page: pageId })
      bookmarkEvent.emit('delete', pageId)
      return data
    } catch (err) {
      debug('Bookmark.remove failed (removeBookmarkByPage)', err)
      throw err
    }
  }
 
  bookmarkSchema.statics.removeBookmark = async function(page, user) {
    const Bookmark = this
 
    try {
      const data = await Bookmark.findOneAndRemove({ page, user })
      bookmarkEvent.emit('delete', page)
      return data
    } catch (err) {
      debug('Bookmark.findOneAndRemove failed', err)
      throw err
    }
  }
 
  return mongoose.model('Bookmark', bookmarkSchema)
}