-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathmodels.js
109 lines (88 loc) · 2.42 KB
/
models.js
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
var mongoose = require('mongoose'),
config = require('./config/config'),
_ = require('underscore'),
Schema = mongoose.Schema,
stream_node = require('getstream-node');
mongoose.Promise = global.Promise;
var connection = mongoose.connect(config.get('MONGODB_URI'), {
useMongoClient: true,
});
var FeedManager = stream_node.FeedManager;
var StreamMongoose = stream_node.mongoose;
var userSchema = new Schema(
{
username: { type: String, required: true },
avatar_url: { type: String, required: true },
},
{
collection: 'User',
}
);
var User = mongoose.model('User', userSchema);
var itemSchema = new Schema(
{
user: { type: Schema.Types.ObjectId, required: true, ref: 'User' },
image_url: { type: String, required: true },
pin_count: { type: Number, default: 0 },
},
{
collection: 'Item',
}
);
var Item = mongoose.model('Item', itemSchema);
var pinSchema = new Schema(
{
user: { type: Schema.Types.ObjectId, required: true, ref: 'User' },
item: { type: Schema.Types.ObjectId, required: true, ref: 'Item' },
},
{
collection: 'Pin',
}
);
pinSchema.plugin(StreamMongoose.activity);
pinSchema.statics.pathsToPopulate = function() {
return ['user', 'item'];
};
pinSchema.methods.activityForeignId = function() {
return this.user._id + ':' + this.item._id;
};
var Pin = mongoose.model('Pin', pinSchema);
var followSchema = new Schema(
{
user: { type: Schema.Types.ObjectId, required: true, ref: 'User' },
target: { type: Schema.Types.ObjectId, required: true, ref: 'User' },
},
{
collection: 'Follow',
}
);
followSchema.plugin(StreamMongoose.activity);
followSchema.methods.activityNotify = function() {
target_feed = FeedManager.getNotificationFeed(this.target._id);
return [target_feed];
};
followSchema.methods.activityForeignId = function() {
return this.user._id + ':' + this.target._id;
};
followSchema.statics.pathsToPopulate = function() {
return ['user', 'target'];
};
followSchema.post('save', function(doc) {
if (doc.wasNew) {
var userId = doc.user._id || doc.user;
var targetId = doc.target._id || doc.target;
FeedManager.followUser(userId, targetId);
}
});
followSchema.post('remove', function(doc) {
FeedManager.unfollowUser(doc.user, doc.target);
});
var Follow = mongoose.model('Follow', followSchema);
// send the mongoose instance with registered models to StreamMongoose
StreamMongoose.setupMongoose(mongoose);
module.exports = {
User: User,
Item: Item,
Pin: Pin,
Follow: Follow,
};