Browse code

Refaktorizacia #1

Cinan Rakosnik authored on 06/05/2013 at 12:45:21
Showing 3 changed files
... ...
@@ -3,22 +3,29 @@ 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
6
+                from = message.from.strip.to_s
7
+                to   = message.to.strip.to_s
8
+                chat_id = message.attribute('chat_id').to_s
7 9
 
10
+                #noinspection RubyAssignmentExpressionInConditionalInspection
8 11
                 if message.body
9
-                    process_incoming_message(message)
12
+                    process_incoming_message(from, to, message.body, chat_id)
10 13
                 elsif request = message.first_element('sync_contacts_request')
11 14
                     # toto mozem prijat len ako admin multichatu
12
-                    send_contacts(message, request.attribute('chat_id').to_s)
15
+                    send_contacts(from, to, request.attribute('chat_id').to_s)
13 16
                 elsif answer = message.first_element('synced_contacts')
14 17
                     # toto mozem prijat len ako ucastnik multichatu (nie admin)
15
-                    sync_contacts_frontend(message, answer.attribute('chat_id').to_s)
18
+                    contacts = xml_contacts_to_array(answer)
19
+                    sync_contacts_frontend(from, to, answer.attribute('chat_id').to_s, contacts)
16 20
                 elsif answer = message.first_element('exported_chat')
17
-                    import_people_in_chat(message, answer.attribute('chat_id').to_s)
21
+                    contacts = xml_contacts_to_array(message.first_element('exported_chat'))
22
+                    import_people_in_chat(from, to, answer.attribute('chat_id').to_s, contacts)
18 23
                 elsif message.attribute('destroy_multichat')
19
-                    destroy_multichat(message, message.attribute('chat_id').to_s)
24
+                    destroy_multichat(to, chat_id)
20 25
                 elsif message.attribute('req_update_contacts')
21
-                    update_attendants_in_multichat(message, message.attribute('chat_id').to_s)
26
+                    added   = xml_contacts_to_array(message.first_element('added'))
27
+                    removed = xml_contacts_to_array(message.first_element('removed'))
28
+                    update_attendants_in_multichat(from, to, chat_id, removed, added)
22 29
                 end
23 30
                 #TODO: upozornit na pisanie spravy
24 31
                 #TODO: odoslat informaciu o tom, ze pisem spravu
... ...
@@ -33,20 +40,19 @@ class WsChatController < WsController
33 33
         end
34 34
     end
35 35
 
36
-    def import_people_in_chat(message, chat_id)
36
+    def import_people_in_chat(from, to, chat_id, contacts)
37 37
         #Rails.logger.debug ['imported chat arrived', message.to_s, chat_id]
38 38
 
39
-        client = find_client(message.to.strip.to_s)
40
-        contacts = xml_contacts_to_array(message.first_element('exported_chat'))
39
+        client = find_client(to)
41 40
 
42 41
         connection_store[:opened_chats] = {} if ! connection_store[:opened_chats]
43 42
         connection_store[:opened_chats][client] = {} if ! connection_store[:opened_chats][client]
44
-        connection_store[:opened_chats][client][chat_id] = {attendants: contacts, owner: message.from.strip.to_s}
43
+        connection_store[:opened_chats][client][chat_id] = {attendants: contacts, owner: from}
45 44
 
46 45
         #Rails.logger.debug [connection_store[:opened_chats][client], client.jid.to_s]
47 46
 
48 47
         send_message 'app.chat.importChat',
49
-                     owner:    message.from.strip.to_s,
48
+                     owner:    from,
50 49
                      chat_id:  chat_id,
51 50
                      contacts: contacts
52 51
     end
... ...
@@ -162,48 +168,45 @@ class WsChatController < WsController
162 162
 
163 163
     private
164 164
 
165
-    def process_incoming_message(message)
165
+    def process_incoming_message(from, to, body, chat_id = nil)
166 166
         #Rails.logger.debug [message, message.to.strip.to_s]
167 167
         send_message 'app.chat.messageReceived',
168
-                     from: message.from.strip.to_s,
169
-                     to: message.to.strip.to_s,
170
-                     message: message.body,
171
-                     chat_id: message.attribute('chat_id').to_s
168
+                     from: from,
169
+                     to: to,
170
+                     message: body,
171
+                     chat_id: chat_id
172 172
     end
