Browse code

Rozpoznavanie medzi multichatom a singlechatom

Cinan Rakosnik authored on 20/04/2013 at 13:07:15
Showing 7 changed files
... ...
@@ -59,13 +59,16 @@ this.App =
59 59
         )
60 60
       )
61 61
 
62
-    sendMessage: (message, chatId, from, callbackOk, callbackFail) ->
62
+    sendMessage: (message, to, from, callbackOk, callbackFail) ->
63
+      App.Com.trigger(event: 'app.chat.sendMessage', data: {message: message, to: to, from: from}, success: callbackOk, error: callbackFail)
64
+
65
+    sendMultiMessage: sendMessage: (message, chatId, from, callbackOk, callbackFail) ->
63 66
       App.Com.trigger(event: 'app.chat.sendMessage', data: {message: message, chatId: chatId, from: from}, success: callbackOk, error: callbackFail)
64 67
 
65
-    openNewChatId: (chatOwner, attendant, chat) ->
66
-      @trigger(event: 'app.chat.newChatId', data: {chatOwner: chatOwner}, success: (response) =>
68
+    openNewMultiChatId: (chatOwner, attendant, chat) ->
69
+      @trigger(event: 'app.chat.newMultiChatId', data: {chatOwner: chatOwner}, success: (response) =>
67 70
         chat.setChatId(response.id)
68
-        @trigger(event: 'app.chat.addToChat', data: {chatOwner: chatOwner, chatId: response.id, jid: attendant} )
71
+        @trigger(event: 'app.chat.addToMultiChat', data: {chatOwner: chatOwner, chatId: response.id, jid: attendant} )
69 72
       )
70 73
 
71 74
     updateMyStatus: (message, state)->
... ...
@@ -9,7 +9,7 @@ class Xmpp.Models.Chat extends Xmpp.Models.Model
9 9
   initialize: ->
10 10
     _.bindAll(this)
11 11
 
12
-    App.Com.openNewChatId(@get('who').get('jid'), @get('withWhom').get('jid'), this)
12
+    #App.Com.openNewMultiChatId(@get('who').get('jid'), @get('withWhom').get('jid'), this)
13 13
 
14 14
   setChatId: (id) ->
15 15
     App.debug ['chat has id', id]
... ...
@@ -17,7 +17,10 @@ class Xmpp.Models.Me extends Xmpp.Models.Contact
17 17
     @view = new Xmpp.Views.Contacts.MeView(model: this)
18 18
     @view.render()
19 19
 
20
-  sendMessage: (message, chatId, callbackOk, callbackFail) ->
20
+  sendMessage: (message, chatId, to, callbackOk, callbackFail) ->
21 21
     App.debug ['sending from:', @get('jid'), message]
22 22
 
23
-    App.Com.sendMessage(message, chatId, @get('jid'), callbackOk, callbackFail)
24 23
\ No newline at end of file
24
+    if (! chatId)
25
+      App.Com.sendMessage(message, to, @get('jid'), callbackOk, callbackFail)
26
+    else
27
+      App.Com.sendMultiMessage(message, chatId, @get('jid'), callbackOk, callbackFail)
25 28
\ No newline at end of file
... ...
@@ -51,7 +51,8 @@ class Xmpp.Views.Chat.WindowView extends Backbone.View
51 51
     input.val('').focus()
52 52
 
53 53
     if (message)
54
-      @tab.getOwner().sendMessage(message, @tab.getChatId()
54
+      attendant = @tab.getAttendant().get('jid')
55
+      @tab.getOwner().sendMessage(message, @tab.getChatId(), attendant
55 56
       , (message) =>
56 57
         @sendSuccess(@tab.getOwner(), message)
57 58
       , (message) ->
... ...
@@ -63,4 +63,7 @@ class Xmpp.Views.Tabbar.TabView extends Backbone.View
63 63
     @model.get('who')
64 64
 
65 65
   getChatId: ->
66
-    @model.get('chatId')
67 66
\ No newline at end of file
67
+    @model.get('chatId')
68
+
69
+  getAttendant: ->
70
+    @model.get('withWhom')
68 71
\ No newline at end of file
... ...
@@ -23,7 +23,7 @@ class WsChatController < WsController
23 23
         end
24 24
     end
25 25
 
26
-    def open_chat
26
+    def open_multichat
27 27
         id = Time.now.to_f
28 28
         me = message[:chatOwner]
29 29
         client = find_client(me)
... ...
@@ -49,7 +49,7 @@ class WsChatController < WsController
49 49
         client = find_client(me)
50 50
 
51 51
         if client
52
-            messages = build_messages(message[:message], client, message[:chatId])
52
+            messages = build_messages(message[:message], client, message[:chatId], message[:to])
53 53
 
54 54
             # Xmpp4r doesn't support XEP-0033 (multicast messages)
55 55
             messages.each do |message|
... ...
@@ -64,9 +64,10 @@ class WsChatController < WsController
64 64
 
65 65
     private
66 66
 
67
-    def build_messages(message, client_from, chat_id)
67
+    def build_messages(message, client_from, chat_id, jid_to)
68 68
         from = client_from.jid.to_s
69
-        attendants = connection_store[:opened_chats][client_from][chat_id]
69
+
70
+        attendants = chat_id ? connection_store[:opened_chats][client_from][chat_id] : [jid_to]
70 71
 
71 72
         attendants.map do |person|
72 73
             message = Jabber::Message.new(person, message)
... ...
@@ -60,8 +60,8 @@ WebsocketRails::EventMap.describe do
60 60
         end
61 61
 
62 62
         namespace :chat do
63
-            subscribe :newChatId,           to: WsChatController, with_method: :open_chat
64
-            subscribe :addToChat,           to: WsChatController, with_method: :add_to_chat
63
+            subscribe :newMultiChatId,      to: WsChatController, with_method: :open_multichat
64
+            subscribe :addToMultiChat,      to: WsChatController, with_method: :add_to_multichat
65 65
             subscribe :sendMessage,         to: WsChatController, with_method: :send_chat_message
66 66
             subscribe :startPollingMessages,to: WsChatController, with_method: :start_polling_messages
67 67
         end