Browse code

Odoslanie a prijatie udajov o multichate (owner a kontakty)

Cinan Rakosnik authored on 28/04/2013 at 15:23:48
Showing 4 changed files
... ...
@@ -106,6 +106,17 @@ this.App =
106 106
         App.Collections.contacts.subscriptionChanged(subscription)
107 107
       )
108 108
 
109
+      App._dispatcher.bind('app.chat.importChat', (chatData) ->
110
+        newChat = new Xmpp.Models.Chat(
111
+          chatId: chatData.chat_id,
112
+          isMultiChat: true
113
+        );
114
+
115
+        App.Collections.chats.add(newChat);
116
+        newChat.syncContacts(chatData.contacts, chatData.owner);
117
+        Backbone.Events.trigger('openChat', newChat)
118
+      )
119
+
109 120
       App._dispatcher.bind('app.chat.messageReceived', (result) ->
110 121
         App.debug 'message received'
111 122
 
... ...
@@ -29,6 +29,34 @@ class Xmpp.Models.Chat extends Xmpp.Models.Model
29 29
     App.debug ['multichat has id', id]
30 30
     @set('chatId', id)
31 31
 
32
+  syncContacts: (contacts, owner) ->
33
+    contactsWithoutMe = _.filter(contacts, (jid) ->
34
+      jid != App.Models.me.get('jid')
35
+    )
36
+
37
+    attendants = _.map(contactsWithoutMe, (jid) =>
38
+      contact = App.Collections.contacts.findByJid(jid)
39
+      if not contact
40
+        newTempContact = @createTempContact(jid)
41
+        App.Collections.contacts.add(newTempContact)
42
+        contact = newTempContact
43
+
44
+      contact
45
+    )
46
+
47
+    @set('withWhom', attendants);
48
+
49
+    if owner
50
+      ownerContact = App.Collections.contacts.findByJid(owner)
51
+      if not ownerContact
52
+        ownerContact = @createTempContact(owner)
53
+        App.Collections.contacts.add(ownerContact)
54
+
55
+      @set('who', ownerContact)
56
+
57
+    App.debug ['importing contacts to chat', this]
58
+
59
+
32 60
 class Xmpp.Collections.ChatsCollection extends Backbone.Collection
33 61
   model: Xmpp.Models.Chat
34 62
 
... ...
@@ -48,4 +76,8 @@ class Xmpp.Collections.ChatsCollection extends Backbone.Collection
48 48
     @findWhere(chatId: id)
49 49
 
50 50
   removeChat: (chat) ->
51
-    @models = _.without(@openedChats, chat)
52 51
\ No newline at end of file
52
+    @models = _.without(@openedChats, chat)
53
+
54
+  createTempContact: (jid) ->
55
+    newTempContact = new Xmpp.Models.Contact(jid: jid, belongsTo: [App.Models.me])
56
+    newTempContact
53 57
\ No newline at end of file
... ...
@@ -15,6 +15,8 @@ class Xmpp.Models.Contact extends Xmpp.Models.Model
15 15
     if ! @get('name')
16 16
       @set(name: @get('jid'))
17 17
 
18
+    @set('id', @get('jid'))
19
+
18 20
   firstname: ->
19 21
     @get('name').split(' ')[0]
20 22
 
... ...
@@ -9,12 +9,12 @@ class WsChatController < WsController
9 9
                     process_incoming_message(message)
10 10
                 elsif request = message.first_element('sync_contacts_request')
11 11
                     # toto mozem prijat len ako admin multichatu
12
-                    send_contacts(message, request.attribute('chat_id'))
12
+                    send_contacts(message, request.attribute('chat_id').to_s)
13 13
                 elsif answer = message.first_element('synced_contacts')
14 14
                     # toto mozem prijat len ako ucastnik multichatu (nie admin)
15
-                    sync_contacts_frontend(message, answer.attribute('chat_id'))
16
-                elsif message.first_element('exported_chat')
17
-                    import_people_in_chat(message)
15
+                    sync_contacts_frontend(message, answer.attribute('chat_id').to_s)
16
+                elsif answer = message.first_element('exported_chat')
17
+                    import_people_in_chat(message, answer.attribute('chat_id').to_s)
18 18
                 end
19 19
                 #TODO: upozornit na pisanie spravy
20 20
                 #TODO: odoslat informaciu o tom, ze pisem spravu
... ...
@@ -29,14 +29,26 @@ class WsChatController < WsController
29 29
         end
30 30
     end
31 31
 
32
-    def import_people_in_chat(message)
32
+    def import_people_in_chat(message, chat_id)
33
+        Rails.logger.debug ['imported chat arrived', message.to_s, chat_id]
34
+
33 35
         client = find_client(message.to)
34
-        chat_id = message.attribute('chat_id')
35 36
         contacts = xml_contacts_to_array(message.first_element('exported_chat'))
36 37
 
37 38
         connection_store[:opened_chats] = {} if ! connection_store[:opened_chats]
38 39
         connection_store[:opened_chats][client] = {} if ! connection_store[:opened_chats][client]
39
-        connection_store[:opened_chats][client][chat_id] = {attendants: contacts, owner: message.from}
40
+        connection_store[:opened_chats][client][chat_id] = {attendants: contacts, owner: message.from.node + '@' + message.from.domain}
41
+
42
+        Rails.logger.debug [
43
+            owner: message.from.node + '@' + message.from.domain,
44
+            chat_id:  chat_id,
45
+            contacts: contacts
46
+        ]
47
+
48
+        send_message 'app.chat.importChat',
49
+                     owner:    message.from.node + '@' + message.from.domain,
50
+                     chat_id:  chat_id,
51
+                     contacts: contacts
40 52
     end
41 53
 
42 54
     # Owner vytvori najprv u seba novy multichat