Browse code

Ukoncenie multichatu, ak sa odpoji owner

Cinan Rakosnik authored on 07/05/2013 at 22:35:45
Showing 2 changed files
... ...
@@ -14,15 +14,13 @@ class WsController < WebsocketRails::BaseController
14 14
         end
15 15
     end
16 16
 
17
-    def where_i_am_multichat_owner
18
-        chats_owner = {}
19
-
20
-        if connection_store[:opened_chats]
21
-            connection_store[:clients].each do |client|
22
-                connection_store[:opened_chats][client].each do |chat_id, contacts|
23
-                    if contacts[:owner] == client.jid.to_s
24
-                        chats_owner[client] = chat_id
25
-                    end
17
+    def where_i_am_multichat_owner(client)
18
+        chats_owner = []
19
+
20
+        if connection_store[:opened_chats] and connection_store[:opened_chats][client]
21
+            connection_store[:opened_chats][client].each do |chat_id, contacts|
22
+                if contacts[:owner] == client.jid.to_s
23
+                    chats_owner << chat_id
26 24
                 end
27 25
             end
28 26
         end
... ...
@@ -30,11 +28,11 @@ class WsController < WebsocketRails::BaseController
30 30
         chats_owner
31 31
     end
32 32
 
33
-    def kick_from_all_multichats(kick)
34
-        chats = where_i_am_multichat_owner()
35
-        chats.each do |client, chat_id|
33
+    def kick_from_all_multichats(client, somebody_to_kick)
34
+        chats = where_i_am_multichat_owner(client)
35
+        chats.each do |chat_id|
36 36
             contacts = connection_store[:opened_chats][client][chat_id][:attendants]
37
-            contacts -= [kick.to_s]
37
+            contacts -= [somebody_to_kick.to_s]
38 38
 
39 39
             if contacts.empty?
40 40
                 destroy_multichat(client.jid, chat_id)
... ...
@@ -48,6 +46,16 @@ class WsController < WebsocketRails::BaseController
48 48
         end
49 49
     end
50 50
 
51
+    def kick_myself_from_multichat(client, owner)
52
+        if connection_store[:opened_chats] and connection_store[:opened_chats][client]
53
+            connection_store[:opened_chats][client].each do |chat_id, chat|
54
+                if chat[:owner] == owner.to_s and chat[:attendants].include? client.jid.to_s
55
+                    destroy_multichat(client.jid, chat_id)
56
+                end
57
+            end
58
+        end
59
+    end
60
+
51 61
     def sync_contacts_frontend(owner, me, chat_id, contacts)
52 62
         send_message 'app.chat.updateSyncedContacts',
53 63
                      me: me.strip.to_s,
... ...
@@ -255,8 +255,12 @@ class WsRosterController < WsController
255 255
                     connection_store[:presences][roster_item.jid.strip.to_s.to_sym].delete(new_presence.from.to_s)
256 256
                 end
257 257
 
258
+                client = connection_store[:link_roster_client][roster]
258 259
                 # mozno treba vyhodit cloveka z multichatu, ak som jeho owner
259
-                kick_from_all_multichats(new_presence.from)
260
+                kick_from_all_multichats(client, new_presence.from)
261
+
262
+                # mozno treba sa odpojit z multichatu, ak sa odpojil jeho owner
263
+                kick_myself_from_multichat(client, new_presence.from)
260 264
             else
261 265
                 status = uniform_presence(new_presence.show)
262 266
                 result = {message: new_presence.status.to_s}