Continue refactoring playlist

This commit is contained in:
calzoneman 2013-09-29 19:53:27 -05:00
parent 21bd270813
commit a371ca6440
2 changed files with 103 additions and 89 deletions

View file

@ -22,6 +22,7 @@ var Playlist = require("./playlist");
var sanitize = require("validator").sanitize;
var $util = require("./utilities");
var url = require("url");
var AsyncQueue = require("./asyncqueue");
var Channel = function(name, Server) {
var self = this;
@ -36,6 +37,7 @@ var Channel = function(name, Server) {
self.registered = false;
self.users = [];
self.playlist = new Playlist(self);
self.plqueue = new AsyncQueue();
self.position = -1;
self.drinks = 0;
self.leader = null;
@ -1371,116 +1373,109 @@ Channel.prototype.addMedia = function(data, user) {
data.maxlength = self.hasPermission(user, "exceedmaxlength")
? 0
: this.opts.maxlength;
if (data.pos === "end")
data.pos = "append";
var postAdd = function (item, cached) {
if (self.dead)
if (data.type === "cu" && data.title) {
var t = data.title;
if(t.length > 100)
t = t.substring(0, 97) + "...";
data.title = t;
}
var afterData = function (q, c, m) {
if (data.maxlength && data.seconds > data.maxlength) {
user.socket.emit("queueFail",
"Media is too long!");
q.release();
return;
if(item.media.type === "cu" && data.title) {
var t = data.title;
if(t.length > 100)
t = t.substring(0, 97) + "...";
item.media.title = t;
}
self.logger.log("### " + user.name + " queued " + item.media.title);
m.pos = data.pos;
m.queueby = data.queueby;
m.temp = data.temp;
var res = self.playlist.addMedia(m);
if (res.error) {
user.socket.emit("queueFail", res.error);
q.release();
return;
}
var item = res.item;
self.logger.log("### " + user.name + " queued " +
item.media.title);
self.sendAll("queue", {
item: item.pack(),
after: item.prev ? item.prev.uid : "prepend"
});
self.broadcastPlaylistMeta();
if(!cached && !item.temp)
if (!c && !item.temp)
self.cacheMedia(item.media);
}
q.release();
};
// No need to check library for livestreams - they aren't cached
if(isLive(data.type)) {
self.playlist.addMedia(data, function (err, data) {
// Don't check library for livestreams or if the channel is
// unregistered
if (!self.registered || isLive(data.type)) {
self.plqueue.queue(function (q) {
if (self.dead)
return;
var cb = afterData.bind(self, q, false);
self.server.infogetter.getMedia(data.id, data.type,
function (e, m) {
if (self.dead)
return;
if (e) {
user.socket.emit("queueFail", e);
q.release();
return;
}
if(err) {
if(err === true)
err = false;
if(user)
user.socket.emit("queueFail", err);
return;
}
else {
postAdd(data, false);
}
cb(m);
});
});
return;
}
// Don't search library if the channel isn't registered
if(!self.registered) {
self.playlist.addMedia(data, function(err, item) {
} else {
self.server.db.getLibraryItem(self.name, data.id,
function (err, item) {
if (self.dead)
return;
if(err) {
if(err === true)
err = false;
if(user)
user.socket.emit("queueFail", err);
if (err) {
user.socket.emit("queueFail", "Internal error: " + err);
return;
}
if (item !== null) {
if (data.maxlength && item.seconds > data.maxlength) {
user.socket.emit("queueFail", "Media is too long!");
return;
}
self.plqueue.queue(function (q) {
if (self.dead)
return;
afterData.bind(self, q, true)(item);
});
} else {
postAdd(item, false);
self.plqueue.queue(function (q) {
if (self.dead)
return;
self.server.infogetter.getMedia(data.id, data.type,
function (e, m) {
if (self.dead)
return;
if (e) {
user.socket.emit("queueFail", e);
q.release();
return;
}
afterData.bind(self, q, false)(m);
});
});
}
});
return;
}
self.server.db.getLibraryItem(self.name, data.id,
function (err, item) {
if (self.dead)
return;
if(err) {
user.socket.emit("queueFail", "Internal error: " + err);
return;
}
if(item !== null) {
var m = new Media(item.id, item.title, item.seconds, item.type);
if(data.maxlength && m.seconds > data.maxlength) {
user.socket.emit("queueFail", "Media is too long!");
return;
}
data.media = m;
self.playlist.addCachedMedia(data, function (err, item) {
if (self.dead)
return;
if(err) {
if(err === true)
err = false;
if(user)
user.socket.emit("queueFail", err);
return;
} else {
postAdd(item, true);
}
});
} else {
self.playlist.addMedia(data, function(err, item) {
if (self.dead)
return;
if(err) {
if(err === true)
err = false;
if(user)
user.socket.emit("queueFail", err);
return;
} else {
postAdd(item, false);
}
});
}
});
}
Channel.prototype.addMediaList = function(data, user) {

View file

@ -224,6 +224,25 @@ Playlist.prototype.add = function(item, pos) {
return false;
}
Playlist.prototype.addMedia = function (data) {
var pos = data.pos;
if (pos === "next") {
if (this.current !== null)
pos = this.current.uid;
else
pos = "append"
}
var m = new Media(data.id, data.title, data.seconds, data.type);
var item = this.makeItem(m);
item.queueby = data.queueby;
item.temp = data.temp;
return {
item: item,
error: this.add(item, data.pos)
};
};
Playlist.prototype.addCachedMedia = function(data, callback) {
var pos = "append";
if(data.pos == "next") {
@ -249,7 +268,7 @@ Playlist.prototype.addCachedMedia = function(data, callback) {
this.queueAction(action);
}
Playlist.prototype.addMedia = function (data, cb) {
Playlist.prototype.__addMedia = function (data, cb) {
var self = this;
if (data.type === "yp") {
this.addYouTubePlaylist(data, cb);