Browse code

Pripravene niektore metody pre inicializaciu multichatu

Cinan Rakosnik authored on 28/04/2013 at 11:57:00
Showing 4 changed files
... ...
@@ -62,7 +62,7 @@ this.App =
62 62
     sendMessage: (message, to, from, callbackOk, callbackFail) ->
63 63
       App.Com.trigger(event: 'app.chat.sendMessage', data: {message: message, to: to, from: from}, success: callbackOk, error: callbackFail)
64 64
 
65
-    sendMultiMessage: sendMessage: (message, chatId, from, callbackOk, callbackFail) ->
65
+    sendMultiMessage: (message, chatId, from, callbackOk, callbackFail) ->
66 66
       App.Com.trigger(event: 'app.chat.sendMessage', data: {message: message, chatId: chatId, from: from}, success: callbackOk, error: callbackFail)
67 67
 
68 68
     openNewMultiChatId: (chatOwner, attendant, chat) ->
... ...
@@ -71,6 +71,11 @@ this.App =
71 71
         @trigger(event: 'app.chat.addToMultiChat', data: {chatOwner: chatOwner, chatId: response.id, jid: attendant} )
72 72
       )
73 73
 
74
+    syncMultiChatContacts: (me, chatId) ->
75
+      @trigger(event: 'app.chat.syncMultiChatContacts', data: {me: me, chatId: chatId}, success: (contacts) =>
76
+        App.debug contacts
77
+      )
78
+
74 79
     updateMyStatus: (message, state)->
75 80
       App.Com.trigger(event: 'app.roster.updateMyStatus', data: {message: message, state: state})
76 81
 
... ...
@@ -3,12 +3,18 @@ class WsChatController < WsController
3 3
     def start_polling_messages
4 4
         connection_store[:clients].each do |client|
5 5
             client.add_message_callback do |message|
6
+                #noinspection RubyAssignmentExpressionInConditionalInspection
7
+
6 8
                 if message.body
7
-                    send_message 'app.chat.messageReceived',
8
-                                 from: message.from.strip.to_s,
9
-                                 to: message.to.strip.to_s,
10
-                                 message: message.body,
11
-                                 chat_id: if message.attribute(:is_simulating) then message.attribute(:chat_id) end
9
+                    process_incoming_message(message)
10
+                elsif request = message.first_element('sync_contacts_request')
11
+                    # toto mozem prijat len ako admin multichatu
12
+                    send_contacts(message, request.attribute('chat_id'))
13
+                elsif answer = message.first_element('synced_contacts')
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)
12 18
                 end
13 19
                 #TODO: upozornit na pisanie spravy
14 20
                 #TODO: odoslat informaciu o tom, ze pisem spravu
... ...
@@ -23,25 +29,39 @@ class WsChatController < WsController
23 23
         end
24 24
     end
25 25
 
26
-    def open_multichat
26
+    def import_people_in_chat(message)
27
+        client = find_client(message.to)
28
+        chat_id = message.attribute('chat_id')
29
+        contacts = xml_contacts_to_array(message.first_element('exported_chat'))
30
+
31
+        connection_store[:opened_chats] = {} if ! connection_store[:opened_chats]
32
+        connection_store[:opened_chats][client] = {} if ! connection_store[:opened_chats][client]
33
+        connection_store[:opened_chats][client][chat_id] = {attendants: contacts, owner: message.from}
34
+    end
35
+
36
+    def new_multichat
27 37
         id = Time.now.to_f
28 38
         me = message[:chatOwner]
29 39
         client = find_client(me)
30 40
 
31 41
         connection_store[:opened_chats] = {} if ! connection_store[:opened_chats]
32 42
         connection_store[:opened_chats][client] = {} if ! connection_store[:opened_chats][client]
33
-        connection_store[:opened_chats][client][id] = []
43
+        connection_store[:opened_chats][client][id] = {attendants: [], owner: nil}
34 44
 
