forked from socky/socky-server-ruby
/
presence.rb
49 lines (40 loc) · 1.61 KB
/
presence.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
module Socky
module Server
class Channel
class Presence < Private
def add_subscriber(connection, message, subscriber_data = nil)
self.send_data({ 'event' => 'socky:member:added', 'connection_id' => connection.id, 'channel' => self.name, 'data' => subscriber_data }, connection.id) unless rights(message)['hide']
super
end
def remove_subscriber(connection)
self.send_data({ 'event' => 'socky:member:removed', 'connection_id' => connection.id, 'channel' => self.name }, connection.id)
super
end
protected
def subscribe_successful(connection, message)
user_data = JSON.parse(message.user_data) rescue nil
user_data = {} unless user_data.is_a?(Hash)
self.add_subscriber(connection, message, user_data)
connection.send_data('event' => 'socky:subscribe:success', 'channel' => self.name, 'members' => member_list(connection))
end
def member_list(connection)
list = []
self.subscribers.each do |connection_id, member|
list << { 'connection_id' => connection_id, 'data' => member['data'] } unless connection_id == connection.id
end
list
end
def hash_from_message(connection, message)
hash = super
hash.merge!('data' => message.user_data)
hash
end
def rights(message)
r = super
r.merge!( 'hide' => !!message.hide ) unless message.hide.nil?
r
end
end
end
end
end