app/controllers/ws_chat_controller.rb
2a61cdcc
 class WsChatController < WsController
 
1f685c13
     def start_polling_messages
         connection_store[:clients].each do |client|
             client.add_message_callback do |message|
24b129a9
                 #noinspection RubyAssignmentExpressionInConditionalInspection
 
1f685c13
                 if message.body
24b129a9
                     process_incoming_message(message)
                 elsif request = message.first_element('sync_contacts_request')
                     # toto mozem prijat len ako admin multichatu
c6c526f1
                     send_contacts(message, request.attribute('chat_id').to_s)
24b129a9
                 elsif answer = message.first_element('synced_contacts')
                     # toto mozem prijat len ako ucastnik multichatu (nie admin)
c6c526f1
                     sync_contacts_frontend(message, answer.attribute('chat_id').to_s)
                 elsif answer = message.first_element('exported_chat')
                     import_people_in_chat(message, answer.attribute('chat_id').to_s)
1f685c13
                 end
                 #TODO: upozornit na pisanie spravy
                 #TODO: odoslat informaciu o tom, ze pisem spravu
                 #else
                 #    send_message 'app.chat.messageState',
                 #                 state: message.chat_state,
                 #                 from: message.from.strip.to_s,
                 #                 to: message.to.strip.to_s,
                 #                 message: message.body,
                 #                 chat_id: if message.attribute(:is_simulating) then message.attribute(:chat_id) end
             end
         end
     end
 
c6c526f1
     def import_people_in_chat(message, chat_id)
b1c6b359
         #Rails.logger.debug ['imported chat arrived', message.to_s, chat_id]
c6c526f1
 
b1c6b359
         client = find_client(message.to.strip.to_s)
24b129a9
         contacts = xml_contacts_to_array(message.first_element('exported_chat'))
 
         connection_store[:opened_chats] = {} if ! connection_store[:opened_chats]
         connection_store[:opened_chats][client] = {} if ! connection_store[:opened_chats][client]
c6c526f1
         connection_store[:opened_chats][client][chat_id] = {attendants: contacts, owner: message.from.node + '@' + message.from.domain}
 
b1c6b359
         #Rails.logger.debug [
         #    owner: message.from.node + '@' + message.from.domain,
         #    chat_id:  chat_id,
         #    contacts: contacts
         #]
c6c526f1
 
         send_message 'app.chat.importChat',
                      owner:    message.from.node + '@' + message.from.domain,
                      chat_id:  chat_id,
                      contacts: contacts
24b129a9
     end
 
94bd8368
     # Owner vytvori najprv u seba novy multichat
24b129a9
     def new_multichat
2a61cdcc
         me = message[:chatOwner]
94bd8368
         hash = Digest::SHA2.hexdigest(me)
         id = hash + Time.now.to_f.to_s
2a61cdcc
         client = find_client(me)
 
         connection_store[:opened_chats] = {} if ! connection_store[:opened_chats]
         connection_store[:opened_chats][client] = {} if ! connection_store[:opened_chats][client]
94bd8368
         connection_store[:opened_chats][client][id] = {attendants: [], owner: me}
2a61cdcc
 
         trigger_success id: id
     end
 
24b129a9
     # Owner posle novemu cloveku informaciu o chat_id a kontaktoch
4e59a143
     def add_to_multichat
2a61cdcc
         client = find_client(message[:chatOwner])
 
24b129a9
         chat_id  = message[:chatId]
2a61cdcc
         somebody = message[:jid]
 
24b129a9
         connection_store[:opened_chats][client][chat_id][:attendants] << somebody
 
         contacts = connection_store[:opened_chats][client][chat_id][:attendants]
         client.send(MessageBuilder::export_multichat(client.jid.strip.to_s, somebody, chat_id, contacts))
94bd8368
 
         trigger_success
2a61cdcc
     end
424fd46f
 
     def send_chat_message
2a61cdcc
         me = message[:from]
         client = find_client(me)
 
00f00dbe
         if client
24b129a9
             chat_id = message[:chatId]
             if chat_id
b1c6b359
                 attendants = connection_store[:opened_chats][client][chat_id][:attendants] + [connection_store[:opened_chats][client][chat_id][:owner]]
                 attendants -= [client.jid.strip.to_s]
24b129a9
 
                 messages = MessageBuilder::build_multi_messages(message[:message], client.jid.strip.to_s, attendants, chat_id)
             else
                 messages = [MessageBuilder::build_message(message[:message], client.jid.strip.to_s, message[:to])]
             end
2a61cdcc
 
             # Xmpp4r doesn't support XEP-0033 (multicast messages)
             messages.each do |message|
b1c6b359
                 #Rails.logger.debug message.to_s
2a61cdcc
                 client.send(message)
             end
 
00f00dbe
             trigger_success message[:message]
         else
             trigger_failure
2a61cdcc
         end
     end
 
24b129a9
     def sync_chat_contacts
         client = find_client(message[:me])
         chat_id = message[:chatId]
 
         owner = connection_store[:opened_chats][client][chat_id][:owner]
         client.send(MessageBuilder::ask_for_multichat_contacts(client.jid.strip.to_s, owner, chat_id))
     end
 
2a61cdcc
     private
 
24b129a9
     def process_incoming_message(message)
b1c6b359
         #Rails.logger.debug message
24b129a9
         send_message 'app.chat.messageReceived',
                      from: message.from.strip.to_s,
                      to: message.to.strip.to_s,
                      message: message.body,
b1c6b359
                      chat_id: message.attribute('chat_id').to_s
24b129a9
     end
31111b63
 
24b129a9
     def send_contacts(message, chat_id)
b1c6b359
         from = message.from.strip.to_s
         client = find_client(message.to.strip.to_s)
24b129a9
         contacts = connection_store[:opened_chats][client][chat_id][:attendants]
         client.send(MessageBuilder::send_multichat_contacts(client.jid.strip.to_s, from, chat_id, contacts))
     end
 
     def sync_contacts_frontend(message, chat_id)
         contacts = xml_contacts_to_array(message.first_element('sync_contacts'))
 
         send_message 'app.chat.updateSyncedContacts',
                      me: message.to.strip.to_s,
                      contacts: contacts,
                      chat_id: chat_id
     end
2a61cdcc
 
24b129a9
     def xml_contacts_to_array(xml_contacts)
         xml_contacts.get_elements('contact').map do |contact|
             contact.text
2a61cdcc
         end
424fd46f
     end
 end