From 0251dda8add24c5c43b100d9a9fcdd324c4a403c Mon Sep 17 00:00:00 2001 From: Sheogorath Date: Wed, 17 Jul 2019 21:45:29 +0200 Subject: [PATCH] Add sendMsg function to channel class This function implements sending messages to all users in a channel while sending the channel name as target to the user's socket. --- src/channel.js | 7 +++++++ test/channel.js | 17 +++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/channel.js b/src/channel.js index cf0eddf..b6cb5ee 100644 --- a/src/channel.js +++ b/src/channel.js @@ -20,6 +20,13 @@ function Channel(name) { this.userlist.splice(index, 1) } } + + this.sendMsg = function(from, message) { + const channel = this + this.userlist.forEach(function(item) { + item.sendMsg(from, message, channel.name) + }) + } } module.exports = Channel diff --git a/test/channel.js b/test/channel.js index 68accce..870a640 100644 --- a/test/channel.js +++ b/test/channel.js @@ -38,4 +38,21 @@ describe('Channel', function () { channel.part(mockedUser) }) }) + + describe('#sendMsg(from, message)', function () { + it("should remove a user from a channel and send a PART command to the user", function (done) { + const channel = new Channel("#testchan") + let mockedUser = {nickname: "some_nick", sendRaw: function() {}} + mockedUser.sendMsg = function(from, message, to) { + assert.equal(from, mockedUser) + assert.equal(message, "test message") + assert.equal(to, "#testchan") + done() + } + // we can't part a channel without joining it first + channel.join(mockedUser) + + channel.sendMsg(mockedUser, "test message") + }) + }) })