diff --git a/changelog b/changelog index 1f9ce5e8..56770ae1 100644 --- a/changelog +++ b/changelog @@ -1,3 +1,7 @@ +Sat Sep 21 00:08 2013 CDT + * lib/playlist.js: Make .clean(filter) an instance method + * lib/chatcommand.js: Make a few minor changes to unbibium's /clean + Wed Sep 18 18:26 2013 CDT * lib/user.js: Change channel checks to include checking for whether the channel is dead diff --git a/lib/chatcommand.js b/lib/chatcommand.js index 11f8b5e0..dfc87031 100644 --- a/lib/chatcommand.js +++ b/lib/chatcommand.js @@ -249,63 +249,39 @@ function handleClear(chan, user) { chan.sendAll("clearchat"); } -var user_input_re = /^(-[img]+\s+)/i +/* + /clean and /cleantitle contributed by http://github.com/unbibium. + Modifications by Calvin Montgomery +*/ -function user_input_regexp(target) { - var m = target.match(user_input_re); +function generateTargetRegex(target) { + const flagsre = /^(-[img]+\s+)/i + var m = target.match(flagsre); var flags = ""; if (m) { flags = m[0].slice(1,-1); - target = target.replace(user_input_re, ""); + target = target.replace(flagsre, ""); } return new RegExp(target, flags); } function handleClean(chan, user, target) { - // you can use regexps, in case someone tries - // to fool you with cyrillic or something. - target = user_input_regexp(target); - cleanPlaylist(chan, user, function(item) { + if (!chan.hasPermission(user, "playlistdelete")) + return; + target = generateTargetRegex(target); + chan.playlist.clean(function (item) { return target.test(item.queueby); }); } function handleCleanTitle(chan, user, target) { - target = user_input_regexp(target); - cleanPlaylist(chan, user, function(item) { + if (!chan.hasPermission(user, "playlistdelete")) + return; + target = generateTargetRegex(target); + chan.playlist.clean(function (item) { return target.exec(item.media.title) !== null; }); } -/** -Remove all videos by a particular user. -*/ -function cleanPlaylist(chan, user, filter) { - if(!chan.hasPermission(user, "playlistdelete")) { - return; - } - // local variables for deleteNext() callback - var pl = chan.playlist; - var count = 0; - var matches = pl.items.findAll(filter); - var deleteNext; - deleteNext = function() { - if (count < matches.length) { - var uid=matches[count].uid; - count += 1 - chan.sendAll("delete", { - uid: uid - }); - 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/lib/playlist.js b/lib/playlist.js index 9ec0ac8c..624a6bdf 100644 --- a/lib/playlist.js +++ b/lib/playlist.js @@ -563,4 +563,31 @@ Playlist.prototype._leadLoop = function() { } } +/* + Delete items from the playlist for which filter(item) returns + a truthy value + + based on code contributed by http://github.com/unbibium +*/ +Playlist.prototype.clean = function (filter) { + var self = this; + var matches = self.items.findAll(filter); + var count = 0; + var deleteNext = function () { + if (count < matches.length) { + var uid = matches[count].uid; + count++; + self.channel.sendAll("delete", { + uid: uid + }); + self.remove(uid, deleteNext); + } else { + // refresh meta only once, at the end + self.channel.broadcastPlaylistMeta(); + } + }; + // start initial callback + deleteNext(); +}; + module.exports = Playlist;