Browse code

Refaktorizacia #2

Cinan Rakosnik authored on 06/05/2013 at 13:38:47
Showing 4 changed files
... ...
@@ -88,13 +88,14 @@ class WsChatController < WsController
88 88
         if client
89 89
             chat_id = message[:chatId]
90 90
             if chat_id
91
+                me = client.jid.strip.to_s
91 92
                 attendants = connection_store[:opened_chats][client][chat_id][:attendants] + [connection_store[:opened_chats][client][chat_id][:owner]]
92
-                attendants -= [client.jid.strip.to_s]
93
+                attendants -= [me]
93 94
 
94
-                messages = MessageBuilder::build_multi_messages(message[:message], client.jid.strip.to_s, attendants, chat_id)
95
+                messages = MessageBuilder::build_multi_messages(message[:message], me, attendants, chat_id)
95 96
                 #Rails.logger.debug messages
96 97
             else
97
-                messages = [MessageBuilder::build_message(message[:message], client.jid.strip.to_s, message[:to])]
98
+                messages = [MessageBuilder::build_message(message[:message], me, message[:to])]
98 99
             end
99 100
 
100 101
             # Xmpp4r doesn't support XEP-0033 (multicast messages)
... ...
@@ -125,7 +126,7 @@ class WsChatController < WsController
125 125
         if owner == me
126 126
             attendants = connection_store[:opened_chats][client][chat_id][:attendants]
127 127
             attendants.each do |attendant|
128
-                client.send(MessageBuilder::destroy_multichat(me, attendant, chat_id))
128
+                client.send(MessageBuilder::kick_from_multichat(me, attendant, chat_id))
129 129
             end
130 130
         else
131 131
             changes = {removed: [me]}
... ...
@@ -135,29 +136,6 @@ class WsChatController < WsController
135 135
         connection_store[:opened_chats][client].delete(chat_id)
136 136
     end
137 137
 
138
-    def kick_from_multichat
139
-        chat_id = message[:chatId]
140
-        client  = find_client(message[:me])
141
-        owner   = client.jid.strip.to_s
142
-        kick    = message[:kick]
143
-
144
-        contacts = connection_store[:opened_chats][client][chat_id][:attendants]
145
-        contacts -= [kick]
146
-
147
-        connection_store[:opened_chats][client][chat_id][:attendants] = contacts
148
-
149
-        client.send(MessageBuilder::destroy_multichat(owner, kick, chat_id))
150
-
151
-        if contacts.empty?
152
-            connection_store[:opened_chats][client].delete(chat_id)
153
-            send_message 'app.chat.destroyMultichat', chat_id: chat_id
154
-        else
155
-            contacts.each do |contact|
156
-                client.send(MessageBuilder::send_multichat_contacts(client.jid.strip.to_s, contact, chat_id, contacts))
157
-            end
158
-        end
159
-    end
160
-
161 138
     private
162 139
 
163 140
     def process_incoming_message(from, to, body, chat_id = nil)
... ...
@@ -175,13 +153,6 @@ class WsChatController < WsController
175 175
         client.send(MessageBuilder::send_multichat_contacts(client.jid.strip.to_s, from, chat_id, contacts))
176 176
     end
177 177
 
178
-    def destroy_multichat(to, chat_id)
179
-        client = find_client(to)
180
-        connection_store[:opened_chats][client].delete(chat_id)
181
-
182
-        send_message 'app.chat.destroyMultichat', chat_id: chat_id
183
-    end
184
-
185 178
     def update_attendants_in_multichat(from, to, chat_id, removed, added)
186 179
         client = find_client(to)
187 180
 
... ...
@@ -19,7 +19,7 @@ class WsController < WebsocketRails::BaseController
19 19
         client.jid.strip.to_s
20 20
     end
21 21
 
22
-    def multichats_owner
22
+    def where_i_am_multichat_owner
23 23
         chats_owner = {}
24 24
         connection_store[:clients].each do |client|
25 25
             connection_store[:opened_chats][client].each do |chat_id, contacts|
... ...
@@ -32,15 +32,14 @@ class WsController < WebsocketRails::BaseController
32 32
         chats_owner
33 33
     end
34 34
 
35
-    def kick_from_multichat2(kick)
36
-        chats = multichats_owner()
35
+    def kick_from_all_multichats(kick)
36
+        chats = where_i_am_multichat_owner()
37 37
         chats.each do |client, chat_id|
38 38
             contacts = connection_store[:opened_chats][client][chat_id][:attendants]
39 39
             contacts -= [kick]
40 40
 
41 41
             if contacts.empty?
42
-                connection_store[:opened_chats][client].delete(chat_id)
43
-                send_message 'app.chat.destroyMultichat', chat_id: chat_id
42
+                destroy_multichat(client.jid.strip.to_s, chat_id)
44 43
             else
45 44
                 contacts.each do |contact|
46 45
                     client.send(MessageBuilder::send_multichat_contacts(client.jid.strip.to_s, contact, chat_id, contacts))
... ...
@@ -59,6 +58,13 @@ class WsController < WebsocketRails::BaseController
59 59
                      chat_id: chat_id
60 60
     end
61 61
 
62
+    def destroy_multichat(to, chat_id)
63
+        client = find_client(to)
64
+        connection_store[:opened_chats][client].delete(chat_id)
65
+
66
+        send_message 'app.chat.destroyMultichat', chat_id: chat_id
67
+    end
68
+
62 69
     def xml_contacts_to_array(xml_contacts)
63 70
         xml_contacts.get_elements('contact').map do |contact|
64 71
             contact.text
... ...
@@ -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_all_multichats(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 }
... ...
@@ -81,7 +81,7 @@ module MessageBuilder
81 81
         message
82 82
     end
83 83
 
84
-    def self.destroy_multichat(from, to, chat_id)
84
+    def self.kick_from_multichat(from, to, chat_id)
85 85
         message = Jabber::Message.new(to)
86 86
         message.from = from
87 87
         message.add_attributes('destroy_multichat' => 'true')