-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotificationModel.js
More file actions
91 lines (73 loc) · 2.29 KB
/
notificationModel.js
File metadata and controls
91 lines (73 loc) · 2.29 KB
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
const { Schema, models, model } = require('mongoose')
/* Show notification when user:
. follow user
. replyTo a tweet
. retweet a tweet
. like a tweet
*/
// notificationType: 'Tweet', // notification for 'tweet
// entityId: updatedTweet._id, // on which notification user liked ?
// userFrom: userId, // Who liked it ?
// userTo: updatedTweet.user._id, // this tweet created by whom ?
/*
{
entityId: '',
userFrom: '',
userTo: '',
type: 'like', // ['like', 'retweet', 'replyTo', 'follow', 'new-message']
kind: 'tweet', // ['user', 'tweet', 'message' ]
isOpened: false
}
*/
const notificationSchema = new Schema({
entityId: {
type: Schema.Types.ObjectId, // On which task notification applied on
required: true,
},
userFrom: {
type: Schema.Types.ObjectId, // Who create notification
ref: 'User',
required: true
},
userTo: {
type: Schema.Types.ObjectId, // on which user's task notification apploed on
ref: 'User',
required: true
},
type: { // for which task this notification this
type: String,
enum: ['like', 'retweet', 'replyTo', 'follow', 'new-message'],
required: true,
},
kind: { // what kind of notification is this
type: String,
enum: ['user', 'tweet', 'message' ],
required: true,
},
isOpened: { // To check it notification clicked: seen or unseen
type: Boolean,
default: false
}
}, { timestamps: true })
/*
// GET /api/tweets/:id/retweet
...
if(!deletedTweet) {
await Notification.insertNotification({
entityId: updatedTweet._id, // on which notification user liked ?
userFrom: userId, // Who liked it ?
userTo: updatedTweet.user._id, // which user create this tweet ?
type: 'retweet', // ['like', 'retweet', 'replyTo', 'follow']
kind: 'tweet', // ['tweet', 'message' ]
})
}
*/
notificationSchema.statics.insertNotification = async function ( data ) {
await this.deleteOne(data) // if already exist one, then delete that before create new one
return this.create(data)
}
notificationSchema.pre(/^find/, function(next) {
this.populate('userFrom userTo')
next()
})
module.exports = models.Notification || model('Notification', notificationSchema)