polyglot/app/controllers/pages_controller.rb

182 lines
5.4 KiB
Ruby
Raw Normal View History

class PagesController < ApplicationController
2018-08-28 13:46:15 +00:00
# skip_before_action :authenticate_user!, only: [:call]
skip_before_action :verify_authenticity_token
2018-08-28 12:48:32 +00:00
def call
end
2018-08-28 16:18:58 +00:00
def index
2018-08-31 14:06:42 +00:00
@contact = Connection.new
2018-08-28 16:18:58 +00:00
end
2018-08-28 16:21:12 +00:00
2018-08-28 14:00:54 +00:00
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!("&#39;", "'")
ActionCable.server.broadcast "chat_room_#{params[:chat_room_id]}", {
translation: translation,
input: params[:input],
userId: current_user.id
}
end
2018-08-31 14:06:42 +00:00
def cable_testing
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: 'test',
user_info: user_info,
2018-09-05 17:17:13 +00:00
time_stamp: Time.now,
photo_url: photo_url
}
})
head :ok
end
2018-09-05 13:59:39 +00:00
def translate_message
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
)
target = params[:target]
message = params[:message]
userId = params[:userId]
detection = translate.detect(message)
original = detection.language
translated_message = message
unless original == target
translation = translate.translate(message, { from: original, to: target })
translated_message = translation.text.gsub("&#39;", "'")
end
ActionCable.server.broadcast "chat_room_#{params[:chat_room_id]}", {
translated_message: translated_message,
userId: userId,
photo_url: params[:photo_url]
2018-09-05 13:59:39 +00:00
}
end
def send_message
puts params
chatroom = 'chat_room_' + params[:chat_room_id]
puts params
user_info = {}
photo_url = current_user.photo.url || "https://upload.wikimedia.org/wikipedia/commons/8/89/Portrait_Placeholder.png"
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,
2018-09-05 13:59:39 +00:00
time_stamp: Time.now.strftime("%H:%M"),
userId: current_user.id,
photo_url: photo_url
2018-09-05 13:59:39 +00:00
}
})
head :ok
end
def establish_call
head :ok
puts "params: #{params}"
chat_room = ChatRoom.create!
puts "Created chat room with id: #{chat_room.id}"
chat_room_participation = ChatRoomParticipation.create!(chat_room: chat_room, user: current_user)
puts "Created chat room participation with user: #{current_user.email} assigned to chat_room #{chat_room.id}"
puts "Subscribed user to chat room"
contact = User.find(params[:contact_id])
request = Request.create!(chat_room: chat_room, user: contact)
puts "Made a request to call #{contact.email}"
2018-09-03 17:32:03 +00:00
caller_info = nil
if current_user.first_name.nil? || current_user.last_name.nil?
caller_info = current_user.email
else
caller_info = "#{current_user.first_name} #{current_user.last_name}"
end
caller_photo = "https://upload.wikimedia.org/wikipedia/commons/8/89/Portrait_Placeholder.png"
unless current_user.photo.url.nil?
caller_photo = current_user.photo.url
end
ActionCable.server.broadcast('notifications', {
message: {
user_id: contact.id,
2018-09-03 17:32:03 +00:00
chat_room_id: chat_room.id,
caller_info: caller_info,
caller_photo: caller_photo
}
})
end
def accept_call
puts "-----------------------------------------"
puts params
puts "IT WORKED"
chat_room = ChatRoom.find(params[:chat_room_id])
request = Request.where("user_id = ? AND chat_room_id = ?", current_user.id, chat_room.id)
request[0].accepted = true
puts "create new chat room participation"
chat_room_participation = ChatRoomParticipation.create!(chat_room: chat_room, user: current_user)
puts "Created chat room participation with user: #{current_user.email} assigned to chat_room #{chat_room.id}"
other_caller = chat_room.users.find { |u| u != current_user } # remember to update this later
puts ">>>>>>>>>>>>>>>>>>>>>>>>>>>>..HHHHHHHHH"
puts other_caller
puts ">>>>>>>>>>>>>>>>>>>>>>>>>>>>..HHHHHHHHH"
# redirect caller to chat room
ActionCable.server.broadcast('notifications', {
head: 302, # redirection code, just to make it clear what you're doing
path: chat_room_path(chat_room), # you'll need to use url_helpers, so include them in your file
body: { caller: other_caller.id }
}
# other_caller, # or however you identify your subscriber
)
# redirect callee to chat room
redirect_to chat_room_path(chat_room)
# broadcast another message to caller
# head: 302
end
end