diff --git a/modules/historyManager.js b/modules/historyManager.js index 5d805b8..9c15204 100644 --- a/modules/historyManager.js +++ b/modules/historyManager.js @@ -1,23 +1,21 @@ -var Q = require('q'), - mongoose = require('mongoose'), - QueueTrack = mongoose.model('QueueTrack'), - HistoryTrack = mongoose.model('HistoryTrack'); +var Q = require('q'); function addToHistory(trackUri) { var deferred = Q.defer(); - QueueTrack.findOne({ 'uri': trackUri }, function (err, track) { - if (err) return console.error(err); - HistoryTrack.convert(track, function(convertedTrack) { - var historyTrack = new HistoryTrack(convertedTrack); - - historyTrack.save(function (err, savedTrack) { - if (err) return console.error(err); - deferred.resolve(); - }); - }) - - }); + // QueueTrack.findOne({ 'uri': trackUri }, function (err, track) { + // if (err) return console.error(err); + // HistoryTrack.convert(track, function(convertedTrack) { + // var historyTrack = new HistoryTrack(convertedTrack); + // + // historyTrack.save(function (err, savedTrack) { + // if (err) return console.error(err); + // deferred.resolve(); + // }); + // }) + // + // }); + deferred.resolve(); return deferred.promise; } diff --git a/modules/mopidyCom.js b/modules/mopidyCom.js index 9429c83..04628a3 100644 --- a/modules/mopidyCom.js +++ b/modules/mopidyCom.js @@ -1,9 +1,8 @@ var Q = require('q'), Mopidy = require('mopidy'), - config = require('../config.js'), - mongoose = require('mongoose'), - QueueTrack = mongoose.model('QueueTrack'), - HistoryTrack = mongoose.model('HistoryTrack'); + config = require('../config.js'); + +var models = require('./models.js'); var playlistManager = require('./playlistManager.js'), historyManager = require('./historyManager.js'), @@ -54,9 +53,16 @@ function init () { mopidy.on('event:trackPlaybackStarted', function (track) { track = track.tl_track.track; - QueueTrack.convert(track, function (currentTrack) { - socket.sendTrack(true, currentTrack); - }); + var currentTrack = new models.Track( + track.artists[0].name, + track.name, + track.uri, + 0, + '', + track.length + ); + + socket.sendTrack(true, currentTrack); socket.broadcastUpcomingTracks(); @@ -73,9 +79,14 @@ function getCurrentTrack () { mopidy.playback.getCurrentTrack() .then(function (track) { if (track) { - QueueTrack.convert(track, function (currentTrack) { - deferred.resolve(currentTrack); - }) + deferred.resolve(new models.Track( + track.artists[0].name, + track.name, + track.uri, + 0, + '', + track.length + )); } else { deferred.reject('No current track'); } @@ -124,12 +135,12 @@ function queueNextTrack (isInit) { // 5 seconds before the current track stops, queue next track setTimeout(function () { queueNextTrack(); - }, nextTracks[0]._doc.length - (firstSong ? 10*1000 : 0)); + }, nextTracks[0].length - (firstSong ? 10*1000 : 0)); firstSong = false; - socket.sendTrack(false, nextTracks[0]._doc); + socket.sendTrack(false, nextTracks[0]); socket.broadcastUpcomingTracks(); - trackUri = nextTracks[0]._doc.uri; - return mopidy.tracklist.add(null, null, nextTracks[0]._doc.uri, null); + trackUri = nextTracks[0].uri; + return mopidy.tracklist.add(null, null, nextTracks[0].uri, null); }) .then(function () { return historyManager.addToHistory(trackUri); @@ -180,13 +191,19 @@ function searchTrack (query) { var convertedResults = []; for (var i = 0; i < concatenatedResults.length; i++) { - QueueTrack.convert(concatenatedResults[i], function (currentTrack) { - convertedResults.push(currentTrack); + convertedResults.push(new models.Track( + concatenatedResults[i].artists[0].name, + concatenatedResults[i].name, + concatenatedResults[i].uri, + 0, + '', + concatenatedResults[i].length + )); + + if (concatenatedResults.length === convertedResults.length) { + deferred.resolve(convertedResults); + }; - if (concatenatedResults.length === convertedResults.length) { - deferred.resolve(convertedResults); - }; - }) }; }) .done(); diff --git a/modules/playlistManager.js b/modules/playlistManager.js index a46844f..079c19a 100644 --- a/modules/playlistManager.js +++ b/modules/playlistManager.js @@ -1,9 +1,11 @@ var Q = require('q'), - config = require('../config.js'), - mongoose = require('mongoose'), - QueueTrack = mongoose.model('QueueTrack'); + nedb = require('nedb'), + config = require('../config.js'); + +var dbHandler = require('../modules/dbHandler.js'); var socket = require('./socketLogic.js'); +var models = require('../modules/models.js'); var playlistManager = { defaultTracks: [] @@ -29,17 +31,26 @@ playlistManager.repopulateDB = function (tracks) { var savedTracks = 0; for (var i = tracks.length - 1; i >= 0; i--) { - QueueTrack.convert(tracks[i], function (track) { - track = new QueueTrack(track); - - track.save(function (err, track) { - if (err) return console.error(err); + var track = new models.Track( + tracks[i].artists[0].name, + tracks[i].name, + tracks[i].uri, + 0, + '', + tracks[i].length + ); + + dbHandler.saveTrack(track) + .then(function(savedTrack) { savedTracks++; if (savedTracks === tracks.length) { deferred.resolve(); }; - }); - }); + }) + .fail(function(err) { + deferred.reject(err); + }) + .done(); }; return deferred.promise; @@ -53,23 +64,14 @@ playlistManager.getNextTracks = function (numberOfTracks) { numberOfTracks = 10; }; - var query = QueueTrack.find().sort('-rating dateAdded').limit(numberOfTracks); - var promise = query.exec(); - promise.addBack(function (err, tracks) { - if (err) return console.error(err); - if (tracks.length < numberOfTracks) { - playlistManager.repopulateDB() - .then(function () { - return playlistManager.getNextTracks(numberOfTracks); - }) - .then(function (nextTracks) { - deferred.resolve(nextTracks); - }) - .done(); - } else { - deferred.resolve(tracks); - } - }); + dbHandler.getNextTracks('queue', numberOfTracks) + .then(function (nextTracks) { + deferred.resolve(nextTracks); + }) + .fail(function(err) { + deferred.reject(err); + }) + .done(); return deferred.promise; } @@ -78,45 +80,52 @@ playlistManager.getNextTracks = function (numberOfTracks) { playlistManager.removeNextTrack = function() { playlistManager.getNextTracks(1) .then(function (nextTracks) { - QueueTrack.remove({uri: nextTracks[0]._doc.uri}, function (err) { - if (err) return console.error(err); - }); + return dbHandler.deleteTrack(nextTracks[0].uri); + }) + .fail(function(err) { + if (err) return console.error(err); }) .done(); -} +}; playlistManager.addTrack = function(newTrack) { - QueueTrack.findOne({ 'uri': newTrack.uri }, function (err, track) { - if (err) return console.error(err); - - // Only add track if not already in queue - if (track) { - // Vote track up if already in queue - playlistManager.voteUp(newTrack.uri); - } else { - track = new QueueTrack(newTrack); - - track.save(function (err, track) { - if (err) return console.error(err); - socket.broadcastUpcomingTracks(); - }); - } - }); -} + dbHandler.getTrack(newTrack.uri) + .then(function(track) { + // Only add track if not already in queue + if (track) { + // Vote track up if already in queue + playlistManager.voteUp(newTrack.uri); + } else { + track = new QueueTrack(newTrack); + + return dbHandler.saveTrack(track) + .then(function(savedTrack) { + socket.broadcastUpcomingTracks(); + }) + .fail(function(err) { + deferred.reject(err); + }) + .done(); + } + }) + .fail(function(err) { + if (err) return console.error(err); + }) + .done(); +}; playlistManager.voteUp = function (trackUri) { - QueueTrack.findOne({ 'uri': trackUri }, function (err, track) { - if (err) return console.error(err); - - track.rating++; - track.save(function (err, track) { - if (err) return console.error(err); + dbHandler.voteUp(trackUri, 'queue') + .then(function(newTrack) { socket.broadcastUpcomingTracks(); - }); - }); -} + }) + .fail(function(err) { + if (err) return console.error(err); + }) + .done(); +}; exports.setDefaultTracks = playlistManager.setDefaultTracks; diff --git a/server.js b/server.js index b8407c5..039d668 100644 --- a/server.js +++ b/server.js @@ -1,33 +1,11 @@ var Hapi = require('hapi'), server = new Hapi.Server(), - nedb = require('nedb'), glob = require('glob'); // Load configfile var config = require('./config.js'); -// Load database -var db = {}; -db.queue = new nedb({ filename: config.db.queuePath, autoload: true }); -db.history = new nedb({ filename: config.db.historyPath, autoload: true }); - - - -// var models = glob.sync('./models/*.js'); -// models.forEach(function (model) { -// require(model); -// }); - -// if (process.env.ENV !== 'prod') { -// mongoose.connection.collections['queuetracks'].drop( function(err) { -// console.log('old queue tracks dropped'); -// }); -// } - -/* -var socket = require('./modules/socketLogic.js'); -socket.init(server.listener); var mopidy = require('./modules/mopidyCom.js'); mopidy.init();