From d85ac40cd396cf745df4ed2ffa3c68b4be174776 Mon Sep 17 00:00:00 2001 From: Chirag Date: Fri, 20 Oct 2017 15:51:17 +0530 Subject: [PATCH 1/2] add command to list online users --- app.coffee | 17 +++++++++++------ public/polymer.coffee | 8 +++----- userinfo.coffee | 30 +++++++++++++++++++----------- 3 files changed, 33 insertions(+), 22 deletions(-) diff --git a/app.coffee b/app.coffee index 0690a5b..bb1f52e 100644 --- a/app.coffee +++ b/app.coffee @@ -76,6 +76,9 @@ interpretCommand = (commandText, adminNick) -> else for i in [0..parseInt(secondWord)] messages.pop() + else if firstWord is "users" + msg = userInfoHandler.getOnlineUsers().join ', ' + slack.postMessage msg, process.env.SLACK_CHANNEL, 'Jinora' else if firstWord is "help" announcementHandler.showHelp() @@ -190,17 +193,19 @@ app.io.route 'chat:demand', (req)-> app.io.route 'member:connect', (req)-> userInfoHandler.addUser req if connectNotify == "on" - slack.postMessage req.data.nick + " entered channel", process.env.SLACK_CHANNEL, "Jinora" - -app.io.route 'member:disconnect', (req)-> - if connectNotify == "on" - slack.postMessage req.data.nick + " left channel", process.env.SLACK_CHANNEL, "Jinora" + slack.postMessage "#{req.data.nick} entered channel", process.env.SLACK_CHANNEL, "Jinora" app.io.route 'presence:demand', (req)-> req.io.emit 'presence:list', onlineMemberList app.io.route 'userinfo:data', (req)-> - userInfoHandler.addUserIp req.data.nick, req.data.localip + userInfoHandler.addUserIp req + +app.io.on 'connection', (socket)-> + socket.on 'disconnect', ()-> + nick = userInfoHandler.removeUser socket.id + if connectNotify == "on" + slack.postMessage "#{nick} left channel", process.env.SLACK_CHANNEL, "Jinora" presence.on 'change', ()-> onlineMemberList = [] diff --git a/public/polymer.coffee b/public/polymer.coffee index 2bebdc7..d33e2c6 100644 --- a/public/polymer.coffee +++ b/public/polymer.coffee @@ -141,8 +141,6 @@ socket.on 'connect', onconnect socket.on 'disconnect', -> template.status = 'disconnected' - socket.emit 'member:disconnect', - nick: template.userName socket.on 'reconnect', -> template.status = 'connected' @@ -177,7 +175,7 @@ socket.on 'chat:log', (log)-> socket.on 'presence:list', (list)-> template.users = list - + getUserInfo = -> RTCPeerConnection = window.webkitRTCPeerConnection or window.mozRTCPeerConnection if RTCPeerConnection @@ -185,7 +183,7 @@ getUserInfo = -> rtc = new RTCPeerConnection(iceServers: []) if rtc rtc.createDataChannel '', reliable: false - rtc.createOffer (offerDesc) -> + rtc.createOffer (offerDesc) -> rtc.setLocalDescription offerDesc , (e) -> console.warn 'offer failed', e @@ -198,4 +196,4 @@ getUserInfo = -> nick: template.userName localip: ip[1] else - console.log 'RTCPeerConnection failed' \ No newline at end of file + console.log 'RTCPeerConnection failed' diff --git a/userinfo.coffee b/userinfo.coffee index 9b6bcfd..87319b2 100644 --- a/userinfo.coffee +++ b/userinfo.coffee @@ -3,23 +3,31 @@ platform = require('platform') module.exports = (ioObject, slackObject, setConnectNotify) -> io = ioObject slack = slackObject - users = [] + users = {} + addUser: (req) -> user = nick: req.data.nick platform: platform.parse(req.headers['user-agent']) - ip: + ip: public: req.headers['x-forwarded-for'] local: [] - users.unshift user - users.pop if users.length > 100 + users[req.io.socket.id] = user + + removeUser: (id)-> + nick = users[id].nick + delete users[id] + nick - addUserIp: (nick, ip) -> - users.some (user) -> - if user.nick == nick - user.ip.local.push ip - return true - false + addUserIp: (req) -> + user = users[req.io.socket.id] + user.ip.local.push req.data.localip if user + + getOnlineUsers: ()-> + onlineUsers = [] + for id, user of users + onlineUsers.push user.nick + onlineUsers interpret: (message) -> index = message.indexOf(' ') @@ -44,4 +52,4 @@ module.exports = (ioObject, slackObject, setConnectNotify) -> return true false slack.postMessage "No such user found", process.env.SLACK_CHANNEL, 'Jinora' if !userExist - \ No newline at end of file + From 866a5d1ee5e0d6534199db9f82212d5ba7a1c86b Mon Sep 17 00:00:00 2001 From: Chirag Date: Fri, 27 Oct 2017 06:40:56 +0530 Subject: [PATCH 2/2] add help message for online users --- announcements.coffee | 1 + 1 file changed, 1 insertion(+) diff --git a/announcements.coffee b/announcements.coffee index 19cc0e3..ddbd574 100644 --- a/announcements.coffee +++ b/announcements.coffee @@ -22,6 +22,7 @@ module.exports = (ioObject, slackObject) -> \t!info _to get user info._ \t!clear _to remove all the latest messages from the public view._ \t!clear [x] _to remove only the last x messages from public view._ + \t!users _to show a list of online users._ \t!help _to show this message._ """