app/controllers/ws_controller.rb
2a61cdcc
 class WsController < WebsocketRails::BaseController
 
     protected
 
0f555f4a
     def create_opened_chat(client, chat_id, owner = '', attendants = [])
         connection_store[:opened_chats] = {} if ! connection_store[:opened_chats]
         connection_store[:opened_chats][client] = {} if ! connection_store[:opened_chats][client]
         connection_store[:opened_chats][client][chat_id] = {owner: owner, attendants: attendants}
     end
 
2a61cdcc
     def find_client(client_jid)
         connection_store[:clients].find do |client|
             client.jid.strip.to_s == client_jid
         end
     end
 
     def find_client_jid(client_jid)
         client = find_client(client_jid)
         client.jid.strip.to_s
     end
5011e360
 
88c1de39
     def where_i_am_multichat_owner
5011e360
         chats_owner = {}
51b580c8
         connection_store[:clients].is_a? Array && connection_store[:clients].each do |client|
5011e360
             connection_store[:opened_chats][client].each do |chat_id, contacts|
                 if contacts[:owner] == client.jid.strip.to_s
                     chats_owner[client] = chat_id
                 end
             end
         end
 
         chats_owner
     end
 
88c1de39
     def kick_from_all_multichats(kick)
         chats = where_i_am_multichat_owner()
5011e360
         chats.each do |client, chat_id|
             contacts = connection_store[:opened_chats][client][chat_id][:attendants]
             contacts -= [kick]
 
             if contacts.empty?
88c1de39
                 destroy_multichat(client.jid.strip.to_s, chat_id)
5011e360
             else
                 contacts.each do |contact|
                     client.send(MessageBuilder::send_multichat_contacts(client.jid.strip.to_s, contact, chat_id, contacts))
                 end
 
9948b399
                 sync_contacts_frontend('', client.jid.strip.to_s, chat_id, contacts)
5011e360
             end
         end
     end
 
9948b399
     def sync_contacts_frontend(from, to, chat_id, contacts)
5011e360
         send_message 'app.chat.updateSyncedContacts',
9948b399
                      me: to,
5011e360
                      contacts: contacts,
9948b399
                      owner: from,
5011e360
                      chat_id: chat_id
     end
 
88c1de39
     def destroy_multichat(to, chat_id)
         client = find_client(to)
         connection_store[:opened_chats][client].delete(chat_id)
 
         send_message 'app.chat.destroyMultichat', chat_id: chat_id
     end
 
5011e360
     def xml_contacts_to_array(xml_contacts)
         xml_contacts.get_elements('contact').map do |contact|
             contact.text
         end
     end
2a61cdcc
 end