Resolve merge conflict

This commit is contained in:
calzoneman 2013-10-01 23:05:02 -05:00
commit 9d35636be2
12 changed files with 508 additions and 487 deletions

View File

@ -1,3 +1,17 @@
Tue Oct 01 22:57 2013 CDT
* lib/asyncqueue.js: Add a generalized queue class for queueing async
functions to execute in order
* lib/channel.js, lib/playlist.js: Clean up playlist addition/move/
deletion. Should fix #285.
* lib/server.js: Fix announcement initialization to null
* tests/naokosimulator2013.js: Add a simple bot that authenticates
and vomits a giant list of videos into a channel
* www/assets/js/callbacks.js, www/assets/js/util.js: Clean up
playlist add/move/delete
* www/assets/js/data.js: Update CL_VERSION which I always forget
to do.
* www/assets/js/ui.js: Small fix to comply with server changes
Sun Sep 29 21:30 2013 CDT Sun Sep 29 21:30 2013 CDT
* lib/get-info.js: Add an extra check to ytv2 to make sure embedding is * lib/get-info.js: Add an extra check to ytv2 to make sure embedding is
allowed. If not, send a queueFail. allowed. If not, send a queueFail.
@ -140,7 +154,7 @@ Wed Sep 11 20:19 2013 CDT
allow for changing the permission to view obscured polls. allow for changing the permission to view obscured polls.
Tue Sep 10 22:40 2013 CDT Tue Sep 10 22:40 2013 CDT
* www/account.html, www/acp.html, www/login.html: * www/account.html, www/acp.html, www/login.html:
Import www/assets/js/data.js for easy access to user preferences Import www/assets/js/data.js for easy access to user preferences
* www/assets/js/account.js, www/assets/js/acp.js: remove redundant * www/assets/js/account.js, www/assets/js/acp.js: remove redundant
cookie util functions cookie util functions

65
lib/asyncqueue.js Normal file
View File

