Browse code

Posielanie sprav medzi ludmi v multichate

Cinan Rakosnik authored on 30/04/2013 at 21:21:03
Showing 7 changed files
... ...
@@ -122,39 +122,56 @@ this.App =
122 122
           isMultiChat: true
123 123
         );
124 124
 
125
+        App.debug newChat.get('chatId')
126
+
125 127
         App.Collections.chats.add(newChat);
126 128
         newChat.syncContacts(chatData.contacts, chatData.owner);
127 129
         Backbone.Events.trigger('openChat', newChat)
128 130
       )
129 131
 
130 132
       App._dispatcher.bind('app.chat.messageReceived', (result) ->
131
-        App.debug 'message received'
133
+        App.debug ['message received', result]
132 134
 
133
-        contact = App.Collections.contacts.findByJid(result.from)
135
+        if (result.chat_id)
136
+          _.each(App.Collections.chats.models, (chat) ->
137
+            App.debug chat.get('chatId')
138
+          )
139
+          chat = App.Collections.chats.findById(result.chat_id)
134 140
 
135
-        if contact
136
-          contactView = App.Collections.contacts.friendsList.hasContact(contact) ||
137
-            App.Collections.contacts.activeList.hasContact(contact)
138
-        else
139
-          contact = new Xmpp.Models.Contact(
140
-            id: result.from
141
-            jid: result.from
142
-            belongsTo: [result.to]
141
+          Backbone.Events.trigger('openChat', chat)
142
+
143
+          tab = _.find(App.Views.tabbar.tabs, (tab) ->
144
+            tab.getChatId() == result.chat_id
143 145
           )
144
-          App.Collections.contacts.add(contact, merge: true)
145
-          contactView = App.Collections.contacts.friendsList.hasContact(contact)
146 146
 
147
-        contactView.startChat()
147
+          tab.showChat()
148
+          contact = App.Collections.contacts.findByJid(result.from)
149
+          tab.chatWindow.appendMessage(contact, new Date(), result.message)
150
+        else
151
+          #TODO: prepisat tuto hrozu
152
+
148 153
 
149
-        tab = _.find(App.Views.tabbar.tabs, (tab) ->
150
-          tab.hasParticipants(App.Models.me, contact)
151
-        )
154
+          contact = App.Collections.contacts.findByJid(result.from)
155
+
156
+          if contact
157
+            contactView = App.Collections.contacts.friendsList.hasContact(contact) ||
158
+              App.Collections.contacts.activeList.hasContact(contact)
159
+          else
160
+            contact = new Xmpp.Models.Contact(
161
+              id: result.from
162
+              jid: result.from
163
+              belongsTo: [result.to]
164
+            )
165
+            App.Collections.contacts.add(contact, merge: true)
166
+            contactView = App.Collections.contacts.friendsList.hasContact(contact)
167
+
168
+          contactView.startChat()
152 169
 
153
-        tab.chatWindow.appendMessage(contact, new Date(), result.message)
170
+          tab = _.find(App.Views.tabbar.tabs, (tab) ->
171
+            tab.hasParticipants(App.Models.me, contact)
172
+          )
154 173
 
155
-#        if (result.chat_id)
156
-#          #todo otvorit chat podla chat_id. Spytat sa servera, kto vsetko je ucastnikom
157
-#          #chat = App.Collections.chats.findById(result.chat_id)
174
+          tab.chatWindow.appendMessage(contact, new Date(), result.message)
158 175
       )
159 176
 
160 177
   Models:
... ...
@@ -13,8 +13,6 @@ class Xmpp.Models.Chat extends Xmpp.Models.Model
13 13
     if (@get('isMultiChat'))
14 14
       @set('withWhom', [])
15 15
 
16
-    #App.Com.openNewMultiChatId(@get('who').get('jid'), @get('withWhom').get('jid'), this)
17
-
18 16
   appendWithWhom: (newPerson) ->
19 17
     allWithWhom = @get('withWhom');
20 18
 
... ...
@@ -57,7 +55,7 @@ class Xmpp.Models.Chat extends Xmpp.Models.Model
57 57
 
58 58
       @set('who', ownerContact)
59 59
 
60
-    App.debug ['importing contacts to chat', this]
60
+    App.debug ['importing contacts to chat', this, this.get('chatId')]
61 61
 
62 62
 
63 63
 class Xmpp.Collections.ChatsCollection extends Backbone.Collection
... ...
@@ -76,7 +74,9 @@ class Xmpp.Collections.ChatsCollection extends Backbone.Collection
76 76
     )
77 77
 
78 78
   findById: (id) ->
79
-    @findWhere(chatId: id)
79
+    _.find(@models, (chat) ->
80
+      chat.get('chatId') == id
81
+    )
80 82
 
81 83
   removeChat: (chat) ->
82 84
     @models = _.without(@openedChats, chat)
... ...
@@ -41,6 +41,7 @@ class Xmpp.Collections.ContactsCollection extends Backbone.Collection
41 41
 
42 42
     Backbone.Events.on('openChat', (chat) =>
43 43
       @moveToInactiveList('all')
44
+#      App.debug chat
44 45
       @moveToActiveList(chat.get('withWhom'))
45 46
     )
46 47
 
... ...
@@ -20,7 +20,7 @@ class Xmpp.Models.Me extends Xmpp.Models.Contact
20 20
   sendMessage: (message, chatId, to, callbackOk, callbackFail) ->
21 21
     App.debug ['sending from:', @get('jid'), message]
22 22
 
