diff --git a/Gemfile b/Gemfile index ffa11e3..9ff157a 100644 --- a/Gemfile +++ b/Gemfile @@ -1,6 +1,7 @@ source 'https://rubygems.org' ruby '2.4.4' +gem 'google-cloud-translate' gem 'bootsnap', require: false gem 'devise' gem 'jbuilder', '~> 2.0' diff --git a/Gemfile.lock b/Gemfile.lock index ec82a00..9244307 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -42,6 +42,8 @@ GEM i18n (>= 0.7, < 2) minitest (~> 5.1) tzinfo (~> 1.1) + addressable (2.5.2) + public_suffix (>= 2.0.2, < 4.0) arel (9.0.0) autoprefixer-rails (9.1.3) execjs @@ -79,11 +81,28 @@ GEM railties (>= 3.2, < 6.0) erubi (1.7.1) execjs (2.7.0) + faraday (0.15.2) + multipart-post (>= 1.2, < 3) ffi (1.9.25) font-awesome-sass (5.0.13) sassc (>= 1.11) globalid (0.4.1) activesupport (>= 4.2.0) + google-cloud-core (1.2.3) + google-cloud-env (~> 1.0) + google-cloud-env (1.0.2) + faraday (~> 0.11) + google-cloud-translate (1.2.1) + faraday (~> 0.13) + google-cloud-core (~> 1.2) + googleauth (~> 0.6.2) + googleauth (0.6.6) + faraday (~> 0.12) + jwt (>= 1.4, < 3.0) + memoist (~> 0.12) + multi_json (~> 1.11) + os (>= 0.9, < 2.0) + signet (~> 0.7) http-cookie (1.0.3) domain_name (~> 0.5) i18n (1.1.0) @@ -91,6 +110,7 @@ GEM jbuilder (2.7.0) activesupport (>= 4.2.0) multi_json (>= 1.2) + jwt (2.1.0) listen (3.0.8) rb-fsevent (~> 0.9, >= 0.9.4) rb-inotify (~> 0.9, >= 0.9.7) @@ -101,6 +121,7 @@ GEM mini_mime (>= 0.1.1) marcel (0.3.2) mimemagic (~> 0.3.2) + memoist (0.16.0) method_source (0.9.0) mime-types (3.2.2) mime-types-data (~> 3.2015) @@ -111,11 +132,13 @@ GEM minitest (5.11.3) msgpack (1.2.4) multi_json (1.13.1) + multipart-post (2.0.0) netrc (0.11.0) nio4r (2.3.1) nokogiri (1.8.4) mini_portile2 (~> 2.3.0) orm_adapter (0.5.0) + os (1.0.0) pg (0.21.0) pry (0.11.3) coderay (~> 1.1.0) @@ -125,6 +148,7 @@ GEM pry (~> 0.10) pry-rails (0.3.6) pry (>= 0.10.4) + public_suffix (3.0.3) puma (3.12.0) rack (2.0.5) rack-proxy (0.6.4) @@ -181,6 +205,11 @@ GEM sassc (1.12.1) ffi (~> 1.9.6) sass (>= 3.3.0) + signet (0.9.1) + addressable (~> 2.3) + faraday (~> 0.9) + jwt (>= 1.5, < 3.0) + multi_json (~> 1.10) simple_form (4.0.1) actionpack (>= 5.0) activemodel (>= 5.0) @@ -233,6 +262,7 @@ DEPENDENCIES devise dotenv-rails font-awesome-sass (~> 5.0.9) + google-cloud-translate jbuilder (~> 2.0) listen (~> 3.0.5) pg (~> 0.21) diff --git a/app/controllers/chat_rooms_controller.rb b/app/controllers/chat_rooms_controller.rb index 57f3ec2..ab0736a 100644 --- a/app/controllers/chat_rooms_controller.rb +++ b/app/controllers/chat_rooms_controller.rb @@ -1,7 +1,19 @@ class ChatRoomsController < ApplicationController def show + require "google/cloud/translate" + @chat_room = ChatRoom.find(params[:id]) + + keyfile = ENV['TRANSLATION_CREDENTIALS'] + creds = Google::Cloud::Translate::Credentials.new(keyfile) + + translate = Google::Cloud::Translate.new( + project_id: ENV["PROJECT_ID"], + credentials: creds + ) + + @languages = translate.languages("en") end def create diff --git a/app/controllers/pages_controller.rb b/app/controllers/pages_controller.rb index 3f14af6..e53db5a 100644 --- a/app/controllers/pages_controller.rb +++ b/app/controllers/pages_controller.rb @@ -12,11 +12,68 @@ class PagesController < ApplicationController def home end + def translate + require 'google/cloud/translate' + + keyfile = ENV["TRANSLATION_CREDENTIALS"] + creds = Google::Cloud::Translate::Credentials.new(keyfile) + + translate = Google::Cloud::Translate.new( + project_id: ENV["PROJECT_ID"], + credentials: creds + ) + original = params[:original] + target = params[:target] + text = params[:text] + translation = translate.translate(text, { from: original, to: target }) + translation.text.gsub!("'", "'") + ActionCable.server.broadcast "chat_room_#{params[:chat_room_id]}", { + translation: translation, + input: params[:input], + userId: current_user.id + } + end + def cable_testing chatroom = 'chat_room_' + params[:chat_room_id] puts params - ActionCable.server.broadcast(chatroom, { message: 'test' }) + user_info = {} + + if current_user.first_name.nil? || current_user.last_name.nil? + user_info[:name] = current_user.email + else + user_info[:name] = "#{current_user.first_name} #{current_user.last_name}" + end + + ActionCable.server.broadcast(chatroom, { + chat_message: { + message: 'test', + user_info: user_info, + time_stamp: Time.now } + }) + head :ok + end + + + def send_message + puts params + chatroom = 'chat_room_' + params[:chat_room_id] + puts params + user_info = {} + + if current_user.first_name.nil? || current_user.last_name.nil? + user_info[:name] = current_user.email + else + user_info[:name] = "#{current_user.first_name} #{current_user.last_name}" + end + + ActionCable.server.broadcast(chatroom, { + chat_message: { + message: params[:message], + user_info: user_info, + time_stamp: Time.now.strftime("%H:%M") } + }) head :ok end diff --git a/app/javascript/packs/chatrooms.js b/app/javascript/packs/chatrooms.js index 787598c..85572b4 100644 --- a/app/javascript/packs/chatrooms.js +++ b/app/javascript/packs/chatrooms.js @@ -10,22 +10,37 @@ import ActionCable from 'actioncable' // find chatroom id const chatroomId = document.getElementById('chatroom-hook').dataset["chatroomId"] +const userId = document.getElementById('current-user').innerText // create subsciptions App['chatroom' + chatroomId] = App.cable.subscriptions.create({ - channel: 'ChatRoomsChannel', - room: chatroomId -}, { - connected: () => { - }, - received: data => { - if (data.hangUp) { + channel: 'ChatRoomsChannel', + room: chatroomId + }, { + connected: () => {}, + received: data => { + if (data["chat_message"]) { + const chatMessage = data["chat_message"] + const messagesContainer = document.getElementById('messages-container') + const message = document.createElement("p") + message.innerText = `${chatMessage["time_stamp"]} ${chatMessage["user_info"]["name"]}: ${chatMessage["message"]}` + messagesContainer.appendChild(message) + } else if (data["translation"] && data["userId"] == userId) { + if (data["input"] == 1) { + document.getElementById('language-2-input').value = data["translation"].text + } else { + document.getElementById('language-1-input').value = data["translation"].text + } + } else { + // console.log(data) + } + if (data.hangUp) { + document.location.pathname = '/contacts' + } + }, + disconnected: () => { document.location.pathname = '/contacts' } - }, - disconnected: () => { - document.location.pathname = '/contacts' - } }) const hangUpIcon = document.querySelector('.fa-hand-paper') @@ -40,13 +55,27 @@ hangUpIcon.addEventListener('click', event => { }) // Testing ActionCable -const testBtn = document.getElementById('test-btn') -testBtn.addEventListener('click', event => { - fetch(`/chat_rooms/${chatroomId}/cable_testing` , { - method: 'POST', - body: JSON.stringify({}) - }) +// const testBtn = document.getElementById('test-btn') +// testBtn.addEventListener('click', event => { +// fetch(`/chat_rooms/${chatroomId}/cable_testing` , { +// method: 'POST', +// body: JSON.stringify({}) +// }) +// }) + + +const sendBtn = document.getElementById('send-btn') +sendBtn.addEventListener('click', event => { + const chatInput = document.getElementById('chat-input') + if (chatInput && chatInput.value != chatInput.value.match(/^\s*$/g)) { + console.log(chatInput.value) + fetch(`/chat_rooms/${chatroomId}/send_message` , { + method: 'POST', + body: JSON.stringify({message: chatInput.value}), + headers: { "content-type": "application/json", "X-CSRF-Token": document.querySelector('meta[name=csrf-token]').content } + }) + } + chatInput.value = "" }) - diff --git a/app/javascript/packs/translate.js b/app/javascript/packs/translate.js new file mode 100644 index 0000000..33e09ec --- /dev/null +++ b/app/javascript/packs/translate.js @@ -0,0 +1,51 @@ +const userId = document.getElementById('current-user').innerText +const languageForm1 = document.getElementById('lang-1') +const languageForm2 = document.getElementById('lang-2') +const chatroomId = document.getElementById('chatroom-hook').dataset["chatroomId"] + +languageForm1.addEventListener('submit', event => { + event.preventDefault() + const original = document.getElementById('language-1').value + const target = document.getElementById('language-2').value + const text = document.getElementById('language-1-input').value + + if (original !== target) { + fetch(`/chat_rooms/${chatroomId}/translate` , { + method: 'POST', + body: JSON.stringify({ + original, + target, + text, + input: 1, + userId + }), + headers: { "content-type": "application/json", "X-CSRF-Token": document.querySelector('meta[name=csrf-token]').content } + }) + } else { + document.getElementById('language-2-input').value = text + } + // post request and change form 1 +}) + +languageForm2.addEventListener('submit', event => { + event.preventDefault() + const original = document.getElementById('language-2').value + const target = document.getElementById('language-1').value + const text = document.getElementById('language-2-input').value + // post request and change form 2 + if (original !== target) { + fetch(`/chat_rooms/${chatroomId}/translate` , { + method: 'POST', + body: JSON.stringify({ + original: original, + target: target, + text: text, + input: 2, + userId + }), + headers: { "content-type": "application/json", "X-CSRF-Token": document.querySelector('meta[name=csrf-token]').content } + }) + } else { + document.getElementById('language-1-input').value = text + } +}) diff --git a/app/views/chat_rooms/show.html.erb b/app/views/chat_rooms/show.html.erb index b10234e..abb0525 100644 --- a/app/views/chat_rooms/show.html.erb +++ b/app/views/chat_rooms/show.html.erb @@ -1,4 +1,24 @@ - + + +