This commit is contained in:
calzoneman 2013-07-03 17:29:49 -04:00
parent 860d81afbc
commit d20e676443
5 changed files with 118 additions and 46 deletions

View File

@ -208,13 +208,13 @@ Channel.prototype.loadDump = function() {
if(f[0] != undefined) { if(f[0] != undefined) {
var filt = new Filter("", f[0], "g", f[1]); var filt = new Filter("", f[0], "g", f[1]);
filt.active = f[2]; filt.active = f[2];
this.updateFilter(filt); this.updateFilter(filt, false);
} }
else { else {
var filt = new Filter(f.name, f.source, f.flags, f.replace); var filt = new Filter(f.name, f.source, f.flags, f.replace);
filt.active = f.active; filt.active = f.active;
filt.filterlinks = f.filterlinks; filt.filterlinks = f.filterlinks;
this.updateFilter(filt); this.updateFilter(filt, false);
} }
} }
this.broadcastChatFilters(); this.broadcastChatFilters();
@ -1186,29 +1186,46 @@ Channel.prototype.tryQueue = function(user, data) {
} }
if(data.list) if(data.list)
this.enqueueList(data, user); this.addMediaList(data, user);
else else
this.addMedia(data, user); this.addMedia(data, user);
} }
Channel.prototype.addMedia = function(data, user, callback) { Channel.prototype.addMedia = function(data, user) {
// TODO fix caching
if(data.id in this.library) {
data.type = this.library[data.id].type;
}
if(isLive(data.type) && !this.hasPermission(user, "playlistaddlive")) {
user.socket.emit("queueFail", "You don't have permission to queue livestreams");
return;
}
data.temp = isLive(data.type) || !this.hasPermission(user, "addnontemp"); data.temp = isLive(data.type) || !this.hasPermission(user, "addnontemp");
data.queueby = user ? user.name : ""; data.queueby = user ? user.name : "";
var chan = this; var chan = this;
this.playlist.addMedia(data, function(err, item) { if(data.id in this.library) {
var m = this.library[data.id].dup();
data.media = m;
this.playlist.addCachedMedia(data, function (err, item) {
if(err) {
if(err === true)
err = false;
if(user)
user.socket.emit("queueFail", err);
return;
}
else {
chan.sendAll("queue", {
item: item.pack(),
after: item.prev ? item.prev.uid : "prepend"
});
chan.broadcastPlaylistMeta();
chan.cacheMedia(item.media);
}
});
}
if(isLive(data.type) && !this.hasPermission(user, "playlistaddlive")) {
user.socket.emit("queueFail", "You don't have permission to queue livestreams");
return;
}
data.temp = isLive(data.type) || !this.hasPermission(user, "addnontemp");
data.queueby = user ? user.name : "";
this.playlist.addMedia(data, function(err, item) {
if(err) { if(err) {
if(callback)
callback(false);
if(err === true) if(err === true)
err = false; err = false;
if(user) if(user)
@ -1222,13 +1239,11 @@ Channel.prototype.addMedia = function(data, user, callback) {
}); });
chan.broadcastPlaylistMeta(); chan.broadcastPlaylistMeta();
chan.cacheMedia(item.media); chan.cacheMedia(item.media);
if(callback)
callback(true);
} }
}); });
} }
Channel.prototype.enqueueList = function(data, user) { Channel.prototype.addMediaList = function(data, user) {
var pl = data.list; var pl = data.list;
var chan = this; var chan = this;
this.playlist.addMediaList(data, function(err, item) { this.playlist.addMediaList(data, function(err, item) {
@ -1266,7 +1281,7 @@ Channel.prototype.tryQueuePlaylist = function(user, data) {
var pl = Database.loadUserPlaylist(user.name, data.name); var pl = Database.loadUserPlaylist(user.name, data.name);
data.list = pl; data.list = pl;
this.enqueueList(data, user); this.addMediaList(data, user);
} }
Channel.prototype.setTemp = function(uid, temp) { Channel.prototype.setTemp = function(uid, temp) {
@ -1374,18 +1389,20 @@ Channel.prototype.tryClearqueue = function(user) {
Channel.prototype.shufflequeue = function() { Channel.prototype.shufflequeue = function() {
var n = []; var n = [];
var pl = this.playlist.items.toArray(); var pl = this.playlist.items.toArray(false);
var current = pl.current; this.playlist.clear();
while(pl.length > 0) { while(pl.length > 0) {
var i = parseInt(Math.random() * pl.length); var i = parseInt(Math.random() * pl.length);
n.push(pl[i]); var item = this.playlist.makeItem(pl[i].media);
item.temp = pl[i].temp;
item.queueby = pl[i].queueby;
this.playlist.items.append(item);
pl.splice(i, 1); pl.splice(i, 1);
} }
// TODO fix this.playlist.current = this.playlist.items.first;
this.playlist.current = this.playlist.last;
this.playNext();
this.sendAll("playlist", this.playlist.items.toArray()); this.sendAll("playlist", this.playlist.items.toArray());
this.sendAll("setPlaylistMeta", this.plmeta); this.sendAll("setPlaylistMeta", this.plmeta);
this.playlist.startPlayback();
} }
Channel.prototype.tryShufflequeue = function(user) { Channel.prototype.tryShufflequeue = function(user) {
@ -1551,7 +1568,7 @@ Channel.prototype.removeFilter = function(filter) {
this.broadcastChatFilters(); this.broadcastChatFilters();
} }
Channel.prototype.updateFilter = function(filter) { Channel.prototype.updateFilter = function(filter, emit) {
if(filter.name == "") if(filter.name == "")
filter.name = filter.source; filter.name = filter.source;
var found = false; var found = false;
@ -1565,6 +1582,7 @@ Channel.prototype.updateFilter = function(filter) {
if(!found) { if(!found) {
this.filters.push(filter); this.filters.push(filter);
} }
if(emit !== false)
this.broadcastChatFilters(); this.broadcastChatFilters();
} }

View File

@ -1,4 +1,4 @@
var ULList = require("./ullist").ULList; ULList = require("./ullist").ULList;
var Media = require("./media").Media; var Media = require("./media").Media;
var InfoGetter = require("./get-info"); var InfoGetter = require("./get-info");
@ -33,7 +33,7 @@ function Playlist(chan) {
"remove": [], "remove": [],
}; };
this.lock = false; this.lock = false;
this.alter_queue = []; this.action_queue = [];
this._qaInterval = false; this._qaInterval = false;
if(chan) { if(chan) {
@ -54,26 +54,24 @@ function Playlist(chan) {
} }
Playlist.prototype.queueAction = function(data) { Playlist.prototype.queueAction = function(data) {
this.alter_queue.push(data); this.action_queue.push(data);
if(this._qaInterval) if(this._qaInterval)
return; return;
var pl = this; var pl = this;
this._qaInterval = setInterval(function() { this._qaInterval = setInterval(function() {
if(!pl.lock) { var data = pl.action_queue.shift();
var data = pl.alter_queue.shift();
if(data.waiting) { if(data.waiting) {
if(!("expire" in data)) if(!("expire" in data))
data.expire = Date.now() + 5000; data.expire = Date.now() + 10000;
if(Date.now() < data.expire) if(Date.now() < data.expire)
pl.alter_queue.unshift(data); pl.action_queue.unshift(data);
return;
} }
else
data.fn(); data.fn();
if(pl.alter_queue.length == 0) { if(pl.action_queue.length == 0) {
clearInterval(pl._qaInterval); clearInterval(pl._qaInterval);
pl._qaInterval = false; pl._qaInterval = false;
} }
}
}, 100); }, 100);
} }
@ -159,6 +157,31 @@ Playlist.prototype.add = function(item, pos) {
return success; return success;
} }
Playlist.prototype.addCachedMedia = function(data, callback) {
var pos = "append";
if(data.pos == "next") {
if(!this.current)
pos = "prepend";
else
pos = this.current.uid;
}
var it = this.makeItem(data.media);
it.temp = data.temp;
it.queueby = data.queueby;
var pl = this;
var action = {
fn: function() {
if(pl.add(it, pos))
callback(false, it);
},
waiting: false
};
this.queueAction(action);
}
Playlist.prototype.addMedia = function(data, callback) { Playlist.prototype.addMedia = function(data, callback) {
if(data.type == "yp") { if(data.type == "yp") {
@ -178,8 +201,9 @@ Playlist.prototype.addMedia = function(data, callback) {
var pl = this; var pl = this;
var action = { var action = {
fn: function() { fn: function() {
if(pl.add(it, pos)) if(pl.add(it, pos)) {
callback(false, it); callback(false, it);
}
}, },
waiting: true waiting: true
}; };
@ -200,13 +224,30 @@ Playlist.prototype.addMedia = function(data, callback) {
} }
Playlist.prototype.addMediaList = function(data, callback) { Playlist.prototype.addMediaList = function(data, callback) {
if(data.pos == "next") var start = false;
if(data.pos == "next") {
data.list = data.list.reverse(); data.list = data.list.reverse();
start = data.list[data.list.length - 1];
}
var pl = this; var pl = this;
data.list.forEach(function(x) { data.list.forEach(function(x) {
x.pos = data.pos; x.pos = data.pos;
if(start && x == start) {
pl.addMedia(x, function (err, item) {
if(err) {
callback(err, item);
}
else {
callback(err, item);
pl.current = item;
pl.startPlayback();
}
});
}
else {
pl.addMedia(x, callback); pl.addMedia(x, callback);
}
}); });
} }
@ -333,7 +374,10 @@ Playlist.prototype.jump = function(uid) {
} }
if(it.temp) { if(it.temp) {
this.remove(it.uid); var pl = this;
this.remove(it.uid, function () {
pl.on("remove")(it);
});
} }
return this.current; return this.current;
@ -342,6 +386,7 @@ Playlist.prototype.jump = function(uid) {
Playlist.prototype.clear = function() { Playlist.prototype.clear = function() {
this.items.clear(); this.items.clear();
this.next_uid = 0; this.next_uid = 0;
this.current = null;
clearInterval(this._leadInterval); clearInterval(this._leadInterval);
} }

View File

@ -136,12 +136,12 @@ ULList.prototype.clear = function() {
} }
/* Dump the contents of the list into an array */ /* Dump the contents of the list into an array */
ULList.prototype.toArray = function() { ULList.prototype.toArray = function(pack) {
var arr = new Array(this.length); var arr = new Array(this.length);
var item = this.first; var item = this.first;
var i = 0; var i = 0;
while(item !== null) { while(item !== null) {
if(typeof item.pack == "function") if(pack !== false && typeof item.pack == "function")
arr[i++] = item.pack(); arr[i++] = item.pack();
else else
arr[i++] = item; arr[i++] = item;

View File

@ -749,9 +749,7 @@ Callbacks = {
} }
li.addClass("queue_active"); li.addClass("queue_active");
$("#queue").scrollTop(0); scrollQueue();
var scroll = li.position().top - $("#queue").position().top;
$("#queue").scrollTop(scroll);
return true; return true;
}, },
can_wait: true can_wait: true

View File

@ -204,6 +204,17 @@ function addUserDropdown(entry, name) {
/* queue stuff */ /* queue stuff */
function scrollQueue() {
var li = playlistFind(PL_CURRENT);
if(!li)
return;
li = $(li);
$("#queue").scrollTop(0);
var scroll = li.position().top - $("#queue").position().top;
$("#queue").scrollTop(scroll);
}
function makeQueueEntry(item, addbtns) { function makeQueueEntry(item, addbtns) {
var video = item.media; var video = item.media;
var li = $("<li/>"); var li = $("<li/>");