Skip to content
This repository has been archived by the owner on Jun 14, 2019. It is now read-only.

Commit

Permalink
Use NeDB instead of mongo
Browse files Browse the repository at this point in the history
  • Loading branch information
eljenso committed Dec 4, 2015
1 parent f6c7d6c commit 8a273e1
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 115 deletions.
30 changes: 14 additions & 16 deletions modules/historyManager.js
Original file line number Diff line number Diff line change
@@ -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;
}
Expand Down
57 changes: 37 additions & 20 deletions modules/mopidyCom.js
Original file line number Diff line number Diff line change
@@ -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'),
Expand Down Expand Up @@ -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();

Expand All @@ -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');
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down
123 changes: 66 additions & 57 deletions modules/playlistManager.js
Original file line number Diff line number Diff line change
@@ -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: []
Expand All @@ -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;
Expand All @@ -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;
}
Expand All @@ -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;
Expand Down
22 changes: 0 additions & 22 deletions server.js
Original file line number Diff line number Diff line change
@@ -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();
Expand Down

0 comments on commit 8a273e1

Please sign in to comment.