Skip to content

Commit 7dc5d94

Browse files
nickvergessenbackportbot[bot]
authored andcommitted
Move Room::getParticipantUserIds() to participant service
Signed-off-by: Joas Schilling <[email protected]>
1 parent d28c5db commit 7dc5d94

File tree

10 files changed

+80
-40
lines changed

10 files changed

+80
-40
lines changed

lib/Activity/Listener.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use OCA\Talk\Events\ModifyParticipantEvent;
2929
use OCA\Talk\Events\RoomEvent;
3030
use OCA\Talk\Room;
31+
use OCA\Talk\Service\ParticipantService;
3132
use OCP\Activity\IManager;
3233
use OCP\AppFramework\Utility\ITimeFactory;
3334
use OCP\EventDispatcher\IEventDispatcher;
@@ -46,6 +47,9 @@ class Listener {
4647
/** @var ChatManager */
4748
protected $chatManager;
4849

50+
/** @var ParticipantService */
51+
protected $participantService;
52+
4953
/** @var LoggerInterface */
5054
protected $logger;
5155

@@ -55,11 +59,13 @@ class Listener {
5559
public function __construct(IManager $activityManager,
5660
IUserSession $userSession,
5761
ChatManager $chatManager,
62+
ParticipantService $participantService,
5863
LoggerInterface $logger,
5964
ITimeFactory $timeFactory) {
6065
$this->activityManager = $activityManager;
6166
$this->userSession = $userSession;
6267
$this->chatManager = $chatManager;
68+
$this->participantService = $participantService;
6369
$this->logger = $logger;
6470
$this->timeFactory = $timeFactory;
6571
}
@@ -107,7 +113,7 @@ public function generateCallActivity(Room $room): bool {
107113
}
108114

109115
$duration = $this->timeFactory->getTime() - $activeSince->getTimestamp();
110-
$userIds = $room->getParticipantUserIds($activeSince);
116+
$userIds = $this->participantService->getParticipantUserIds($room, $activeSince);
111117

112118
if ((\count($userIds) + $room->getActiveGuests()) === 1) {
113119
// Single user pinged or guests only => no summary/activity

lib/Chat/Notifier.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use OCA\Talk\Manager;
3131
use OCA\Talk\Participant;
3232
use OCA\Talk\Room;
33+
use OCA\Talk\Service\ParticipantService;
3334
use OCP\Comments\IComment;
3435
use OCP\IConfig;
3536
use OCP\Notification\IManager as INotificationManager;
@@ -49,6 +50,8 @@ class Notifier {
4950
private $notificationManager;
5051
/** @var IUserManager */
5152
private $userManager;
53+
/** @var ParticipantService */
54+
private $participantService;
5255
/** @var Manager */
5356
private $manager;
5457
/** @var IConfig */
@@ -58,11 +61,13 @@ class Notifier {
5861

5962
public function __construct(INotificationManager $notificationManager,
6063
IUserManager $userManager,
64+
ParticipantService $participantService,
6165
Manager $manager,
6266
IConfig $config,
6367
Util $util) {
6468
$this->notificationManager = $notificationManager;
6569
$this->userManager = $userManager;
70+
$this->participantService = $participantService;
6671
$this->manager = $manager;
6772
$this->config = $config;
6873
$this->util = $util;
@@ -91,6 +96,7 @@ public function notifyMentionedUsers(Room $chat, IComment $comment, array $alrea
9196
$mentionedAll = array_search('all', $mentionedUserIds, true);
9297

9398
if ($mentionedAll !== false) {
99+
$mentionedUserIds = array_unique(array_merge($mentionedUserIds, $this->participantService->getParticipantUserIds($chat)));
94100
$mentionedUserIds = array_unique(array_merge($mentionedUserIds, $chat->getParticipantUserIds()));
95101
}
96102

lib/Controller/RoomController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1100,7 +1100,7 @@ public function addParticipantToRoom(string $newParticipant, string $source = 'u
11001100
return new DataResponse([], Http::STATUS_BAD_REQUEST);
11011101
}
11021102

1103-
$participants = $this->room->getParticipantUserIds();
1103+
$participants = $this->participantService->getParticipantUserIds($this->room);
11041104

11051105
$participantsToAdd = [];
11061106
if ($source === 'users') {

lib/Manager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -777,7 +777,7 @@ public function resolveRoomDisplayName(Room $room, string $userId): string {
777777
}
778778

779779
protected function getRoomNameByParticipants(Room $room): string {
780-
$users = $room->getParticipantUserIds();
780+
$users = $this->participantService->getParticipantUserIds($room);
781781
$displayNames = [];
782782

783783
foreach ($users as $participantId) {

lib/Model/AttendeeMapper.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
namespace OCA\Talk\Model;
2525

2626
use OCP\AppFramework\Db\QBMapper;
27+
use OCP\DB\QueryBuilder\IQueryBuilder;
2728
use OCP\IDBConnection;
2829

2930
/**
@@ -56,6 +57,26 @@ public function findByActor(int $roomId, string $actorType, string $actorId): At
5657
return $this->findEntity($query);
5758
}
5859

60+
/**
61+
* @param int $roomId
62+
* @param string $actorType
63+
* @param \DateTime|null $lastJoinedCall
64+
* @return Attendee[]
65+
*/
66+
public function getActorsByType(int $roomId, string $actorType, ?\DateTime $lastJoinedCall = null): array {
67+
$query = $this->db->getQueryBuilder();
68+
$query->select('*')
69+
->from($this->getTableName())
70+
->where($query->expr()->eq('room_id', $query->createNamedParameter($roomId, IQueryBuilder::PARAM_INT)))
71+
->andWhere($query->expr()->eq('actor_type', $query->createNamedParameter($actorType)));
72+
73+
if ($lastJoinedCall instanceof \DateTimeInterface) {
74+
$query->andWhere($query->expr()->gte('last_joined_call', $query->createNamedParameter($lastJoinedCall, IQueryBuilder::PARAM_DATE)));
75+
}
76+
77+
return $this->findEntities($query);
78+
}
79+
5980
public function createAttendeeFromRow(array $row): Attendee {
6081
return $this->mapRowToEntity([
6182
'id' => $row['a_id'],

lib/PublicShareAuth/Listener.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use OCA\Talk\Exceptions\ParticipantNotFoundException;
3131
use OCA\Talk\Participant;
3232
use OCA\Talk\Room;
33+
use OCA\Talk\Service\ParticipantService;
3334
use OCP\EventDispatcher\IEventDispatcher;
3435

3536
/**
@@ -83,13 +84,15 @@ public static function preventExtraUsersFromJoining(Room $room, string $userId):
8384

8485
try {
8586
$participant = $room->getParticipant($userId);
86-
if ($participant->getParticipantType() === Participant::OWNER) {
87+
if ($participant->getAttendee()->getParticipantType() === Participant::OWNER) {
8788
return;
8889
}
8990
} catch (ParticipantNotFoundException $e) {
9091
}
9192

92-
if ($room->getActiveGuests() > 0 || \count($room->getParticipantUserIds()) > 1) {
93+
$participantService = \OC::$server->get(ParticipantService::class);
94+
$users = $participantService->getParticipantUserIds($room);
95+
if ($room->getActiveGuests() > 0 || \count($users) > 1) {
9396
throw new \OverflowException('Only the owner and another participant are allowed in rooms to request the password for a share');
9497
}
9598
}
@@ -108,7 +111,9 @@ public static function preventExtraGuestsFromJoining(Room $room): void {
108111
return;
109112
}
110113

111-
if ($room->getActiveGuests() > 0 || \count($room->getParticipantUserIds()) > 1) {
114+
$participantService = \OC::$server->get(ParticipantService::class);
115+
$users = $participantService->getParticipantUserIds($room);
116+
if ($room->getActiveGuests() > 0 || \count($users) > 1) {
112117
throw new \OverflowException('Only the owner and another participant are allowed in rooms to request the password for a share');
113118
}
114119
}

lib/Room.php

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
use OCA\Talk\Exceptions\InvalidPasswordException;
4343
use OCA\Talk\Exceptions\ParticipantNotFoundException;
4444
use OCA\Talk\Exceptions\UnauthorizedException;
45+
use OCA\Talk\Service\ParticipantService;
4546
use OCP\AppFramework\Utility\ITimeFactory;
4647
use OCP\Comments\IComment;
4748
use OCP\DB\QueryBuilder\IQueryBuilder;
@@ -250,13 +251,17 @@ public function getToken(): string {
250251
public function getName(): string {
251252
if ($this->type === self::ONE_TO_ONE_CALL) {
252253
if ($this->name === '') {
254+
// TODO use DI
255+
$participantService = \OC::$server->get(ParticipantService::class);
253256
// Fill the room name with the participants for 1-to-1 conversations
254-
$users = $this->getParticipantUserIds();
257+
$users = $participantService->getParticipantUserIds($this);
255258
sort($users);
256259
$this->setName(json_encode($users), '');
257260
} elseif (strpos($this->name, '["') !== 0) {
261+
// TODO use DI
262+
$participantService = \OC::$server->get(ParticipantService::class);
258263
// Not the json array, but the old fallback when someone left
259-
$users = $this->getParticipantUserIds();
264+
$users = $participantService->getParticipantUserIds($this);
260265
if (count($users) !== 2) {
261266
$users[] = $this->name;
262267
}
@@ -1183,32 +1188,6 @@ public function getParticipantsLegacy(int $lastPing = 0): array {
11831188
];
11841189
}
11851190

1186-
/**
1187-
* @param null|\DateTime $maxLastJoined When the "last joined call" is older than the given DateTime, the user is ignored
1188-
* @return string[]
1189-
*/
1190-
public function getParticipantUserIds(\DateTime $maxLastJoined = null): array {
1191-
$query = $this->db->getQueryBuilder();
1192-
$query->select('user_id')
1193-
->from('talk_participants')
1194-
->where($query->expr()->eq('room_id', $query->createNamedParameter($this->getId(), IQueryBuilder::PARAM_INT)))
1195-
->andWhere($query->expr()->nonEmptyString('user_id'));
1196-
1197-
if ($maxLastJoined instanceof \DateTimeInterface) {
1198-
$query->andWhere($query->expr()->gte('last_joined_call', $query->createNamedParameter($maxLastJoined, IQueryBuilder::PARAM_DATE)));
1199-
}
1200-
1201-
$result = $query->execute();
1202-
1203-
$users = [];
1204-
while ($row = $result->fetch()) {
1205-
$users[] = $row['user_id'];
1206-
}
1207-
$result->closeCursor();
1208-
1209-
return $users;
1210-
}
1211-
12121191
/**
12131192
* @param int $notificationLevel
12141193
* @return Participant[] Array of participants

lib/Service/ParticipantService.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ public function ensureOneToOneRoomIsFilled(Room $room): void {
217217
}
218218

219219
$users = json_decode($room->getName(), true);
220-
$participants = $room->getParticipantUserIds();
220+
$participants = $this->getParticipantUserIds($room);
221221
$missingUsers = array_diff($users, $participants);
222222

223223
foreach ($missingUsers as $userId) {
@@ -292,4 +292,17 @@ public function getParticipantsForRoom(Room $room): array {
292292

293293
return $participants;
294294
}
295+
296+
/**
297+
* @param Room $room
298+
* @param \DateTime|null $maxLastJoined
299+
* @return string[]
300+
*/
301+
public function getParticipantUserIds(Room $room, \DateTime $maxLastJoined = null): array {
302+
$attendees = $this->attendeeMapper->getActorsByType($room->getId(), 'users', $maxLastJoined);
303+
304+
return array_map(function(Attendee $attendee) {
305+
return $attendee->getActorId();
306+
}, $attendees);
307+
}
295308
}

lib/Share/RoomShareProvider.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
use OCA\Talk\Manager;
3838
use OCA\Talk\Participant;
3939
use OCA\Talk\Room;
40+
use OCA\Talk\Service\ParticipantService;
4041
use OCP\AppFramework\Utility\ITimeFactory;
4142
use OCP\DB\QueryBuilder\IQueryBuilder;
4243
use OCP\EventDispatcher\IEventDispatcher;
@@ -82,6 +83,8 @@ class RoomShareProvider implements IShareProvider {
8283
private $dispatcher;
8384
/** @var Manager */
8485
private $manager;
86+
/** @var ParticipantService */
87+
private $participantService;
8588
/** @var ITimeFactory */
8689
protected $timeFactory;
8790
/** @var IL10N */
@@ -95,6 +98,7 @@ public function __construct(
9598
IShareManager $shareManager,
9699
EventDispatcherInterface $dispatcher,
97100
Manager $manager,
101+
ParticipantService $participantService,
98102
ITimeFactory $timeFactory,
99103
IL10N $l,
100104
IMimeTypeLoader $mimeTypeLoader
@@ -104,6 +108,7 @@ public function __construct(
104108
$this->shareManager = $shareManager;
105109
$this->dispatcher = $dispatcher;
106110
$this->manager = $manager;
111+
$this->participantService = $participantService;
107112
$this->timeFactory = $timeFactory;
108113
$this->l = $l;
109114
$this->mimeTypeLoader = $mimeTypeLoader;
@@ -976,7 +981,7 @@ public function getAccessList($nodes, $currentAccess): array {
976981
continue;
977982
}
978983

979-
$userList = $room->getParticipantUserIds();
984+
$userList = $this->participantService->getParticipantUserIds($room);
980985
foreach ($userList as $uid) {
981986
$users[$uid] = $users[$uid] ?? [];
982987
$users[$uid][$row['id']] = $row;

lib/Signaling/BackendNotifier.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use OCA\Talk\Config;
2929
use OCA\Talk\Participant;
3030
use OCA\Talk\Room;
31+
use OCA\Talk\Service\ParticipantService;
3132
use OCP\Http\Client\IClientService;
3233
use OCP\IURLGenerator;
3334
use OCP\Security\ISecureRandom;
@@ -44,6 +45,8 @@ class BackendNotifier {
4445
private $secureRandom;
4546
/** @var Manager */
4647
private $signalingManager;
48+
/** @var ParticipantService */
49+
private $participantService;
4750
/** @var IUrlGenerator */
4851
private $urlGenerator;
4952

@@ -52,12 +55,14 @@ public function __construct(Config $config,
5255
IClientService $clientService,
5356
ISecureRandom $secureRandom,
5457
Manager $signalingManager,
58+
ParticipantService $participantService,
5559
IURLGenerator $urlGenerator) {
5660
$this->config = $config;
5761
$this->logger = $logger;
5862
$this->clientService = $clientService;
5963
$this->secureRandom = $secureRandom;
6064
$this->signalingManager = $signalingManager;
65+
$this->participantService = $participantService;
6166
$this->urlGenerator = $urlGenerator;
6267
}
6368

@@ -149,7 +154,7 @@ public function roomInvited(Room $room, array $users): void {
149154
'userids' => $userIds,
150155
// TODO(fancycode): We should try to get rid of 'alluserids' and
151156
// find a better way to notify existing users to update the room.
152-
'alluserids' => $room->getParticipantUserIds(),
157+
'alluserids' => $this->participantService->getParticipantUserIds($room),
153158
'properties' => $room->getPropertiesForSignaling(''),
154159
],
155160
]);
@@ -170,7 +175,7 @@ public function roomsDisinvited(Room $room, array $userIds): void {
170175
'userids' => $userIds,
171176
// TODO(fancycode): We should try to get rid of 'alluserids' and
172177
// find a better way to notify existing users to update the room.
173-
'alluserids' => $room->getParticipantUserIds(),
178+
'alluserids' => $this->participantService->getParticipantUserIds($room),
174179
'properties' => $room->getPropertiesForSignaling(''),
175180
],
176181
]);
@@ -191,7 +196,7 @@ public function roomSessionsRemoved(Room $room, array $sessionIds): void {
191196
'sessionids' => $sessionIds,
192197
// TODO(fancycode): We should try to get rid of 'alluserids' and
193198
// find a better way to notify existing users to update the room.
194-
'alluserids' => $room->getParticipantUserIds(),
199+
'alluserids' => $this->participantService->getParticipantUserIds($room),
195200
'properties' => $room->getPropertiesForSignaling(''),
196201
],
197202
]);
@@ -208,7 +213,7 @@ public function roomModified(Room $room): void {
208213
$this->backendRequest($room, [
209214
'type' => 'update',
210215
'update' => [
211-
'userids' => $room->getParticipantUserIds(),
216+
'userids' => $this->participantService->getParticipantUserIds($room),
212217
'properties' => $room->getPropertiesForSignaling(''),
213218
],
214219
]);

0 commit comments

Comments
 (0)