From a6c579165df7dcb94bb5077cc06b6fc6b0d55609 Mon Sep 17 00:00:00 2001 From: Miikka Lahtinen Date: Fri, 26 Aug 2022 09:04:41 +0300 Subject: [PATCH 1/9] Remove all old playlist iterations in favor of new functions --- docker/node/server.js | 246 +++++++++---------- web/js/callbacks.js | 10 +- web/js/functions.js | 175 ++++++------- web/js/init.js | 158 ++++++------ web/plugins/videoBlacklist/videoBlacklist.js | 14 +- 5 files changed, 284 insertions(+), 319 deletions(-) diff --git a/docker/node/server.js b/docker/node/server.js index 74414f5c..f756195a 100644 --- a/docker/node/server.js +++ b/docker/node/server.js @@ -197,6 +197,75 @@ LinkedList.Circular.prototype.toArray = function () { return out; }; + +LinkedList.Circular.prototype.some = function(cb) { + return this.find(cb) !== null; +} + +LinkedList.Circular.prototype.find = function(cb) { + let video = this.first; + + for (let i = 0; i < this.length; i++) { + if (cb(video, i)) { + return video; + } + + video = video.next; + } + + return null; +} +LinkedList.Circular.prototype.multiple = function(indexes) { + let map = new Map( + indexes.map(i => [i, null]) + ); + + let video = this.first; + let max = Math.max(...indexes); + + for (let i = 0; i <= max; i++) { + if (map.has(i)) { + map.set(i, video); + } + + video = video.next; + } + + return indexes.map(i => map.get(i)); +} +LinkedList.Circular.prototype.at = function(index) { + let video = this.first; + + for (let i = 0; i < index; i++) { + video = video.next; + } + + return video; +} + +LinkedList.Circular.prototype.each = function(cb) { + let video = this.first; + + for (let i = 0; i < this.length; i++) { + cb(video, i); + video = video.next; + } +} + +LinkedList.Circular.prototype.indexOf = function (cb) { + let index = -1; + + this.find((video, idx) => { + if (cb(video)) { + index = idx; + } + + return index > -1; + }); + + return index; +} + /* VAR INIT */ SERVER.PLAYLIST = new LinkedList.Circular(); SERVER.ACTIVE = null; @@ -279,20 +348,17 @@ function initPlaylist(callback) { } function initResumePosition(callback) { getMisc({ name: 'server_active_videoid' }, function (old_videoid) { - var elem = SERVER.PLAYLIST.first; - for (var i = 0; i < SERVER.PLAYLIST.length; i++) { - if (elem.videoid == old_videoid) { - SERVER.ACTIVE = elem; - getMisc({ name: 'server_time' }, function (old_time) { - if (+old_time) { - SERVER.TIME = +old_time + 1; - } - if (callback) { callback(); } - }); - return; - } - elem = elem.next; + SERVER.ACTIVE = PLAYLIST.find(video => video.videoid() === old_videoid); + + if (SERVER.ACTIVE) { + getMisc({ name: 'server_time' }, function (old_time) { + if (+old_time) { + SERVER.TIME = +old_time + 1; + } + if (callback) { callback(); } + }); } + if (callback) { callback(); } }); } @@ -481,14 +547,14 @@ function doorStuck(socket) { } function playNext() { const active = { - position: getVideoPosition(SERVER.ACTIVE), + position: SERVER.PLAYLIST.indexOf(SERVER.ACTIVE), node: SERVER.ACTIVE }; SERVER.ACTIVE = SERVER.ACTIVE.next; if (!active.node.volat && 'colorTagVolat' in active.node.meta) { - _setVideoColorTag(active.node, active.position, false, false); + setVideoColorTag(active.node, active.position, false, false); } if (active.node.volat) { @@ -658,17 +724,15 @@ function kickUserByNick(socket, nick, reason) { sessionService.forNick(nick, session => session.kick(reason, getSocketName(socket))); } var commit = function () { - var elem = SERVER.PLAYLIST.first; - for (var i = 0; i < SERVER.PLAYLIST.length; i++) { + SERVER.PLAYLIST.each(video => { var sql = `update ${SERVER.dbcon.video_table} set position = ? where videoid = ?`; - mysql.query(sql, [i, '' + elem.videoid], function (err) { + mysql.query(sql, [i, '' + video.videoid], function (err) { if (err) { DefaultLog.error(events.EVENT_DB_QUERY, "query \"{sql}\" failed", { sql }, err); return; } }); - elem = elem.next; - } + }); for (var i = 0; i < SERVER.AREAS.length; i++) { var sql = 'update areas set html = ? where name = ?'; @@ -867,13 +931,9 @@ function applyPluginFilters(msg, socket) { return msg; } -function setVideoVolatile(socket, pos, isVolat) { - var elem = SERVER.PLAYLIST.first; - for (var i = 0; i < pos; i++) { - elem = elem.next; - } - elem.volat = isVolat; - +function setVideoVolatile(socket, video, pos, isVolat) { + video.volat = isVolat; + DefaultLog.info(events.EVENT_ADMIN_SET_VOLATILE, "{mod} set {title} to {status}", { mod: getSocketName(socket), type: "playlist", title: decodeURIComponent(elem.videotitle), status: isVolat ? "volatile" : "not volatile" }); @@ -883,14 +943,8 @@ function setVideoVolatile(socket, pos, isVolat) { volat: isVolat }); } -function setVideoColorTag(pos, tag, volat) { - var elem = SERVER.PLAYLIST.first; - for (var i = 0; i < pos; i++) { - elem = elem.next; - } - _setVideoColorTag(elem, pos, tag, volat); -} -function _setVideoColorTag(elem, pos, tag, volat) { + +function setVideoColorTag(elem, pos, tag, volat) { if (tag == false) { delete elem.meta.colorTag; @@ -1342,41 +1396,6 @@ function sendToggleables(socket) { socket.emit("setToggleables", data); } -function getVideoPosition(node) { - let video = SERVER.PLAYLIST.first; - - for (let index = 0; index < SERVER.PLAYLIST.length; index++) { - if (video === node) { - return index; - } - - video = video.next; - } - - return -1; -} - -function getVideoAt(index) { - if (index < 0 || index > SERVER.PLAYLIST.length) { - return null; - } - - let video = SERVER.PLAYLIST.first; - - for (let i = 0; i < SERVER.PLAYLIST.length; i++) { - if (i === index) { - return { - position: index, - node: video - }; - } - - video = video.next; - } - - return null; -} - function saveToHistory(node) { const historyQuery = "insert into videos_history (videoid, videotitle, videolength, videotype, date_added, meta) values (?,?,?,?,NOW(),?)"; const historyQueryParams = [ @@ -2746,29 +2765,19 @@ io.sockets.on('connection', function (ioSocket) { return; } - if (data.from == data.to) { return; } //wat. - if (data.from < 0 || data.to < 0) { return; } //wat. - var elem = SERVER.PLAYLIST.first; - var fromelem, toelem; - for (var i = 0; i < SERVER.PLAYLIST.length; i++) { - if (i == data.from) { - fromelem = elem; - break; - } - elem = elem.next; - } - if (data.sanityid && elem.videoid != data.sanityid) { return doorStuck(socket); } - elem = SERVER.PLAYLIST.first; - for (var i = 0; i < SERVER.PLAYLIST.length; i++) { - if (i == data.to) { - toelem = elem; - break; - } - elem = elem.next; + const [fromelem, toelem] = SERVER.PLAYLIST.multiple([data.from, data.to]); + + if (data.sanityid && fromelem.videoid === data.sanityid) { + doorStuck(); } + SERVER.PLAYLIST.remove(fromelem); - if (data.to > data.from) { SERVER.PLAYLIST.insertAfter(toelem, fromelem); } - else { SERVER.PLAYLIST.insertBefore(toelem, fromelem); } + if (data.to > data.from) { + SERVER.PLAYLIST.insertAfter(toelem, fromelem); + } + else { + SERVER.PLAYLIST.insertBefore(toelem, fromelem); + } io.sockets.emit("sortPlaylist", data); @@ -2782,28 +2791,17 @@ io.sockets.on('connection', function (ioSocket) { return; } - let prev = null; - let next = null; - - let video = SERVER.PLAYLIST.first; - for (let index = 0; index < SERVER.PLAYLIST.length; index++) { - if (video === SERVER.ACTIVE) { - prev = {node: video, position: index}; - } - - if (index === data.index) { - next = {node: video, position: index}; - } - - if (next && prev) { - break; - } - - video = video.next; - } + const prev = { + node: SERVER.ACTIVE, + position: SERVER.PLAYLIST.indexOf(video => video.videoid === node.videoid) + }; + const next = { + node: SERVER.PLAYLIST.at(data.index), + position: data.index + }; //check if we actually got both - if (!next || !prev) { + if (!next.node || prev.position === -1) { return doorStuck(socket); } @@ -2812,7 +2810,7 @@ io.sockets.on('connection', function (ioSocket) { } if (!prev.node.volat && 'colorTagVolat' in prev.node.meta) { - _setVideoColorTag(prev.node, prev.position, false, false); + setVideoColorTag(prev.node, prev.position, false, false); } SERVER.ACTIVE = next.node; @@ -2834,7 +2832,7 @@ io.sockets.on('connection', function (ioSocket) { return; } - const video = getVideoAt(data.index); + const video = SERVER.PLAYLIST.at(data.index); if (video.node.videoid !== data.sanityid) { return doorStuck(socket); @@ -3076,11 +3074,11 @@ io.sockets.on('connection', function (ioSocket) { }); socket.on("fondleVideo", function (data) { // New abstraction for messing with video details - var elem = SERVER.PLAYLIST.first; - for (var i = 0; i < data.info.pos; i++) { - elem = elem.next; + const video = SERVER.PLAYLIST.at(data.info.pos); + + if (data.sanityid && video.videoid != data.sanityid) { + return doorStuck(socket); } - if (data.sanityid && elem.videoid != data.sanityid) { return doorStuck(socket); } if ("action" in data) { if (data.action == "setVolatile") { @@ -3090,9 +3088,7 @@ io.sockets.on('connection', function (ioSocket) { return; } - pos = data.pos; - isVolat = data.volat; - setVideoVolatile(socket, pos, isVolat); + setVideoVolatile(socket, video, data.pos, data.volat); } if (data.action == "setColorTag") { data = data.info; // Drop action name. @@ -3101,10 +3097,12 @@ io.sockets.on('connection', function (ioSocket) { return; } - pos = ("pos" in data ? data.pos : 0); - tag = ("tag" in data ? data.tag : false); - volat = ("volat" in data ? data.volat : false); - setVideoColorTag(pos, tag, volat); + setVideoColorTag( + video, + data.pos, + data.tag, + data.volat + ); } } }); diff --git a/web/js/callbacks.js b/web/js/callbacks.js index 1093447a..0a0eda30 100644 --- a/web/js/callbacks.js +++ b/web/js/callbacks.js @@ -68,17 +68,19 @@ socket.on("renewPos", function (data) { }); socket.on("recvNewPlaylist", function (data) { PLAYLIST = new LinkedList.Circular(); - for (var i in data) { - PLAYLIST.append(data[i]); + for (const video of data) { + PLAYLIST.append(video); } + newPlaylist($("#plul")); socket.emit("renewPos"); }); socket.on("recvPlaylist", function (data) { PLAYLIST = new LinkedList.Circular(); - for (var i in data) { - PLAYLIST.append(data[i]); + for (const video of data) { + PLAYLIST.append(video); } + whenExists("#leftpane", function (obj) { initPlaylist($(obj)); setVal("PLREADY", true); diff --git a/web/js/functions.js b/web/js/functions.js index bcaf74e5..f7603e86 100644 --- a/web/js/functions.js +++ b/web/js/functions.js @@ -988,12 +988,11 @@ function recalcStats() { var timeMan = $("#plstats .totalLength"); var x = 0; - elem = PLAYLIST.first; - dbg(PLAYLIST.first.videolength); - for (var i = 0; i < PLAYLIST.length; i++) { - x += (elem.videolength); - elem = elem.next; - } + + PLAYLIST.each(video => { + x += video.videolength; + }); + timeMan.text(secToTime(x)); numberMan.text(PLAYLIST.length + " Videos"); @@ -1706,46 +1705,48 @@ function addNewMailMessage(nick, msg) { } } function plSearch(term) { + const refresh = (index) => { + smartRefreshScrollbar(); + scrollToPlEntry(); + realignPosHelper(); + }; if (typeof term == "undefined" || term.match(/^$/) || term.length < 3) { $("#playlist").removeClass("searching"); $("#plul li").removeClass("search-hidden"); $("#plul li.history").remove(); $("#plul li .title").removeAttr("active-offset"); - smartRefreshScrollbar(); - scrollToPlEntry(ACTIVE.domobj.index()); - realignPosHelper(); - } else { - if (TYPE >= 1 || (LEADER && getToggleable('berryqueue'))) { - socket.emit('searchHistory', { search: term }); - } - - $("#playlist").addClass("searching"); - $("#plul li").addClass("search-hidden"); - $("#plul li.active").removeClass("search-hidden"); - elem = PLAYLIST.first; - for (var i = 0; i < PLAYLIST.length; i++) { - name = decodeURI(elem.videotitle); - var rx = new RegExp(term, 'i'); - if (name.match(rx)) { - console.log(name); - var index = i - ACTIVE.domobj.index(); - if (index < 0) { - index = '(' + index + ') '; - } - else if (index > 0) { - index = '(+' + index + ') '; - } - else { - index = ''; - } - $(elem.domobj).removeClass("search-hidden").find(".title").attr("active-offset", index); + refresh(ACTIVE.domobj.index()); + + return; + } + + if (TYPE >= 1 || (LEADER && getToggleable('berryqueue'))) { + socket.emit('searchHistory', { search: term }); + } + + $("#playlist").addClass("searching"); + $("#plul li").addClass("search-hidden"); + $("#plul li.active").removeClass("search-hidden"); + + const rx = new RegExp(term, 'i'); + const activeIndex = ACTIVE.domobj.index(); + + PLAYLIST.each((video, index) => { + if (rx.test(decodeURI(elem.videotitle))) { + const name = decodeURI(video.videotitle); + const item = video.domobj[0]; + + if (rx.test(name)) { + const diff = index - activeIndex; + const str = diff !== 0 ? `(${diff > 0 ? '+' : '-'})` : ''; + + item.classList.remove('search-hidden'); + item.querySelector('.title').setAttribute('active-offset', str); } - elem = elem.next; } - smartRefreshScrollbar(); - scrollToPlEntry(0); - realignPosHelper(); - } + }) + + refresh(0); } function newPoll(data) { if (data.ghost && IGNORE_GHOST_MESSAGES) { @@ -2143,25 +2144,19 @@ function attachAreaEdit(elem, name) { } } function setVidVolatile(pos, isVolat) { - elem = PLAYLIST.first; - for (var i = 0; i < pos; i++) { - elem = elem.next; - } + const video = PLAYLIST.at(pos); - elem.volat = isVolat; + video.volat = isVolat; if (isVolat) { - $(elem.domobj).addClass("volatile"); + $(video.domobj).addClass("volatile"); } else { - $(elem.domobj).removeClass("volatile"); + $(video.domobj).removeClass("volatile"); } - console.log(elem.domobj); + + console.log(video.domobj); } function setVidColorTag(pos, tag, volat) { - elem = PLAYLIST.first; - for (var i = 0; i < pos; i++) { - elem = elem.next; - } - _setVidColorTag(elem.domobj, tag, volat); + _setVidColorTag(PLAYLIST.at(pos).domobj, tag, volat); } function _setVidColorTag(domobj, tag, volat) { var ct = $(domobj).find(".colorTag"); @@ -2599,21 +2594,13 @@ function setPlaylistPosition(to) { ACTIVE.domobj.removeClass("active"); } - var elem = PLAYLIST.first; - ACTIVE = PLAYLIST.first; - for (var i = 0; i < PLAYLIST.length; i++) { - //dbg(elem.videoid+" =?= "+to.video.videoid); - if (elem.videoid == to.video.videoid) { - ACTIVE = elem; - //scrollToPlEntry(i); - break; - } - elem = elem.next; - } + ACTIVE = PLAYLIST.find((video) => { + return video.videoid === to.video.videoid; + }); + if (typeof ACTIVE.domobj != "undefined") { ACTIVE.domobj.addClass("active"); } - //PL_POSITION = to; smartRefreshScrollbar(); realignPosHelper(); @@ -2739,53 +2726,33 @@ function notifyNewMsg(channel, isSquee, isRcv) { } function sortPlaylist(data) { - setVal("sorting", true); - var elem = PLAYLIST.first; - var fromelem, toelem; - for (var i = 0; i < PLAYLIST.length; i++) { - if (i == data.from) { - fromelem = elem; - break; - } - elem = elem.next; - } + const [from, to] = PLAYLIST.multiple([data.from, data.to]); + // Sanity check - if (fromelem.videoid != data.sanityid) { + if (from.videoid != data.sanityid) { // DOOR STUCK - setVal("sorting", false); - socket.emit("refreshMyPlaylist"); + return socket.emit("refreshMyPlaylist"); + } + + setVal("sorting", true); + + PLAYLIST.remove(from); + + if (data.to > data.from) { + PLAYLIST.insertAfter(to, from); + } else { + PLAYLIST.insertBefore(to, from); } - else { - elem = PLAYLIST.first; - for (var i = 0; i < PLAYLIST.length; i++) { - if (i == data.to) { - toelem = elem; - break; - } - elem = elem.next; - } - PLAYLIST.remove(fromelem); + from.domobj.hide("blind", function () { if (data.to > data.from) { - PLAYLIST.insertAfter(toelem, fromelem); - fromelem.domobj.hide("blind", function () { - fromelem.domobj.insertAfter(toelem.domobj).show("blind", function () { - fromelem.domobj.css("display", "list-item"); - realignPosHelper(); - setVal("sorting", false); - }); - }); + from.domobj.insertAfter(to.domobj).show("blind"); } else { - PLAYLIST.insertBefore(toelem, fromelem); - fromelem.domobj.hide("blind", function () { - fromelem.domobj.insertBefore(toelem.domobj).show("blind", function () { - fromelem.domobj.css("display", "list-item"); - realignPosHelper(); - setVal("sorting", false); - }); - }); + from.domobj.insertBefore(to.domobj).show("blind"); } - } + }); + + setVal("sorting", false); } function filterAdminLog() { diff --git a/web/js/init.js b/web/js/init.js index 4a061fcd..7394a449 100644 --- a/web/js/init.js +++ b/web/js/init.js @@ -96,6 +96,61 @@ LinkedList.Circular.prototype.remove = function (node) { node.next = null; this.length--; }; + +LinkedList.Circular.prototype.some = function(cb) { + return this.find(cb) !== null; +} + +LinkedList.Circular.prototype.find = function(cb) { + let video = this.first; + + for (let i = 0; i < this.length; i++) { + if (cb(video, i)) { + return video; + } + + video = video.next; + } + + return null; +} +LinkedList.Circular.prototype.multiple = function(indexes) { + let map = new Map( + indexes.map(i => [i, null]) + ); + + let video = this.first; + let max = Math.max(...indexes); + + for (let i = 0; i <= max; i++) { + if (map.has(i)) { + map.set(i, video); + } + + video = video.next; + } + + return indexes.map(i => map.get(i)); +} +LinkedList.Circular.prototype.at = function(index) { + let video = this.first; + + for (let i = 0; i < index; i++) { + video = video.next; + } + + return video; +} + +LinkedList.Circular.prototype.each = function(cb) { + let video = this.first; + + for (let i = 0; i < this.length; i++) { + cb(video, i); + video = video.next; + } +} + LinkedList.Circular.prototype.toArray = function () { var elem = this.first; var out = []; @@ -1200,17 +1255,11 @@ function initPlaylistControls(plwrap) { if (controlsPlaylist()) { var btn = $(this); parseVideoURL($(videoImport).val(), function (id, type, videotitle) { - elem = PLAYLIST.first; var found = false; - for (var i = 0; i < PLAYLIST.length; i++) { - if (elem.videoid == id) { - found = true; - doRequeue(elem.domobj); - console.log("found"); - break; - } - elem = elem.next; - } - if (!found) { + const video = PLAYLIST.find(video => video.videoid === id); + + if (video) { + doRequeue(video.domobj); + } else { btn.data('revertTxt', "Q"); btn.text('').addClass("loading"); LAST_QUEUE_ATTEMPT = { @@ -1231,18 +1280,12 @@ function initPlaylistControls(plwrap) { if (controlsPlaylist()) { var btn = $(this); parseVideoURL($(videoImport).val(), function (id, type, videotitle) { - elem = PLAYLIST.first; var found = false; - for (var i = 0; i < PLAYLIST.length; i++) { - if (elem.videoid == id) { - found = true; - doRequeue(elem.domobj); - console.log("found"); - break; - } - elem = elem.next; - } - if (!found) { - btn.data('revertTxt', "V"); + const video = PLAYLIST.find(video => video.videoid === id); + + if (video) { + doRequeue(video.domobj); + } else { + btn.data('revertTxt', "Q"); btn.text('').addClass("loading"); LAST_QUEUE_ATTEMPT = { queue: true, @@ -1257,39 +1300,6 @@ function initPlaylistControls(plwrap) { } }); - /* - var vaddBtn = $('
').addClass("impele").addClass("btn").text("+").appendTo(container); - vaddBtn.click(function(){ - if(controlsPlaylist()){ - var btn = $(this); - parseVideoURL($(videoImport).val(),function(id,type,videotitle){ - elem=PLAYLIST.first; var found = false; - for(var i=0;i').addClass("clear").appendTo(container); @@ -1434,23 +1444,19 @@ function initMultiqueue(){ function queue(link) { parseVideoURL(link, function (id, type, videotitle) { - elem = PLAYLIST.first; - var found = false; - for (var i = 0; i < PLAYLIST.length; i++) { - if (elem.videoid == id) { - found = true; - doRequeue(elem.domobj); - break; - } - elem = elem.next; - } - if (!found) { + const video = PLAYLIST.find(video => video.videoid === id); + + if (video) { + doRequeue(video.domobj); + } else { + btn.data('revertTxt', "Q"); + btn.text('').addClass("loading"); LAST_QUEUE_ATTEMPT = { queue: true, videotype: type, videoid: id, videotitle: videotitle, - volat: true, + volat: true }; socket.emit("addVideo", LAST_QUEUE_ATTEMPT); } @@ -1468,17 +1474,15 @@ function doPlaylistJump(elem) { function newPlaylist(plul) { $(plul).children().remove(); - var elem = PLAYLIST.first; - for (var i = 0; i < PLAYLIST.length; i++) { - var entry = $("
  • ").appendTo(plul); - entry.data('plobject', elem); - elem.domobj = entry; + PLAYLIST.each(video => { + const entry = $('
  • ').appendTo(plul); - populatePlEntry(entry, elem); + entry.data('plobject', video); + video.domobj = entry; + + populatePlEntry(entry, video); + }); - elem = elem.next; - } - dbg(PLAYLIST.first.videolength); recalcStats(); } function initPlaylist(parent) { diff --git a/web/plugins/videoBlacklist/videoBlacklist.js b/web/plugins/videoBlacklist/videoBlacklist.js index de3a9f8a..d5d9e72e 100644 --- a/web/plugins/videoBlacklist/videoBlacklist.js +++ b/web/plugins/videoBlacklist/videoBlacklist.js @@ -16,16 +16,10 @@ new function(){ } ns.scan = function(){ - // Scan playlist - var elem = PLAYLIST.first; - do{ - if(!ns.isOk(ns.getStub(elem))){ - $(elem.domobj).addClass("blacklisted"); - } else { - $(elem.domobj).removeClass("blacklisted"); - } - elem = elem.next; - }while(elem != PLAYLIST.first) + // Scan playlist + PLAYLIST.each(video => { + video.domobj.toggleClass('blacklisted', !ns.isOk(ns.getStub(video))); + }); } ns.uid = function(digits){ From b23e513be2d5067a0fee7a4ea777d72c16e9bcad Mon Sep 17 00:00:00 2001 From: Miikka Lahtinen Date: Fri, 26 Aug 2022 10:21:08 +0300 Subject: [PATCH 2/9] Fix few issues --- docker/node/server.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docker/node/server.js b/docker/node/server.js index f756195a..08a53aea 100644 --- a/docker/node/server.js +++ b/docker/node/server.js @@ -348,7 +348,7 @@ function initPlaylist(callback) { } function initResumePosition(callback) { getMisc({ name: 'server_active_videoid' }, function (old_videoid) { - SERVER.ACTIVE = PLAYLIST.find(video => video.videoid() === old_videoid); + SERVER.ACTIVE = SERVER.PLAYLIST.find(video => video.videoid === old_videoid); if (SERVER.ACTIVE) { getMisc({ name: 'server_time' }, function (old_time) { @@ -547,7 +547,7 @@ function doorStuck(socket) { } function playNext() { const active = { - position: SERVER.PLAYLIST.indexOf(SERVER.ACTIVE), + position: SERVER.PLAYLIST.indexOf(video => video.videoid === SERVER.ACTIVE.videoid), node: SERVER.ACTIVE }; @@ -724,7 +724,7 @@ function kickUserByNick(socket, nick, reason) { sessionService.forNick(nick, session => session.kick(reason, getSocketName(socket))); } var commit = function () { - SERVER.PLAYLIST.each(video => { + SERVER.PLAYLIST.each((video, i) => { var sql = `update ${SERVER.dbcon.video_table} set position = ? where videoid = ?`; mysql.query(sql, [i, '' + video.videoid], function (err) { if (err) { From 66e520007131fd18dc53fe86710cf2e52ebfc3bf Mon Sep 17 00:00:00 2001 From: Miikka Lahtinen Date: Fri, 26 Aug 2022 10:38:21 +0300 Subject: [PATCH 3/9] More minor fixes --- docker/node/server.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker/node/server.js b/docker/node/server.js index 08a53aea..27e265b2 100644 --- a/docker/node/server.js +++ b/docker/node/server.js @@ -2793,7 +2793,7 @@ io.sockets.on('connection', function (ioSocket) { const prev = { node: SERVER.ACTIVE, - position: SERVER.PLAYLIST.indexOf(video => video.videoid === node.videoid) + position: SERVER.PLAYLIST.indexOf(video => video.videoid === SERVER.ACTIVE.videoid) }; const next = { node: SERVER.PLAYLIST.at(data.index), From b2bf0352792a19cdaadb75127169a55356e3b9d1 Mon Sep 17 00:00:00 2001 From: Miikka Lahtinen Date: Sat, 27 Aug 2022 10:33:41 +0300 Subject: [PATCH 4/9] Incorrect check, not correct --- docker/node/server.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/docker/node/server.js b/docker/node/server.js index 27e265b2..07c4cc03 100644 --- a/docker/node/server.js +++ b/docker/node/server.js @@ -2767,8 +2767,8 @@ io.sockets.on('connection', function (ioSocket) { const [fromelem, toelem] = SERVER.PLAYLIST.multiple([data.from, data.to]); - if (data.sanityid && fromelem.videoid === data.sanityid) { - doorStuck(); + if (data.sanityid && fromelem.videoid !== data.sanityid) { + return doorStuck(socket); } SERVER.PLAYLIST.remove(fromelem); @@ -2795,6 +2795,7 @@ io.sockets.on('connection', function (ioSocket) { node: SERVER.ACTIVE, position: SERVER.PLAYLIST.indexOf(video => video.videoid === SERVER.ACTIVE.videoid) }; + const next = { node: SERVER.PLAYLIST.at(data.index), position: data.index From 9a0fe996162fb8711055fef3b41689777f9595d2 Mon Sep 17 00:00:00 2001 From: Miikka Lahtinen Date: Mon, 19 Sep 2022 20:04:10 +0300 Subject: [PATCH 5/9] Don't use find in indexOf --- docker/node/server.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/docker/node/server.js b/docker/node/server.js index 07c4cc03..e849bf3d 100644 --- a/docker/node/server.js +++ b/docker/node/server.js @@ -252,18 +252,18 @@ LinkedList.Circular.prototype.each = function(cb) { } } -LinkedList.Circular.prototype.indexOf = function (cb) { - let index = -1; - - this.find((video, idx) => { - if (cb(video)) { - index = idx; +LinkedList.Circular.prototype.indexOf = function(cb) { + let video = this.first; + + for (let i = 0; i < this.length; i++) { + if (cb(video, i)) { + return i; } - return index > -1; - }); + video = video.next; + } - return index; + return -1; } /* VAR INIT */ From 810e81e1e32a5e0b99864e1967d92d04b189c75f Mon Sep 17 00:00:00 2001 From: Miikka Lahtinen Date: Mon, 19 Sep 2022 20:20:38 +0300 Subject: [PATCH 6/9] Fixes and minor cleanups --- docker/node/server.js | 2 +- web/js/callbacks.js | 9 ++------- web/js/functions.js | 31 ++++++++++++++----------------- 3 files changed, 17 insertions(+), 25 deletions(-) diff --git a/docker/node/server.js b/docker/node/server.js index e849bf3d..240da4fa 100644 --- a/docker/node/server.js +++ b/docker/node/server.js @@ -936,7 +936,7 @@ function setVideoVolatile(socket, video, pos, isVolat) { DefaultLog.info(events.EVENT_ADMIN_SET_VOLATILE, "{mod} set {title} to {status}", - { mod: getSocketName(socket), type: "playlist", title: decodeURIComponent(elem.videotitle), status: isVolat ? "volatile" : "not volatile" }); + { mod: getSocketName(socket), type: "playlist", title: decodeURIComponent(video.videotitle), status: isVolat ? "volatile" : "not volatile" }); io.sockets.emit("setVidVolatile", { pos: pos, diff --git a/web/js/callbacks.js b/web/js/callbacks.js index 0a0eda30..234e518f 100644 --- a/web/js/callbacks.js +++ b/web/js/callbacks.js @@ -308,15 +308,10 @@ socket.on( } ); socket.on("setVidVolatile", function (data) { - pos = data.pos; - isVolat = data.volat; - setVidVolatile(pos, isVolat); + setVidVolatile(data.pos, data.volat); }); socket.on("setVidColorTag", function (data) { - var pos = data.pos; - var tag = data.tag; - var volat = data.volat; - setVidColorTag(pos, tag, volat); + setVidColorTag(data.pos, data.tag, data.volat); }); socket.on("kicked", function (reason) { var msg = "You have been kicked"; diff --git a/web/js/functions.js b/web/js/functions.js index f7603e86..64aff7a3 100644 --- a/web/js/functions.js +++ b/web/js/functions.js @@ -1707,9 +1707,10 @@ function addNewMailMessage(nick, msg) { function plSearch(term) { const refresh = (index) => { smartRefreshScrollbar(); - scrollToPlEntry(); + scrollToPlEntry(index); realignPosHelper(); }; + if (typeof term == "undefined" || term.match(/^$/) || term.length < 3) { $("#playlist").removeClass("searching"); $("#plul li").removeClass("search-hidden"); @@ -1732,17 +1733,18 @@ function plSearch(term) { const activeIndex = ACTIVE.domobj.index(); PLAYLIST.each((video, index) => { - if (rx.test(decodeURI(elem.videotitle))) { - const name = decodeURI(video.videotitle); + if (rx.test(decodeURI(video.videotitle))) { const item = video.domobj[0]; - - if (rx.test(name)) { - const diff = index - activeIndex; - const str = diff !== 0 ? `(${diff > 0 ? '+' : '-'})` : ''; - - item.classList.remove('search-hidden'); - item.querySelector('.title').setAttribute('active-offset', str); + const diff = index - activeIndex; + + let str = ''; + + if (diff !== 0) { + str = `(${diff}) `; } + + item.classList.remove('search-hidden'); + item.querySelector('.title').setAttribute('active-offset', str); } }) @@ -2147,13 +2149,7 @@ function setVidVolatile(pos, isVolat) { const video = PLAYLIST.at(pos); video.volat = isVolat; - if (isVolat) { - $(video.domobj).addClass("volatile"); - } else { - $(video.domobj).removeClass("volatile"); - } - - console.log(video.domobj); + video.domobj.toggleClass('volatile', isVolat); } function setVidColorTag(pos, tag, volat) { _setVidColorTag(PLAYLIST.at(pos).domobj, tag, volat); @@ -2752,6 +2748,7 @@ function sortPlaylist(data) { } }); + realignPosHelper(); setVal("sorting", false); } From 8dc436e07f592c843b7b40c89f3034dbae29023e Mon Sep 17 00:00:00 2001 From: Miikka Lahtinen Date: Mon, 19 Sep 2022 20:30:09 +0300 Subject: [PATCH 7/9] More minor fixes --- docker/node/server.js | 6 ++++-- web/js/functions.js | 5 ++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/docker/node/server.js b/docker/node/server.js index 240da4fa..92d6cb9d 100644 --- a/docker/node/server.js +++ b/docker/node/server.js @@ -348,9 +348,11 @@ function initPlaylist(callback) { } function initResumePosition(callback) { getMisc({ name: 'server_active_videoid' }, function (old_videoid) { - SERVER.ACTIVE = SERVER.PLAYLIST.find(video => video.videoid === old_videoid); + const active = SERVER.PLAYLIST.find(video => video.videoid === old_videoid); - if (SERVER.ACTIVE) { + if (active) { + SERVER.ACTIVE = active; + getMisc({ name: 'server_time' }, function (old_time) { if (+old_time) { SERVER.TIME = +old_time + 1; diff --git a/web/js/functions.js b/web/js/functions.js index 64aff7a3..73747040 100644 --- a/web/js/functions.js +++ b/web/js/functions.js @@ -1726,8 +1726,7 @@ function plSearch(term) { } $("#playlist").addClass("searching"); - $("#plul li").addClass("search-hidden"); - $("#plul li.active").removeClass("search-hidden"); + $("#plul li:not(.active)").addClass("search-hidden"); const rx = new RegExp(term, 'i'); const activeIndex = ACTIVE.domobj.index(); @@ -1740,7 +1739,7 @@ function plSearch(term) { let str = ''; if (diff !== 0) { - str = `(${diff}) `; + str = diff < 0 ? `(${diff}) ` : `(+${diff}) `; } item.classList.remove('search-hidden'); From 525be35a95e43befdc6d21d84c066036d36acd70 Mon Sep 17 00:00:00 2001 From: Miikka Lahtinen Date: Mon, 19 Sep 2022 22:08:05 +0300 Subject: [PATCH 8/9] More small fixes --- web/js/functions.js | 4 ++++ web/js/init.js | 3 +-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/web/js/functions.js b/web/js/functions.js index 73747040..fa13d69d 100644 --- a/web/js/functions.js +++ b/web/js/functions.js @@ -2593,6 +2593,10 @@ function setPlaylistPosition(to) { return video.videoid === to.video.videoid; }); + if (!ACTIVE) { + ACTIVE = PLAYLIST.first; + } + if (typeof ACTIVE.domobj != "undefined") { ACTIVE.domobj.addClass("active"); } diff --git a/web/js/init.js b/web/js/init.js index 7394a449..d94938bf 100644 --- a/web/js/init.js +++ b/web/js/init.js @@ -1449,8 +1449,6 @@ function initMultiqueue(){ if (video) { doRequeue(video.domobj); } else { - btn.data('revertTxt', "Q"); - btn.text('').addClass("loading"); LAST_QUEUE_ATTEMPT = { queue: true, videotype: type, @@ -1458,6 +1456,7 @@ function initMultiqueue(){ videotitle: videotitle, volat: true }; + socket.emit("addVideo", LAST_QUEUE_ATTEMPT); } }); From 780335acca92bf06bc6520ecfa561f7458f3115a Mon Sep 17 00:00:00 2001 From: Miikka Lahtinen Date: Mon, 19 Sep 2022 22:40:24 +0300 Subject: [PATCH 9/9] Rename variable to better reflect its use, and fix wrong text --- web/js/functions.js | 6 +++--- web/js/init.js | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/web/js/functions.js b/web/js/functions.js index fa13d69d..08a6c8fe 100644 --- a/web/js/functions.js +++ b/web/js/functions.js @@ -1736,14 +1736,14 @@ function plSearch(term) { const item = video.domobj[0]; const diff = index - activeIndex; - let str = ''; + let offsetIndex = ''; if (diff !== 0) { - str = diff < 0 ? `(${diff}) ` : `(+${diff}) `; + offsetIndex = diff < 0 ? `(${diff}) ` : `(+${diff}) `; } item.classList.remove('search-hidden'); - item.querySelector('.title').setAttribute('active-offset', str); + item.querySelector('.title').setAttribute('active-offset', offsetIndex); } }) diff --git a/web/js/init.js b/web/js/init.js index d94938bf..824f641a 100644 --- a/web/js/init.js +++ b/web/js/init.js @@ -1285,7 +1285,7 @@ function initPlaylistControls(plwrap) { if (video) { doRequeue(video.domobj); } else { - btn.data('revertTxt', "Q"); + btn.data('revertTxt', "V"); btn.text('').addClass("loading"); LAST_QUEUE_ATTEMPT = { queue: true,