173 173
 
174
-    def send_contacts(message, chat_id)
175
-        from = message.from.strip.to_s
176
-        client = find_client(message.to.strip.to_s)
174
+    def send_contacts(from, to, chat_id)
175
+        client = find_client(to)
177 176
         contacts = connection_store[:opened_chats][client][chat_id][:attendants]
178 177
         client.send(MessageBuilder::send_multichat_contacts(client.jid.strip.to_s, from, chat_id, contacts))
179 178
     end
180 179
 
181
-    def destroy_multichat(message, chat_id)
182
-        client = find_client(message.to.strip.to_s)
180
+    def destroy_multichat(to, chat_id)
181
+        client = find_client(to)
183 182
         connection_store[:opened_chats][client].delete(chat_id)
184 183
 
185 184
         send_message 'app.chat.destroyMultichat', chat_id: chat_id
186 185
     end
187 186
 
188
-    def update_attendants_in_multichat(message, chat_id)
189
-        client = find_client(message.to.strip.to_s)
190
-        added = message.first_element('added')
191
-        removed = message.first_element('removed')
187
+    def update_attendants_in_multichat(from, to, chat_id, removed, added)
188
+        client = find_client(to)
192 189
 
193 190
         contacts = connection_store[:opened_chats][client][chat_id][:attendants]
194
-        contacts -= xml_contacts_to_array(removed)
195
-        contacts += xml_contacts_to_array(added)
191
+        contacts -= removed
192
+        contacts += added
196 193
 
197 194
         connection_store[:opened_chats][client][chat_id][:attendants] = contacts
198 195
 
199 196
         if contacts.empty?
200
-            destroy_multichat(message, chat_id)
197
+            destroy_multichat(to, chat_id)
201 198
         else
202 199
             contacts.each do |contact|
203
-                client.send(MessageBuilder::send_multichat_contacts(client.jid.strip.to_s, contact, chat_id, contacts))
200
+                client.send(MessageBuilder::send_multichat_contacts(to, contact, chat_id, contacts))
204 201
             end
205 202
 
206
-            sync_contacts_frontend(MessageBuilder::send_multichat_contacts('', client.jid.strip.to_s, chat_id, contacts), chat_id)
203
+            sync_contacts_frontend(from, to, chat_id, contacts)
207 204
         end
208 205
     end
209 206
 end
210 207
\ No newline at end of file
... ...
@@ -40,18 +40,16 @@ class WsController < WebsocketRails::BaseController
40 40
                     client.send(MessageBuilder::send_multichat_contacts(client.jid.strip.to_s, contact, chat_id, contacts))
41 41
                 end
42 42
 
43
-                sync_contacts_frontend(MessageBuilder::send_multichat_contacts('', client.jid.strip.to_s, chat_id, contacts), chat_id)
43
+                sync_contacts_frontend('', client.jid.strip.to_s, chat_id, contacts)
44 44
             end
45 45
         end
46 46
     end
47 47
 
48
-    def sync_contacts_frontend(message, chat_id)
49
-        contacts = xml_contacts_to_array(message.first_element('synced_contacts'))
50
-
48
+    def sync_contacts_frontend(from, to, chat_id, contacts)
51 49
         send_message 'app.chat.updateSyncedContacts',
52
-                     me: message.to.strip.to_s,
50
+                     me: to,
53 51
                      contacts: contacts,
54
-                     owner: message.from.strip.to_s,
52
+                     owner: from,
55 53
                      chat_id: chat_id
56 54
     end
57 55
 
... ...
@@ -109,7 +109,7 @@ class WsRosterController < WsController
109 109
                 if new_presence.type == :unavailable
110 110
                     result = {status: :offline, message: ''}
111 111
                     # mozno treba vyhodit cloveka z multichatu, ak som jeho owner
112
-                    kick_from_multichat2(roster_item.jid.strip.to_s)
112
+                    #kick_from_multichat2(roster_item.jid.strip.to_s)
113 113
                 else
114 114
                     status = uniform_presence(new_presence.show)
115 115
                     result = { status: status, message: new_presence.status.to_s }