sync/lib/userplaylists.js

78 lines
1.9 KiB
JavaScript
Raw Normal View History

2014-02-02 18:41:41 +00:00
var db = require("./database");
function listPlaylists(user) {
db.listUserPlaylists(user.name, function (err, rows) {
if (err) {
user.socket.emit("errorMsg", {
msg: "Database error when attempting to fetch list of playlists"
});
return;
}
user.socket.emit("listPlaylists", rows);
});
}
function clonePlaylist(user, data) {
if (!user.inChannel()) {
user.socket.emit("errorMsg", {
msg: "You must be in a channel in order to clone its playlist"
});
return;
}
if (typeof data.name !== "string") {
return;
}
var pl = user.channel.playlist.items.toArray();
db.saveUserPlaylist(pl, user.name, data.name, function (err, res) {
if (err) {
user.socket.emit("errorMsg", {
msg: "Database error when saving playlist"
});
} else {
listPlaylists(user);
}
});
}
function deletePlaylist(user, data) {
if (typeof data.name !== "string") {
return;
}
db.deleteUserPlaylist(user.name, data.name, function (err) {
if (err) {
user.socket.emit("errorMsg", {
msg: err
});
return;
}
setImmediate(function () {
listPlaylists(user);
});
});
}
module.exports.init = function (user) {
console.log('Initializing playlists for ' + user.name);
var s = user.socket;
var wrap = function (cb) {
return function (data) {
if (!user.loggedIn || user.rank < 1) {
s.emit("errorMsg", {
msg: "You must be logged in to manage playlists"
});
return;
}
cb(user, data);
};
};
s.on("listPlaylists", wrap(listPlaylists));
s.on("clonePlaylist", wrap(clonePlaylist));
s.on("deletePlaylist", wrap(deletePlaylist));
};