A few minor changes to the /clean merge

This commit is contained in:
calzoneman 2013-09-21 00:10:22 -05:00
parent 04b9537afc
commit 4517741455
3 changed files with 47 additions and 40 deletions

View file

@ -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 Wed Sep 18 18:26 2013 CDT
* lib/user.js: Change channel checks to include checking for whether * lib/user.js: Change channel checks to include checking for whether
the channel is dead the channel is dead

View file

@ -249,63 +249,39 @@ function handleClear(chan, user) {
chan.sendAll("clearchat"); 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) { function generateTargetRegex(target) {
var m = target.match(user_input_re); const flagsre = /^(-[img]+\s+)/i
var m = target.match(flagsre);
var flags = ""; var flags = "";
if (m) { if (m) {
flags = m[0].slice(1,-1); flags = m[0].slice(1,-1);
target = target.replace(user_input_re, ""); target = target.replace(flagsre, "");
} }
return new RegExp(target, flags); return new RegExp(target, flags);
} }
function handleClean(chan, user, target) { function handleClean(chan, user, target) {
// you can use regexps, in case someone tries if (!chan.hasPermission(user, "playlistdelete"))
// to fool you with cyrillic or something. return;
target = user_input_regexp(target); target = generateTargetRegex(target);
cleanPlaylist(chan, user, function(item) { chan.playlist.clean(function (item) {
return target.test(item.queueby); return target.test(item.queueby);
}); });
} }
function handleCleanTitle(chan, user, target) { function handleCleanTitle(chan, user, target) {
target = user_input_regexp(target); if (!chan.hasPermission(user, "playlistdelete"))
cleanPlaylist(chan, user, function(item) { return;
target = generateTargetRegex(target);
chan.playlist.clean(function (item) {
return target.exec(item.media.title) !== null; 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; exports.handle = handle;

View file

@ -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; module.exports = Playlist;