getTitle(); } /** * Returns whether the given user can see at least * one chat room. If no user is given the current user * should be assumed */ public static function canSeeAny(?UserProfile $user = null): bool { $rooms = RoomCache::getInstance()->getRooms(); foreach ($rooms as $room) { if ($room->canSee($user)) { return true; } } return false; } /** * Returns whether the given user can see this room. * If no user is given the current user should be assumed. */ public function canSee(?UserProfile $user = null, ?\Exception &$reason = null): bool { static $cache = [ ]; if ($user === null) { $user = new UserProfile(WCF::getUser()); } if (!isset($cache[$this->roomID])) { $cache[$this->roomID] = []; } if (\array_key_exists($user->userID, $cache[$this->roomID])) { return ($reason = $cache[$this->roomID][$user->userID]) === null; } if (!$user->userID) { $reason = new PermissionDeniedException(); return ($cache[$this->roomID][$user->userID] = $reason) === null; } $result = null; if (!PermissionHandler::get($user)->getPermission($this, 'user.canSee')) { $result = new PermissionDeniedException(); } $parameters = [ 'user' => $user, 'result' => $result, ]; EventHandler::getInstance()->fireAction($this, 'canSee', $parameters); $reason = $parameters['result']; if (!($reason === null || $reason instanceof \Throwable)) { throw new \DomainException('Result of canSee must be a \Throwable or null.'); } return ($cache[$this->roomID][$user->userID] = $reason) === null; } /** * Returns whether the given user can see the log of this room. * If no user is given the current user should be assumed. */ public function canSeeLog(?UserProfile $user = null, ?\Exception &$reason = null): bool { static $cache = [ ]; if ($user === null) { $user = new UserProfile(WCF::getUser()); } if (!isset($cache[$this->roomID])) { $cache[$this->roomID] = []; } if (\array_key_exists($user->userID, $cache[$this->roomID])) { return ($reason = $cache[$this->roomID][$user->userID]) === null; } $result = null; if (!PermissionHandler::get($user)->getPermission($this, 'user.canSeeLog')) { $result = new PermissionDeniedException(); } $parameters = [ 'user' => $user, 'result' => $result, ]; EventHandler::getInstance()->fireAction($this, 'canSeeLog', $parameters); $reason = $parameters['result']; if (!($reason === null || $reason instanceof \Throwable)) { throw new \DomainException('Result of canSeeLog must be a \Throwable or null.'); } return ($cache[$this->roomID][$user->userID] = $reason) === null; } /** * Returns whether the given user can join this room. * If no user is given the current user should be assumed. */ public function canJoin(?UserProfile $user = null, ?\Exception &$reason = null): bool { static $cache = [ ]; if ($user === null) { $user = new UserProfile(WCF::getUser()); } if (!isset($cache[$this->roomID])) { $cache[$this->roomID] = []; } if (\array_key_exists($user->userID, $cache[$this->roomID])) { return ($reason = $cache[$this->roomID][$user->userID]) === null; } $parameters = [ 'user' => $user, 'result' => null, ]; EventHandler::getInstance()->fireAction($this, 'canJoin', $parameters); $reason = $parameters['result']; if (!($reason === null || $reason instanceof \Throwable)) { throw new \DomainException('Result of canJoin must be a \Throwable or null.'); } return ($cache[$this->roomID][$user->userID] = $reason) === null; } /** * Returns whether the given user can write public messages in this room. * If no user is given the current user should be assumed. */ public function canWritePublicly(?UserProfile $user = null, ?\Exception &$reason = null): bool { static $cache = [ ]; if ($user === null) { $user = new UserProfile(WCF::getUser()); } if (!isset($cache[$this->roomID])) { $cache[$this->roomID] = []; } if (\array_key_exists($user->userID, $cache[$this->roomID])) { return ($reason = $cache[$this->roomID][$user->userID]) === null; } $result = null; if (!PermissionHandler::get($user)->getPermission($this, 'user.canWrite')) { $result = new PermissionDeniedException(); } $parameters = [ 'user' => $user, 'result' => $result, ]; EventHandler::getInstance()->fireAction($this, 'canWritePublicly', $parameters); $reason = $parameters['result']; if (!($reason === null || $reason instanceof \Throwable)) { throw new \DomainException('Result of canWritePublicly must be a \Throwable or null.'); } return ($cache[$this->roomID][$user->userID] = $reason) === null; } /** * @inheritDoc */ public function getTitle(): string { return WCF::getLanguage()->get($this->title); } /** * @inheritDoc */ public function getTopic(): string { $topic = StringUtil::trim(WCF::getLanguage()->get($this->topic)); if (!$this->topicUseHtml) { $topic = StringUtil::encodeHTML($topic); } return $topic; } /** * Returns an array of users in this room. * * @return \chat\data\user\User[] */ public function getUsers() { if (self::$userToRoom === null) { $sql = "SELECT r2u.userID, r2u.roomID FROM chat1_room_to_user r2u INNER JOIN wcf1_user u ON r2u.userID = u.userID WHERE r2u.active = ? ORDER BY u.username ASC"; $statement = WCF::getDB()->prepare($sql); $statement->execute([ 1 ]); self::$userToRoom = $statement->fetchMap('roomID', 'userID', false); if (!empty(self::$userToRoom)) { ChatUserRuntimeCache::getInstance()->cacheObjectIDs(\array_merge(...self::$userToRoom)); } } if (!isset(self::$userToRoom[$this->roomID])) { return [ ]; } return ChatUserRuntimeCache::getInstance()->getObjects(self::$userToRoom[$this->roomID]); } /** * @inheritDoc */ public function getLink(): string { return LinkHandler::getInstance()->getLink( 'Room', [ 'application' => 'chat', 'object' => $this, 'forceFrontend' => true, ] ); } /** * @inheritDoc */ public function jsonSerialize() { return [ 'title' => $this->getTitle(), 'topic' => $this->getTopic(), 'link' => $this->getLink(), ]; } }