Skip to content

Commit

Permalink
Merge pull request #60 from sdslabs/online-users
Browse files Browse the repository at this point in the history
List Online users
  • Loading branch information
arpitsingla96 authored Dec 25, 2017
2 parents 9c73f7e + 866a5d1 commit 81ac1c1
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 19 deletions.
1 change: 1 addition & 0 deletions announcements.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module.exports = (ioObject, slackObject) ->
\t!info <nick> _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._
"""

Expand Down
17 changes: 11 additions & 6 deletions app.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,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()

Expand Down Expand Up @@ -213,17 +216,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 = []
Expand Down
2 changes: 0 additions & 2 deletions public/polymer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,6 @@ socket.on 'connect', onconnect

socket.on 'disconnect', ->
template.status = 'disconnected'
socket.emit 'member:disconnect',
nick: template.userName

socket.on 'reconnect', ->
template.status = 'connected'
Expand Down
30 changes: 19 additions & 11 deletions userinfo.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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(' ')
Expand All @@ -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

0 comments on commit 81ac1c1

Please sign in to comment.