class WsChatController < WsController
def start_polling_messages
connection_store[:clients].each do |client|
client.add_message_callback do |message|
if message.body
process_incoming_message(message)
elsif request = message.first_element('sync_contacts_request')
send_contacts(message, request.attribute('chat_id').to_s)
elsif answer = message.first_element('synced_contacts')
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)
elsif message.attribute('destroy_multichat')
destroy_multichat(message, message.attribute('chat_id').to_s)
elsif message.attribute('req_update_contacts')
update_attendants_in_multichat(message, message.attribute('chat_id').to_s)
end
end
end
end
def import_people_in_chat(message, chat_id)
client = find_client(message.to.strip.to_s)
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]
connection_store[:opened_chats][client][chat_id] = {attendants: contacts, owner: message.from.strip.to_s}
send_message 'app.chat.importChat',
owner: message.from.strip.to_s,
chat_id: chat_id,
contacts: contacts
end
def new_multichat
me = message[:chatOwner]
hash = Digest::SHA2.hexdigest(me)
id = hash + Time.now.to_f.to_s
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] = {attendants: [], owner: me}
trigger_success id: id
end
def add_to_multichat
client = find_client(message[:chatOwner])
chat_id = message[:chatId]
add = message[:jid]
connection_store[:opened_chats][client][chat_id][:attendants] << add
contacts = connection_store[:opened_chats][client][chat_id][:attendants]
contacts.each do |contact|
client.send(MessageBuilder::export_multichat(client.jid.strip.to_s, contact, chat_id, contacts))
end
trigger_success
end
def send_chat_message
me = message[:from]
client = find_client(me)
if client
chat_id = message[:chatId]
if chat_id
attendants = connection_store[:opened_chats][client][chat_id][:attendants] + [connection_store[:opened_chats][client][chat_id][:owner]]
attendants -= [client.jid.strip.to_s]
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
messages.each do |message|