Skip to content

Commit 940d92e

Browse files
committed
Review fixes
Signed-off-by: Gary Kim <[email protected]>
1 parent 6e235f9 commit 940d92e

File tree

5 files changed

+48
-31
lines changed

5 files changed

+48
-31
lines changed

lib/BackgroundJob/RetryJob.php

+6-4
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,6 @@ class RetryJob extends Job {
5151
/** @var int max number of attempts to send the request */
5252
private $maxTry = 20;
5353

54-
/** @var int how much time should be between two tries (10 minutes) */
55-
private $interval = 600;
56-
5754

5855
public function __construct(Notifications $notifications,
5956
ITimeFactory $timeFactory) {
@@ -94,6 +91,11 @@ protected function run($argument): void {
9491
*/
9592
protected function shouldRun(array $argument): bool {
9693
$lastRun = (int)$argument['lastRun'];
97-
return (($this->time->getTime() - $lastRun) > $this->interval);
94+
$try = (int)$argument['try'];
95+
return (($this->time->getTime() - $lastRun) > $this->nextRunBreak($try));
96+
}
97+
98+
protected function nextRunBreak(int $try): int {
99+
return min(($try + 1) * 300, 3600);
98100
}
99101
}

lib/Federation/FederationManager.php

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
*/
5151
class FederationManager {
5252
public const TALK_ROOM_RESOURCE = 'talk-room';
53+
public const TALK_PROTOCOL_NAME = 'nctalk';
5354
public const TOKEN_LENGTH = 15;
5455

5556
/** @var IConfig */

lib/Federation/Notifications.php

+11-23
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,8 @@
2929
use OCA\Talk\AppInfo\Application;
3030
use OCA\Talk\BackgroundJob\RetryJob;
3131
use OCA\Talk\Exceptions\RoomHasNoModeratorException;
32-
use OCA\Talk\Model\Attendee;
33-
use OCA\Talk\Model\AttendeeMapper;
34-
use OCA\Talk\Participant;
3532
use OCA\Talk\Room;
33+
use OCA\Talk\Service\ParticipantService;
3634
use OCP\BackgroundJob\IJobList;
3735
use OCP\Federation\ICloudFederationFactory;
3836
use OCP\Federation\ICloudFederationNotification;
@@ -60,8 +58,8 @@ class Notifications {
6058
/** @var IUserManager */
6159
private $userManager;
6260

63-
/** @var AttendeeMapper */
64-
private $attendeeMapper;
61+
/** @var ParticipantService */
62+
private $participantService;
6563

6664
public function __construct(
6765
ICloudFederationFactory $cloudFederationFactory,
@@ -70,15 +68,15 @@ public function __construct(
7068
ICloudFederationProviderManager $federationProviderManager,
7169
IJobList $jobList,
7270
IUserManager $userManager,
73-
AttendeeMapper $attendeeMapper
71+
ParticipantService $participantService
7472
) {
7573
$this->cloudFederationFactory = $cloudFederationFactory;
7674
$this->addressHandler = $addressHandler;
7775
$this->logger = $logger;
7876
$this->federationProviderManager = $federationProviderManager;
7977
$this->jobList = $jobList;
8078
$this->userManager = $userManager;
81-
$this->attendeeMapper = $attendeeMapper;
79+
$this->participantService = $participantService;
8280
}
8381

8482
/**
@@ -104,21 +102,11 @@ public function sendRemoteShare(string $providerId, string $token, string $share
104102

105103
/** @var IUser|null $roomOwner */
106104
$roomOwner = null;
107-
try {
108-
$roomOwners = $this->attendeeMapper->getActorsByParticipantTypes($room->getId(), [Participant::OWNER]);
109-
if (!empty($roomOwners) && $roomOwners[0]->getActorType() === Attendee::ACTOR_USERS) {
110-
$roomOwner = $this->userManager->get($roomOwners[0]->getActorId());
111-
}
112-
} catch (\Exception $e) {
113-
// Get a local moderator instead
114-
try {
115-
$roomOwners = $this->attendeeMapper->getActorsByParticipantTypes($room->getId(), [Participant::MODERATOR]);
116-
if (!empty($roomOwners) && $roomOwners[0]->getActorType() === Attendee::ACTOR_USERS) {
117-
$roomOwner = $this->userManager->get($roomOwners[0]->getActorId());
118-
}
119-
} catch (\Exception $e) {
120-
throw new RoomHasNoModeratorException();
121-
}
105+
$roomOwnerAttendee = $this->participantService->getHighestPermissionAttendee($room);
106+
if ($roomOwnerAttendee) {
107+
$roomOwner = $this->userManager->get($roomOwnerAttendee->getActorId());
108+
} else {
109+
throw new RoomHasNoModeratorException();
122110
}
123111

124112
$remote = $this->prepareRemoteUrl($remote);
@@ -141,7 +129,7 @@ public function sendRemoteShare(string $providerId, string $token, string $share
141129
$protocol = $share->getProtocol();
142130
$protocol['roomName'] = $roomName;
143131
$protocol['roomType'] = $roomType;
144-
$protocol['name'] = 'nctalk';
132+
$protocol['name'] = FederationManager::TALK_PROTOCOL_NAME;
145133
$share->setProtocol($protocol);
146134

147135
$response = $this->federationProviderManager->sendShare($share);

lib/Service/ParticipantService.php

+25-1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
use OCP\AppFramework\Db\DoesNotExistException;
5454
use OCP\AppFramework\Utility\ITimeFactory;
5555
use OCP\Comments\IComment;
56+
use OCP\DB\Exception as DBException;
5657
use OCP\DB\QueryBuilder\IQueryBuilder;
5758
use OCP\EventDispatcher\IEventDispatcher;
5859
use OCP\IConfig;
@@ -306,6 +307,7 @@ public function joinRoomAsNewGuest(Room $room, string $password, bool $passedPas
306307
* @param Room $room
307308
* @param array $participants
308309
* @param IUser|null $addedBy User that is attempting to add these users (must be set for federated users to be added)
310+
* @throws \Exception thrown if $addedBy is not set when adding a federated user
309311
*/
310312
public function addUsers(Room $room, array $participants, ?IUser $addedBy = null): void {
311313
if (empty($participants)) {
@@ -326,7 +328,7 @@ public function addUsers(Room $room, array $participants, ?IUser $addedBy = null
326328
$readPrivacy = $this->talkConfig->getUserReadPrivacy($participant['actorId']);
327329
} elseif ($participant['actorType'] === Attendee::ACTOR_FEDERATED_USERS) {
328330
if ($addedBy === null) {
329-
continue;
331+
throw new \Exception('$addedBy must be set to add a federated user');
330332
}
331333
$participant['accessToken'] = $this->secureRandom->generate(
332334
FederationManager::TOKEN_LENGTH,
@@ -369,6 +371,28 @@ private function sendRemoteShare(Room $room, IUser $addedBy, string $addingUserI
369371
$this->notifications->sendRemoteShare((string) $attendeeId, $token, $addingUserId, $addedBy->getDisplayName(), $addedBy->getCloudId(), 'user', $room);
370372
}
371373

374+
public function getHighestPermissionAttendee(Room $room): ?Attendee {
375+
$attendee = null;
376+
try {
377+
$roomOwners = $this->attendeeMapper->getActorsByParticipantTypes($room->getId(), [Participant::OWNER]);
378+
if (!empty($roomOwners) && $roomOwners[0]->getActorType() === Attendee::ACTOR_USERS) {
379+
$attendee = $roomOwners[0];
380+
} else {
381+
$roomModerators = $this->attendeeMapper->getActorsByParticipantTypes($room->getId(), [Participant::MODERATOR]);
382+
if (!empty($roomOwners)) {
383+
foreach ($roomModerators as $moderator) {
384+
if ($moderator->getActorType() === Attendee::ACTOR_USERS) {
385+
$attendee = $moderator;
386+
break;
387+
}
388+
}
389+
}
390+
}
391+
} catch (DBException $e) {
392+
}
393+
return $attendee;
394+
}
395+
372396
/**
373397
* @param Room $room
374398
* @param IGroup $group

tests/php/Federation/FederationTest.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
use OCA\Talk\Federation\FederationManager;
2929
use OCA\Talk\Federation\Notifications;
3030
use OCA\Talk\Manager;
31-
use OCA\Talk\MatterbridgeManager;
3231
use OCA\Talk\Model\Attendee;
3332
use OCA\Talk\Model\AttendeeMapper;
3433
use OCA\Talk\Participant;
@@ -75,6 +74,9 @@ class FederationTest extends TestCase {
7574
/** @var AttendeeMapper */
7675
protected $attendeeMapper;
7776

77+
/** @var ParticipantService */
78+
protected $participantService;
79+
7880
public function setUp(): void {
7981
parent::setUp();
8082

@@ -83,6 +85,7 @@ public function setUp(): void {
8385
$this->addressHandler = $this->createMock(AddressHandler::class);
8486
$this->userManager = $this->createMock(IUserManager::class);
8587
$this->attendeeMapper = $this->createMock(AttendeeMapper::class);
88+
$this->participantService = $this->createMock(ParticipantService::class);
8689

8790
$this->notifications = new Notifications(
8891
$this->cloudFederationFactory,
@@ -91,8 +94,7 @@ public function setUp(): void {
9194
$this->cloudFederationProviderManager,
9295
$this->createMock(IJobList::class),
9396
$this->userManager,
94-
$this->attendeeMapper,
95-
$this->createMock(MatterbridgeManager::class),
97+
$this->participantService,
9698
);
9799

98100
$this->federationManager = $this->createMock(FederationManager::class);

0 commit comments

Comments
 (0)