Changeset View
Changeset View
Standalone View
Standalone View
src/app/Observers/OpenVidu/ConnectionObserver.php
Show All 28 Lines | public function updated(Connection $connection) | ||||
// Detect metadata changes for specified properties | // Detect metadata changes for specified properties | ||||
$keys = [ | $keys = [ | ||||
'hand' => 'bool', | 'hand' => 'bool', | ||||
'language' => '', | 'language' => '', | ||||
]; | ]; | ||||
foreach ($keys as $key => $type) { | foreach ($keys as $key => $type) { | ||||
$newState = $connection->metadata[$key] ?? null; | $newState = $connection->metadata[$key] ?? null; | ||||
$oldState = $this->getOriginal($connection, 'metadata')[$key] ?? null; | $oldState = $connection->getOriginal('metadata')[$key] ?? null; | ||||
if ($newState !== $oldState) { | if ($newState !== $oldState) { | ||||
$params[$key] = $type == 'bool' ? !empty($newState) : $newState; | $params[$key] = $type == 'bool' ? !empty($newState) : $newState; | ||||
} | } | ||||
} | } | ||||
// Send the signal to all participants | // Send the signal to all participants | ||||
if (!empty($params)) { | if (!empty($params)) { | ||||
$params['connectionId'] = $connection->id; | $params['connectionId'] = $connection->id; | ||||
$connection->room->signal('connectionUpdate', $params); | $connection->room->signal('connectionUpdate', $params); | ||||
} | } | ||||
} | } | ||||
/** | |||||
* A wrapper to getOriginal() on an object | |||||
* | |||||
* @param \App\OpenVidu\Connection $connection The connection. | |||||
* @param string $property The property name | |||||
* | |||||
* @return mixed | |||||
*/ | |||||
private function getOriginal($connection, $property) | |||||
{ | |||||
$original = $connection->getOriginal($property); | |||||
// The original value for a property is in a format stored in database | |||||
// I.e. for 'metadata' it is a JSON string instead of an array | |||||
if ($property == 'metadata') { | |||||
$original = json_decode($original, true); | |||||
} | |||||
return $original; | |||||
} | |||||
} | } |