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 | 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 4x 4x 15x 15x 15x 15x 15x 4x 4x 4x 1x 3x 3x 15x 2x 2x 2x 15x 1x 1x 15x 1x 1x 15x 15x 15x | module.exports = function(crowi) {
// const debug = require('debug')('crowi:models:share')
const mongoose = require('mongoose')
const uuidv4 = require('uuid/v4')
const mongoosePaginate = require('mongoose-paginate')
const ObjectId = mongoose.Schema.Types.ObjectId
const STATUS_ACTIVE = 'active'
const STATUS_INACTIVE = 'inactive'
const shareSchema = new mongoose.Schema({
uuid: { type: String, required: true, index: true, unique: true },
page: { type: ObjectId, ref: 'Page', required: true, index: true },
status: { type: String, default: STATUS_ACTIVE, index: true },
creator: { type: ObjectId, ref: 'User', required: true, index: true },
secretKeyword: String,
createdAt: { type: Date, default: Date.now },
updatedAt: Date,
})
shareSchema.virtual('accesses', {
ref: 'ShareAccess',
localField: '_id',
foreignField: 'share',
})
shareSchema.set('toObject', { virtuals: true })
shareSchema.set('toJSON', { virtuals: true })
shareSchema.plugin(mongoosePaginate)
shareSchema.methods.isActive = function() {
return this.status === STATUS_ACTIVE
}
shareSchema.methods.isInactive = function() {
return this.status === STATUS_INACTIVE
}
shareSchema.methods.isCreator = function(userData) {
this.populate('creator')
const creatorId = this.creator._id.toString()
const userId = userData._id.toString()
return creatorId === userId
}
shareSchema.statics.isExists = async function(query) {
const count = await this.count(query)
return count > 0
}
shareSchema.statics.findShares = async function(query, options = {}) {
const page = options.page || 1
const limit = options.limit || 50
const sort = options.sort || { createdAt: -1 }
const { populateAccesses = false } = options
const optionalDocs = populateAccesses
? [
{
path: 'accesses',
populate: { path: 'tracking' },
options: { sort: { lastAccessedAt: -1 } },
},
]
: []
return this.paginate(query, {
page,
limit,
sort,
populate: [
...optionalDocs,
{
path: 'page',
},
{
path: 'creator',
},
],
})
}
shareSchema.statics.findShare = async function(query, options = {}) {
const Page = crowi.model('Page')
const { populateAccesses = false } = options
const optionalDocs = populateAccesses
? [
{
path: 'accesses',
populate: { path: 'tracking' },
},
]
: []
const shareData = await this.findOne(query)
.findOne(query)
.populate([...optionalDocs, { path: 'page' }, { path: 'creator' }])
.exec()
if (shareData === null) {
const shareNotFoundError = new Error('Share Not Found')
shareNotFoundError.name = 'Crowi:Share:NotFound'
throw shareNotFoundError
}
shareData.page = await Page.populatePageData(shareData.page)
return shareData
}
shareSchema.statics.findShareByUuid = async function(uuid, query, options) {
query = Object.assign({ uuid }, query !== undefined ? query : {})
return this.findShare(query, options)
}
shareSchema.statics.findShareByPageId = async function(pageId, query, options) {
query = Object.assign({ page: pageId }, query !== undefined ? query : {})
return this.findShare(query, options)
}
shareSchema.statics.create = async function(pageId, user) {
const Share = this
const isExists = await Share.isExists({
page: pageId,
status: STATUS_ACTIVE,
})
if (isExists) {
throw new Error('Cannot create new share.')
}
const newShare = new Share({
uuid: uuidv4(),
page: pageId,
creator: user,
createdAt: Date.now(),
updatedAt: Date.now(),
status: STATUS_ACTIVE,
})
return newShare.save()
}
shareSchema.statics.delete = async function(query = {}) {
const Share = this
const defaultQuery = { status: STATUS_ACTIVE }
return Share.findOneAndUpdate({ ...query, ...defaultQuery }, { status: STATUS_INACTIVE }, { new: true }).exec()
}
shareSchema.statics.deleteById = async function(id) {
const Share = this
return Share.delete({ _id: id })
}
shareSchema.statics.deleteByPageId = async function(pageId) {
const Share = this
return Share.delete({ page: pageId })
}
shareSchema.statics.STATUS_ACTIVE = STATUS_ACTIVE
shareSchema.statics.STATUS_INACTIVE = STATUS_INACTIVE
return mongoose.model('Share', shareSchema)
}
|