Browse code

Pamatanie si vsetkych pripojeni jedneho kontaktu kvoli spravnemu urceniu presence

Cinan Rakosnik authored on 06/05/2013 at 22:04:53
Showing 1 changed files
... ...
@@ -6,7 +6,7 @@ class WsRosterController < WsController
6 6
         super
7 7
 
8 8
         @storages_arr = [:clients, :rosters]
9
-        @storages_hash = [:link_roster_client, :presences]
9
+        @storages_hash = [:link_roster_client, :my_presences, :presences]
10 10
     end
11 11
 
12 12
     def initialize_storage
... ...
@@ -112,14 +112,30 @@ class WsRosterController < WsController
112 112
 
113 113
             roster.add_presence_callback do |roster_item, old_presence, new_presence|
114 114
                 if new_presence.type == :unavailable
115
-                    result = {status: :offline, message: ''}
115
+                    result = {message: ''}
116
+
117
+                    unless connection_store[:presences][roster_item.jid.strip.to_s].nil?
118
+                        connection_store[:presences][roster_item.jid.strip.to_s].delete(new_presence.from.to_s)
119
+                    end
120
+
116 121
                     # mozno treba vyhodit cloveka z multichatu, ak som jeho owner
117
-                    kick_from_all_multichats(roster_item.jid.strip.to_s)
122
+                    #kick_from_all_multichats(roster_item.jid.to_s)
118 123
                 else
119 124
                     status = uniform_presence(new_presence.show)
120
-                    result = { status: status, message: new_presence.status.to_s }
125
+                    result = {message: new_presence.status.to_s}
126
+
127
+                    if connection_store[:presences][roster_item.jid.strip.to_s].nil?
128
+                        connection_store[:presences][roster_item.jid.strip.to_s] = Hash.new()
129
+                    end
130
+
131
+                    connection_store[:presences][roster_item.jid.strip.to_s][new_presence.from.to_s] = {
132
+                        status: status,
133
+                        multichat: false
134
+                    }
121 135
                 end
122 136
 
137
+                result[:status] = select_most_online_status(roster_item.jid.strip.to_s)
138
+
123 139
                 send_message 'app.roster.statusChanged',
124 140
                              jid: roster_item.jid.strip.to_s, status: result
125 141
             end
... ...
@@ -135,7 +151,7 @@ class WsRosterController < WsController
135 135
         connection_store[:clients].each do |client|
136 136
             presence = Jabber::Presence.new.set_type(:available)
137 137
             client.send(presence)
138
-            connection_store[:presences][client] = presence
138
+            connection_store[:my_presences][client] = presence
139 139
         end
140 140
     end
141 141
 
... ...
@@ -165,7 +181,7 @@ class WsRosterController < WsController
165 165
         connection_store[:clients].each do |client|
166 166
             vcard = get_vcard(client)
167 167
             jid = client.jid.strip.to_s
168
-            presence = uniform_presence(connection_store[:presences][client].show)
168
+            presence = uniform_presence(connection_store[:my_presences][client].show)
169 169
         end
170 170
 
171 171
         trigger_success jid: jid, vcard: vcard, status: presence
... ...
@@ -232,6 +248,18 @@ class WsRosterController < WsController
232 232
 
233 233
     private
234 234
 
235
+    def select_most_online_status(jid_stripped)
236
+        if connection_store[:presences][jid_stripped].nil? || connection_store[:presences][jid_stripped].empty?
237
+            return uniform_presence(:offline)
238
+        end
239
+
240
+        statuses = { offline: 0, dnd: 1, away: 2, online: 3 }
241
+
242
+        Hash[connection_store[:presences][jid_stripped].map do |jid, data|
243
+            [ statuses[data[:status]], data[:status] ]
244
+        end].max.second
245
+    end
246
+
235 247
     def get_vcard(me, contact_jid = nil)
236 248
         vcard = Jabber::Vcard::Helper.get(me, contact_jid)
237 249
 
... ...
@@ -246,6 +274,7 @@ class WsRosterController < WsController
246 246
 
247 247
     def uniform_presence(xmpp_presence)
248 248
         case xmpp_presence
249
+            when :offline   then :offline
249 250
             when :away, :xa then :away
250 251
             when :dnd       then :dnd
251 252
             else                 :online