Changeset View
Changeset View
Standalone View
Standalone View
src/app/OpenVidu/Room.php
Show First 20 Lines • Show All 167 Lines • ▼ Show 20 Lines | public function deleteSession(): bool | ||||
return true; | return true; | ||||
} | } | ||||
return false; | return false; | ||||
} | } | ||||
/** | /** | ||||
* Returns metadata for every connection in a session. | |||||
* | |||||
* @return array Connections metadata, indexed by connection identifier | |||||
* @throws \Exception if session does not exist | |||||
*/ | |||||
public function getSessionConnections(): array | |||||
{ | |||||
if (!$this->session_id) { | |||||
throw new \Exception("The room session does not exist"); | |||||
} | |||||
return Connection::where('session_id', $this->session_id) | |||||
// Ignore screen sharing connection for now | |||||
->whereRaw("(role & " . self::ROLE_SCREEN . ") = 0") | |||||
->get() | |||||
->keyBy('id') | |||||
->map(function ($item) { | |||||
// For now we need only 'role' property, it might change in the future. | |||||
// Make sure to not return all metadata here as it might contain sensitive data. | |||||
return ['role' => $item->role]; | |||||
}) | |||||
->all(); | |||||
} | |||||
/** | |||||
* Create a OpenVidu session (connection) token | * Create a OpenVidu session (connection) token | ||||
* | * | ||||
* @param int $role User role (see self::ROLE_* constants) | * @param int $role User role (see self::ROLE_* constants) | ||||
* | * | ||||
* @return array|null Token data on success, NULL otherwise | * @return array|null Token data on success, NULL otherwise | ||||
* @throws \Exception if session does not exist | * @throws \Exception if session does not exist | ||||
*/ | */ | ||||
public function getSessionToken($role = self::ROLE_SUBSCRIBER): ?array | public function getSessionToken($role = self::ROLE_SUBSCRIBER): ?array | ||||
Show All 16 Lines | public function getSessionToken($role = self::ROLE_SUBSCRIBER): ?array | ||||
] | ] | ||||
]; | ]; | ||||
$response = $this->client()->request('POST', $url, $post); | $response = $this->client()->request('POST', $url, $post); | ||||
if ($response->getStatusCode() == 200) { | if ($response->getStatusCode() == 200) { | ||||
$json = json_decode($response->getBody(), true); | $json = json_decode($response->getBody(), true); | ||||
$authToken = base64_encode($json['id'] . ':' . \random_bytes(16)); | |||||
// Extract the 'token' part of the token, it will be used to authenticate the connection. | // Extract the 'token' part of the token, it will be used to authenticate the connection. | ||||
// It will be needed in next iterations e.g. to authenticate moderators that aren't | // It will be needed in next iterations e.g. to authenticate moderators that aren't | ||||
// Kolab4 users (or are just not logged in to Kolab4). | // Kolab4 users (or are just not logged in to Kolab4). | ||||
// FIXME: we could as well generate our own token for auth purposes | // FIXME: we could as well generate our own token for auth purposes | ||||
parse_str(parse_url($json['token'], PHP_URL_QUERY), $url); | parse_str(parse_url($json['token'], PHP_URL_QUERY), $url); | ||||
// Create the connection reference in our database | // Create the connection reference in our database | ||||
$conn = new Connection(); | $conn = new Connection(); | ||||
$conn->id = $json['id']; | $conn->id = $json['id']; | ||||
$conn->session_id = $this->session_id; | $conn->session_id = $this->session_id; | ||||
$conn->room_id = $this->id; | $conn->room_id = $this->id; | ||||
$conn->role = $role; | $conn->role = $role; | ||||
$conn->metadata = ['token' => $url['token']]; | $conn->metadata = ['token' => $url['token'], 'authToken' => $authToken]; | ||||
$conn->save(); | $conn->save(); | ||||
return [ | return [ | ||||
'session' => $this->session_id, | 'session' => $this->session_id, | ||||
'token' => $json['token'], | 'token' => $json['token'], | ||||
'authToken' => $authToken, | |||||
'connectionId' => $json['id'], | 'connectionId' => $json['id'], | ||||
'role' => $role, | 'role' => $role, | ||||
]; | ]; | ||||
} | } | ||||
return null; | return null; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 151 Lines • Show Last 20 Lines |