Browse code

Odstranenie cloveka z multichatu, ak sa stane offline

Cinan Rakosnik authored on 06/05/2013 at 11:07:56
Showing 5 changed files
... ...
@@ -23,7 +23,7 @@ this.App =
23 23
       _.defaults(options, {data: {} })
24 24
       App._dispatcher.trigger(options.event, options.data, options.success, options.error)
25 25
 
26
-    initRoster: ->
26
+    initRoster: (callback) ->
27 27
       App.Com.trigger(
28 28
         event: 'app.roster.initRoster'
29 29
         success: (data) ->
... ...
@@ -34,6 +34,8 @@ this.App =
34 34
               belongsTo: [contact.belongsTo]
35 35
             )
36 36
             App.Collections.contacts.add(newContact, merge: true)
37
+
38
+            callback?()
37 39
           )
38 40
       )
39 41
 
... ...
@@ -11,12 +11,13 @@
11 11
 #= require chat
12 12
 
13 13
 App.Com.connect(->
14
-  App.Com.initRoster()
15
-  App.Com.startFetchingVcards()
16
-  App.Com.startPollingRoster()
17
-  App.Com.whoUseThisApp()
18
-  App.Com.setPresence()
14
+  App.Com.initRoster(->
15
+    App.Com.startFetchingVcards()
16
+    App.Com.startPollingRoster()
17
+    App.Com.whoUseThisApp()
18
+    App.Com.setPresence()
19 19
 
20
-  App.Com.getMe()
21
-  App.Com.startPollingMessages()
20
+    App.Com.getMe()
21
+    App.Com.startPollingMessages()
22
+  )
22 23
 )
... ...
@@ -140,7 +140,7 @@ class WsChatController < WsController
140 140
     def kick_from_multichat
141 141
         chat_id = message[:chatId]
142 142
         client  = find_client(message[:me])
143
-        me      = client.jid.strip.to_s
143
+        owner   = client.jid.strip.to_s
144 144
         kick    = message[:kick]
145 145
 
146 146
         contacts = connection_store[:opened_chats][client][chat_id][:attendants]
... ...
@@ -148,7 +148,7 @@ class WsChatController < WsController
148 148
 
149 149
         connection_store[:opened_chats][client][chat_id][:attendants] = contacts
150 150
 
151
-        client.send(MessageBuilder::destroy_multichat(me, kick, chat_id))
151
+        client.send(MessageBuilder::destroy_multichat(owner, kick, chat_id))
152 152
 
153 153
         if contacts.empty?
154 154
             connection_store[:opened_chats][client].delete(chat_id)
... ...
@@ -178,22 +178,6 @@ class WsChatController < WsController
178 178
         client.send(MessageBuilder::send_multichat_contacts(client.jid.strip.to_s, from, chat_id, contacts))
179 179
     end
180 180
 
181
-    def sync_contacts_frontend(message, chat_id)
182
-        contacts = xml_contacts_to_array(message.first_element('synced_contacts'))
183
-
184
-        send_message 'app.chat.updateSyncedContacts',
185
-                     me: message.to.strip.to_s,
186
-                     contacts: contacts,
187
-                     owner: message.from.strip.to_s,
188
-                     chat_id: chat_id
189
-    end
190
-
191
-    def xml_contacts_to_array(xml_contacts)
192
-        xml_contacts.get_elements('contact').map do |contact|
193
-            contact.text
194
-        end
195
-    end
196
-
197 181
     def destroy_multichat(message, chat_id)
198 182
         client = find_client(message.to.strip.to_s)
199 183
         connection_store[:opened_chats][client].delete(chat_id)
... ...
@@ -12,4 +12,52 @@ class WsController < WebsocketRails::BaseController
12 12
         client = find_client(client_jid)
13 13
         client.jid.strip.to_s
14 14
     end
15
+
16
+    def multichats_owner
17
+        chats_owner = {}
18
+        connection_store[:clients].each do |client|
19
+            connection_store[:opened_chats][client].each do |chat_id, contacts|
20
+                if contacts[:owner] == client.jid.strip.to_s
21
+                    chats_owner[client] = chat_id
22
+                end
23
+            end
24
+        end
25
+
26
+        chats_owner
27
+    end
28
+
29
+    def kick_from_multichat2(kick)
30
+        chats = multichats_owner()
31
+        chats.each do |client, chat_id|
32
+            contacts = connection_store[:opened_chats][client][chat_id][:attendants]
33
+            contacts -= [kick]
34
+
35
+            if contacts.empty?
36
+                connection_store[:opened_chats][client].delete(chat_id)
37
+                send_message 'app.chat.destroyMultichat', chat_id: chat_id
38
+            else
39
+                contacts.each do |contact|
40
+                    client.send(MessageBuilder::send_multichat_contacts(client.jid.strip.to_s, contact, chat_id, contacts))
41
+                end
42
+
43
+                sync_contacts_frontend(MessageBuilder::send_multichat_contacts('', client.jid.strip.to_s, chat_id, contacts), chat_id)
44
+            end
45
+        end
46
+    end
47
+
48
+    def sync_contacts_frontend(message, chat_id)
49
+        contacts = xml_contacts_to_array(message.first_element('synced_contacts'))
50
+
51
+        send_message 'app.chat.updateSyncedContacts',
52
+                     me: message.to.strip.to_s,
53
+                     contacts: contacts,
54
+                     owner: message.from.strip.to_s,
55
+                     chat_id: chat_id
56
+    end
57
+
58
+    def xml_contacts_to_array(xml_contacts)
59
+        xml_contacts.get_elements('contact').map do |contact|
60
+            contact.text
61
+        end
62
+    end
15 63
 end
16 64
\ No newline at end of file
... ...
@@ -27,6 +27,7 @@ class WsRosterController < WsController
27 27
         # TODO: Pouzit najprv:
28 28
         # clients = Token.fing_user_accounts_having_to_token(session[:token])
29 29
         # ale toto, az ked budem mat dokonceny multiaccount (settings a popup)
30
+        # TODO: skusit zrychlit
30 31
         cookies = env['rack.request.cookie_hash'] # TODO: nahlasit bug na websocket-rails, lebo sa neda pristupit ku `cookies'
31 32
         cipher_key = cookies['key']
32 33
         cipher_iv = cookies['iv']
... ...
@@ -107,6 +108,8 @@ class WsRosterController < WsController
107 107
             roster.add_presence_callback do |roster_item, old_presence, new_presence|
108 108
                 if new_presence.type == :unavailable
109 109
                     result = {status: :offline, message: ''}
110
+                    # mozno treba vyhodit cloveka z multichatu, ak som jeho owner
111
+                    kick_from_multichat2(roster_item.jid.strip.to_s)
110 112
                 else
111 113
                     status = uniform_presence(new_presence.show)
112 114
                     result = { status: status, message: new_presence.status.to_s }