/
active_users.rb
72 lines (60 loc) · 1.72 KB
/
active_users.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
class ActiveUsers
# TODO has to account for users signed on from multiple devices
# key: user_id
# value: {:user => user_object, :client_ids: [] }
@@users = {}
class << self
def add(client_id, user)
if not find_by_user_id(user.id)
@@users[user.id] = {:user => user, :client_ids => [client_id]}
publish_message "connect", user
else
@@users[user.id][:client_ids].push client_id
end
end
def remove_by_client_id(client_id)
user_id = find_by_client_id(client_id)
if user_id
@@users[user_id][:client_ids].delete client_id
if @@users[user_id][:client_ids].empty?
publish_message "disconnect", @@users[user_id][:user]
@@users.delete(user_id)
end
end
end
def remove_by_user_id(user_id)
@@users.delete(user_id)
end
def find_by_client_id(client_id)
@@users.each do |user_id, detail|
return user_id if detail[:client_ids].include?(client_id)
end
false
end
def find_by_user_id(user_id)
return @@users[user_id][:client_ids] if @@users.keys.include?(user_id)
false
end
def update_user(user)
if find_by_user_id(user.id)
@@users[user.id][:user] = user
publish_message "update", user
end
end
def all
users = []
@@users.values.each do |detail|
users.push detail[:user]
end
return users
end
def publish_message(event, user)
Channel.send("user_#{event}", user) if not event == "update"
FAYE_CLIENT.publish("/app/activities", {
:event => "user##{event}",
:entity => user,
:extra => { :active_users => ActiveUsers.all }
})
end
end
end