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 188 189 190 191 | 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 11x 11x 11x 11x 11x 11x 11x 10x 10x 10x 1x 1x 15x 1x 1x 1x 1x 1x 1x 1x 1x 15x 1x 1x 15x 1x 1x 1x 1x 1x 15x 1x 1x 1x 1x 1x 1x 1x 1x 15x 2x 2x 2x 2x 2x 15x 11x 11x 15x 15x 15x 15x | module.exports = function(crowi) { 'use strict' const debug = require('debug')('crowi:models:notification') const mongoose = require('mongoose') const ObjectId = mongoose.Schema.Types.ObjectId const ActivityDefine = require('../util/activityDefine')() const STATUS_UNREAD = 'UNREAD' const STATUS_UNOPENED = 'UNOPENED' const STATUS_OPENED = 'OPENED' const STATUSES = [STATUS_UNREAD, STATUS_UNOPENED, STATUS_OPENED] const notificationEvent = crowi.event('Notification') const notificationSchema = new mongoose.Schema({ user: { type: ObjectId, ref: 'User', index: true, require: true, }, targetModel: { type: String, require: true, enum: ActivityDefine.getSupportTargetModelNames(), }, target: { type: ObjectId, refPath: 'targetModel', require: true, }, action: { type: String, require: true, enum: ActivityDefine.getSupportActionNames(), }, activities: [ { type: ObjectId, ref: 'Activity', }, ], status: { type: String, default: STATUS_UNREAD, enum: STATUSES, index: true, require: true, }, createdAt: { type: Date, default: Date.now, }, }) notificationSchema.virtual('actionUsers').get(function() { const Activity = crowi.model('Activity') return Activity.getActionUsersFromActivities(this.activities) }) const transform = (doc, ret) => { delete ret.activities } notificationSchema.set('toObject', { virtuals: true, transform }) notificationSchema.set('toJSON', { virtuals: true, transform }) notificationSchema.index({ user: 1, target: 1, action: 1, createdAt: 1 }, { unique: true }) notificationSchema.statics.findLatestNotificationsByUser = function(user, limit, offset) { const Notification = this limit = limit || 10 return new Promise(function(resolve, reject) { Notification.find({ user: user }) .sort({ createdAt: -1 }) .skip(offset) .limit(limit) .populate(['user', 'target']) .populate({ path: 'activities', populate: { path: 'user' } }) .exec(function(err, notifications) { if (err) { reject(err) } // debug('notifications', notifications); resolve(notifications) }) }) } notificationSchema.statics.upsertByActivity = async function(user, sameActivities, activity) { const Notification = this const { targetModel, target, action } = activity const query = { user, target, action } const parameters = { user, targetModel, target, action, activities: sameActivities, status: STATUS_UNREAD, createdAt: Date.now(), } const options = { upsert: true, new: true, setDefaultsOnInsert: true, runValidators: true, } try { const result = await Notification.findOneAndUpdate(query, parameters, options) Eif (result) { notificationEvent.emit('update', result.user) } return result } catch (err) { debug(err) throw new Error(err.message) } } notificationSchema.statics.removeActivity = async function(activity) { const Notification = this const { _id, target, action } = activity const query = { target, action } const parameters = { $pull: { activities: _id } } const options = { multi: true } const result = await Notification.update(query, parameters, options) await Notification.removeEmpty() return result } notificationSchema.statics.removeEmpty = function() { const Notification = this return Notification.remove({ activities: { $size: 0 } }) } notificationSchema.statics.read = async function(user) { const Notification = this const query = { user, status: STATUS_UNREAD } const parameters = { status: STATUS_UNOPENED } const options = { multi: true } return Notification.update(query, parameters, options) } notificationSchema.statics.open = async function(user, id) { const Notification = this const query = { _id: id, user: user._id } const parameters = { status: STATUS_OPENED } const options = { new: true } const notification = await Notification.findOneAndUpdate(query, parameters, options) Eif (notification !== undefined) { notificationEvent.emit('update', notification.user) } return notification } notificationSchema.statics.getUnreadCountByUser = async function(user) { const Notification = this const query = { user, status: STATUS_UNREAD } try { const count = await Notification.count(query) return count } catch (err) { debug('Error on getUnreadCountByUser', err) throw err } } notificationEvent.on('update', user => { const io = crowi.getIo() Iif (io) { io.sockets.emit('notification updated', { user }) } }) notificationSchema.statics.STATUS_UNOPENED = STATUS_UNOPENED notificationSchema.statics.STATUS_UNREAD = STATUS_UNREAD notificationSchema.statics.STATUS_OPENED = STATUS_OPENED return mongoose.model('Notification', notificationSchema) } |