35 45
         trigger_success id: id
36 46
     end
37 47
 
38
-    def add_to_chat
48
+    # Owner posle novemu cloveku informaciu o chat_id a kontaktoch
49
+    def add_to_multichatw
39 50
         client = find_client(message[:chatOwner])
40 51
 
41
-        chat_id = message[:chatId]
52
+        chat_id  = message[:chatId]
42 53
         somebody = message[:jid]
43 54
 
44
-        connection_store[:opened_chats][client][chat_id] << somebody
55
+        connection_store[:opened_chats][client][chat_id][:attendants] << somebody
56
+
57
+        contacts = connection_store[:opened_chats][client][chat_id][:attendants]
58
+        client.send(MessageBuilder::export_multichat(client.jid.strip.to_s, somebody, chat_id, contacts))
45 59
     end
46 60
 
47 61
     def send_chat_message
... ...
@@ -49,7 +69,15 @@ class WsChatController < WsController
49 49
         client = find_client(me)
50 50
 
51 51
         if client
52
-            messages = build_messages(message[:message], client, message[:chatId], message[:to])
52
+            chat_id = message[:chatId]
53
+            if chat_id
54
+                attendants = connection_store[:opened_chats][client][chat_id][:attendants]
55
+                           + connection_store[:opened_chats][client][chat_id][:owner]
56
+
57
+                messages = MessageBuilder::build_multi_messages(message[:message], client.jid.strip.to_s, attendants, chat_id)
58
+            else
59
+                messages = [MessageBuilder::build_message(message[:message], client.jid.strip.to_s, message[:to])]
60
+            end
53 61
 
54 62
             # Xmpp4r doesn't support XEP-0033 (multicast messages)
55 63
             messages.each do |message|
... ...
@@ -62,19 +90,43 @@ class WsChatController < WsController
62 62
         end
63 63
     end
64 64
 
65
+    def sync_chat_contacts
66
+        client = find_client(message[:me])
67
+        chat_id = message[:chatId]
68
+
69
+        owner = connection_store[:opened_chats][client][chat_id][:owner]
70
+        client.send(MessageBuilder::ask_for_multichat_contacts(client.jid.strip.to_s, owner, chat_id))
71
+    end
72
+
65 73
     private
66 74
 
67
-    def build_messages(message, client_from, chat_id, jid_to)
68
-        from = client_from.jid.to_s
75
+    def process_incoming_message(message)
76
+        send_message 'app.chat.messageReceived',
77
+                     from: message.from.strip.to_s,
78
+                     to: message.to.strip.to_s,
79
+                     message: message.body,
80
+                     chat_id: if message.attribute(:is_simulating) then message.attribute(:chat_id) end
81
+    end
69 82
 
70
-        attendants = chat_id ? connection_store[:opened_chats][client_from][chat_id] : [jid_to]
83
+    def send_contacts(message, chat_id)
84
+        from = message.from
85
+        client = find_client(message.to)
86
+        contacts = connection_store[:opened_chats][client][chat_id][:attendants]
87
+        client.send(MessageBuilder::send_multichat_contacts(client.jid.strip.to_s, from, chat_id, contacts))
88
+    end
89
+
90
+    def sync_contacts_frontend(message, chat_id)
91
+        contacts = xml_contacts_to_array(message.first_element('sync_contacts'))
92
+
93
+        send_message 'app.chat.updateSyncedContacts',
94
+                     me: message.to.strip.to_s,
95
+                     contacts: contacts,
96
+                     chat_id: chat_id
97
+    end
71 98
 
72
-        attendants.map do |person|
73
-            message = Jabber::Message.new(person, message)
74
-            message.from = from
75
-            message.add_attribute('chat_id', chat_id)
76
-            message.add_attribute('is_simulating', true)
77
-            message
99
+    def xml_contacts_to_array(xml_contacts)
100
+        xml_contacts.get_elements('contact').map do |contact|
101
+            contact.text
78 102
         end
