示例#1
0
 private function leaveRoom(Room $room) : Promise
 {
     assert(!$room->isPermanent(), new InvalidRoomException('Cannot leave a permanent room'));
     $this->storage->setApproved($room->getIdentifier(), false);
     $this->removeScheduledActionsForUnapprovedRoom($room->getIdentifier());
     $body = (new FormBody())->addField('fkey', $room->getSession()->getFKey())->addField('quiet', 'true');
     $request = (new HttpRequest())->setMethod('POST')->setUri($this->urlResolver->getEndpointURL($room, Endpoint::CHATROOM_LEAVE))->setBody($body);
     $this->storage->removeRoom($room->getIdentifier());
     $room->getWebsocketHandler()->getEndpoint()->close();
     return $this->httpClient->request($request);
 }
示例#2
0
 public function postMessage(ChatRoom $room, string $text, int $flags = PostFlags::NONE) : Promise
 {
     return resolve(function () use($room, $text, $flags) {
         // the order of these two conditions is very important! MUST short circuit on $flags or new rooms will block on the welcome message!
         if (!($flags & PostFlags::FORCE) && !(yield $room->isApproved())) {
             throw new RoomNotApprovedException('Bot is not approved for message posting in this room');
         }
         $text = $this->applyPostFlagsToText($text, $flags);
         $body = (new FormBody())->addField("text", $text)->addField("fkey", (string) $room->getSession()->getFKey());
         $url = $this->urlResolver->getEndpointURL($room, ChatRoomEndpoint::CHATROOM_POST_MESSAGE);
         $request = (new HttpRequest())->setUri($url)->setMethod("POST")->setBody($body);
         $action = $this->actionFactory->createPostMessageAction($request, $room, $text);
         $this->actionExecutor->enqueue($action);
         return $action->promise();
     });
 }