Ejemplo n.º 1
0
 /**
  * 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])));
 }
Ejemplo n.º 2
0
 /**
  * Index a document in ES (a chat message)
  *
  * @param      Client            $clientFrom  The client to send the message from
  * @param      ClientCollection  $clientsTo   The client(s) to send the message
  * @param      Room              $room        The room
  * @param      string            $message     The text message
  * @param      string            $type        The message type ('public' || 'private')
  * @param      string            $date        The server micro timestamp at the moment the message was sent
  */
 private function indexMessage(Client $clientFrom, ClientCollection $clientsTo, Room $room, string $message, string $type, string $date)
 {
     if ($clientFrom->getConnection()->getRemoteAddress() !== '127.0.0.1') {
         foreach ($clientsTo as $clientTo) {
             $es = EsClientBuilder::create()->build();
             $params = ['index' => $this->esIndex . '_write', 'type' => 'message', 'body' => ['message' => $message, 'type' => $type, 'date' => $date, 'room' => $room->id, 'userFrom' => ['id' => $clientFrom->isRegistered() ? $clientFrom->getUser()->id : -1, 'ip' => $clientFrom->getConnection()->getRemoteAddress(), 'location' => $clientFrom->getLocation(), 'pseudonym' => $room->getClientPseudonym($clientFrom)], 'userTo' => ['id' => $clientTo->isRegistered() ? $clientTo->getUser()->id : -1, 'ip' => $clientTo->getConnection()->getRemoteAddress(), 'location' => $clientTo->getLocation(), 'pseudonym' => $room->getClientPseudonym($clientTo)]]];
             try {
                 $es->index($params);
             } catch (\Exception $e) {
                 $this->logger->log(LogLevel::ERROR, sprintf('[chatService] Document not indexed in ES `%s` %s', $e, static::formatVariable($params)));
             }
         }
     }
 }