23
-    if (! chatId)
23
+    if (to)
24 24
       App.Com.sendMessage(message, to, @get('jid'), callbackOk, callbackFail)
25 25
     else
26 26
       App.Com.sendMultiMessage(message, chatId, @get('jid'), callbackOk, callbackFail)
27 27
\ No newline at end of file
... ...
@@ -47,15 +47,17 @@ class Xmpp.Views.Chat.WindowView extends Backbone.View
47 47
     e.preventDefault()
48 48
     $this = $(e.currentTarget)
49 49
     input = $this.find(@inputSelector)
50
-    message = input.val().trim()
50
+    message = input.val()
51 51
     input.val('').focus()
52 52
 
53
-    if (message)
54
-      attendant = @tab.getAttendant().get('jid')
55
-      @tab.getOwner().sendMessage(message, @tab.getChatId(), attendant
53
+    if (message.trim())
54
+      chatId = @tab.getChatId()
55
+      attendant = if not chatId then @tab.getAttendant().get('jid') else null
56
+
57
+      App.Models.me.sendMessage(message, chatId, attendant
56 58
       , (message) =>
57 59
         @sendSuccess(@tab.getOwner(), message)
58
-      , (message) ->
60
+      , (message) =>
59 61
         @sendFail(message))
60 62
 
61 63
   sendSuccess: (me, msg) ->
... ...
@@ -65,10 +65,11 @@ class Xmpp.Views.Contacts.ContactView extends Backbone.View
65 65
     App.debug 'new multichat'
66 66
     withWhom = @model
67 67
 
68
-    newChat = new Xmpp.Models.Chat(isMultiChat: true);
69
-    newChat.set('who', App.Models.me);
70
-    newChat.appendWithWhom(withWhom);
68
+    newChat = new Xmpp.Models.Chat(isMultiChat: true)
69
+    newChat.set('who', App.Models.me)
70
+    newChat.appendWithWhom(withWhom)
71 71
 
72
+    App.Collections.chats.add(newChat)
72 73
     App.Com.openNewMultiChat(App.Models.me, withWhom, newChat)
73 74
 
74 75
   openChatById: (chatId) ->
... ...
@@ -30,20 +30,20 @@ class WsChatController < WsController
30 30
     end
31 31
 
32 32
     def import_people_in_chat(message, chat_id)
33
-        Rails.logger.debug ['imported chat arrived', message.to_s, chat_id]
33
+        #Rails.logger.debug ['imported chat arrived', message.to_s, chat_id]
34 34
 
35
-        client = find_client(message.to)
35
+        client = find_client(message.to.strip.to_s)
36 36
         contacts = xml_contacts_to_array(message.first_element('exported_chat'))
37 37
 
38 38
         connection_store[:opened_chats] = {} if ! connection_store[:opened_chats]
39 39
         connection_store[:opened_chats][client] = {} if ! connection_store[:opened_chats][client]
40 40
         connection_store[:opened_chats][client][chat_id] = {attendants: contacts, owner: message.from.node + '@' + message.from.domain}
41 41
 
42
-        Rails.logger.debug [
43
-            owner: message.from.node + '@' + message.from.domain,
44
-            chat_id:  chat_id,
45
-            contacts: contacts
46
-        ]
42
+        #Rails.logger.debug [
43
+        #    owner: message.from.node + '@' + message.from.domain,
44
+        #    chat_id:  chat_id,
45
+        #    contacts: contacts
46
+        #]
47 47
 
48 48
         send_message 'app.chat.importChat',
49 49
                      owner:    message.from.node + '@' + message.from.domain,
... ...
@@ -87,8 +87,8 @@ class WsChatController < WsController
87 87
         if client
88 88
             chat_id = message[:chatId]
89 89
             if chat_id
90
-                attendants = connection_store[:opened_chats][client][chat_id][:attendants]
91
-                           + connection_store[:opened_chats][client][chat_id][:owner]
90
+                attendants = connection_store[:opened_chats][client][chat_id][:attendants] + [connection_store[:opened_chats][client][chat_id][:owner]]
91
+                attendants -= [client.jid.strip.to_s]
92 92
 
93 93
                 messages = MessageBuilder::build_multi_messages(message[:message], client.jid.strip.to_s, attendants, chat_id)
94 94
             else
... ...
@@ -97,6 +97,7 @@ class WsChatController < WsController
97 97
 
98 98
             # Xmpp4r doesn't support XEP-0033 (multicast messages)
99 99
             messages.each do |message|
100
+                #Rails.logger.debug message.to_s
100 101
                 client.send(message)
101 102
             end
102 103
 
... ...
@@ -117,16 +118,17 @@ class WsChatController < WsController
117 117
     private
118 118
 
119 119
     def process_incoming_message(message)
120
+        #Rails.logger.debug message
120 121
         send_message 'app.chat.messageReceived',
121 122
                      from: message.from.strip.to_s,
122 123
                      to: message.to.strip.to_s,
123 124
                      message: message.body,
124
-                     chat_id: if message.attribute(:is_simulating) then message.attribute(:chat_id) end
125
+                     chat_id: message.attribute('chat_id').to_s
125 126
     end
126 127
 
127 128
     def send_contacts(message, chat_id)
128
-        from = message.from
129
-        client = find_client(message.to)
129
+        from = message.from.strip.to_s
130
+        client = find_client(message.to.strip.to_s)
130 131
         contacts = connection_store[:opened_chats][client][chat_id][:attendants]
131 132
         client.send(MessageBuilder::send_multichat_contacts(client.jid.strip.to_s, from, chat_id, contacts))
132 133
     end