Browse code

FIX: nefukncne otvaranie a posielanie sprav v multichate

Cinan Rakosnik authored on 07/05/2013 at 21:52:00
Showing 4 changed files
... ...
@@ -188,9 +188,6 @@ this.App =
188 188
         App.debug ['message received', result]
189 189
 
190 190
         if (result.chat_id)
191
-          _.each(App.Collections.chats.models, (chat) ->
192
-            App.debug chat.get('chatId')
193
-          )
194 191
           chat = App.Collections.chats.findById(result.chat_id)
195 192
 
196 193
           Backbone.Events.trigger('openChat', chat)
... ...
@@ -200,7 +197,7 @@ this.App =
200 200
           )
201 201
 
202 202
           tab.showChat()
203
-          contact = App.Collections.contacts.findByJid(result.from)
203
+          contact = App.Collections.contacts.findByJid(App.stripJid(result.from))
204 204
           tab.chatWindow.appendMessage(contact, new Date(), result.message)
205 205
         else
206 206
           #TODO: prepisat tuto hrozu
... ...
@@ -15,9 +15,9 @@ class WsChatController < WsController
15 15
                 #    # toto mozem prijat len ako ucastnik multichatu (nie admin)
16 16
                 #    contacts = xml_contacts_to_array(answer)
17 17
                 #    sync_contacts_frontend(message.from, message.to, answer.attribute('chat_id').to_s, contacts)
18
-                #elsif answer = message.first_element('exported_chat')
19
-                #    contacts = xml_contacts_to_array(message.first_element('exported_chat'))
20
-                #    import_people_in_chat(message.from, message.to, answer.attribute('chat_id').to_s, contacts)
18
+                elsif answer = message.first_element('exported_chat')
19
+                    contacts = xml_contacts_to_array(message.first_element('exported_chat'))
20
+                    import_people_in_chat(message.from, message.to, answer.attribute('chat_id').to_s, contacts)
21 21
                 #elsif message.attribute('destroy_multichat')
22 22
                 #    destroy_multichat(to, chat_id)
23 23
                 #elsif message.attribute('req_update_contacts')
... ...
@@ -38,15 +38,15 @@ class WsChatController < WsController
38 38
         end
39 39
     end
40 40
 
41
-    def import_people_in_chat(from, to, chat_id, contacts)
41
+    def import_people_in_chat(owner, me, chat_id, contacts)
42 42
         #Rails.logger.debug ['imported chat arrived', message.to_s, chat_id]
43
-        client = find_client(to)
44
-        create_opened_chat(client, chat_id, from, contacts)
43
+        client = find_client(me.strip.to_s)
44
+        create_opened_chat(client, chat_id, owner.to_s, contacts)
45 45
 
46 46
         send_message 'app.chat.importChat',
47
-                     owner:    from,
47
+                     owner:    owner.strip.to_s,
48 48
                      chat_id:  chat_id,
49
-                     contacts: contacts
49
+                     contacts: strip_all(contacts)
50 50
     end
51 51
 
52 52
     # Owner vytvori najprv u seba novy multichat
... ...
@@ -56,7 +56,7 @@ class WsChatController < WsController
56 56
         chat_id = hash + Time.now.to_f.to_s
57 57
         client = find_client(me)
58 58
 
59
-        create_opened_chat(client, chat_id, me)
59
+        create_opened_chat(client, chat_id, client.jid.to_s)
60 60
 
61 61
         trigger_success id: chat_id
62 62
     end
... ...
@@ -67,33 +67,39 @@ class WsChatController < WsController
67 67
 
68 68
         chat_id  = message[:chatId]
69 69
         add      = message[:jid]
70
+        add_resource = find_multichat_supported(add)
70 71
 
71
-        connection_store[:opened_chats][client][chat_id][:attendants] << add
72
+        if add_resource
73
+            connection_store[:opened_chats][client][chat_id][:attendants] << add_resource
74
+            #Rails.logger.debug ['adding to multichat', add_resource]
72 75
 
73
-        contacts = connection_store[:opened_chats][client][chat_id][:attendants]
76
+            contacts = connection_store[:opened_chats][client][chat_id][:attendants]
74 77
 