@ -0,0 +1,65 @@
/*
The MIT License (MIT)
Copyright (c) 2013 Calvin Montgomery
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
var AsyncQueue = function () {
this._q = [];
this._lock = false;
this._tm = 0;
};
AsyncQueue.prototype.next = function () {
if (this._q.length > 0) {
if (!this.lock())
return;
var item = this._q.shift();
var fn = item[0], tm = item[1];
this._tm = Date.now() + item[1];
fn(this);
}
};
AsyncQueue.prototype.lock = function () {
if (this._lock) {
if (this._tm > 0 && Date.now() > this._tm) {
this._tm = 0;
return true;
}
return false;
}
this._lock = true;
return true;
};
AsyncQueue.prototype.release = function () {
var self = this;
if (!self._lock)
return false;
self._lock = false;
setImmediate(function () {
self.next();
});
return true;
};
AsyncQueue.prototype.queue = function (fn) {
var self = this;
self._q.push([fn, 20000]);
self.next();
};
AsyncQueue.prototype.reset = function () {
this._q = [];
this._lock = false;
};
module.exports = AsyncQueue;

View File

@ -22,6 +22,7 @@ var Playlist = require("./playlist");
var sanitize = require("validator").sanitize; var sanitize = require("validator").sanitize;
var $util = require("./utilities"); var $util = require("./utilities");
var url = require("url"); var url = require("url");
var AsyncQueue = require("./asyncqueue");
var Channel = function(name, Server) { var Channel = function(name, Server) {
var self = this; var self = this;
@ -36,6 +37,7 @@ var Channel = function(name, Server) {
self.registered = false; self.registered = false;
self.users = []; self.users = [];
self.playlist = new Playlist(self); self.playlist = new Playlist(self);
self.plqueue = new AsyncQueue();
self.position = -1; self.position = -1;
self.drinks = 0; self.drinks = 0;
self.leader = null; self.leader = null;
@ -1313,7 +1315,7 @@ Channel.prototype.tryQueue = function(user, data) {
return; return;
} }
if(data.pos == "next" && !this.hasPermission(user, "playlistnext")) { if (data.pos === "next" && !this.hasPermission(user, "playlistnext")) {
return; return;
} }
@ -1334,16 +1336,34 @@ Channel.prototype.tryQueue = function(user, data) {
return; return;
} }
if(typeof data.title !== "string") if (typeof data.title !== "string" || data.type !== "cu")
data.title = false; data.title = false;
data.queueby = user ? user.name : ""; data.queueby = user ? user.name : "";
data.temp = !this.hasPermission(user, "addnontemp"); data.temp = !this.hasPermission(user, "addnontemp");
if(data.list) if (data.list) {
this.addMediaList(data, user); if (data.pos === "next") {
else data.list.reverse();
if (this.playlist.items.length === 0)
data.list.unshift(data.list.pop());
}
var i = 0;
var self = this;
var next = function () {
if (self.dead)
return;
if (i < data.list.length) {
data.list[i].pos = data.pos;
self.tryQueue(user, data.list[i]);
i++;
setTimeout(next, 2000);
}
};
next();
} else {
this.addMedia(data, user); this.addMedia(data, user);
}
} }
Channel.prototype.addMedia = function(data, user) { Channel.prototype.addMedia = function(data, user) {
@ -1371,158 +1391,153 @@ Channel.prototype.addMedia = function(data, user) {
data.maxlength = self.hasPermission(user, "exceedmaxlength") data.maxlength = self.hasPermission(user, "exceedmaxlength")
? 0 ? 0
: this.opts.maxlength; : this.opts.maxlength;
if (data.pos === "end")
data.pos = "append";
var postAdd = function (item, cached) { if (data.type === "cu" && data.title) {
if (self.dead) 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; 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", { self.sendAll("queue", {
item: item.pack(), item: item.pack(),
after: item.prev ? item.prev.uid : "prepend" after: item.prev ? item.prev.uid : "prepend"
}); });
self.broadcastPlaylistMeta(); self.broadcastPlaylistMeta();
if(!cached && !item.temp) if (!c && !item.temp)
self.cacheMedia(item.media); self.cacheMedia(item.media);
} q.release();
};
// No need to check library for livestreams - they aren't cached // special case for youtube playlists
if(isLive(data.type)) { if (data.type === "yp") {
self.playlist.addMedia(data, function (err, data) { self.plqueue.queue(function (q) {
if (self.dead) if (self.dead)
return; return;
self.server.infogetter.getMedia(data.id, data.type,
function (e, vids) {
if (e) {
user.socket.emit("queueFail", e);
q.release();
return;
}
if(err) { if (data.pos === "next") {
if(err === true) vids.reverse();
err = false; if (self.playlist.length === 0)
if(user) vids.unshift(vids.pop());
user.socket.emit("queueFail", err); }
return;
} var fake = { release: function () { } };
else { var cb = afterData.bind(self, fake, false);
postAdd(data, false); for (var i = 0; i < vids.length; i++) {
} cb(vids[i]);
}
q.release();
});
}); });
return; return;
} }
// Don't search library if the channel isn't registered // Don't check library for livestreams or if the channel is
if(!self.registered) { // unregistered
self.playlist.addMedia(data, function(err, item) { 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;
}
cb(m);
});
});
} else {
self.server.db.getLibraryItem(self.name, data.id,
function (err, item) {
if (self.dead) if (self.dead)
return; return;
if(err) { if (err) {
if(err === true) user.socket.emit("queueFail", "Internal error: " + err);
err = false;
if(user)
user.socket.emit("queueFail", err);
return; 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 { } 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) {
var chan = this;
this.playlist.addMediaList(data, function(err, item) {
if (chan.dead)
return;
if(err) {
if(err === true)
err = false;
if(user)
user.socket.emit("queueFail", err);
return;
}
else {
chan.logger.log("### " + user.name + " queued " + item.media.title);
item.temp = data.temp;
item.queueby = data.queueby;
chan.sendAll("queue", {
item: item.pack(),
after: item.prev ? item.prev.uid : "prepend"
});
chan.broadcastPlaylistMeta();
if(!item.temp)
chan.cacheMedia(item.media);
}
});
}
Channel.prototype.tryQueuePlaylist = function(user, data) { Channel.prototype.tryQueuePlaylist = function(user, data) {
var self = this; var self = this;
if(!this.hasPermission(user, "playlistaddlist")) { if (!self.hasPermission(user, "playlistaddlist")) {
return; return;
} }
if(typeof data.name != "string" || if(typeof data.name !== "string" ||
typeof data.pos != "string") { typeof data.pos !== "string") {
return; return;
} }
if(data.pos == "next" && !this.hasPermission(user, "playlistnext")) { if (data.pos == "next" && !this.hasPermission(user, "playlistnext")) {
return; return;
} }
@ -1531,16 +1546,24 @@ Channel.prototype.tryQueuePlaylist = function(user, data) {
if (self.dead) if (self.dead)
return; return;
if(err) { if (err) {
user.socket.emit("errorMsg", { user.socket.emit("errorMsg", {
msg: "Playlist load failed: " + err msg: "Playlist load failed: " + err
}); });
return; return;
} }
data.list = pl;
data.queueby = user.name; if (data.pos === "next") {
data.temp = !self.hasPermission(user, "addnontemp"); pl.reverse();
self.addMediaList(data, user); if (self.playlist.items.length === 0)
pl.unshift(pl.pop());
}
for (var i = 0; i < pl.length; i++) {
pl[i].pos = data.pos;
pl[i].temp = !self.hasPermission(user, "addnontemp");
self.addMedia(pl[i], user);
}
}); });
} }
@ -1572,15 +1595,20 @@ Channel.prototype.trySetTemp = function(user, data) {
Channel.prototype.dequeue = function(uid) { Channel.prototype.dequeue = function(uid) {
var chan = this; var self = this;
function afterDelete() { self.plqueue.queue(function (q) {
chan.sendAll("delete", { if (self.dead)
uid: uid return;
});
chan.broadcastPlaylistMeta(); if (self.playlist.remove(uid)) {
} self.sendAll("delete", {
if(!this.playlist.remove(uid, afterDelete)) uid: uid
return; });
self.broadcastPlaylistMeta();
}
q.release();
});
} }
Channel.prototype.tryDequeue = function(user, data) { Channel.prototype.tryDequeue = function(user, data) {
@ -1651,6 +1679,7 @@ Channel.prototype.tryJumpTo = function(user, data) {
Channel.prototype.clearqueue = function() { Channel.prototype.clearqueue = function() {
this.playlist.clear(); this.playlist.clear();
this.plqueue.reset();
this.sendAll("playlist", this.playlist.items.toArray()); this.sendAll("playlist", this.playlist.items.toArray());
this.broadcastPlaylistMeta(); this.broadcastPlaylistMeta();
} }
@ -1668,6 +1697,7 @@ Channel.prototype.shufflequeue = function() {
var n = []; var n = [];
var pl = this.playlist.items.toArray(false); var pl = this.playlist.items.toArray(false);
this.playlist.clear(); this.playlist.clear();
this.plqueue.reset();
while(pl.length > 0) { while(pl.length > 0) {
var i = parseInt(Math.random() * pl.length); var i = parseInt(Math.random() * pl.length);
var item = this.playlist.makeItem(pl[i].media); var item = this.playlist.makeItem(pl[i].media);
@ -1717,32 +1747,36 @@ Channel.prototype.tryUpdate = function(user, data) {
} }
Channel.prototype.move = function(data, user) { Channel.prototype.move = function(data, user) {
var chan = this; var self = this;
function afterMove() { self.plqueue.queue(function (q) {
if (chan.dead) if (self.dead)
return; return;
var moveby = user && user.name ? user.name : null; if (self.playlist.move(data.from, data.after)) {
if(typeof data.moveby !== "undefined") var moveby = user && user.name ? user.name : null;
moveby = data.moveby; if (typeof data.moveby !== "undefined")
moveby = data.moveby;
var fromit = chan.playlist.items.find(data.from); var fromit = self.playlist.items.find(data.from);
var afterit = chan.playlist.items.find(data.after); var afterit = self.playlist.items.find(data.after);
var aftertitle = afterit && afterit.media ? afterit.media.title : ""; var aftertitle = (afterit && afterit.media)
if(fromit) { ? afterit.media.title : "";
chan.logger.log("### " + user.name + " moved " + fromit.media.title if (fromit) {
+ (aftertitle ? " after " + aftertitle : "")); self.logger.log("### " + user.name + " moved " +
fromit.media.title +
(aftertitle ? " after " + aftertitle : ""));
}
self.sendAll("moveVideo", {
from: data.from,
after: data.after,
moveby: moveby
});
} }
chan.sendAll("moveVideo", { q.release();
from: data.from, });
after: data.after,
moveby: moveby
});
}
this.playlist.move(data.from, data.after, afterMove);
} }
Channel.prototype.tryMove = function(user, data) { Channel.prototype.tryMove = function(user, data) {

View File

@ -23,6 +23,7 @@ module.exports = function (Server) {
// This should cut down on needing to restart the server // This should cut down on needing to restart the server
var d = domain.create(); var d = domain.create();
d.on("error", function (err) { d.on("error", function (err) {
Logger.errlog.log(err.trace());
Logger.errlog.log("urlRetrieve failed: " + err); Logger.errlog.log("urlRetrieve failed: " + err);
Logger.errlog.log("Request was: " + options.host + options.path); Logger.errlog.log("Request was: " + options.host + options.path);
callback(503, err); callback(503, err);
@ -81,14 +82,18 @@ module.exports = function (Server) {
} else if(status === 403) { } else if(status === 403) {
callback("Private video", null); callback("Private video", null);
return; return;
} else if(status === 400) {
callback("Invalid video", null);
return;
} else if(status === 503) { } else if(status === 503) {
callback("API failure", null); callback("API failure", null);
return; return;
} else if(status !== 200) { } else if(status !== 200) {
callback(true, null); callback("HTTP " + status, null);
return; return;
} }
var buffer = data;
try { try {
data = JSON.parse(data); data = JSON.parse(data);
if (data.entry.yt$accessControl) { if (data.entry.yt$accessControl) {
@ -611,6 +616,8 @@ module.exports = function (Server) {
getMedia: function (id, type, callback) { getMedia: function (id, type, callback) {
if(type in this.Getters) { if(type in this.Getters) {
this.Getters[type](id, callback); this.Getters[type](id, callback);
} else {
callback("Unknown media type '" + type + "'", null);
} }
} }
}; };

View File

@ -10,6 +10,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
*/ */
ULList = require("./ullist").ULList; ULList = require("./ullist").ULList;
var AsyncQueue = require("./asyncqueue");
var Media = require("./media").Media; var Media = require("./media").Media;
var AllPlaylists = {}; var AllPlaylists = {};
@ -50,9 +51,7 @@ function Playlist(chan) {
"mediaUpdate": [], "mediaUpdate": [],
"remove": [], "remove": [],
}; };
this.lock = false; this.fnqueue = new AsyncQueue();
this.action_queue = [];
this._qaInterval = false;
AllPlaylists[name] = this; AllPlaylists[name] = this;
this.channel = chan; this.channel = chan;
@ -86,28 +85,6 @@ function Playlist(chan) {
}); });
} }
Playlist.prototype.queueAction = function(data) {
this.action_queue.push(data);
if(this._qaInterval)
return;
var pl = this;
this._qaInterval = setInterval(function() {
var data = pl.action_queue.shift();
if(data.waiting) {
if(!("expire" in data))
data.expire = Date.now() + 10000;
if(Date.now() < data.expire)
pl.action_queue.unshift(data);
}
else
data.fn();
if(pl.action_queue.length == 0) {
clearInterval(pl._qaInterval);
pl._qaInterval = false;
}
}, 100);
}
Playlist.prototype.dump = function() { Playlist.prototype.dump = function() {
var arr = this.items.toArray(); var arr = this.items.toArray();
var pos = 0; var pos = 0;
@ -222,204 +199,54 @@ Playlist.prototype.add = function(item, pos) {
return false; return false;
} }
Playlist.prototype.addCachedMedia = function(data, callback) { Playlist.prototype.addMedia = function (data) {
var pos = "append"; var pos = data.pos;
if(data.pos == "next") { if (pos === "next") {
if(!this.current) if (this.current !== null)
pos = "prepend";
else
pos = this.current.uid; pos = this.current.uid;
else
pos = "append";
} }
var it = this.makeItem(data.media); var m = new Media(data.id, data.title, data.seconds, data.type);
it.temp = data.temp; var item = this.makeItem(m);
it.queueby = data.queueby; item.queueby = data.queueby;
item.temp = data.temp;
var pl = this; return {
item: item,
var action = { error: this.add(item, pos)
fn: function() {
var err = pl.add(it, pos);
callback(err, err ? null : it);
},
waiting: false
}; };
this.queueAction(action); };
}
Playlist.prototype.addMedia = function(data, callback) { Playlist.prototype.remove = function (uid) {
var self = this;
if(data.type == "yp") { var item = self.items.find(uid);
this.addYouTubePlaylist(data, callback); if (item && self.items.remove(uid)) {
return; if (item === self.current) {
} setImmediate(function () { self._next(); });
var pos = "append";
if(data.pos == "next") {
if(!this.current)
pos = "prepend";
else
pos = this.current.uid;
}
var it = this.makeItem(null);
var pl = this;
var action = {
fn: function() {
var err = pl.add(it, pos);
callback(err, err ? null : it);
},
waiting: true
};
this.queueAction(action);
// Pre-cached data
if(typeof data.title === "string" &&
typeof data.seconds === "number") {
if(data.maxlength && data.seconds > data.maxlength) {
action.expire = 0;
callback("Media is too long!", null);
return;
}
it.media = new Media(data.id, data.title, data.seconds, data.type);
action.waiting = false;
return;
}
this.server.infogetter.getMedia(data.id, data.type, function(err, media) {
if(err) {
action.expire = 0;
callback(err, null);
return;
}
if(data.maxlength && media.seconds > data.maxlength) {
action.expire = 0;
callback("Media is too long!", null);
return;
}
it.media = media;
it.temp = data.temp;
it.queueby = data.queueby;
action.waiting = false;
});
}
Playlist.prototype.addMediaList = function(data, callback) {
var start = false;
if(data.pos == "next") {
data.list = data.list.reverse();
start = data.list[data.list.length - 1];
}
if(this.items.length != 0)
start = false;
var pl = this;
for(var i = 0; i < data.list.length; i++) {
var x = data.list[i];
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);
} }
return true;
} else {
return false;
} }
} }
Playlist.prototype.addYouTubePlaylist = function(data, callback) { Playlist.prototype.move = function (from, after) {
var pos = "append";
if(data.pos == "next") {
if(!this.current)
pos = "prepend";
else
pos = this.current.uid;
}
var pl = this;
this.server.infogetter.getMedia(data.id, data.type, function(err, vids) {
if(err) {
callback(err, null);
return;
}
if(data.pos === "next")
vids.reverse();
vids.forEach(function(media) {
if(data.maxlength && media.seconds > data.maxlength) {
callback("Media is too long!", null);
return;
}
var it = pl.makeItem(media);
it.temp = data.temp;
it.queueby = data.queueby;
pl.queueAction({
fn: function() {
var err = pl.add(it, pos);
callback(err, err ? null : it);
},
});
});
});
}
Playlist.prototype.remove = function(uid, callback) {
var pl = this;
this.queueAction({
fn: function() {
var item = pl.items.find(uid);
if(pl.items.remove(uid)) {
if(callback)
callback();
if(item == pl.current)
pl._next();
}
},
waiting: false
});
}
Playlist.prototype.move = function(from, after, callback) {
var pl = this;
this.queueAction({
fn: function() {
pl._move(from, after, callback);
},
waiting: false
});
}
Playlist.prototype._move = function(from, after, callback) {
var it = this.items.find(from); var it = this.items.find(from);
if(!this.items.remove(from)) if (!this.items.remove(from))
return; return false;
if(after === "prepend") { if (after === "prepend") {
if(!this.items.prepend(it)) if (!this.items.prepend(it))
return; return false;
} else if (after === "append") {
if (!this.items.append(it))
return false;
} else if (!this.items.insertAfter(it, after)) {
return false;
} }
else if(after === "append") { return true;
if(!this.items.append(it))
return;
}
else if(!this.items.insertAfter(it, after))
return;
callback();
} }
Playlist.prototype.next = function() { Playlist.prototype.next = function() {
@ -428,13 +255,11 @@ Playlist.prototype.next = function() {
var it = this.current; var it = this.current;
if(it.temp) { if (it.temp) {
var pl = this; if (this.remove(it.uid)) {
this.remove(it.uid, function() { this.on("remove")(it);
pl.on("remove")(it); }
}); } else {
}
else {
this._next(); this._next();
} }
@ -470,10 +295,9 @@ Playlist.prototype.jump = function(uid) {
} }
if(it.temp) { if(it.temp) {
var pl = this; if (this.remove(it.uid)) {
this.remove(it.uid, function () { this.on("remove")(it);
pl.on("remove")(it); }
});
} }
return this.current; return this.current;
@ -599,10 +423,12 @@ Playlist.prototype.clean = function (filter) {
if (count < matches.length) { if (count < matches.length) {
var uid = matches[count].uid; var uid = matches[count].uid;
count++; count++;
self.channel.sendAll("delete", { if (self.remove(uid)) {
uid: uid self.channel.sendAll("delete", {
}); uid: uid
self.remove(uid, deleteNext); });
}
deleteNext();
} else { } else {
// refresh meta only once, at the end // refresh meta only once, at the end
self.channel.broadcastPlaylistMeta(); self.channel.broadcastPlaylistMeta();

View File

@ -8,7 +8,7 @@ var Logger = require("./logger");
var Channel = require("./channel"); var Channel = require("./channel");
var User = require("./user"); var User = require("./user");
const VERSION = "2.4.2"; const VERSION = "2.4.3";
function getIP(req) { function getIP(req) {
var raw = req.connection.remoteAddress; var raw = req.connection.remoteAddress;
@ -79,6 +79,7 @@ var Server = {
db: null, db: null,
ips: {}, ips: {},
acp: null, acp: null,
announcement: null,
httpaccess: null, httpaccess: null,
actionlog: null, actionlog: null,
logHTTP: function (req, status) { logHTTP: function (req, status) {

View File

@ -2,7 +2,7 @@
"author": "Calvin Montgomery", "author": "Calvin Montgomery",
"name": "CyTube", "name": "CyTube",
"description": "Online media synchronizer and chat", "description": "Online media synchronizer and chat",
"version": "2.4.2", "version": "2.4.3",
"repository": { "repository": {
"url": "http://github.com/calzoneman/sync" "url": "http://github.com/calzoneman/sync"
}, },

View File

@ -0,0 +1,38 @@
var fs = require('fs');
var io = require('socket.io-client');
var socket = io.connect('http://localhost:1337');
socket.on('connect', function () {
socket.emit('login', { name: 'test', pw: 'test' });
socket.emit('joinChannel', { name: 'test' });
});
socket.on('queueFail', function (msg) {
console.log(msg);
});
/* Stress test adding a lot of videos in a very short timespan */
function testAddVideos() {
var pl = fs.readFileSync('largepl.json') + '';
pl = JSON.parse(pl);
var ids = [];
for (var i = 0; i < pl.length; i++) {
if (pl[i].type === 'yt')
ids.push(pl[i].id);
}
for (var i = 0; i < ids.length; i++) {
(function (i) {
setTimeout(function () {
console.log('queue', ids[i]);
socket.emit('queue', {
id: ids[i],
type: 'yt',
pos: 'end'
});
}, 1050 * i);
})(i);
}
}
testAddVideos();

View File

@ -764,43 +764,64 @@ Callbacks = {
}, },
queue: function(data) { queue: function(data) {
queueAction({ PL_ACTION_QUEUE.queue(function (plq) {
fn: function () { var li = makeQueueEntry(data.item, true);
var li = makeQueueEntry(data.item, true); if (data.item.uid === PL_CURRENT)
li.hide(); li.addClass("queue_active");
var q = $("#queue"); li.hide();
li.attr("title", data.item.queueby var q = $("#queue");
? ("Added by: " + data.item.queueby) li.attr("title", data.item.queueby
: "Added by: Unknown"); ? ("Added by: " + data.item.queueby)
if(data.after === "prepend") { : "Added by: Unknown");
li.prependTo(q); if (data.after === "prepend") {
li.show("blind"); li.prependTo(q);
return true; li.show("blind", function () {
} plq.release();
else if(data.after === "append") { });
li.appendTo(q); } else if (data.after === "append") {
li.show("blind"); li.appendTo(q);
return true; li.show("blind", function () {
} plq.release();
else { });
var liafter = playlistFind(data.after); } else {
if(!liafter) { var liafter = playlistFind(data.after);
return false; if (!liafter) {
} plq.release();
li.insertAfter(liafter); return;
li.show("blind");
return true;
} }
li.insertAfter(liafter);
li.show("blind", function () {
plq.release();
});
} }
}); });
}, },
queueFail: function(data) { queueFail: function(data) {
if(!data) { if (!data || data === true) {
data = "Queue failed. Check your link to make sure it is valid."; data = "Queue failed. Check your link to make sure it is valid.";
} }
var alerts = $(".qfalert");
for (var i = 0; i < alerts.length; i++) {
var al = $(alerts[i]);
var cl = al.clone();
cl.children().remove();
if (cl.text() === data) {
var tag = al.find(".label-important");
if (tag.length > 0) {
var count = parseInt(tag.text().match(/\d+/)[0]) + 1;
tag.text(tag.text().replace(/\d+/, ""+count));
} else {
$("<span/>")
.addClass("label label-important pull-right")
.text("+ 1 more")
.appendTo(al);
}
return;
}
}
makeAlert("Error", data, "alert-error") makeAlert("Error", data, "alert-error")
.addClass("span12") .addClass("span12 qfalert")
.insertBefore($("#extended_controls")); .insertBefore($("#extended_controls"));
}, },
@ -829,52 +850,40 @@ Callbacks = {
}, },
"delete": function(data) { "delete": function(data) {
queueAction({ PL_ACTION_QUEUE.queue(function (plq) {
fn: function () { PL_WAIT_SCROLL = true;
PL_WAIT_SCROLL = true; var li = $(".pluid-" + data.uid);
var li = $(".pluid-" + data.uid); li.hide("blind", function() {
li.hide("blind", function() { li.remove();
li.remove(); plq.release();
PL_WAIT_SCROLL = false; PL_WAIT_SCROLL = false;
}); });
return true;
}
}); });
}, },
moveVideo: function(data) { moveVideo: function(data) {
if(data.moveby != CLIENT.name) { if (data.moveby != CLIENT.name) {
queueAction({ PL_ACTION_QUEUE.queue(function (plq) {
fn: function () { playlistMove(data.from, data.after, function () {
playlistMove(data.from, data.after); plq.release();
return true; });
}
}); });
} }
}, },
setCurrent: function(uid) { setCurrent: function(uid) {
queueAction({ PL_CURRENT = uid;
fn: function () { $("#queue li").removeClass("queue_active");
PL_CURRENT = uid; var li = $(".pluid-" + uid);
var qli = $("#queue li"); if (li.length !== 0) {
qli.removeClass("queue_active"); li.addClass("queue_active");
var li = $(".pluid-" + uid); var tmr = setInterval(function () {
if(li.length == 0) { if (!PL_WAIT_SCROLL) {
return false; scrollQueue();
clearInterval(tmr);
} }
}, 100);
li.addClass("queue_active"); }
var timer = setInterval(function () {
if(!PL_WAIT_SCROLL) {
scrollQueue();
clearInterval(timer);
}
}, 100);
return true;
},
can_wait: true
});
}, },
changeMedia: function(data) { changeMedia: function(data) {

View File

@ -9,7 +9,7 @@ The above copyright notice and this permission notice shall be included in all c
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
var CL_VERSION = "2.0.0"; var CL_VERSION = "2.4.3";
var CLIENT = { var CLIENT = {
rank: -1, rank: -1,
@ -44,9 +44,6 @@ if($("#videowidth").length > 0) {
VWIDTH = $("#videowidth").css("width").replace("px", ""); VWIDTH = $("#videowidth").css("width").replace("px", "");
VHEIGHT = ""+parseInt(parseInt(VWIDTH) * 9 / 16); VHEIGHT = ""+parseInt(parseInt(VWIDTH) * 9 / 16);
} }
var PL_MOVING = false;
var PL_ADDING = false;
var PL_DELETING = false;
var REBUILDING = false; var REBUILDING = false;
var socket = { var socket = {
emit: function() { emit: function() {

View File

@ -323,9 +323,6 @@ function queue(pos) {
return; return;
} }
var links = $("#mediaurl").val().split(","); var links = $("#mediaurl").val().split(",");
if(pos == "next") {
links = links.reverse();
}
var parsed = []; var parsed = [];
links.forEach(function(link) { links.forEach(function(link) {
var data = parseMediaLink(link); var data = parseMediaLink(link);

View File

@ -1191,29 +1191,58 @@ function addLibraryButtons(li, id, source) {
/* queue stuff */ /* queue stuff */
var PL_QUEUED_ACTIONS = []; var AsyncQueue = function () {
var PL_ACTION_INTERVAL = false; this._q = [];
this._lock = false;
this._tm = 0;
};
function queueAction(data) { AsyncQueue.prototype.next = function () {
PL_QUEUED_ACTIONS.push(data); if (this._q.length > 0) {
if(PL_ACTION_INTERVAL) if (!this.lock())
return; return;
PL_ACTION_INTERVAL = setInterval(function () { var item = this._q.shift();
var data = PL_QUEUED_ACTIONS.shift(); var fn = item[0], tm = item[1];
if(!("expire" in data)) this._tm = Date.now() + item[1];
data.expire = Date.now() + 5000; fn(this);
if(!data.fn()) { }
if(data.can_wait && Date.now() < data.expire) };
PL_QUEUED_ACTIONS.push(data);
else if(Date.now() < data.expire) AsyncQueue.prototype.lock = function () {
PL_QUEUED_ACTIONS.unshift(data); if (this._lock) {
if (this._tm > 0 && Date.now() > this._tm) {
this._tm = 0;
return true;
} }
if(PL_QUEUED_ACTIONS.length == 0) { return false;
clearInterval(PL_ACTION_INTERVAL); }
PL_ACTION_INTERVAL = false;
} this._lock = true;
}, 100); return true;
} };
AsyncQueue.prototype.release = function () {
var self = this;
if (!self._lock)
return false;
self._lock = false;
self.next();
return true;
};
AsyncQueue.prototype.queue = function (fn) {
var self = this;
self._q.push([fn, 20000]);
self.next();
};
AsyncQueue.prototype.reset = function () {
this._q = [];
this._lock = false;
};
var PL_ACTION_QUEUE = new AsyncQueue();
// Because jQuery UI does weird things // Because jQuery UI does weird things
function playlistFind(uid) { function playlistFind(uid) {
@ -1227,10 +1256,12 @@ function playlistFind(uid) {
return false; return false;
} }
function playlistMove(from, after) { function playlistMove(from, after, cb) {
var lifrom = $(".pluid-" + from); var lifrom = $(".pluid-" + from);
if(lifrom.length == 0) if(lifrom.length == 0) {
return false; cb(false);
return;
}
var q = $("#queue"); var q = $("#queue");
@ -1238,24 +1269,26 @@ function playlistMove(from, after) {
lifrom.hide("blind", function() { lifrom.hide("blind", function() {
lifrom.detach(); lifrom.detach();
lifrom.prependTo(q); lifrom.prependTo(q);
lifrom.show("blind"); lifrom.show("blind", cb);
}); });
} }
else if(after === "append") { else if(after === "append") {
lifrom.hide("blind", function() { lifrom.hide("blind", function() {
lifrom.detach(); lifrom.detach();
lifrom.appendTo(q); lifrom.appendTo(q);
lifrom.show("blind"); lifrom.show("blind", cb);
}); });
} }
else { else {
var liafter = $(".pluid-" + after); var liafter = $(".pluid-" + after);
if(liafter.length == 0) if(liafter.length == 0) {
return false; cb(false);
return;
}
lifrom.hide("blind", function() { lifrom.hide("blind", function() {
lifrom.detach(); lifrom.detach();
lifrom.insertAfter(liafter); lifrom.insertAfter(liafter);
lifrom.show("blind"); lifrom.show("blind", cb);
}); });
} }
} }