Browse code

Odoslanie spravy

Cinan Rakosnik authored on 13/04/2013 at 09:26:45
Showing 8 changed files
... ...
@@ -57,8 +57,14 @@ this.App =
57 57
       )
58 58
 
59 59
     sendMessage: (message, chatId, from) ->
60
-      App.Com.trigger(event: 'app.chat.sendMessage', data: {message: message, chatId: chatId, from: from}, success: (response) ->
61
-        App.debug response
60
+      App.Com.trigger(event: 'app.chat.sendMessage', data: {message: message, chatId: chatId, from: from}, success: ->
61
+        App.debug 'message sent from server'
62
+      )
63
+
64
+    openNewChatId: (chatOwner, attendant, chat) ->
65
+      @trigger(event: 'app.chat.newChatId', data: {chatOwner: chatOwner}, success: (response) =>
66
+        chat.setChatId(response.id)
67
+        @trigger(event: 'app.chat.addToChat', data: {chatOwner: chatOwner, chatId: response.id, jid: attendant} )
62 68
       )
63 69
 
64 70
     updateMyStatus: (message, state)->
... ...
@@ -4,10 +4,17 @@ class Xmpp.Models.Chat extends Xmpp.Models.Model
4 4
   defaults:
5 5
     who: null
6 6
     withWhom: null
7
+    chatId: null
7 8
 
8 9
   initialize: ->
9 10
     _.bindAll(this)
10 11
 
12
+    App.Com.openNewChatId(@get('who').get('jid'), @get('withWhom').get('jid'), this)
13
+
14
+  setChatId: (id) ->
15
+    App.debug ['chat has id', id]
16
+    @set('chatId', id)
17
+
11 18
 class Xmpp.Collections.ChatsCollection extends Backbone.Collection
12 19
   model: Xmpp.Models.Chat
13 20
 
... ...
@@ -38,5 +38,5 @@ class Xmpp.Views.Chat.WindowView extends Backbone.View
38 38
     message = input.val().trim()
39 39
 
40 40
     if (message)
41
-      @tab.getOwner().sendMessage(message, 1) #1 = chatId
41
+      @tab.getOwner().sendMessage(message, @tab.getChatId())
42 42
 
... ...
@@ -60,4 +60,7 @@ class Xmpp.Views.Tabbar.TabView extends Backbone.View
60 60
     Backbone.Events.trigger('openChat', @model)
61 61
 
62 62
   getOwner: ->
63
-    @model.get('who')
64 63
\ No newline at end of file
64
+    @model.get('who')
65
+
66
+  getChatId: ->
67
+    @model.get('chatId')
65 68
\ No newline at end of file
... ...
@@ -1,6 +1,56 @@
1
-class WsChatController < WebsocketRails::BaseController
1
+class WsChatController < WsController
2
+
3
+    def open_chat
4
+        id = Time.now.to_f
5
+        me = message[:chatOwner]
6
+        client = find_client(me)
7
+
8
+        connection_store[:opened_chats] = {} if ! connection_store[:opened_chats]
9
+        connection_store[:opened_chats][client] = {} if ! connection_store[:opened_chats][client]
10
+        connection_store[:opened_chats][client][id] = []
11
+
12
+        trigger_success id: id
13
+    end
14
+
15
+    def add_to_chat
16
+        client = find_client(message[:chatOwner])
17
+
18
+        chat_id = message[:chatId]
19
+        somebody = message[:jid]
20
+
21
+        connection_store[:opened_chats][client][chat_id] << somebody
22
+    end
2 23
 
3 24
     def send_chat_message
4
-        trigger_success status: 'sent'
25
+        me = message[:from]
26
+        client = find_client(me)
27
+
28
+        if !client
29
+            trigger_failure
30
+        else
31
+            messages = build_messages(message[:message], client, message[:chatId])
32
+
33
+            # Xmpp4r doesn't support XEP-0033 (multicast messages)
34
+            messages.each do |message|
35
+                client.send(message)
36
+            end
37
+
38
+            trigger_success
39
+        end
40
+    end
41
+
42
+    private
43
+
44
+    def build_messages(message, client_from, chat_id)
45
+        from = client_from.jid.to_s
46
+        attendants = connection_store[:opened_chats][client_from][chat_id]
47
+
48
+        attendants.map do |person|
49
+            message = Jabber::Message.new(person, message)
50
+            message.from = from
51
+            message.add_attribute('chat_id', chat_id)
52
+            message.add_attribute('is_simulating', true)
53
+            message
54
+        end
5 55
     end
6 56
 end
7 57
\ No newline at end of file
8 58
new file mode 100644
... ...
@@ -0,0 +1,15 @@
0
+class WsController < WebsocketRails::BaseController
1
+
2
+    protected
3
+
4
+    def find_client(client_jid)
5
+        connection_store[:clients].find do |client|
6
+            client.jid.strip.to_s == client_jid
7
+        end
8
+    end
9
+
10
+    def find_client_jid(client_jid)
11
+        client = find_client(client_jid)
12
+        client.jid.strip.to_s
13
+    end
14
+end
0 15
\ No newline at end of file
... ...
@@ -1,4 +1,4 @@
1
-class WsRosterController < WebsocketRails::BaseController
1
+class WsRosterController < WsController
2 2
     require 'xmpp4r/roster'
3 3
     require 'xmpp4r/vcard'
4 4
 
... ...
@@ -60,6 +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 65
             subscribe :sendMessage,         to: WsChatController, with_method: :send_chat_message
64 66
         end
65 67
     end