-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodels.js
104 lines (86 loc) · 2.29 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
/*
* CONFIG COLLECTION
*/
configDb = new Meteor.Collection('configDb');
configDb.initConfig = function(key){
var config = configDb.findOne({key:key});
if(!config){
configDb.insert({
key:key
});
}
};
configDb.getConfig = function(key){
var config = configDb.findOne({key:key});
return config ? config.value : null;
};
configDb.setConfig = function(key, value){
configDb.initConfig(key);
configDb.update({key:key},{key:key,value:value});
};
/*
* VIDEO COLLECTION
*/
videos = new Meteor.Collection('videos');
videos.getLastVideosAdded = function(uId){
var plIds = _.map(playlists.find({'followers':uId}).fetch(), function(pl){
return pl._id;
});
return videos.find({playlist:{$in:plIds},owner:{$not:{$in:[uId]}}}, {sort:{createdAt:-1},limit:10});
};
videos.isOwner = function(video, user){
if(!user) user = Meteor.user();
return video
&& user
&& video.owner
&& user._id
&& video.owner === user._id;
};
/*
* PLAYLIST COLLECTION
*/
playlists = new Meteor.Collection('playlists');
playlists.isOwner = function(playlist, user){
if(!user) user = Meteor.user();
return playlist
&& user
&& playlist.owner
&& user._id
&& playlist.owner === user._id;
};
playlists.canAccess = function(playlist, user){
if(!user) user = Meteor.user();
if(!user) return false;
var pos = _.find(playlist.followers, function(u){
return u === user._id;
});
return !_.isUndefined(pos);
};
playlists.canAddVideo = function(playlist, user){
if(!user) user = Meteor.user();
if(!user) return false;
return playlists.isOwner(playlist, user) || playlist && playlist.privacy && playlist.privacy === 'public';
};
playlists.canRemoveVideo = function(playlist, video, user){
if(!user) user = Meteor.user();
if(!user) return false;
return playlists.isOwner(playlist, user) || videos.isOwner(video, user);
};
playlists.updatePlaylistThumbnails = function(playlist){
var v = videos.find({playlist:playlist}, {limit:4}).fetch()
, thumbnails = []
;
for(var i in v){
thumbnails.push(v[i].data.thumbnail_url);
}
playlists.update({_id:playlist},{
$set:{thumbnails:thumbnails}
})
};
playlists.updateVideoCount = function(pl){
playlists.update({_id:pl}, {
$set:{
nbVideos:videos.find({playlist:pl}).count()
}
});
};