diff --git a/src/app/OpenVidu/Room.php b/src/app/OpenVidu/Room.php index b2cd33b4..cc1c11cb 100644 --- a/src/app/OpenVidu/Room.php +++ b/src/app/OpenVidu/Room.php @@ -1,99 +1,109 @@ belongsTo('\App\User', 'user_id', 'id'); } private function client() { if (!self::$client) { self::$client = new \GuzzleHttp\Client( [ 'http_errors' => false, // No exceptions from Guzzle 'base_uri' => \config('openvidu.api_url'), 'verify' => \config('openvidu.api_verify_tls'), 'auth' => [ \config('openvidu.api_username'), \config('openvidu.api_password') ] ] ); } return self::$client; } public function createSession() { $response = $this->client()->request( 'POST', "sessions", [ 'json' => [ 'mediaMode' => 'ROUTED', 'recordingMode' => 'MANUAL' ] ] ); if ($response->getStatusCode() !== 200) { $this->session_id = null; $this->save(); } $session = json_decode($response->getBody(), true); $this->session_id = $session['id']; $this->save(); return $session; } public function getSessionToken($role = 'PUBLISHER') { $response = $this->client()->request( 'POST', 'tokens', [ 'json' => [ 'session' => $this->session_id, 'role' => $role ] ] ); $json = json_decode($response->getBody(), true); return $json; } public function hasSession() { if (!$this->session_id) { return false; } $response = $this->client()->request('GET', "sessions/{$this->session_id}"); return $response->getStatusCode() == 200; } + + /** + * Any (additional) properties of this room. + * + * @return \Illuminate\Database\Eloquent\Relations\HasMany + */ + public function settings() + { + return $this->hasMany('App\OpenVidu\RoomSetting', 'room_id'); + } } diff --git a/src/app/Traits/OpenVidu/RoomSettingsTrait.php b/src/app/Traits/OpenVidu/RoomSettingsTrait.php deleted file mode 100644 index 0b2805b4..00000000 --- a/src/app/Traits/OpenVidu/RoomSettingsTrait.php +++ /dev/null @@ -1,121 +0,0 @@ - 'some-legible-name']); - * $locked = $room->getSetting('locked'); - * ``` - * - * @param string $key Lookup key - * - * @return string|null - */ - public function getSetting(string $key) - { - $settings = $this->getCache(); - - if (!array_key_exists($key, $settings)) { - return null; - } - - $value = $settings[$key]; - - return empty($value) ? null : $value; - } - - /** - * Create or update a setting. - * - * Example Usage: - * - * ```php - * $room = Room::firstOrCreate(['id' => 'some-legible-name']); - * $room->setSetting('locked', true); - * ``` - * - * @param string $key Setting name - * @param string|null $value The new value for the setting. - * - * @return void - */ - public function setSetting(string $key, $value) - { - $this->storeSetting($key, $value); - $this->setCache(); - } - - /** - * Create or update multiple settings in one fell swoop. - * - * Example Usage: - * - * ```php - * $room = Room::firstOrCreate(['id' => 'some-legible-name']); - * $room->setSettings(['locked' => true]); - * ``` - * - * @param array $data An associative array of key value pairs. - * - * @return void - */ - public function setSettings(array $data = []) - { - foreach ($data as $key => $value) { - $this->storeSetting($key, $value); - } - - $this->setCache(); - } - - private function storeSetting(string $key, $value): void - { - if ($value === null || $value === '') { - if ($setting = RoomSetting::where(['room_id' => $this->id, 'key' => $key])->first()) { - $setting->delete(); - } - } else { - RoomSetting::updateOrCreate( - ['room_id' => $this->id, 'key' => $key], - ['value' => $value] - ); - } - } - - private function getCache() - { - if (Cache::has('room_settings_' . $this->id)) { - return Cache::get('room_settings_' . $this->id); - } - - return $this->setCache(); - } - - private function setCache() - { - if (Cache::has('room_settings_' . $this->id)) { - Cache::forget('room_settings_' . $this->id); - } - - $cached = []; - foreach ($this->settings()->get() as $entry) { - if ($entry->value !== null && $entry->value !== '') { - $cached[$entry->key] = $entry->value; - } - } - - Cache::forever('room_settings_' . $this->id, $cached); - - return $this->getCache(); - } -} diff --git a/src/app/VerificationCode.php b/src/app/VerificationCode.php index 0aa0284c..89c90d5f 100644 --- a/src/app/VerificationCode.php +++ b/src/app/VerificationCode.php @@ -1,65 +1,56 @@ belongsTo('\App\User', 'user_id', 'id'); } /** * Generate a short code (for human). * * @return string */ public static function generateShortCode(): string { $code_length = env('VERIFICATION_CODE_LENGTH', self::SHORTCODE_LENGTH); - $code_chars = env('VERIFICATION_CODE_CHARS', self::SHORTCODE_CHARS); - $random = []; - for ($i = 1; $i <= $code_length; $i++) { - $random[] = $code_chars[rand(0, strlen($code_chars) - 1)]; - } - - shuffle($random); - - return implode('', $random); + return \App\Utils::randStr($code_length); } } diff --git a/src/database/seeds/DatabaseSeeder.php b/src/database/seeds/DatabaseSeeder.php index 995b0793..306a95ab 100644 --- a/src/database/seeds/DatabaseSeeder.php +++ b/src/database/seeds/DatabaseSeeder.php @@ -1,38 +1,38 @@ $name) { $class = "Database\\Seeds\\$env\\$name"; $seeders[$idx] = class_exists($class) ? $class : null; } $seeders = array_filter($seeders); $this->call($seeders); } } diff --git a/src/database/seeds/OpenViduRoomSeeder.php b/src/database/seeds/OpenViduRoomSeeder.php deleted file mode 100644 index f4eef6d2..00000000 --- a/src/database/seeds/OpenViduRoomSeeder.php +++ /dev/null @@ -1,33 +0,0 @@ -first(); - $jack = \App\User::where('email', 'jack@kolab.org')->first(); - - \App\OpenVidu\Room::create( - [ - 'user_id' => $john->id, - 'name' => 'john' - ] - ); - - \App\OpenVidu\Room::create( - [ - 'user_id' => $jack->id, - 'name' => strtolower(\App\Utils::randStr(3, 3, '-')) - ] - ); - } -} diff --git a/src/tests/Unit/SignupCodeTest.php b/src/tests/Unit/SignupCodeTest.php index ed2ed2fb..6ce97526 100644 --- a/src/tests/Unit/SignupCodeTest.php +++ b/src/tests/Unit/SignupCodeTest.php @@ -1,23 +1,23 @@ assertTrue(is_string($code)); $this->assertTrue(strlen($code) === env('SIGNUP_CODE_LENGTH', SignupCode::SHORTCODE_LENGTH)); - $this->assertTrue(strspn($code, env('SIGNUP_CODE_CHARS', SignupCode::SHORTCODE_CHARS)) === strlen($code)); + $this->assertTrue(strspn($code, env('SIGNUP_CODE_CHARS', \App\Utils::CHARS)) === strlen($code)); } } diff --git a/src/tests/Unit/VerificationCodeTest.php b/src/tests/Unit/VerificationCodeTest.php index a1902dd6..515c16b3 100644 --- a/src/tests/Unit/VerificationCodeTest.php +++ b/src/tests/Unit/VerificationCodeTest.php @@ -1,26 +1,25 @@ assertTrue(is_string($code)); $this->assertTrue(strlen($code) === $code_length); - $this->assertTrue(strspn($code, $code_chars) === strlen($code)); + $this->assertTrue(strspn($code, \App\Utils::CHARS) === strlen($code)); } }