summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoas Schilling <coding@schilljs.com>2018-11-09 14:14:57 +0100
committerJoas Schilling <coding@schilljs.com>2018-11-12 11:35:31 +0100
commit4afa2d7946f121d07610eeb0165b20ee135fb819 (patch)
treefe1bd72a5b3772752445355e4fa43766ca715dcf
parent5448a59d40061f71a279a427b8100079dafc1ecc (diff)
Do not create the participant when we don't need them
Signed-off-by: Joas Schilling <coding@schilljs.com>
-rw-r--r--lib/Activity/Hooks.php3
-rw-r--r--lib/Chat/AutoComplete/SearchPlugin.php3
-rw-r--r--lib/Controller/RoomController.php6
-rw-r--r--lib/PublicShareAuth/Listener.php21
-rw-r--r--lib/Room.php26
-rw-r--r--lib/Share/Helper/DeletedShareAPIController.php14
-rw-r--r--lib/Share/Helper/ShareAPIController.php14
-rw-r--r--lib/Share/RoomShareProvider.php8
-rw-r--r--lib/Signaling/BackendNotifier.php19
-rw-r--r--tests/php/Chat/AutoComplete/SearchPluginTest.php4
10 files changed, 70 insertions, 48 deletions
diff --git a/lib/Activity/Hooks.php b/lib/Activity/Hooks.php
index ac3be3046..9e7f119a7 100644
--- a/lib/Activity/Hooks.php
+++ b/lib/Activity/Hooks.php
@@ -77,8 +77,7 @@ class Hooks {
}
$duration = $this->timeFactory->getTime() - $activeSince->getTimestamp();
- $participants = $room->getParticipants($activeSince->getTimestamp());
- $userIds = array_map('\strval', array_keys($participants['users']));
+ $userIds = $room->getParticipantUserIds($activeSince->getTimestamp());
if (empty($userIds) || (\count($userIds) === 1 && $room->getActiveGuests() === 0)) {
// Single user pinged or guests only => no activity
diff --git a/lib/Chat/AutoComplete/SearchPlugin.php b/lib/Chat/AutoComplete/SearchPlugin.php
index 976fc4468..3b7ec2d12 100644
--- a/lib/Chat/AutoComplete/SearchPlugin.php
+++ b/lib/Chat/AutoComplete/SearchPlugin.php
@@ -58,10 +58,9 @@ class SearchPlugin implements ISearchPlugin {
* @since 13.0.0
*/
public function search($search, $limit, $offset, ISearchResult $searchResult) {
- $participants = $this->room->getParticipants();
- $this->searchUsers($search, array_map('strval', array_keys($participants['users'])), $searchResult);
// FIXME Handle guests
+ $this->searchUsers($search, $this->room->getParticipantUserIds(), $searchResult);
return false;
}
diff --git a/lib/Controller/RoomController.php b/lib/Controller/RoomController.php
index 38789491f..b8277877a 100644
--- a/lib/Controller/RoomController.php
+++ b/lib/Controller/RoomController.php
@@ -769,7 +769,7 @@ class RoomController extends OCSController {
return new DataResponse([], Http::STATUS_FORBIDDEN);
}
- $participants = $room->getParticipants();
+ $participants = $room->getParticipantUserIds();
$updateRoomType = $room->getType() === Room::ONE_TO_ONE_CALL ? Room::GROUP_CALL : false;
$participantsToAdd = [];
@@ -779,7 +779,7 @@ class RoomController extends OCSController {
return new DataResponse([], Http::STATUS_NOT_FOUND);
}
- if (isset($participants['users'][$newParticipant])) {
+ if (\in_array($newParticipant, $participants, true)) {
return new DataResponse([]);
}
@@ -794,7 +794,7 @@ class RoomController extends OCSController {
$usersInGroup = $group->getUsers();
foreach ($usersInGroup as $user) {
- if (isset($participants['users'][$user->getUID()])) {
+ if (\in_array($user->getUID(), $participants, true)) {
continue;
}
diff --git a/lib/PublicShareAuth/Listener.php b/lib/PublicShareAuth/Listener.php
index 11d70905d..b622e2162 100644
--- a/lib/PublicShareAuth/Listener.php
+++ b/lib/PublicShareAuth/Listener.php
@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace OCA\Spreed\PublicShareAuth;
+use OCA\Spreed\Exceptions\ParticipantNotFoundException;
use OCA\Spreed\Participant;
use OCA\Spreed\Room;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
@@ -92,15 +93,15 @@ class Listener {
return;
}
- $participants = $room->getParticipants();
- $users = $participants['users'];
- $guests = $participants['guests'];
-
- if (array_key_exists($userId, $users) && $users[$userId]['participantType'] === Participant::OWNER) {
- return;
+ try {
+ $participant = $room->getParticipant($userId);
+ if ($participant->getParticipantType() === Participant::OWNER) {
+ return;
+ }
+ } catch (ParticipantNotFoundException $e) {
}
- if (\count($users) > 1 || \count($guests) > 0) {
+ if ($room->getActiveGuests() > 0 || \count($room->getParticipantUserIds()) > 1) {
throw new \OverflowException('Only the owner and another participant are allowed in rooms to request the password for a share');
}
}
@@ -119,11 +120,7 @@ class Listener {
return;
}
- $participants = $room->getParticipants();
- $users = $participants['users'];
- $guests = $participants['guests'];
-
- if (\count($users) > 1 || \count($guests) > 0) {
+ if ($room->getActiveGuests() > 0 || \count($room->getParticipantUserIds()) > 1) {
throw new \OverflowException('Only the owner and another participant are allowed in rooms to request the password for a share');
}
}
diff --git a/lib/Room.php b/lib/Room.php
index 294eb63f4..379125e24 100644
--- a/lib/Room.php
+++ b/lib/Room.php
@@ -828,6 +828,32 @@ class Room {
}
/**
+ * @param int $lastPing When the last ping is older than the given timestamp, the user is ignored
+ * @return string[]
+ */
+ public function getParticipantUserIds(int $lastPing = 0): array {
+ $query = $this->db->getQueryBuilder();
+ $query->select('*')
+ ->from('talk_participants')
+ ->where($query->expr()->eq('room_id', $query->createNamedParameter($this->getId(), IQueryBuilder::PARAM_INT)))
+ ->andWhere($query->expr()->nonEmptyString('user_id'));
+
+ if ($lastPing > 0) {
+ $query->andWhere($query->expr()->gt('last_ping', $query->createNamedParameter($lastPing, IQueryBuilder::PARAM_INT)));
+ }
+
+ $result = $query->execute();
+
+ $users = [];
+ while ($row = $result->fetch()) {
+ $users[] = $row['user_id'];
+ }
+ $result->closeCursor();
+
+ return $users;
+ }
+
+ /**
* @param int $notificationLevel
* @return Participant[] Array of participants
*/
diff --git a/lib/Share/Helper/DeletedShareAPIController.php b/lib/Share/Helper/DeletedShareAPIController.php
index 717ffd40a..1e75c5e8c 100644
--- a/lib/Share/Helper/DeletedShareAPIController.php
+++ b/lib/Share/Helper/DeletedShareAPIController.php
@@ -27,6 +27,7 @@ namespace OCA\Spreed\Share\Helper;
use OCA\Spreed\Exceptions\RoomNotFoundException;
use OCA\Spreed\Manager;
use OCA\Spreed\Room;
+use OCP\IUser;
use OCP\IUserManager;
use OCP\Share\IShare;
@@ -87,10 +88,15 @@ class DeletedShareAPIController {
// the other participant.
$roomName = $room->getName();
if ($room->getType() === Room::ONE_TO_ONE_CALL) {
- $participantsList = $room->getParticipants()['users'];
- unset($participantsList[$this->userId]);
-
- $roomName = $this->userManager->get(key($participantsList))->getDisplayName();
+ $userIds = $room->getParticipantUserIds();
+ foreach ($userIds as $userId) {
+ if ($this->userId !== $userId) {
+ $user = $this->userManager->get($userId);
+ if ($user instanceof IUser) {
+ $roomName = $user->getDisplayName();
+ }
+ }
+ }
}
$result['share_with_displayname'] = $roomName;
diff --git a/lib/Share/Helper/ShareAPIController.php b/lib/Share/Helper/ShareAPIController.php
index e46c3019b..b18984371 100644
--- a/lib/Share/Helper/ShareAPIController.php
+++ b/lib/Share/Helper/ShareAPIController.php
@@ -30,6 +30,7 @@ use OCA\Spreed\Manager;
use OCA\Spreed\Room;
use OCP\AppFramework\OCS\OCSNotFoundException;
use OCP\IL10N;
+use OCP\IUser;
use OCP\IUserManager;
use OCP\Share\IShare;
@@ -94,10 +95,15 @@ class ShareAPIController {
// the other participant.
$roomName = $room->getName();
if ($room->getType() === Room::ONE_TO_ONE_CALL) {
- $participantsList = $room->getParticipants()['users'];
- unset($participantsList[$this->userId]);
-
- $roomName = $this->userManager->get(key($participantsList))->getDisplayName();
+ $userIds = $room->getParticipantUserIds();
+ foreach ($userIds as $userId) {
+ if ($this->userId !== $userId) {
+ $user = $this->userManager->get($userId);
+ if ($user instanceof IUser) {
+ $roomName = $user->getDisplayName();
+ }
+ }
+ }
}
$result['share_with_displayname'] = $roomName;
diff --git a/lib/Share/RoomShareProvider.php b/lib/Share/RoomShareProvider.php
index 7b5f7678c..e905dd8fd 100644
--- a/lib/Share/RoomShareProvider.php
+++ b/lib/Share/RoomShareProvider.php
@@ -902,14 +902,14 @@ class RoomShareProvider implements IShareProvider {
continue;
}
- $userList = $room->getParticipants()['users'];
- foreach ($userList as $uid => $participantData) {
- $users[$uid] = isset($users[$uid]) ? $users[$uid] : [];
+ $userList = $room->getParticipantUserIds();
+ foreach ($userList as $uid) {
+ $users[$uid] = $users[$uid] ?? [];
$users[$uid][$row['id']] = $row;
}
} else if ($type === self::SHARE_TYPE_USERROOM && $currentAccess === true) {
$uid = $row['share_with'];
- $users[$uid] = isset($users[$uid]) ? $users[$uid] : [];
+ $users[$uid] = $users[$uid] ?? [];
$users[$uid][$row['id']] = $row;
}
}
diff --git a/lib/Signaling/BackendNotifier.php b/lib/Signaling/BackendNotifier.php
index 78d19599b..9fd5403fc 100644
--- a/lib/Signaling/BackendNotifier.php
+++ b/lib/Signaling/BackendNotifier.php
@@ -113,17 +113,6 @@ class BackendNotifier{
}
/**
- * Return list of userids that are invited to a room.
- *
- * @param Room $room
- * @return array
- */
- private function getRoomUserIds($room) {
- $participants = $room->getParticipants();
- return array_keys($participants['users']);
- }
-
- /**
* The given users are now invited to a room.
*
* @param Room $room
@@ -142,7 +131,7 @@ class BackendNotifier{
'userids' => $userIds,
// TODO(fancycode): We should try to get rid of 'alluserids' and
// find a better way to notify existing users to update the room.
- 'alluserids' => $this->getRoomUserIds($room),
+ 'alluserids' => $room->getParticipantUserIds(),
'properties' => [
'name' => $room->getName(),
'type' => $room->getType(),
@@ -166,7 +155,7 @@ class BackendNotifier{
'userids' => $userIds,
// TODO(fancycode): We should try to get rid of 'alluserids' and
// find a better way to notify existing users to update the room.
- 'alluserids' => $this->getRoomUserIds($room),
+ 'alluserids' => $room->getParticipantUserIds(),
'properties' => [
'name' => $room->getName(),
'type' => $room->getType(),
@@ -190,7 +179,7 @@ class BackendNotifier{
'sessionids' => $sessionIds,
// TODO(fancycode): We should try to get rid of 'alluserids' and
// find a better way to notify existing users to update the room.
- 'alluserids' => $this->getRoomUserIds($room),
+ 'alluserids' => $room->getParticipantUserIds(),
'properties' => [
'name' => $room->getName(),
'type' => $room->getType(),
@@ -210,7 +199,7 @@ class BackendNotifier{
$this->backendRequest('/api/v1/room/' . $room->getToken(), [
'type' => 'update',
'update' => [
- 'userids' => $this->getRoomUserIds($room),
+ 'userids' => $room->getParticipantUserIds(),
'properties' => [
'name' => $room->getName(),
'type' => $room->getType(),
diff --git a/tests/php/Chat/AutoComplete/SearchPluginTest.php b/tests/php/Chat/AutoComplete/SearchPluginTest.php
index d0a89f41d..b4eff43da 100644
--- a/tests/php/Chat/AutoComplete/SearchPluginTest.php
+++ b/tests/php/Chat/AutoComplete/SearchPluginTest.php
@@ -71,8 +71,8 @@ class SearchPluginTest extends \Test\TestCase {
$room = $this->createMock(Room::class);
$room->expects($this->once())
- ->method('getParticipants')
- ->willReturn(['users' => [123 => [], 'foo' => [], 'bar' => []]]);
+ ->method('getParticipantUserIds')
+ ->willReturn(['123', 'foo', 'bar']);
$plugin = $this->getPlugin(['searchUsers']);
$plugin->setContext(['room' => $room]);