/** * Update a user right in a room * * @param array $data JSON decoded client data * @param Client $client The client calling the request * @param RoomCollection $rooms The actives rooms * * @return \Generator * * @todo to test */ private function updateUserRight(array $data, Client $client, RoomCollection $rooms) { $success = false; $message = _('User right updated'); $roomManager = new RoomManager(null, $rooms); $room = null; if (!is_numeric($data['roomId'] ?? null) && !$roomManager->isRoomExist((int) $data['roomId'])) { $message = _('This room does not exist'); } else { $roomManager->loadRoomFromCollection((int) $data['roomId']); $room = $roomManager->getRoom(); if (!$client->isRegistered()) { $message = _('You are not registered so you cannot update the room information'); } elseif (!$roomManager->isClientInTheRoom($client)) { $message = _('You are not logged in this room'); } elseif (!$roomManager->hasGrantRight($client)) { $message = _('You do not have the right to grant a user right in this room'); } elseif ($room->getClients()->getObjectById($data['clientId'] ?? -1) === null) { $message = _('This client does not exists in this room'); } else { try { $success = $roomManager->updateRoomRight($room->getClients()->getObjectById($data['clientId']), $data['rightName'] ?? '', (bool) $data['rightValue'] ?? false); if ($success) { (yield $this->changeUserRight($room, $room->getClients()->getObjectById($data['clientId']))); } } catch (Exception $e) { $message = $e->getMessage(); } } } (yield $client->getConnection()->send(json_encode(['service' => $this->serviceName, 'action' => 'updateUserRight', 'success' => $success, 'text' => $message]))); }
/** * Send a public message to all the users in the room or a private message to one user in the room * * @param array $data JSON decoded client data * @param Client $client The client calling the request * @param RoomCollection $rooms The actives rooms * * @return \Generator * * @todo to test */ private function sendMessage(array $data, Client $client, RoomCollection $rooms) { $success = false; $text = trim($data['message']) ?? ''; $roomManager = new RoomManager(null, $rooms); $receivers = new ClientCollection(); $type = 'public'; $room = null; if (!is_numeric($data['roomId'] ?? null) && !$roomManager->isRoomExist((int) $data['roomId'])) { $message = _('This room does not exist'); } else { $roomManager->loadRoomFromCollection((int) $data['roomId']); $room = $roomManager->getRoom(); if ($text === '') { $message = _('The message cannot be empty'); } elseif (!$roomManager->isClientInTheRoom($client)) { $message = _('You are not logged in this room'); } else { if ($data['receivers'] === 'all') { $receivers = $room->getClients(); } else { foreach ($data['receivers'] ?? [] as $clientId) { $receiver = $room->getClients()->getObjectById($clientId); $type = 'private'; if ($receiver !== null) { $receivers->add($receiver); } } // Self send the message $receivers->add($client); } (yield $this->sendMessageToGroup($client, $receivers, $room, $text, $type)); $message = _('Message successfully sent !'); $success = true; } } (yield $client->getConnection()->send(json_encode(['service' => $this->serviceName, 'action' => 'sendMessage', 'success' => $success, 'text' => $message]))); }