75
-        contacts.each do |contact|
76
-            client.send(MessageBuilder::export_multichat(client.jid.strip.to_s, contact, chat_id, contacts))
77
-        end
78
+            contacts.each do |contact|
79
+                client.send(MessageBuilder::export_multichat(client.jid.to_s, contact, chat_id, contacts))
80
+            end
78 81
 
79
-        trigger_success
82
+            trigger_success
83
+        else
84
+            #Rails.logger.debug ['adding to multichat failure', connection_store[:presences][add]]
85
+            trigger_failure
86
+        end
80 87
     end
81 88
 
82 89
     def send_chat_message
83 90
         me = message[:from]
84 91
         client = find_client(me)
92
+        my_jid = client.jid.to_s
85 93
 
86 94
         if client
87 95
             chat_id = message[:chatId]
88 96
             if chat_id
89
-                me = client.jid.strip.to_s
90 97
                 attendants = connection_store[:opened_chats][client][chat_id][:attendants] + [connection_store[:opened_chats][client][chat_id][:owner]]
91
-                attendants -= [me]
98
+                attendants -= [my_jid]
92 99
 
93
-                messages = MessageBuilder::build_multi_messages(message[:message], me, attendants, chat_id)
94
-                #Rails.logger.debug messages
100
+                messages = MessageBuilder::build_multi_messages(message[:message], my_jid, attendants, chat_id)
95 101
             else
96
-                messages = [MessageBuilder::build_message(message[:message], me, message[:to])]
102
+                messages = [MessageBuilder::build_message(message[:message], my_jid, message[:to])]
97 103
             end
98 104
 
99 105
             # Xmpp4r doesn't support XEP-0033 (multicast messages)
... ...
@@ -14,11 +14,6 @@ class WsController < WebsocketRails::BaseController
14 14
         end
15 15
     end
16 16
 
17
-    def find_client_jid(client_jid)
18
-        client = find_client(client_jid)
19
-        client.jid.strip.to_s
20
-    end
21
-
22 17
     def where_i_am_multichat_owner
23 18
         chats_owner = {}
24 19
 
... ...
@@ -73,4 +68,21 @@ class WsController < WebsocketRails::BaseController
73 73
             contact.text
74 74
         end
75 75
     end
76
+
77
+    def find_multichat_supported(jid_stripped)
78
+        contact = connection_store[:presences][jid_stripped.to_sym].find do |resource, info|
79
+            info[:multichat]
80
+        end
81
+        contact.first
82
+    end
83
+
84
+    def strip_all(contacts)
85
+        if contacts.is_a? Array
86
+            contacts.map do |jid|
87
+                Jabber::JID.new(jid).strip!.to_s
88
+            end
89
+        else
90
+            contacts
91
+        end
92
+    end
76 93
 end
77 94
\ No newline at end of file
... ...
@@ -235,7 +235,7 @@ class WsRosterController < WsController
235 235
         connection_store[:clients].each do |client|
236 236
             client.add_message_callback do |message|
237 237
                 if message.attribute('i_am_using_same_app')
238
-                    connection_store[:presences][message.from.strip.to_s][message.from.to_s.to_sym][multichat: true]
238
+                    connection_store[:presences][message.from.strip.to_s][message.from.to_s.to_sym][:multichat] = true
239 239
 
240 240
                     send_message 'app.roster.using_this_app',
241 241
                                  jid: message.from.strip.to_s
... ...
@@ -271,7 +271,7 @@ class WsRosterController < WsController
271 271
                     priority: new_presence.priority
272 272
                 }
273 273
 
274
-                ask_if_using_this_app(connection_store[:link_roster_client][roster], new_presence.from.strip.to_s)
274
+                ask_if_using_this_app(connection_store[:link_roster_client][roster], new_presence.from.to_s)
275 275
             end
276 276
 
277 277
             result[:status] = select_most_online_status(roster_item.jid.strip.to_s)
... ...
@@ -297,10 +297,6 @@ class WsRosterController < WsController
297 297
     end
298 298
 
299 299
     def ask_if_using_this_app(client, contact)
300
-        return unless connection_store[:presences][contact]
301
-
302
-        connection_store[:presences][contact].each do |jid_resource, state|
303
-            client.send(MessageBuilder::control_question(client.jid.to_s, jid_resource.to_s))
304
-        end
300
+        client.send(MessageBuilder::control_question(client.jid.to_s, contact))
305 301
     end
306 302
 end