From c905280125b9983327c79b21ef4766b261018c53 Mon Sep 17 00:00:00 2001 From: Nick Bensema Date: Mon, 2 Sep 2013 23:36:19 -0700 Subject: [PATCH] Added /clean command to clear a user's additions from a channel Any user with playlistdelete permission can now use the /clean command to wipe out a user's additions to the playlist. This can be helpful for taking down abusive items en masse without resetting the entire playlist. The code should probably be generalized, somehow, to handle title patterns or additions within a certain timeframe. --- chatcommand.js | 45 +++++++++++++++++++++++++++++++++++++++++++++ ullist.js | 17 +++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/chatcommand.js b/chatcommand.js index 1ad2d946..97fd303e 100644 --- a/chatcommand.js +++ b/chatcommand.js @@ -88,6 +88,9 @@ function handle(chan, user, msg, data) { else if(msg.indexOf("/clear") == 0) { handleClear(chan, user); } + else if(msg.indexOf("/clean ") == 0) { + handleClean(chan, user, msg.substring(7)); + } } function handleMute(chan, user, args) { @@ -240,5 +243,47 @@ function handleClear(chan, user) { chan.sendAll("clearchat"); } + +/** +Remove all videos by a particular user. +*/ +function handleClean(chan, user, target) { + if(!chan.hasPermission(user, "playlistdelete")) { + return; + } + // you can use regexps, in case someone tries + // to fool you with cyrillic or something. + target = new RegExp(target); + var uid = false; // to skip first chan.sendAll() + // local variables for deleteNext() callback + var pl = chan.playlist; + var count = 0; + var deleteNext; + // this callback will search for matching items, + // and if one is found, call the remove method + // with itself as the callback to execute after + // the remove is finished. + deleteNext = function() { + // notify channel that this is deleted + if (uid !== false) { + chan.sendAll("delete", { + uid: uid + }); + } + // find next match + uid = pl.items.findSubmitter(target); + if (uid !== false) { + // remove, and restart callback when removed. + pl.remove(uid, deleteNext); + } else { + // refresh playlist only once, at the end + chan.broadcastPlaylistMeta(); + } + } + // start initial callback + deleteNext(); + return; +} + exports.handle = handle; diff --git a/ullist.js b/ullist.js index 24785fad..4838e4ad 100644 --- a/ullist.js +++ b/ullist.js @@ -181,4 +181,21 @@ ULList.prototype.findVideoId = function (id) { return false; }; +/** +returns the UID of the first item in the queue that +was submitted by a user matching the target regexp. +*/ +ULList.prototype.findSubmitter = function(target) { + var item = this.first; + + while(item !== null) { + if(item.queueby && target.test(item.queueby)) { + console.log("found", item.uid, target, item.queueby); + return item.uid; + } + item = item.next; + } + return false; +} + exports.ULList = ULList;