79 103
     end
80 104
 end
81 105
\ No newline at end of file
82 106
new file mode 100644
... ...
@@ -0,0 +1,60 @@
0
+module MessageBuilder
1
+    def self.build_multi_messages(message, from, attendants, chat_id)
2
+        attendants.map do |person|
3
+            message = Jabber::Message.new(person, message)
4
+            message.from = from
5
+            message.add_attribute('chat_id', chat_id)
6
+            message.add_attribute('is_simulating', true)
7
+            message
8
+        end
9
+    end
10
+
11
+    def self.build_message(message, from, to)
12
+        message = Jabber::Message.new(to, message)
13
+        message.from = from
14
+
15
+        message
16
+    end
17
+
18
+    def self.ask_for_multichat_contacts(from, to, chat_id)
19
+        message = Jabber::Message.new(to)
20
+        message.from = from
21
+        sync_request = REXML::Element.new('sync_contacts_request')
22
+        sync_request.add_attributes('chat_id' => chat_id)
23
+        message.add_element sync_request
24
+
25
+        message
26
+    end
27
+
28
+    def self.send_multichat_contacts(from, to, chat_id, contacts)
29
+        message = Jabber::Message.new(to)
30
+        message.from = from
31
+        sync_answer = REXML::Element.new('synced_contacts')
32
+        collect_contacts(sync_answer, contacts)
33
+        sync_answer.add_attributes('chat_id' => chat_id)
34
+        message.add_element sync_answer
35
+
36
+        message
37
+    end
38
+
39
+    def self.export_multichat(from, to, chat_id, contacts)
40
+        message = Jabber::Message.new(to)
41
+        message.from = from
42
+        chat = REXML::Element.new('exported_chat')
43
+        collect_contacts(chat, contacts)
44
+        chat.add_attributes('chat_id' => chat_id)
45
+        message.add_element chat
46
+
47
+        message
48
+    end
49
+
50
+    private
51
+
52
+    def self.collect_contacts(to_element, contacts)
53
+        contacts.each do |contact|
54
+            contact_xml = REXML::Element.new('contact')
55
+            contact_xml.text = contact
56
+            to_element.add_element contact_xml
57
+        end
58
+    end
59
+end
0 60
\ No newline at end of file
... ...
@@ -43,7 +43,7 @@ WebsocketRails::EventMap.describe do
43 43
   # The above will handle an event triggered on the client like `product.new`.
44 44
 
45 45
     #toto asi poide do nadtriedy spajajucej roster a chat a ine veci
46
-    subscribe :client_connected, to: WsRosterController, with_method: :connect
46
+    subscribe :client_connected,    to: WsRosterController, with_method: :connect
47 47
     subscribe :client_disconnected, to: WsRosterController, with_method: :disconnect
48 48
 
49 49
     namespace :app do
... ...
@@ -60,10 +60,11 @@ WebsocketRails::EventMap.describe do
60 60
         end
61 61
 
62 62
         namespace :chat do
63
-            subscribe :newMultiChatId,      to: WsChatController, with_method: :open_multichat
64
-            subscribe :addToMultiChat,      to: WsChatController, with_method: :add_to_multichat
65
-            subscribe :sendMessage,         to: WsChatController, with_method: :send_chat_message
66
-            subscribe :startPollingMessages,to: WsChatController, with_method: :start_polling_messages
63
+            subscribe :newMultiChatId,        to: WsChatController, with_method: :new_multichat
64
+            subscribe :addToMultiChat,        to: WsChatController, with_method: :add_to_multichat
65
+            subscribe :sendMessage,           to: WsChatController, with_method: :send_chat_message
66
+            subscribe :startPollingMessages,  to: WsChatController, with_method: :start_polling_messages
67
+            subscribe :syncMultiChatContacts, to: WsChatController, with_method: :sync_chat_contacts
67 68
         end
68 69
     end
69 70
 end