File: /var/www/vhost/disk-apps/demo-telemedicina.teky.com.co/app/Http/Controllers/OpenTokController.php
<?php
namespace Telemedicina\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Telemedicina\Http\Controllers\OneSignalController;
use OpenTok\OpenTok;
use OpenTok\Role;
use Telemedicina\User;
use Telemedicina\VideoOpenTok;
use Config;
use Carbon\Carbon;
use Session;
class OpenTokController extends Controller
{
private $opentok;
protected $TIME_SESSION = 10 * 60; // Day
public function __construct(){
$this->opentok = new OpenTok(Config::get('services.opentok.api_key'), Config::get('services.opentok.secret'));
}
// Create a session that attempts to use peer-to-peer streaming:
public function createSessionP2P(Request $request){
if(Auth::user()->id == $request->host_id){
Session::flash('data', array('type' => 'error', 'message' => __('messages.opentok.same_user_error')));
return redirect()->back();
}
if(!$request->guest_id && !$request->document){
Session::flash('data', array('type' => 'error', 'message' => __('messages.opentok.not_valid_user_error')));
return redirect()->back();
}
$request_guest_id = $request->guest_id;
if(!$request_guest_id){
$patient = User::where('document', $request->document)->first();
$request_guest_id = $patient->id;
}else{
$patient = User::where('id', $request_guest_id)->first();
}
$exist = VideoOpenTok::where('host_id', Auth::user()->id)->where('guest_id', $request_guest_id)->first();
if($exist){ // Ya existe la sala.
if($exist->is_host_login && $exist->is_guest_login){
Session::flash('data', array('type' => 'error', 'message' => __('messages.opentok.busy_session')));
return redirect()->back();
}
$startTime = Carbon::parse($exist->created_at);
$current = Carbon::now();
$diff = $startTime->diffInMinutes($current);
if($diff < 55){ // Si esta activa.
$this->sendNotificationUser($request_guest_id);
$token = $this->createTokenFromSessionId($exist->session_id);
return view ('video-chat.video-join')
->with('session_id', $exist->session_id)
->with('token', $token)
->with('api_key', Config::get('services.opentok.api_key'))
->with('patient_name', $patient->name . " " . $patient->last_name);
}else{
$exist->delete();
}
}
$session = $this->opentok->createSession();
// Set some options in a token
$token = $session->generateToken(array(
'role' => Role::PUBLISHER,
'expireTime' => time()+($this->TIME_SESSION),
'data' => 'name=' . Auth::user()->name . Auth::user()->last_name,
'initialLayoutClassList' => array('focus')
));
// Store Session.
$v = new VideoOpenTok;
$v->session_id = $session->getSessionId();
$v->host_id = Auth::user()->id;
$v->guest_id = $request_guest_id;
$v->is_host_login = true;
$v->is_full = false;
$v->active = true;
$v->save();
$patient = User::where('id', $request_guest_id)->first();
$this->sendNotificationUser($request_guest_id);
return view ('video-chat.video-join')
->with('session_id', $v->session_id)
->with('token', $token)
->with('api_key', Config::get('services.opentok.api_key'))
->with('patient_name', $patient->name . " " . $patient->last_name);
}
private function createTokenFromSessionId($sessionId){
return $this->opentok->generateToken(
$sessionId,
array(
'role' => Role::PUBLISHER,
'expireTime' => time()+($this->TIME_SESSION),
'data' => 'name=' . Auth::user()->name . Auth::user()->last_name,
'initialLayoutClassList' => array('focus')
));
}
private function sendNotificationUser($user_id){
$tags = array(
["field" => "tag", "key" => "user_id", "relation" => "=", "value" => $user_id],
["field" => "tag", "key" => "app_name", "relation" => "=", "value" => Config::get('app.name')]
);
$data = array(
"type" => "videocall",
"id" => Auth::user()->id
);
OneSignalController::sendToTags(__('messages.opentok.notification_text'), $tags, null, $data, null, null, 'U');
}
// Create a token from session ID.
public function joinSessionUser(Request $request){
$v = VideoOpenTok::where('session_id', $request->session_id)->first();
if(!$v){
return array('m' => __('messages.opentok.not_valid_session'));
}
$token = $this->opentok->generateToken($v->session_id);
return view ('video-chat.video-join')
->with('session_id', $v->session_id)
->with('token', $token)
->with('api_key', Config::get('services.opentok.api_key'));
}
public function unjoinSessionUser(Request $request){
$v = VideoOpenTok::where('session_id', $request->session_id)
->where('host_id', Auth::user()->id)->first();
if(!$v){
return array('m' => __('messages.opentok.not_valid_session'));
}
$v->delete();
return array('m' => 'true');
}
}