/** * Ban a client from a room * * @param Client $client The client to ban * @param Client $admin The admin who banned the client * @param string $reason The reason why the client got banned DEFAULT '' * * @return bool True if the client got banned, false otherwise */ public function banClient(Client $client, Client $admin, string $reason = '') { $roomBan = new RoomBan(['idRoom' => $this->room->id, 'ip' => $client->getConnection()->getRemoteAddress(), 'pseudonym' => $this->room->getClientPseudonym($client), 'admin' => $admin->getUser()->id, 'reason' => $reason, 'date' => new \DateTime()]); $roomBanEntityManager = new RoomBanEntityManager($roomBan); $success = $roomBanEntityManager->saveEntity(); if ($success) { $this->room->getClients()->remove($client); } return $success; }
/** * Inform room's clients to add the new one * * @param Room $room The room to update the clients from * @param Client $client The new client ot add * * @return \Generator */ private function addClientInRoom(Room $room, Client $client) { foreach ($room->getClients() as $roomClient) { if ($roomClient !== $client) { (yield $roomClient->getConnection()->send(json_encode(['service' => $this->serviceName, 'action' => 'addClientInRoom', 'roomId' => $room->id, 'client' => $client->__toArray(), 'pseudonym' => $room->getClientPseudonym($client)]))); } } }
/** * 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))); } } } }