From 9f10b2928df655c3672d9607e864e667d4bc903a Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Sun, 7 Feb 2016 11:23:23 +0100 Subject: Remove useless anonymous functions of files --- models/pods.js | 152 ++++++++++--------- models/poolRequests.js | 84 +++++------ models/videos.js | 394 ++++++++++++++++++++++++------------------------- 3 files changed, 312 insertions(+), 318 deletions(-) (limited to 'models') 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 @@ -;(function () { - 'use strict' - - var mongoose = require('mongoose') - - var constants = require('../initializers/constants') - var logger = require('../helpers/logger') - - // --------------------------------------------------------------------------- - - var podsSchema = mongoose.Schema({ - url: String, - publicKey: String, - score: { type: Number, max: constants.FRIEND_BASE_SCORE } - }) - var PodsDB = mongoose.model('pods', podsSchema) - - // --------------------------------------------------------------------------- - - var Pods = { - add: add, - count: count, - findByUrl: findByUrl, - findBadPods: findBadPods, - incrementScores: incrementScores, - list: list, - remove: remove, - removeAll: removeAll, - removeAllByIds: removeAllByIds +'use strict' + +var mongoose = require('mongoose') + +var constants = require('../initializers/constants') +var logger = require('../helpers/logger') + +// --------------------------------------------------------------------------- + +var podsSchema = mongoose.Schema({ + url: String, + publicKey: String, + score: { type: Number, max: constants.FRIEND_BASE_SCORE } +}) +var PodsDB = mongoose.model('pods', podsSchema) + +// --------------------------------------------------------------------------- + +var Pods = { + add: add, + count: count, + findByUrl: findByUrl, + findBadPods: findBadPods, + incrementScores: incrementScores, + list: list, + remove: remove, + removeAll: removeAll, + removeAllByIds: removeAllByIds +} + +// TODO: check if the pod is not already a friend +function add (data, callback) { + if (!callback) callback = function () {} + var params = { + url: data.url, + publicKey: data.publicKey, + score: constants.FRIEND_BASE_SCORE } - // TODO: check if the pod is not already a friend - function add (data, callback) { - if (!callback) callback = function () {} - var params = { - url: data.url, - publicKey: data.publicKey, - score: constants.FRIEND_BASE_SCORE - } - - PodsDB.create(params, callback) - } + PodsDB.create(params, callback) +} - function count (callback) { - return PodsDB.count(callback) - } +function count (callback) { + return PodsDB.count(callback) +} - function findBadPods (callback) { - PodsDB.find({ score: 0 }, callback) - } +function findBadPods (callback) { + PodsDB.find({ score: 0 }, callback) +} - function findByUrl (url, callback) { - PodsDB.findOne({ url: url }, callback) - } +function findByUrl (url, callback) { + PodsDB.findOne({ url: url }, callback) +} - function incrementScores (ids, value, callback) { - if (!callback) callback = function () {} - PodsDB.update({ _id: { $in: ids } }, { $inc: { score: value } }, { multi: true }, callback) - } +function incrementScores (ids, value, callback) { + if (!callback) callback = function () {} + PodsDB.update({ _id: { $in: ids } }, { $inc: { score: value } }, { multi: true }, callback) +} - function list (callback) { - PodsDB.find(function (err, pods_list) { - if (err) { - logger.error('Cannot get the list of the pods.') - return callback(err) - } +function list (callback) { + PodsDB.find(function (err, pods_list) { + if (err) { + logger.error('Cannot get the list of the pods.') + return callback(err) + } - return callback(null, pods_list) - }) - } + return callback(null, pods_list) + }) +} - function remove (url, callback) { - if (!callback) callback = function () {} - PodsDB.remove({ url: url }, callback) - } +function remove (url, callback) { + if (!callback) callback = function () {} + PodsDB.remove({ url: url }, callback) +} - function removeAll (callback) { - if (!callback) callback = function () {} - PodsDB.remove(callback) - } +function removeAll (callback) { + if (!callback) callback = function () {} + PodsDB.remove(callback) +} - function removeAllByIds (ids, callback) { - if (!callback) callback = function () {} - PodsDB.remove({ _id: { $in: ids } }, callback) - } +function removeAllByIds (ids, callback) { + if (!callback) callback = function () {} + PodsDB.remove({ _id: { $in: ids } }, callback) +} - // --------------------------------------------------------------------------- +// --------------------------------------------------------------------------- - module.exports = Pods -})() +module.exports = Pods 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 @@ -;(function () { - 'use strict' +'use strict' - var mongoose = require('mongoose') +var mongoose = require('mongoose') - var logger = require('../helpers/logger') +var logger = require('../helpers/logger') - // --------------------------------------------------------------------------- +// --------------------------------------------------------------------------- - var poolRequestsSchema = mongoose.Schema({ - type: String, - id: String, // Special id to find duplicates (video created we want to remove...) - request: mongoose.Schema.Types.Mixed - }) - var PoolRequestsDB = mongoose.model('poolRequests', poolRequestsSchema) +var poolRequestsSchema = mongoose.Schema({ + type: String, + id: String, // Special id to find duplicates (video created we want to remove...) + request: mongoose.Schema.Types.Mixed +}) +var PoolRequestsDB = mongoose.model('poolRequests', poolRequestsSchema) - // --------------------------------------------------------------------------- +// --------------------------------------------------------------------------- - var PoolRequests = { - create: create, - findById: findById, - list: list, - removeRequestById: removeRequestById, - removeRequests: removeRequests - } +var PoolRequests = { + create: create, + findById: findById, + list: list, + removeRequestById: removeRequestById, + removeRequests: removeRequests +} - function create (id, type, request, callback) { - PoolRequestsDB.create({ id: id, type: type, request: request }, callback) - } +function create (id, type, request, callback) { + PoolRequestsDB.create({ id: id, type: type, request: request }, callback) +} - function findById (id, callback) { - PoolRequestsDB.findOne({ id: id }, callback) - } +function findById (id, callback) { + PoolRequestsDB.findOne({ id: id }, callback) +} - function list (callback) { - PoolRequestsDB.find({}, { _id: 1, type: 1, request: 1 }, callback) - } +function list (callback) { + PoolRequestsDB.find({}, { _id: 1, type: 1, request: 1 }, callback) +} - function removeRequestById (id, callback) { - PoolRequestsDB.remove({ id: id }, callback) - } +function removeRequestById (id, callback) { + PoolRequestsDB.remove({ id: id }, callback) +} - function removeRequests (ids) { - PoolRequestsDB.remove({ _id: { $in: ids } }, function (err) { - if (err) { - logger.error('Cannot remove requests from the pool requests database.', { error: err }) - return // Abort - } +function removeRequests (ids) { + PoolRequestsDB.remove({ _id: { $in: ids } }, function (err) { + if (err) { + logger.error('Cannot remove requests from the pool requests database.', { error: err }) + return // Abort + } - logger.info('Pool requests flushed.') - }) - } + logger.info('Pool requests flushed.') + }) +} - // --------------------------------------------------------------------------- +// --------------------------------------------------------------------------- - module.exports = PoolRequests -})() +module.exports = PoolRequests 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 @@ -;(function () { - 'use strict' - - var async = require('async') - var config = require('config') - var dz = require('dezalgo') - var fs = require('fs') - var mongoose = require('mongoose') - var path = require('path') - - var logger = require('../helpers/logger') - - var http = config.get('webserver.https') === true ? 'https' : 'http' - var host = config.get('webserver.host') - var port = config.get('webserver.port') - var uploadDir = path.join(__dirname, '..', config.get('storage.uploads')) - - // --------------------------------------------------------------------------- - - var videosSchema = mongoose.Schema({ - name: String, - namePath: String, - description: String, - magnetUri: String, - podUrl: String +'use strict' + +var async = require('async') +var config = require('config') +var dz = require('dezalgo') +var fs = require('fs') +var mongoose = require('mongoose') +var path = require('path') + +var logger = require('../helpers/logger') + +var http = config.get('webserver.https') === true ? 'https' : 'http' +var host = config.get('webserver.host') +var port = config.get('webserver.port') +var uploadDir = path.join(__dirname, '..', config.get('storage.uploads')) + +// --------------------------------------------------------------------------- + +var videosSchema = mongoose.Schema({ + name: String, + namePath: String, + description: String, + magnetUri: String, + podUrl: String +}) +var VideosDB = mongoose.model('videos', videosSchema) + +// --------------------------------------------------------------------------- + +var Videos = { + add: add, + addRemotes: addRemotes, + get: get, + getVideoState: getVideoState, + isOwned: isOwned, + list: list, + listOwned: listOwned, + removeOwned: removeOwned, + removeAllRemotes: removeAllRemotes, + removeAllRemotesOf: removeAllRemotesOf, + removeRemotesOfByMagnetUris: removeRemotesOfByMagnetUris, + search: search +} + +function add (video, callback) { + logger.info('Adding %s video to database.', video.name) + + var params = video + params.podUrl = http + '://' + host + ':' + port + + VideosDB.create(params, function (err, video) { + if (err) { + logger.error('Cannot insert this video into database.') + return callback(err) + } + + callback(null) }) - var VideosDB = mongoose.model('videos', videosSchema) - - // --------------------------------------------------------------------------- - - var Videos = { - add: add, - addRemotes: addRemotes, - get: get, - getVideoState: getVideoState, - isOwned: isOwned, - list: list, - listOwned: listOwned, - removeOwned: removeOwned, - removeAllRemotes: removeAllRemotes, - removeAllRemotesOf: removeAllRemotesOf, - removeRemotesOfByMagnetUris: removeRemotesOfByMagnetUris, - search: search - } - - function add (video, callback) { - logger.info('Adding %s video to database.', video.name) - - var params = video - params.podUrl = http + '://' + host + ':' + port - - VideosDB.create(params, function (err, video) { - if (err) { - logger.error('Cannot insert this video into database.') - return callback(err) - } - - callback(null) - }) - } +} - // TODO: avoid doublons - function addRemotes (videos, callback) { - if (!callback) callback = function () {} +// TODO: avoid doublons +function addRemotes (videos, callback) { + if (!callback) callback = function () {} - var to_add = [] + var to_add = [] - async.each(videos, function (video, callback_each) { - callback_each = dz(callback_each) - logger.debug('Add remote video from pod: %s', video.podUrl) - - var params = { - name: video.name, - namePath: null, - description: video.description, - magnetUri: video.magnetUri, - podUrl: video.podUrl - } + async.each(videos, function (video, callback_each) { + callback_each = dz(callback_each) + logger.debug('Add remote video from pod: %s', video.podUrl) - to_add.push(params) + var params = { + name: video.name, + namePath: null, + description: video.description, + magnetUri: video.magnetUri, + podUrl: video.podUrl + } - callback_each() - }, function () { - VideosDB.create(to_add, function (err, videos) { - if (err) { - logger.error('Cannot insert this remote video.') - return callback(err) - } - - return callback(null, videos) - }) - }) - } + to_add.push(params) - function get (id, callback) { - VideosDB.findById(id, function (err, video) { + callback_each() + }, function () { + VideosDB.create(to_add, function (err, videos) { if (err) { - logger.error('Cannot get this video.') + logger.error('Cannot insert this remote video.') return callback(err) } - return callback(null, video) + return callback(null, videos) }) - } + }) +} - function getVideoState (id, callback) { - get(id, function (err, video) { - if (err) return callback(err) +function get (id, callback) { + VideosDB.findById(id, function (err, video) { + if (err) { + logger.error('Cannot get this video.') + return callback(err) + } - var exist = (video !== null) - var owned = false - if (exist === true) { - owned = (video.namePath !== null) - } + return callback(null, video) + }) +} - return callback(null, { exist: exist, owned: owned }) - }) - } +function getVideoState (id, callback) { + get(id, function (err, video) { + if (err) return callback(err) - function isOwned (id, callback) { - VideosDB.findById(id, function (err, video) { - if (err || !video) { - if (!err) err = new Error('Cannot find this video.') - logger.error('Cannot find this video.') - return callback(err) - } + var exist = (video !== null) + var owned = false + if (exist === true) { + owned = (video.namePath !== null) + } - if (video.namePath === null) { - var error_string = 'Cannot remove the video of another pod.' - logger.error(error_string) - return callback(new Error(error_string), false, video) - } + return callback(null, { exist: exist, owned: owned }) + }) +} + +function isOwned (id, callback) { + VideosDB.findById(id, function (err, video) { + if (err || !video) { + if (!err) err = new Error('Cannot find this video.') + logger.error('Cannot find this video.') + return callback(err) + } + + if (video.namePath === null) { + var error_string = 'Cannot remove the video of another pod.' + logger.error(error_string) + return callback(new Error(error_string), false, video) + } + + callback(null, true, video) + }) +} - callback(null, true, video) - }) - } +function list (callback) { + VideosDB.find(function (err, videos_list) { + if (err) { + logger.error('Cannot get the list of the videos.') + return callback(err) + } - function list (callback) { - VideosDB.find(function (err, videos_list) { - if (err) { - logger.error('Cannot get the list of the videos.') - return callback(err) - } + return callback(null, videos_list) + }) +} - return callback(null, videos_list) - }) - } +function listOwned (callback) { + // If namePath is not null this is *our* video + VideosDB.find({ namePath: { $ne: null } }, function (err, videos_list) { + if (err) { + logger.error('Cannot get the list of owned videos.') + return callback(err) + } + + return callback(null, videos_list) + }) +} + +function removeOwned (id, callback) { + VideosDB.findByIdAndRemove(id, function (err, video) { + if (err) { + logger.error('Cannot remove the torrent.') + return callback(err) + } - function listOwned (callback) { - // If namePath is not null this is *our* video - VideosDB.find({ namePath: { $ne: null } }, function (err, videos_list) { + fs.unlink(uploadDir + video.namePath, function (err) { if (err) { - logger.error('Cannot get the list of owned videos.') + logger.error('Cannot remove this video file.') return callback(err) } - return callback(null, videos_list) + callback(null) }) - } - - function removeOwned (id, callback) { - VideosDB.findByIdAndRemove(id, function (err, video) { - if (err) { - logger.error('Cannot remove the torrent.') - return callback(err) + }) +} + +function removeAllRemotes (callback) { + VideosDB.remove({ namePath: null }, callback) +} + +function removeAllRemotesOf (fromUrl, callback) { + // TODO { podUrl: { $in: urls } } + VideosDB.remove({ podUrl: fromUrl }, callback) +} + +// Use the magnet Uri because the _id field is not the same on different servers +function removeRemotesOfByMagnetUris (fromUrl, magnetUris, callback) { + if (callback === undefined) callback = function () {} + + VideosDB.find({ magnetUri: { $in: magnetUris } }, function (err, videos) { + if (err || !videos) { + logger.error('Cannot find the torrent URI of these remote videos.') + return callback(err) + } + + var to_remove = [] + async.each(videos, function (video, callback_async) { + callback_async = dz(callback_async) + + if (video.podUrl !== fromUrl) { + logger.error('The pod %s has not the rights on the video of %s.', fromUrl, video.podUrl) + } else { + to_remove.push(video._id) } - fs.unlink(uploadDir + video.namePath, function (err) { + callback_async() + }, function () { + VideosDB.remove({ _id: { $in: to_remove } }, function (err) { if (err) { - logger.error('Cannot remove this video file.') + logger.error('Cannot remove the remote videos.') return callback(err) } + logger.info('Removed remote videos from %s.', fromUrl) callback(null) }) }) - } - - function removeAllRemotes (callback) { - VideosDB.remove({ namePath: null }, callback) - } - - function removeAllRemotesOf (fromUrl, callback) { - // TODO { podUrl: { $in: urls } } - VideosDB.remove({ podUrl: fromUrl }, callback) - } - - // Use the magnet Uri because the _id field is not the same on different servers - function removeRemotesOfByMagnetUris (fromUrl, magnetUris, callback) { - if (callback === undefined) callback = function () {} - - VideosDB.find({ magnetUri: { $in: magnetUris } }, function (err, videos) { - if (err || !videos) { - logger.error('Cannot find the torrent URI of these remote videos.') - return callback(err) - } - - var to_remove = [] - async.each(videos, function (video, callback_async) { - callback_async = dz(callback_async) - - if (video.podUrl !== fromUrl) { - logger.error('The pod %s has not the rights on the video of %s.', fromUrl, video.podUrl) - } else { - to_remove.push(video._id) - } - - callback_async() - }, function () { - VideosDB.remove({ _id: { $in: to_remove } }, function (err) { - if (err) { - logger.error('Cannot remove the remote videos.') - return callback(err) - } - - logger.info('Removed remote videos from %s.', fromUrl) - callback(null) - }) - }) - }) - } + }) +} - function search (name, callback) { - VideosDB.find({ name: new RegExp(name) }, function (err, videos) { - if (err) { - logger.error('Cannot search the videos.') - return callback(err) - } +function search (name, callback) { + VideosDB.find({ name: new RegExp(name) }, function (err, videos) { + if (err) { + logger.error('Cannot search the videos.') + return callback(err) + } - return callback(null, videos) - }) - } + return callback(null, videos) + }) +} - // --------------------------------------------------------------------------- +// --------------------------------------------------------------------------- - module.exports = Videos -})() +module.exports = Videos -- cgit v1.2.3