Merge pull request #4275 from hellomouse/isupport-modes

Optimise modes based on ISUPPORT
This commit is contained in:
Max Leiter 2021-07-06 17:09:31 -07:00 committed by GitHub
commit 03d38812e3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 7 deletions

View file

@ -23,7 +23,9 @@ exports.input = function ({irc, nick}, chan, cmd, args) {
return;
}
if (args.length === 0) {
const target = args.filter((arg) => arg !== "");
if (target.length === 0) {
chan.pushMessage(
this,
new Msg({
@ -44,9 +46,13 @@ exports.input = function ({irc, nick}, chan, cmd, args) {
devoice: "-v",
}[cmd];
args.forEach(function (target) {
irc.raw("MODE", chan.name, mode, target);
});
const limit = parseInt(irc.network.supports("MODES")) || target.length;
for (let i = 0; i < target.length; i += limit) {
const targets = target.slice(i, i + limit);
const amode = `${mode[0]}${mode[1].repeat(targets.length)}`;
irc.raw("MODE", chan.name, amode, ...targets);
}
return;
}

View file

@ -17,15 +17,38 @@ describe("Commands", function () {
});
const testableNetwork = {
firstCommand: null,
lastCommand: null,
nick: "xPaw",
irc: {
network: {
supports(type) {
if (type.toUpperCase() === "MODES") {
return "4";
}
},
},
raw(...args) {
testableNetwork.firstCommand = testableNetwork.lastCommand;
testableNetwork.lastCommand = args.join(" ");
},
},
};
const testableNetworkNoSupports = Object.assign({}, testableNetwork, {
irc: {
network: {
supports() {
return null;
},
},
raw(...args) {
testableNetworkNoSupports.firstCommand = testableNetworkNoSupports.lastCommand;
testableNetworkNoSupports.lastCommand = args.join(" ");
},
},
});
it("should not mess with the given target", function () {
const test = function (expected, args) {
ModeCommand.input(testableNetwork, channel, "mode", Array.from(args));
@ -81,10 +104,34 @@ describe("Commands", function () {
ModeCommand.input(testableNetwork, channel, "devoice", ["xPaw"]);
expect(testableNetwork.lastCommand).to.equal("MODE #thelounge -v xPaw");
});
// Multiple arguments are supported, sent as separate commands
ModeCommand.input(testableNetwork, channel, "devoice", ["xPaw", "Max-P"]);
expect(testableNetwork.lastCommand).to.equal("MODE #thelounge -v Max-P");
it("should use ISUPPORT MODES on shorthand commands", function () {
ModeCommand.input(testableNetwork, channel, "voice", ["xPaw", "Max-P"]);
expect(testableNetwork.lastCommand).to.equal("MODE #thelounge +vv xPaw Max-P");
// since the limit for modes on tests is 4, it should send two commands
ModeCommand.input(testableNetwork, channel, "devoice", [
"xPaw",
"Max-P",
"hey",
"idk",
"thelounge",
]);
expect(testableNetwork.firstCommand).to.equal(
"MODE #thelounge -vvvv xPaw Max-P hey idk"
);
expect(testableNetwork.lastCommand).to.equal("MODE #thelounge -v thelounge");
});
it("should fallback to all modes at once for shorthand commands", function () {
ModeCommand.input(testableNetworkNoSupports, channel, "voice", ["xPaw"]);
expect(testableNetworkNoSupports.lastCommand).to.equal("MODE #thelounge +v xPaw");
ModeCommand.input(testableNetworkNoSupports, channel, "devoice", ["xPaw", "Max-P"]);
expect(testableNetworkNoSupports.lastCommand).to.equal(
"MODE #thelounge -vv xPaw Max-P"
);
});
});
});