aboutsummaryrefslogtreecommitdiffhomepage
path: root/models
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2016-02-07 11:23:23 +0100
committerChocobozzz <florian.bigard@gmail.com>2016-02-07 11:23:23 +0100
commit9f10b2928df655c3672d9607e864e667d4bc903a (patch)
tree7743911b974b3a7fb0d4c7cec2a723942466b7f1 /models
parentd7c01e7793d813d804a3b5716d8288f9dcf71a16 (diff)
downloadPeerTube-9f10b2928df655c3672d9607e864e667d4bc903a.tar.gz
PeerTube-9f10b2928df655c3672d9607e864e667d4bc903a.tar.zst
PeerTube-9f10b2928df655c3672d9607e864e667d4bc903a.zip
Remove useless anonymous functions of files
Diffstat (limited to 'models')
-rw-r--r--models/pods.js152
-rw-r--r--models/poolRequests.js84
-rw-r--r--models/videos.js394
3 files changed, 312 insertions, 318 deletions
diff --git a/models/pods.js b/models/pods.js
index 04fd042c3..57ed20292 100644
--- a/models/pods.js
+++ b/models/pods.js
@@ -1,90 +1,88 @@
1;(function () { 1'use strict'
2 'use strict' 2
3 3var mongoose = require('mongoose')
4 var mongoose = require('mongoose') 4
5 5var constants = require('../initializers/constants')
6 var constants = require('../initializers/constants') 6var logger = require('../helpers/logger')
7 var logger = require('../helpers/logger') 7
8 8// ---------------------------------------------------------------------------
9 // --------------------------------------------------------------------------- 9
10 10var podsSchema = mongoose.Schema({
11 var podsSchema = mongoose.Schema({ 11 url: String,
12 url: String, 12 publicKey: String,
13 publicKey: String, 13 score: { type: Number, max: constants.FRIEND_BASE_SCORE }
14 score: { type: Number, max: constants.FRIEND_BASE_SCORE } 14})
15 }) 15var PodsDB = mongoose.model('pods', podsSchema)
16 var PodsDB = mongoose.model('pods', podsSchema) 16
17 17// ---------------------------------------------------------------------------
18 // --------------------------------------------------------------------------- 18
19 19var Pods = {
20 var Pods = { 20 add: add,
21 add: add, 21 count: count,
22 count: count, 22 findByUrl: findByUrl,
23 findByUrl: findByUrl, 23 findBadPods: findBadPods,
24 findBadPods: findBadPods, 24 incrementScores: incrementScores,
25 incrementScores: incrementScores, 25 list: list,
26 list: list, 26 remove: remove,
27 remove: remove, 27 removeAll: removeAll,
28 removeAll: removeAll, 28 removeAllByIds: removeAllByIds
29 removeAllByIds: removeAllByIds 29}
30
31// TODO: check if the pod is not already a friend
32function add (data, callback) {
33 if (!callback) callback = function () {}
34 var params = {
35 url: data.url,
36 publicKey: data.publicKey,
37 score: constants.FRIEND_BASE_SCORE
30 } 38 }
31 39
32 // TODO: check if the pod is not already a friend 40 PodsDB.create(params, callback)
33 function add (data, callback) { 41}
34 if (!callback) callback = function () {}
35 var params = {
36 url: data.url,
37 publicKey: data.publicKey,
38 score: constants.FRIEND_BASE_SCORE
39 }
40
41 PodsDB.create(params, callback)
42 }
43 42
44 function count (callback) { 43function count (callback) {
45 return PodsDB.count(callback) 44 return PodsDB.count(callback)
46 } 45}
47 46
48 function findBadPods (callback) { 47function findBadPods (callback) {
49 PodsDB.find({ score: 0 }, callback) 48 PodsDB.find({ score: 0 }, callback)
50 } 49}
51 50
52 function findByUrl (url, callback) { 51function findByUrl (url, callback) {
53 PodsDB.findOne({ url: url }, callback) 52 PodsDB.findOne({ url: url }, callback)
54 } 53}
55 54
56 function incrementScores (ids, value, callback) { 55function incrementScores (ids, value, callback) {
57 if (!callback) callback = function () {} 56 if (!callback) callback = function () {}
58 PodsDB.update({ _id: { $in: ids } }, { $inc: { score: value } }, { multi: true }, callback) 57 PodsDB.update({ _id: { $in: ids } }, { $inc: { score: value } }, { multi: true }, callback)
59 } 58}
60 59
61 function list (callback) { 60function list (callback) {
62 PodsDB.find(function (err, pods_list) { 61 PodsDB.find(function (err, pods_list) {
63 if (err) { 62 if (err) {
64 logger.error('Cannot get the list of the pods.') 63 logger.error('Cannot get the list of the pods.')
65 return callback(err) 64 return callback(err)
66 } 65 }
67 66
68 return callback(null, pods_list) 67 return callback(null, pods_list)
69 }) 68 })
70 } 69}
71 70
72 function remove (url, callback) { 71function remove (url, callback) {
73 if (!callback) callback = function () {} 72 if (!callback) callback = function () {}
74 PodsDB.remove({ url: url }, callback) 73 PodsDB.remove({ url: url }, callback)
75 } 74}
76 75
77 function removeAll (callback) { 76function removeAll (callback) {
78 if (!callback) callback = function () {} 77 if (!callback) callback = function () {}
79 PodsDB.remove(callback) 78 PodsDB.remove(callback)
80 } 79}
81 80
82 function removeAllByIds (ids, callback) { 81function removeAllByIds (ids, callback) {
83 if (!callback) callback = function () {} 82 if (!callback) callback = function () {}
84 PodsDB.remove({ _id: { $in: ids } }, callback) 83 PodsDB.remove({ _id: { $in: ids } }, callback)
85 } 84}
86 85
87 // --------------------------------------------------------------------------- 86// ---------------------------------------------------------------------------
88 87
89 module.exports = Pods 88module.exports = Pods
90})()
diff --git a/models/poolRequests.js b/models/poolRequests.js
index 5070a1331..970315597 100644
--- a/models/poolRequests.js
+++ b/models/poolRequests.js
@@ -1,57 +1,55 @@
1;(function () { 1'use strict'
2 'use strict'
3 2
4 var mongoose = require('mongoose') 3var mongoose = require('mongoose')
5 4
6 var logger = require('../helpers/logger') 5var logger = require('../helpers/logger')
7 6
8 // --------------------------------------------------------------------------- 7// ---------------------------------------------------------------------------
9 8
10 var poolRequestsSchema = mongoose.Schema({ 9var poolRequestsSchema = mongoose.Schema({
11 type: String, 10 type: String,
12 id: String, // Special id to find duplicates (video created we want to remove...) 11 id: String, // Special id to find duplicates (video created we want to remove...)
13 request: mongoose.Schema.Types.Mixed 12 request: mongoose.Schema.Types.Mixed
14 }) 13})
15 var PoolRequestsDB = mongoose.model('poolRequests', poolRequestsSchema) 14var PoolRequestsDB = mongoose.model('poolRequests', poolRequestsSchema)
16 15
17 // --------------------------------------------------------------------------- 16// ---------------------------------------------------------------------------
18 17
19 var PoolRequests = { 18var PoolRequests = {
20 create: create, 19 create: create,
21 findById: findById, 20 findById: findById,
22 list: list, 21 list: list,
23 removeRequestById: removeRequestById, 22 removeRequestById: removeRequestById,
24 removeRequests: removeRequests 23 removeRequests: removeRequests
25 } 24}
26 25
27 function create (id, type, request, callback) { 26function create (id, type, request, callback) {
28 PoolRequestsDB.create({ id: id, type: type, request: request }, callback) 27 PoolRequestsDB.create({ id: id, type: type, request: request }, callback)
29 } 28}
30 29
31 function findById (id, callback) { 30function findById (id, callback) {
32 PoolRequestsDB.findOne({ id: id }, callback) 31 PoolRequestsDB.findOne({ id: id }, callback)
33 } 32}
34 33
35 function list (callback) { 34function list (callback) {
36 PoolRequestsDB.find({}, { _id: 1, type: 1, request: 1 }, callback) 35 PoolRequestsDB.find({}, { _id: 1, type: 1, request: 1 }, callback)
37 } 36}
38 37
39 function removeRequestById (id, callback) { 38function removeRequestById (id, callback) {
40 PoolRequestsDB.remove({ id: id }, callback) 39 PoolRequestsDB.remove({ id: id }, callback)
41 } 40}
42 41
43 function removeRequests (ids) { 42function removeRequests (ids) {
44 PoolRequestsDB.remove({ _id: { $in: ids } }, function (err) { 43 PoolRequestsDB.remove({ _id: { $in: ids } }, function (err) {
45 if (err) { 44 if (err) {
46 logger.error('Cannot remove requests from the pool requests database.', { error: err }) 45 logger.error('Cannot remove requests from the pool requests database.', { error: err })
47 return // Abort 46 return // Abort
48 } 47 }
49 48
50 logger.info('Pool requests flushed.') 49 logger.info('Pool requests flushed.')
51 }) 50 })
52 } 51}
53 52
54 // --------------------------------------------------------------------------- 53// ---------------------------------------------------------------------------
55 54
56 module.exports = PoolRequests 55module.exports = PoolRequests
57})()
diff --git a/models/videos.js b/models/videos.js
index ea1823e27..4da389c24 100644
--- a/models/videos.js
+++ b/models/videos.js
@@ -1,237 +1,235 @@
1;(function () { 1'use strict'
2 'use strict' 2
3 3var async = require('async')
4 var async = require('async') 4var config = require('config')
5 var config = require('config') 5var dz = require('dezalgo')
6 var dz = require('dezalgo') 6var fs = require('fs')
7 var fs = require('fs') 7var mongoose = require('mongoose')
8 var mongoose = require('mongoose') 8var path = require('path')
9 var path = require('path') 9
10 10var logger = require('../helpers/logger')
11 var logger = require('../helpers/logger') 11
12 12var http = config.get('webserver.https') === true ? 'https' : 'http'
13 var http = config.get('webserver.https') === true ? 'https' : 'http' 13var host = config.get('webserver.host')
14 var host = config.get('webserver.host') 14var port = config.get('webserver.port')
15 var port = config.get('webserver.port') 15var uploadDir = path.join(__dirname, '..', config.get('storage.uploads'))
16 var uploadDir = path.join(__dirname, '..', config.get('storage.uploads')) 16
17 17// ---------------------------------------------------------------------------
18 // --------------------------------------------------------------------------- 18
19 19var videosSchema = mongoose.Schema({
20 var videosSchema = mongoose.Schema({ 20 name: String,
21 name: String, 21 namePath: String,
22 namePath: String, 22 description: String,
23 description: String, 23 magnetUri: String,
24 magnetUri: String, 24 podUrl: String
25 podUrl: String 25})
26var VideosDB = mongoose.model('videos', videosSchema)
27
28// ---------------------------------------------------------------------------
29
30var Videos = {
31 add: add,
32 addRemotes: addRemotes,
33 get: get,
34 getVideoState: getVideoState,
35 isOwned: isOwned,
36 list: list,
37 listOwned: listOwned,
38 removeOwned: removeOwned,
39 removeAllRemotes: removeAllRemotes,
40 removeAllRemotesOf: removeAllRemotesOf,
41 removeRemotesOfByMagnetUris: removeRemotesOfByMagnetUris,
42 search: search
43}
44
45function add (video, callback) {
46 logger.info('Adding %s video to database.', video.name)
47
48 var params = video
49 params.podUrl = http + '://' + host + ':' + port
50
51 VideosDB.create(params, function (err, video) {
52 if (err) {
53 logger.error('Cannot insert this video into database.')
54 return callback(err)
55 }
56
57 callback(null)
26 }) 58 })
27 var VideosDB = mongoose.model('videos', videosSchema) 59}
28
29 // ---------------------------------------------------------------------------
30
31 var Videos = {
32 add: add,
33 addRemotes: addRemotes,
34 get: get,
35 getVideoState: getVideoState,
36 isOwned: isOwned,
37 list: list,
38 listOwned: listOwned,
39 removeOwned: removeOwned,
40 removeAllRemotes: removeAllRemotes,
41 removeAllRemotesOf: removeAllRemotesOf,
42 removeRemotesOfByMagnetUris: removeRemotesOfByMagnetUris,
43 search: search
44 }
45
46 function add (video, callback) {
47 logger.info('Adding %s video to database.', video.name)
48
49 var params = video
50 params.podUrl = http + '://' + host + ':' + port
51
52 VideosDB.create(params, function (err, video) {
53 if (err) {
54 logger.error('Cannot insert this video into database.')
55 return callback(err)
56 }
57
58 callback(null)
59 })
60 }
61 60
62 // TODO: avoid doublons 61// TODO: avoid doublons
63 function addRemotes (videos, callback) { 62function addRemotes (videos, callback) {
64 if (!callback) callback = function () {} 63 if (!callback) callback = function () {}
65 64
66 var to_add = [] 65 var to_add = []
67 66
68 async.each(videos, function (video, callback_each) { 67 async.each(videos, function (video, callback_each) {
69 callback_each = dz(callback_each) 68 callback_each = dz(callback_each)
70 logger.debug('Add remote video from pod: %s', video.podUrl) 69 logger.debug('Add remote video from pod: %s', video.podUrl)
71
72 var params = {
73 name: video.name,
74 namePath: null,
75 description: video.description,
76 magnetUri: video.magnetUri,
77 podUrl: video.podUrl
78 }
79 70
80 to_add.push(params) 71 var params = {
72 name: video.name,
73 namePath: null,
74 description: video.description,
75 magnetUri: video.magnetUri,
76 podUrl: video.podUrl
77 }
81 78
82 callback_each() 79 to_add.push(params)
83 }, function () {
84 VideosDB.create(to_add, function (err, videos) {
85 if (err) {
86 logger.error('Cannot insert this remote video.')
87 return callback(err)
88 }
89
90 return callback(null, videos)
91 })
92 })
93 }
94 80
95 function get (id, callback) { 81 callback_each()
96 VideosDB.findById(id, function (err, video) { 82 }, function () {
83 VideosDB.create(to_add, function (err, videos) {
97 if (err) { 84 if (err) {
98 logger.error('Cannot get this video.') 85 logger.error('Cannot insert this remote video.')
99 return callback(err) 86 return callback(err)
100 } 87 }
101 88
102 return callback(null, video) 89 return callback(null, videos)
103 }) 90 })
104 } 91 })
92}
105 93
106 function getVideoState (id, callback) { 94function get (id, callback) {
107 get(id, function (err, video) { 95 VideosDB.findById(id, function (err, video) {
108 if (err) return callback(err) 96 if (err) {
97 logger.error('Cannot get this video.')
98 return callback(err)
99 }
109 100
110 var exist = (video !== null) 101 return callback(null, video)
111 var owned = false 102 })
112 if (exist === true) { 103}
113 owned = (video.namePath !== null)
114 }
115 104
116 return callback(null, { exist: exist, owned: owned }) 105function getVideoState (id, callback) {
117 }) 106 get(id, function (err, video) {
118 } 107 if (err) return callback(err)
119 108
120 function isOwned (id, callback) { 109 var exist = (video !== null)
121 VideosDB.findById(id, function (err, video) { 110 var owned = false
122 if (err || !video) { 111 if (exist === true) {
123 if (!err) err = new Error('Cannot find this video.') 112 owned = (video.namePath !== null)
124 logger.error('Cannot find this video.') 113 }
125 return callback(err)
126 }
127 114
128 if (video.namePath === null) { 115 return callback(null, { exist: exist, owned: owned })
129 var error_string = 'Cannot remove the video of another pod.' 116 })
130 logger.error(error_string) 117}
131 return callback(new Error(error_string), false, video) 118
132 } 119function isOwned (id, callback) {
120 VideosDB.findById(id, function (err, video) {
121 if (err || !video) {
122 if (!err) err = new Error('Cannot find this video.')
123 logger.error('Cannot find this video.')
124 return callback(err)
125 }
126
127 if (video.namePath === null) {
128 var error_string = 'Cannot remove the video of another pod.'
129 logger.error(error_string)
130 return callback(new Error(error_string), false, video)
131 }
132
133 callback(null, true, video)
134 })
135}
133 136
134 callback(null, true, video) 137function list (callback) {
135 }) 138 VideosDB.find(function (err, videos_list) {
136 } 139 if (err) {
140 logger.error('Cannot get the list of the videos.')
141 return callback(err)
142 }
137 143
138 function list (callback) { 144 return callback(null, videos_list)
139 VideosDB.find(function (err, videos_list) { 145 })
140 if (err) { 146}
141 logger.error('Cannot get the list of the videos.')
142 return callback(err)
143 }
144 147
145 return callback(null, videos_list) 148function listOwned (callback) {
146 }) 149 // If namePath is not null this is *our* video
147 } 150 VideosDB.find({ namePath: { $ne: null } }, function (err, videos_list) {
151 if (err) {
152 logger.error('Cannot get the list of owned videos.')
153 return callback(err)
154 }
155
156 return callback(null, videos_list)
157 })
158}
159
160function removeOwned (id, callback) {
161 VideosDB.findByIdAndRemove(id, function (err, video) {
162 if (err) {
163 logger.error('Cannot remove the torrent.')
164 return callback(err)
165 }
148 166
149 function listOwned (callback) { 167 fs.unlink(uploadDir + video.namePath, function (err) {
150 // If namePath is not null this is *our* video
151 VideosDB.find({ namePath: { $ne: null } }, function (err, videos_list) {
152 if (err) { 168 if (err) {
153 logger.error('Cannot get the list of owned videos.') 169 logger.error('Cannot remove this video file.')
154 return callback(err) 170 return callback(err)
155 } 171 }
156 172
157 return callback(null, videos_list) 173 callback(null)
158 }) 174 })
159 } 175 })
160 176}
161 function removeOwned (id, callback) { 177
162 VideosDB.findByIdAndRemove(id, function (err, video) { 178function removeAllRemotes (callback) {
163 if (err) { 179 VideosDB.remove({ namePath: null }, callback)
164 logger.error('Cannot remove the torrent.') 180}
165 return callback(err) 181
182function removeAllRemotesOf (fromUrl, callback) {
183 // TODO { podUrl: { $in: urls } }
184 VideosDB.remove({ podUrl: fromUrl }, callback)
185}
186
187// Use the magnet Uri because the _id field is not the same on different servers
188function removeRemotesOfByMagnetUris (fromUrl, magnetUris, callback) {
189 if (callback === undefined) callback = function () {}
190
191 VideosDB.find({ magnetUri: { $in: magnetUris } }, function (err, videos) {
192 if (err || !videos) {
193 logger.error('Cannot find the torrent URI of these remote videos.')
194 return callback(err)
195 }
196
197 var to_remove = []
198 async.each(videos, function (video, callback_async) {
199 callback_async = dz(callback_async)
200
201 if (video.podUrl !== fromUrl) {
202 logger.error('The pod %s has not the rights on the video of %s.', fromUrl, video.podUrl)
203 } else {
204 to_remove.push(video._id)
166 } 205 }
167 206
168 fs.unlink(uploadDir + video.namePath, function (err) { 207 callback_async()
208 }, function () {
209 VideosDB.remove({ _id: { $in: to_remove } }, function (err) {
169 if (err) { 210 if (err) {
170 logger.error('Cannot remove this video file.') 211 logger.error('Cannot remove the remote videos.')
171 return callback(err) 212 return callback(err)
172 } 213 }
173 214
215 logger.info('Removed remote videos from %s.', fromUrl)
174 callback(null) 216 callback(null)
175 }) 217 })
176 }) 218 })
177 } 219 })
178 220}
179 function removeAllRemotes (callback) {
180 VideosDB.remove({ namePath: null }, callback)
181 }
182
183 function removeAllRemotesOf (fromUrl, callback) {
184 // TODO { podUrl: { $in: urls } }
185 VideosDB.remove({ podUrl: fromUrl }, callback)
186 }
187
188 // Use the magnet Uri because the _id field is not the same on different servers
189 function removeRemotesOfByMagnetUris (fromUrl, magnetUris, callback) {
190 if (callback === undefined) callback = function () {}
191
192 VideosDB.find({ magnetUri: { $in: magnetUris } }, function (err, videos) {
193 if (err || !videos) {
194 logger.error('Cannot find the torrent URI of these remote videos.')
195 return callback(err)
196 }
197
198 var to_remove = []
199 async.each(videos, function (video, callback_async) {
200 callback_async = dz(callback_async)
201
202 if (video.podUrl !== fromUrl) {
203 logger.error('The pod %s has not the rights on the video of %s.', fromUrl, video.podUrl)
204 } else {
205 to_remove.push(video._id)
206 }
207
208 callback_async()
209 }, function () {
210 VideosDB.remove({ _id: { $in: to_remove } }, function (err) {
211 if (err) {
212 logger.error('Cannot remove the remote videos.')
213 return callback(err)
214 }
215
216 logger.info('Removed remote videos from %s.', fromUrl)
217 callback(null)
218 })
219 })
220 })
221 }
222 221
223 function search (name, callback) { 222function search (name, callback) {
224 VideosDB.find({ name: new RegExp(name) }, function (err, videos) { 223 VideosDB.find({ name: new RegExp(name) }, function (err, videos) {
225 if (err) { 224 if (err) {
226 logger.error('Cannot search the videos.') 225 logger.error('Cannot search the videos.')
227 return callback(err) 226 return callback(err)
228 } 227 }
229 228
230 return callback(null, videos) 229 return callback(null, videos)
231 }) 230 })
232 } 231}
233 232
234 // --------------------------------------------------------------------------- 233// ---------------------------------------------------------------------------
235 234
236 module.exports = Videos 235module.exports = Videos
237})()