Skip to content

Commit 09ce2af

Browse files
committed
refactor(plugins): move sendCapabilities to client.utils
1 parent 9ba230b commit 09ce2af

File tree

3 files changed

+46
-15
lines changed

3 files changed

+46
-15
lines changed

plugins/cap.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ interface CapFeatures {
1818
state: {
1919
capabilities: string[];
2020
};
21+
utils: {
22+
/** Sends CAP sequence with existing capabilities.
23+
*
24+
* Optionaly, takes additional capabilities in argument. */
25+
sendCapabilities: (...capabilities: string[]) => void;
26+
};
2127
}
2228

2329
export default createPlugin("cap")<CapFeatures>((client) => {
@@ -28,4 +34,21 @@ export default createPlugin("cap")<CapFeatures>((client) => {
2834
client.cap = (command, ...params) => {
2935
client.send("CAP", command, ...params);
3036
};
37+
38+
// Provides util to send capabilities
39+
// (currently triggered by plugins/registration)
40+
41+
client.utils.sendCapabilities = (...capabilities: string[]): void => {
42+
const caps = [...client.state.capabilities, ...capabilities];
43+
44+
if (caps.length === 0) {
45+
return;
46+
}
47+
48+
for (const cap of caps) {
49+
client.cap("REQ", cap);
50+
}
51+
52+
client.cap("END");
53+
};
3154
});

plugins/cap_test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,26 @@ describe("plugins/cap", (test) => {
1717

1818
assertEquals(raw, ["CAP REQ capability"]);
1919
});
20+
21+
test("send capabilities", async () => {
22+
const { client, server } = await mock();
23+
24+
client.state.capabilities.push("cap1");
25+
client.utils.sendCapabilities();
26+
27+
assertEquals(server.receive(), [
28+
"CAP REQ multi-prefix", // already provided by plugins/names
29+
"CAP REQ cap1", // provided by previous capabilities.push()
30+
"CAP END",
31+
]);
32+
33+
client.utils.sendCapabilities("cap2");
34+
35+
assertEquals(server.receive(), [
36+
"CAP REQ multi-prefix",
37+
"CAP REQ cap1",
38+
"CAP REQ cap2", // provided by sendCapabilities()
39+
"CAP END",
40+
]);
41+
});
2042
});

plugins/registration.ts

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,6 @@ export default createPlugin(
3535
const { nick, username = nick, realname = nick, password } = options;
3636
client.state.user = { nick, username, realname };
3737

38-
const sendCapabilities = () => {
39-
const { capabilities } = client.state;
40-
41-
if (capabilities.length === 0) {
42-
return;
43-
}
44-
45-
for (const capability of capabilities) {
46-
client.cap("REQ", capability);
47-
}
48-
49-
client.cap("END");
50-
};
51-
5238
const sendRegistration = () => {
5339
if (password !== undefined) {
5440
client.pass(password);
@@ -60,7 +46,7 @@ export default createPlugin(
6046
// Sends capabilities and registers once connected.
6147

6248
client.on("connected", () => {
63-
sendCapabilities();
49+
client.utils.sendCapabilities();
6450
sendRegistration();
6551
});
6652

0 commit comments

Comments
 (0)