2a61cdcc |
class WsChatController < WsController
|
1f685c13 |
def start_polling_messages
connection_store[:clients].each do |client|
client.add_message_callback do |message| |
9948b399 |
from = message.from.strip.to_s
to = message.to.strip.to_s
chat_id = message.attribute('chat_id').to_s |
24b129a9 |
|
9948b399 |
#noinspection RubyAssignmentExpressionInConditionalInspection |
1f685c13 |
if message.body |
9948b399 |
process_incoming_message(from, to, message.body, chat_id) |
24b129a9 |
elsif request = message.first_element('sync_contacts_request')
# toto mozem prijat len ako admin multichatu |
9948b399 |
send_contacts(from, to, request.attribute('chat_id').to_s) |
24b129a9 |
elsif answer = message.first_element('synced_contacts')
# toto mozem prijat len ako ucastnik multichatu (nie admin) |
9948b399 |
contacts = xml_contacts_to_array(answer)
sync_contacts_frontend(from, to, answer.attribute('chat_id').to_s, contacts) |
c6c526f1 |
elsif answer = message.first_element('exported_chat') |
9948b399 |
contacts = xml_contacts_to_array(message.first_element('exported_chat'))
import_people_in_chat(from, to, answer.attribute('chat_id').to_s, contacts) |
aa2c1028 |
elsif message.attribute('destroy_multichat') |
9948b399 |
destroy_multichat(to, chat_id) |
aa2c1028 |
elsif message.attribute('req_update_contacts') |
9948b399 |
added = xml_contacts_to_array(message.first_element('added'))
removed = xml_contacts_to_array(message.first_element('removed'))
update_attendants_in_multichat(from, to, chat_id, removed, added) |
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
|
9948b399 |
def import_people_in_chat(from, to, chat_id, contacts) |
b1c6b359 |
#Rails.logger.debug ['imported chat arrived', message.to_s, chat_id] |
9948b399 |
client = find_client(to) |
0f555f4a |
create_opened_chat(client, chat_id, from, contacts) |
c6c526f1 |
send_message 'app.chat.importChat', |
9948b399 |
owner: from, |
c6c526f1 |
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) |
0f555f4a |
chat_id = hash + Time.now.to_f.to_s |
2a61cdcc |
client = find_client(me)
|
0f555f4a |
create_opened_chat(client, chat_id, me) |
2a61cdcc |
|
0f555f4a |
trigger_success id: chat_id |
2a61cdcc |
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] |
296dd1c6 |
add = message[:jid] |
2a61cdcc |
|
296dd1c6 |
connection_store[:opened_chats][client][chat_id][:attendants] << add |
24b129a9 |
contacts = connection_store[:opened_chats][client][chat_id][:attendants] |
296dd1c6 |
contacts.each do |contact|
client.send(MessageBuilder::export_multichat(client.jid.strip.to_s, contact, chat_id, contacts))
end |
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 |
88c1de39 |
me = client.jid.strip.to_s |
b1c6b359 |
attendants = connection_store[:opened_chats][client][chat_id][:attendants] + [connection_store[:opened_chats][client][chat_id][:owner]] |
88c1de39 |
attendants -= [me] |
24b129a9 |
|
88c1de39 |
messages = MessageBuilder::build_multi_messages(message[:message], me, attendants, chat_id) |
296dd1c6 |
#Rails.logger.debug messages |
24b129a9 |
else |
88c1de39 |
messages = [MessageBuilder::build_message(message[:message], me, message[:to])] |
24b129a9 |
end |
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
|
a0c73ec7 |
def request_sync_chat_contacts |
24b129a9 |
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
|
aa2c1028 |
def i_closed_multichat
chat_id = message[:chatId]
client = find_client(message[:me])
me = client.jid.strip.to_s
owner = connection_store[:opened_chats][client][chat_id][:owner]
if owner == me
attendants = connection_store[:opened_chats][client][chat_id][:attendants]
attendants.each do |attendant| |
88c1de39 |
client.send(MessageBuilder::kick_from_multichat(me, attendant, chat_id)) |
aa2c1028 |
end
else
changes = {removed: [me]}
client.send(MessageBuilder::req_update_multichat_contacts(me, owner, chat_id, changes))
end |
2f61d2f6 |
connection_store[:opened_chats][client].delete(chat_id) |
aa2c1028 |
end
|
2a61cdcc |
private
|
9948b399 |
def process_incoming_message(from, to, body, chat_id = nil) |
296dd1c6 |
#Rails.logger.debug [message, message.to.strip.to_s] |
24b129a9 |
send_message 'app.chat.messageReceived', |
9948b399 |
from: from,
to: to,
message: body,
chat_id: chat_id |
24b129a9 |
end |
31111b63 |
|
9948b399 |
def send_contacts(from, to, chat_id)
client = find_client(to) |
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
|
9948b399 |
def update_attendants_in_multichat(from, to, chat_id, removed, added)
client = find_client(to) |
aa2c1028 |
contacts = connection_store[:opened_chats][client][chat_id][:attendants] |
9948b399 |
contacts -= removed
contacts += added |
aa2c1028 |
connection_store[:opened_chats][client][chat_id][:attendants] = contacts
if contacts.empty? |
9948b399 |
destroy_multichat(to, chat_id) |
aa2c1028 |
else
contacts.each do |contact| |
9948b399 |
client.send(MessageBuilder::send_multichat_contacts(to, contact, chat_id, contacts)) |
aa2c1028 |
end |
a0c73ec7 |
|
9948b399 |
sync_contacts_frontend(from, to, chat_id, contacts) |
aa2c1028 |
end
end |
424fd46f |
end |