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 | 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 3x 3x 3x 3x 3x 15x 2x 15x 9x 9x 15x 9x 9x 15x 15x 15x | module.exports = function(crowi) {
// const debug = require('debug')('crowi:models:watcher')
const mongoose = require('mongoose')
const ObjectId = mongoose.Schema.Types.ObjectId
const ActivityDefine = require('../util/activityDefine')()
const STATUS_WATCH = 'WATCH'
const STATUS_IGNORE = 'IGNORE'
const STATUSES = [STATUS_WATCH, STATUS_IGNORE]
const watcherSchema = new mongoose.Schema({
user: {
type: ObjectId,
ref: 'User',
index: true,
required: true,
},
targetModel: {
type: String,
require: true,
enum: ActivityDefine.getSupportTargetModelNames(),
},
target: {
type: ObjectId,
refPath: 'targetModel',
require: true,
},
status: {
type: String,
require: true,
enum: STATUSES,
},
createdAt: { type: Date, default: Date.now },
})
watcherSchema.methods.isWatching = function() {
return this.status === STATUS_WATCH
}
watcherSchema.methods.isIgnoring = function() {
return this.status === STATUS_IGNORE
}
watcherSchema.statics.findByUserIdAndTargetId = function(userId, targetId) {
return this.findOne({ user: userId, target: targetId })
}
watcherSchema.statics.upsertWatcher = function(user, targetModel, target, status) {
const Watcher = crowi.model('Watcher')
const query = { user, targetModel, target }
const doc = { ...query, status }
const options = { upsert: true, new: true, setDefaultsOnInsert: true, runValidators: true }
return Watcher.findOneAndUpdate(query, doc, options)
}
watcherSchema.statics.watchByPageId = function(user, pageId, status) {
return this.upsertWatcher(user, 'Page', pageId, status)
}
watcherSchema.statics.getWatchers = async function(target) {
const Watcher = crowi.model('Watcher')
return Watcher.find({ target, status: STATUS_WATCH }).distinct('user')
}
watcherSchema.statics.getIgnorers = async function(target) {
const Watcher = crowi.model('Watcher')
return Watcher.find({ target, status: STATUS_IGNORE }).distinct('user')
}
watcherSchema.statics.STATUS_WATCH = STATUS_WATCH
watcherSchema.statics.STATUS_IGNORE = STATUS_IGNORE
return mongoose.model('Watcher', watcherSchema)
}
|