From 0219cf5df0ebc240b71910a9942cc261ea3baaa4 Mon Sep 17 00:00:00 2001 From: Sebastien Marinier Date: Tue, 14 May 2024 11:15:02 +0200 Subject: [PATCH] feat: list all shared rooms for users and moderators --- lib/Db/Room.php | 3 +++ lib/Db/RoomMapper.php | 31 +++++++++++++++---------------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/lib/Db/Room.php b/lib/Db/Room.php index 7fb2816..1ab048e 100644 --- a/lib/Db/Room.php +++ b/lib/Db/Room.php @@ -74,6 +74,7 @@ class Room extends Entity implements JsonSerializable { public $cleanLayout; public $joinMuted; public $running; + public $permission; public function __construct() { $this->addType('maxParticipants', 'integer'); @@ -86,6 +87,7 @@ class Room extends Entity implements JsonSerializable { $this->addType('cleanLayout', 'boolean'); $this->addType('joinMuted', 'boolean'); $this->addType('running', 'boolean'); + $this->addType('permission', 'integer'); } public function jsonSerialize(): array { @@ -102,6 +104,7 @@ class Room extends Entity implements JsonSerializable { 'everyoneIsModerator' => boolval($this->everyoneIsModerator), 'requireModerator' => boolval($this->requireModerator), 'shared' => boolval($this->shared), + 'permission' => $this->permission, 'moderatorToken' => $this->moderatorToken, 'listenOnly' => boolval($this->listenOnly), 'mediaCheck' => boolval($this->mediaCheck), diff --git a/lib/Db/RoomMapper.php b/lib/Db/RoomMapper.php index a1a9c3c..4d117d9 100644 --- a/lib/Db/RoomMapper.php +++ b/lib/Db/RoomMapper.php @@ -12,6 +12,16 @@ class RoomMapper extends QBMapper { parent::__construct($db, 'bbb_rooms', Room::class); } + private function joinShares(IQueryBuilder $qb): IQueryBuilder { + $qb->select('r.*') + ->from($this->tableName, 'r') + ->leftJoin('r', 'bbb_room_shares', 's', $qb->expr()->eq('r.id', 's.room_id')) + ->addSelect($qb->createFunction('count(case when `s`.`permission` IN ('. + RoomShare::PERMISSION_ADMIN.','.RoomShare::PERMISSION_MODERATOR.','.RoomShare::PERMISSION_USER + .') then 1 else null end) as shared')); + return $qb; + } + /** * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException * @throws DoesNotExistException @@ -19,10 +29,7 @@ class RoomMapper extends QBMapper { public function find(int $id): Room { /* @var $qb IQueryBuilder */ $qb = $this->db->getQueryBuilder(); - $qb->select('r.*') - ->from($this->tableName, 'r') - ->leftJoin('r', 'bbb_room_shares', 's', $qb->expr()->eq('r.id', 's.room_id')) - ->addSelect($qb->createFunction('count(case when `s`.`permission` = 0 then 1 else null end) as shared')) + $this->joinShares($qb) ->where($qb->expr()->eq('r.id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))) ->groupBy('r.id'); ; @@ -38,10 +45,7 @@ class RoomMapper extends QBMapper { public function findByUid(string $uid): Room { /* @var $qb IQueryBuilder */ $qb = $this->db->getQueryBuilder(); - $qb->select('r.*') - ->from($this->tableName, 'r') - ->leftJoin('r', 'bbb_room_shares', 's', $qb->expr()->eq('r.id', 's.room_id')) - ->addSelect($qb->createFunction('count(case when `s`.`permission` = 0 then 1 else null end) as shared')) + $this->joinShares($qb) ->where($qb->expr()->eq('r.uid', $qb->createNamedParameter($uid))) ->groupBy('r.id'); ; @@ -70,25 +74,20 @@ class RoomMapper extends QBMapper { public function findAll(string $userId, array $groupIds, array $circleIds): array { /* @var $qb IQueryBuilder */ $qb = $this->db->getQueryBuilder(); - $qb->select('r.*') - ->from($this->tableName, 'r') - ->leftJoin('r', 'bbb_room_shares', 's', $qb->expr()->eq('r.id', 's.room_id')) - ->addSelect($qb->createFunction('count(case when `s`.`permission` = 0 then 1 else null end) as shared')) + $this->joinShares($qb) + ->addSelect($qb->createFunction('min(case when `r`.`user_id` ='. $qb->createNamedParameter($userId).' then '.RoomShare::PERMISSION_ADMIN.' else `s`.`permission` end) as permission')) ->where( $qb->expr()->orX( $qb->expr()->eq('r.user_id', $qb->createNamedParameter($userId)), $qb->expr()->andX( - $qb->expr()->eq('s.permission', $qb->createNamedParameter(RoomShare::PERMISSION_ADMIN, IQueryBuilder::PARAM_INT)), $qb->expr()->eq('s.share_type', $qb->createNamedParameter(RoomShare::SHARE_TYPE_USER, IQueryBuilder::PARAM_INT)), $qb->expr()->eq('s.share_with', $qb->createNamedParameter($userId)) ), $qb->expr()->andX( - $qb->expr()->eq('s.permission', $qb->createNamedParameter(RoomShare::PERMISSION_ADMIN, IQueryBuilder::PARAM_INT)), $qb->expr()->eq('s.share_type', $qb->createNamedParameter(RoomShare::SHARE_TYPE_GROUP, IQueryBuilder::PARAM_INT)), $qb->expr()->in('s.share_with', $qb->createNamedParameter($groupIds, IQueryBuilder::PARAM_STR_ARRAY)) ), $qb->expr()->andX( - $qb->expr()->eq('s.permission', $qb->createNamedParameter(RoomShare::PERMISSION_ADMIN, IQueryBuilder::PARAM_INT)), $qb->expr()->eq('s.share_type', $qb->createNamedParameter(RoomShare::SHARE_TYPE_CIRCLE, IQueryBuilder::PARAM_INT)), $qb->expr()->in('s.share_with', $qb->createNamedParameter($circleIds, IQueryBuilder::PARAM_STR_ARRAY)) ) @@ -99,7 +98,7 @@ class RoomMapper extends QBMapper { /** @var array */ return $this->findEntities($qb); } - + /** * @return array */