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|
                 if message.body
                     send_message 'app.chat.messageReceived',
                                  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
                 #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
 
31111b63
     def open_multichat
2a61cdcc
         id = Time.now.to_f
         me = message[:chatOwner]
         client = find_client(me)
 
         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][id] = []
 
         trigger_success id: id
     end
 
     def add_to_chat
         client = find_client(message[:chatOwner])
 
         chat_id = message[:chatId]
         somebody = message[:jid]
 
         connection_store[:opened_chats][client][chat_id] << somebody
     end
424fd46f
 
     def send_chat_message
2a61cdcc
         me = message[:from]
         client = find_client(me)
 
00f00dbe
         if client
31111b63
             messages = build_messages(message[:message], client, message[:chatId], message[:to])
2a61cdcc
 
             # Xmpp4r doesn't support XEP-0033 (multicast messages)
             messages.each do |message|
                 client.send(message)
             end
 
00f00dbe
             trigger_success message[:message]
         else
             trigger_failure
2a61cdcc
         end
     end
 
     private
 
31111b63
     def build_messages(message, client_from, chat_id, jid_to)
2a61cdcc
         from = client_from.jid.to_s
31111b63
 
         attendants = chat_id ? connection_store[:opened_chats][client_from][chat_id] : [jid_to]
2a61cdcc
 
         attendants.map do |person|
             message = Jabber::Message.new(person, message)
             message.from = from
             message.add_attribute('chat_id', chat_id)
             message.add_attribute('is_simulating', true)
             message
         end
424fd46f
     end
 end