Changeset View
Changeset View
Standalone View
Standalone View
src/app/Meet/Room.php
<?php | <?php | ||||
namespace App\Meet; | namespace App\Meet; | ||||
use App\Traits\BelongsToTenantTrait; | |||||
use App\Traits\EntitleableTrait; | |||||
use App\Traits\Meet\RoomConfigTrait; | |||||
use App\Traits\SettingsTrait; | use App\Traits\SettingsTrait; | ||||
use App\Traits\ShareableTrait; | |||||
use Dyrynda\Database\Support\NullableFields; | |||||
use Illuminate\Database\Eloquent\Model; | use Illuminate\Database\Eloquent\Model; | ||||
use Illuminate\Database\Eloquent\SoftDeletes; | |||||
use Illuminate\Support\Facades\Cache; | use Illuminate\Support\Facades\Cache; | ||||
/** | /** | ||||
* The eloquent definition of a Room. | * The eloquent definition of a Room. | ||||
* | * | ||||
* @property int $id Room identifier | * @property int $id Room identifier | ||||
* @property ?string $description Description | |||||
* @property string $name Room name | * @property string $name Room name | ||||
* @property int $user_id Room owner | * @property int $tenant_id Tenant identifier | ||||
* @property ?string $session_id Meet session identifier | * @property ?string $session_id Meet session identifier | ||||
*/ | */ | ||||
class Room extends Model | class Room extends Model | ||||
{ | { | ||||
use BelongsToTenantTrait; | |||||
use EntitleableTrait; | |||||
use RoomConfigTrait; | |||||
use NullableFields; | |||||
use SettingsTrait; | use SettingsTrait; | ||||
use ShareableTrait; | |||||
use SoftDeletes; | |||||
public const ROLE_SUBSCRIBER = 1 << 0; | public const ROLE_SUBSCRIBER = 1 << 0; | ||||
public const ROLE_PUBLISHER = 1 << 1; | public const ROLE_PUBLISHER = 1 << 1; | ||||
public const ROLE_MODERATOR = 1 << 2; | public const ROLE_MODERATOR = 1 << 2; | ||||
public const ROLE_SCREEN = 1 << 3; | public const ROLE_SCREEN = 1 << 3; | ||||
public const ROLE_OWNER = 1 << 4; | public const ROLE_OWNER = 1 << 4; | ||||
public const REQUEST_ACCEPTED = 'accepted'; | public const REQUEST_ACCEPTED = 'accepted'; | ||||
public const REQUEST_DENIED = 'denied'; | public const REQUEST_DENIED = 'denied'; | ||||
/** @var array<string, string> The attributes that should be cast */ | |||||
protected $casts = [ | |||||
'created_at' => 'datetime:Y-m-d H:i:s', | |||||
'deleted_at' => 'datetime:Y-m-d H:i:s', | |||||
'updated_at' => 'datetime:Y-m-d H:i:s', | |||||
]; | |||||
/** @var array<int, string> The attributes that are mass assignable */ | /** @var array<int, string> The attributes that are mass assignable */ | ||||
protected $fillable = ['user_id', 'name']; | protected $fillable = ['name', 'description']; | ||||
/** @var array<int, string> The attributes that can be not set */ | |||||
protected $nullable = ['description']; | |||||
/** @var string Database table name */ | /** @var string Database table name */ | ||||
protected $table = 'openvidu_rooms'; | protected $table = 'openvidu_rooms'; | ||||
/** @var \GuzzleHttp\Client|null HTTP client instance */ | /** @var \GuzzleHttp\Client|null HTTP client instance */ | ||||
private $client = null; | private $client = null; | ||||
/** | /** | ||||
▲ Show 20 Lines • Show All 136 Lines • ▼ Show 20 Lines | public function hasSession(): bool | ||||
$response = $this->client()->request('GET', "sessions/{$this->session_id}"); | $response = $this->client()->request('GET', "sessions/{$this->session_id}"); | ||||
$this->logError("Failed to check that a meet session exists", $response); | $this->logError("Failed to check that a meet session exists", $response); | ||||
return $response->getStatusCode() == 200; | return $response->getStatusCode() == 200; | ||||
} | } | ||||
/** | /** | ||||
* The room owner. | |||||
* | |||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo | |||||
*/ | |||||
public function owner() | |||||
{ | |||||
return $this->belongsTo('\App\User', 'user_id', 'id'); | |||||
} | |||||
/** | |||||
* Accept the join request. | * Accept the join request. | ||||
* | * | ||||
* @param string $id Request identifier | * @param string $id Request identifier | ||||
* | * | ||||
* @return bool True on success, False on failure | * @return bool True on success, False on failure | ||||
*/ | */ | ||||
public function requestAccept(string $id): bool | public function requestAccept(string $id): bool | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 53 Lines • ▼ Show 20 Lines | public function requestSave(string $id, array $request): bool | ||||
// We don't really need the picture in the cache | // We don't really need the picture in the cache | ||||
// As we use this cache for the request status only | // As we use this cache for the request status only | ||||
unset($request['picture']); | unset($request['picture']); | ||||
return Cache::put($this->session_id . '-' . $id, $request, now()->addHours(1)); | return Cache::put($this->session_id . '-' . $id, $request, now()->addHours(1)); | ||||
} | } | ||||
/** | /** | ||||
* Any (additional) properties of this room. | |||||
* | |||||
* @return \Illuminate\Database\Eloquent\Relations\HasMany | |||||
*/ | |||||
public function settings() | |||||
{ | |||||
return $this->hasMany('App\Meet\RoomSetting', 'room_id'); | |||||
} | |||||
/** | |||||
* Send a signal to the Meet session participants (peers) | * Send a signal to the Meet session participants (peers) | ||||
* | * | ||||
* @param string $name Signal name (type) | * @param string $name Signal name (type) | ||||
* @param array $data Signal data array | * @param array $data Signal data array | ||||
* @param int $target Limit targets by their participant role | * @param int $target Limit targets by their participant role | ||||
* | * | ||||
* @return bool True on success, False on failure | * @return bool True on success, False on failure | ||||
* @throws \Exception if session does not exist | * @throws \Exception if session does not exist | ||||
Show All 14 Lines | public function signal(string $name, array $data = [], $target = null): bool | ||||
$response = $this->client()->request('POST', 'signal', ['json' => $post]); | $response = $this->client()->request('POST', 'signal', ['json' => $post]); | ||||
$this->logError("Failed to send a signal to the meet session", $response); | $this->logError("Failed to send a signal to the meet session", $response); | ||||
return $response->getStatusCode() == 200; | return $response->getStatusCode() == 200; | ||||
} | } | ||||
/** | /** | ||||
* Returns a map of supported ACL labels. | |||||
* | |||||
* @return array Map of supported share rights/ACL labels | |||||
*/ | |||||
protected function supportedACL(): array | |||||
{ | |||||
return [ | |||||
'full' => \App\Share::READ | \App\Share::WRITE | \App\Share::ADMIN, | |||||
]; | |||||
} | |||||
/** | |||||
* Returns room name (required by the EntitleableTrait). | |||||
* | |||||
* @return string|null Room name | |||||
*/ | |||||
public function toString(): ?string | |||||
{ | |||||
return $this->name; | |||||
} | |||||
/** | |||||
* Log an error for a failed request to the meet server | * Log an error for a failed request to the meet server | ||||
* | * | ||||
* @param string $str The error string | * @param string $str The error string | ||||
* @param object $response Guzzle client response | * @param object $response Guzzle client response | ||||
*/ | */ | ||||
private function logError(string $str, $response) | private function logError(string $str, $response) | ||||
{ | { | ||||
$code = $response->getStatusCode(); | $code = $response->getStatusCode(); | ||||
if ($code != 200) { | if ($code != 200) { | ||||
\Log::error("$str [$code]"); | \Log::error("$str [$code]"); | ||||
} | } | ||||
} | } | ||||
} | } |