Browse Source

feat(events): Migrate session join and leave to typed events

Signed-off-by: Joas Schilling <coding@schilljs.com>
pull/10731/head
Joas Schilling 2 years ago
parent
commit
1744ed2f16
No known key found for this signature in database GPG Key ID: 74434EFE0D2E2205
  1. 29
      docs/events.md
  2. 58
      lib/Events/ABeforeJoinedRoomEvent.php
  3. 45
      lib/Events/ASessionLeftRoomEvent.php
  4. 29
      lib/Events/BeforeGuestJoinedRoomEvent.php
  5. 27
      lib/Events/BeforeSessionLeftRoomEvent.php
  6. 46
      lib/Events/BeforeUserJoinedRoomEvent.php
  7. 3
      lib/Events/DuplicatedParticipantEvent.php
  8. 40
      lib/Events/GuestJoinedRoomEvent.php
  9. 3
      lib/Events/JoinRoomGuestEvent.php
  10. 3
      lib/Events/JoinRoomUserEvent.php
  11. 3
      lib/Events/ParticipantEvent.php
  12. 27
      lib/Events/SessionLeftRoomEvent.php
  13. 46
      lib/Events/UserJoinedRoomEvent.php
  14. 6
      lib/Room.php
  15. 47
      lib/Service/ParticipantService.php

29
docs/events.md

@ -169,12 +169,32 @@ These events were not using the typed-event mechanism and are therefore deprecat
* After event: `OCA\Talk\Events\AttendeesRemovedEvent`
* Since: 12.0.0
### User joined a conversation
* Before event: `OCA\Talk\Events\BeforeUserJoinedRoomEvent`
* After event: `OCA\Talk\Events\UserJoinedRoomEvent`
* Since: 18.0.0
### Federated user joined a conversation
* Before event: `OCA\Talk\Events\BeforeFederatedUserJoinedRoomEvent`
* After event: `OCA\Talk\Events\FederatedUserJoinedRoomEvent`
* Since: 18.0.0
### Guest joined a conversation
* Before event: `OCA\Talk\Events\BeforeGuestJoinedRoomEvent`
* After event: `OCA\Talk\Events\GuestJoinedRoomEvent`
* Since: 18.0.0
### Session left a conversation
This is the invert action to `User joined a conversation`, `Federated user joined a conversation` and `Guest joined a conversation`
* Before event: `OCA\Talk\Events\BeforeSessionLeftRoomEvent`
* After event: `OCA\Talk\Events\SessionLeftRoomEvent`
* Since: 18.0.0
### Call notification send
* **internal:** This event is not part of the public API and you should not rely on it
@ -230,7 +250,8 @@ These events were not using the typed-event mechanism and are therefore deprecat
* Before event name: `OCA\Talk\Room::EVENT_BEFORE_ROOM_CONNECT`
* After event name: `OCA\Talk\Room::EVENT_AFTER_ROOM_CONNECT`
* Since: 8.0.0
* Deprecated: 18.0.0
* Deprecated: 18.0.0 - Use `OCA\Talk\Events\BeforeUserJoinedRoomEvent` and `OCA\Talk\Events\UserJoinedRoomEvent` instead
* Removed: 19.0.0
#### Join a conversation as guest (Connect)
@ -238,7 +259,8 @@ These events were not using the typed-event mechanism and are therefore deprecat
* Before event name: `OCA\Talk\Room::EVENT_BEFORE_GUEST_CONNECT`
* After event name: `OCA\Talk\Room::EVENT_AFTER_GUEST_CONNECT`
* Since: 8.0.0
* Deprecated: 18.0.0
* Deprecated: 18.0.0 - Use `OCA\Talk\Events\BeforeGuestJoinedRoomEvent` and `OCA\Talk\Events\GuestJoinedRoomEvent` instead
* Removed: 19.0.0
#### Join a call
@ -262,7 +284,8 @@ These events were not using the typed-event mechanism and are therefore deprecat
* Before event name: `OCA\Talk\Room::EVENT_BEFORE_ROOM_DISCONNECT`
* After event name: `OCA\Talk\Room::EVENT_AFTER_ROOM_DISCONNECT`
* Since: 8.0.0
* Deprecated: 18.0.0
* Deprecated: 18.0.0 - Use `OCA\Talk\Events\BeforeSessionLeftRoomEvent` and `OCA\Talk\Events\SessionLeftRoomEvent` instead
* Removed: 19.0.0
#### Remove user

58
lib/Events/ABeforeJoinedRoomEvent.php

@ -0,0 +1,58 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2023 Joas Schilling <coding@schilljs.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Talk\Events;
use OCA\Talk\Room;
abstract class ABeforeJoinedRoomEvent extends RoomEvent {
protected bool $cancelJoin = false;
public function __construct(
Room $room,
protected string $password,
protected bool $passedPasswordProtection,
) {
parent::__construct($room);
}
public function setCancelJoin(bool $cancelJoin): void {
$this->cancelJoin = $cancelJoin;
}
public function getCancelJoin(): bool {
return $this->cancelJoin;
}
public function getPassword(): string {
return $this->password;
}
public function setPassedPasswordProtection(bool $passedPasswordProtection): void {
$this->passedPasswordProtection = $passedPasswordProtection;
}
public function getPassedPasswordProtection(): bool {
return $this->passedPasswordProtection;
}
}

45
lib/Events/ASessionLeftRoomEvent.php

@ -0,0 +1,45 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2023 Joas Schilling <coding@schilljs.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Talk\Events;
use OCA\Talk\Participant;
use OCA\Talk\Room;
abstract class ASessionLeftRoomEvent extends RoomEvent {
public function __construct(
Room $room,
protected Participant $participant,
protected bool $rejoining,
) {
parent::__construct($room);
}
public function getParticipant(): Participant {
return $this->participant;
}
public function isRejoining(): bool {
return $this->rejoining;
}
}

29
lib/Events/BeforeGuestJoinedRoomEvent.php

@ -0,0 +1,29 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2023 Joas Schilling <coding@schilljs.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Talk\Events;
use OCA\Talk\Room;
class BeforeGuestJoinedRoomEvent extends ABeforeJoinedRoomEvent {
}

27
lib/Events/BeforeSessionLeftRoomEvent.php

@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2023 Joas Schilling <coding@schilljs.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Talk\Events;
class BeforeSessionLeftRoomEvent extends ASessionLeftRoomEvent {
}

46
lib/Events/BeforeUserJoinedRoomEvent.php

@ -0,0 +1,46 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2023 Joas Schilling <coding@schilljs.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Talk\Events;
use OCA\Talk\Room;
use OCP\IUser;
class BeforeUserJoinedRoomEvent extends ABeforeJoinedRoomEvent {
public function __construct(
Room $room,
protected IUser $user,
string $password,
bool $passedPasswordProtection,
) {
parent::__construct($room, $password, $passedPasswordProtection);
}
public function getUser(): IUser {
return $this->user;
}
public function getPassword(): string {
return $this->password;
}
}

3
lib/Events/DuplicatedParticipantEvent.php

@ -26,6 +26,9 @@ namespace OCA\Talk\Events;
use OCA\Talk\Participant;
use OCA\Talk\Room;
/**
* @deprecated
*/
class DuplicatedParticipantEvent extends ParticipantEvent {
public function __construct(
Room $room,

40
lib/Events/GuestJoinedRoomEvent.php

@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2023 Joas Schilling <coding@schilljs.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Talk\Events;
use OCA\Talk\Participant;
use OCA\Talk\Room;
class GuestJoinedRoomEvent extends RoomEvent {
public function __construct(
Room $room,
protected Participant $participant,
) {
parent::__construct($room);
}
public function getParticipant(): Participant {
return $this->participant;
}
}

3
lib/Events/JoinRoomGuestEvent.php

@ -25,6 +25,9 @@ namespace OCA\Talk\Events;
use OCA\Talk\Room;
/**
* @deprecated
*/
class JoinRoomGuestEvent extends RoomEvent {
protected bool $cancelJoin;

3
lib/Events/JoinRoomUserEvent.php

@ -26,6 +26,9 @@ namespace OCA\Talk\Events;
use OCA\Talk\Room;
use OCP\IUser;
/**
* @deprecated
*/
class JoinRoomUserEvent extends RoomEvent {
protected bool $cancelJoin;

3
lib/Events/ParticipantEvent.php

@ -26,6 +26,9 @@ namespace OCA\Talk\Events;
use OCA\Talk\Participant;
use OCA\Talk\Room;
/**
* @deprecated
*/
class ParticipantEvent extends RoomEvent {

27
lib/Events/SessionLeftRoomEvent.php

@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2023 Joas Schilling <coding@schilljs.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Talk\Events;
class SessionLeftRoomEvent extends ASessionLeftRoomEvent {
}

46
lib/Events/UserJoinedRoomEvent.php

@ -0,0 +1,46 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2023 Joas Schilling <coding@schilljs.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Talk\Events;
use OCA\Talk\Participant;
use OCA\Talk\Room;
use OCP\IUser;
class UserJoinedRoomEvent extends RoomEvent {
public function __construct(
Room $room,
protected IUser $user,
protected Participant $participant,
) {
parent::__construct($room);
}
public function getUser(): IUser {
return $this->user;
}
public function getParticipant(): Participant {
return $this->participant;
}
}

6
lib/Room.php

@ -160,11 +160,17 @@ class Room {
public const EVENT_AFTER_USER_REMOVE = self::class . '::postRemoveUser';
public const EVENT_BEFORE_PARTICIPANT_REMOVE = self::class . '::preRemoveBySession';
public const EVENT_AFTER_PARTICIPANT_REMOVE = self::class . '::postRemoveBySession';
/** @deprecated */
public const EVENT_BEFORE_ROOM_CONNECT = self::class . '::preJoinRoom';
/** @deprecated */
public const EVENT_AFTER_ROOM_CONNECT = self::class . '::postJoinRoom';
/** @deprecated */
public const EVENT_BEFORE_ROOM_DISCONNECT = self::class . '::preUserDisconnectRoom';
/** @deprecated */
public const EVENT_AFTER_ROOM_DISCONNECT = self::class . '::postUserDisconnectRoom';
/** @deprecated */
public const EVENT_BEFORE_GUEST_CONNECT = self::class . '::preJoinRoomGuest';
/** @deprecated */
public const EVENT_AFTER_GUEST_CONNECT = self::class . '::postJoinRoomGuest';
/** @deprecated */
public const EVENT_PASSWORD_VERIFY = self::class . '::verifyPassword';

47
lib/Service/ParticipantService.php

@ -33,13 +33,17 @@ use OCA\Talk\Events\AttendeesAddedEvent;
use OCA\Talk\Events\AttendeesRemovedEvent;
use OCA\Talk\Events\BeforeCallEndedForEveryoneEvent;
use OCA\Talk\Events\BeforeFederatedUserJoinedRoomEvent;
use OCA\Talk\Events\BeforeGuestJoinedRoomEvent;
use OCA\Talk\Events\BeforeGuestsCleanedUpEvent;
use OCA\Talk\Events\BeforeSessionLeftRoomEvent;
use OCA\Talk\Events\BeforeUserJoinedRoomEvent;
use OCA\Talk\Events\CallEndedForEveryoneEvent;
use OCA\Talk\Events\CallNotificationSendEvent;
use OCA\Talk\Events\ChatEvent;
use OCA\Talk\Events\DuplicatedParticipantEvent;
use OCA\Talk\Events\EndCallForEveryoneEvent;
use OCA\Talk\Events\FederatedUserJoinedRoomEvent;
use OCA\Talk\Events\GuestJoinedRoomEvent;
use OCA\Talk\Events\GuestsCleanedUpEvent;
use OCA\Talk\Events\JoinRoomGuestEvent;
use OCA\Talk\Events\JoinRoomUserEvent;
@ -50,8 +54,10 @@ use OCA\Talk\Events\RemoveParticipantEvent;
use OCA\Talk\Events\RemoveUserEvent;
use OCA\Talk\Events\RoomEvent;
use OCA\Talk\Events\SendCallNotificationEvent;
use OCA\Talk\Events\SessionLeftRoomEvent;
use OCA\Talk\Events\SilentModifyParticipantEvent;
use OCA\Talk\Events\SystemMessagesMultipleSentEvent;
use OCA\Talk\Events\UserJoinedRoomEvent;
use OCA\Talk\Exceptions\CannotReachRemoteException;
use OCA\Talk\Exceptions\ForbiddenException;
use OCA\Talk\Exceptions\InvalidPasswordException;
@ -298,10 +304,13 @@ class ParticipantService {
* @throws UnauthorizedException
*/
public function joinRoom(RoomService $roomService, Room $room, IUser $user, string $password, bool $passedPasswordProtection = false): Participant {
$event = new JoinRoomUserEvent($room, $user, $password, $passedPasswordProtection);
$this->dispatcher->dispatch(Room::EVENT_BEFORE_ROOM_CONNECT, $event);
$event = new BeforeUserJoinedRoomEvent($room, $user, $password, $passedPasswordProtection);
$this->dispatcher->dispatchTyped($event);
$legacyEvent = new JoinRoomUserEvent($room, $user, $password, $event->getPassedPasswordProtection());
$legacyEvent->setCancelJoin($event->getCancelJoin());
$this->dispatcher->dispatch(Room::EVENT_BEFORE_ROOM_CONNECT, $legacyEvent);
if ($event->getCancelJoin() === true) {
if ($legacyEvent->getCancelJoin() === true) {
$this->removeUser($room, $user, Room::PARTICIPANT_LEFT);
throw new UnauthorizedException('Participant is not allowed to join');
}
@ -313,7 +322,7 @@ class ParticipantService {
$manager = Server::get(Manager::class);
$isListableByUser = $manager->isRoomListableByUser($room, $user->getUID());
if (!$isListableByUser && !$event->getPassedPasswordProtection() && !$roomService->verifyPassword($room, $password)['result']) {
if (!$isListableByUser && !$legacyEvent->getPassedPasswordProtection() && !$roomService->verifyPassword($room, $password)['result']) {
throw new InvalidPasswordException('Provided password is invalid');
}
@ -343,10 +352,13 @@ class ParticipantService {
}
$session = $this->sessionService->createSessionForAttendee($attendee);
$participant = new Participant($room, $attendee, $session);
$this->dispatcher->dispatch(Room::EVENT_AFTER_ROOM_CONNECT, $event);
$this->dispatcher->dispatch(Room::EVENT_AFTER_ROOM_CONNECT, $legacyEvent);
$event = new UserJoinedRoomEvent($room, $user, $participant);
$this->dispatcher->dispatchTyped($event);
return new Participant($room, $attendee, $session);
return $participant;
}
/**
@ -387,14 +399,17 @@ class ParticipantService {
* @throws UnauthorizedException
*/
public function joinRoomAsNewGuest(RoomService $roomService, Room $room, string $password, bool $passedPasswordProtection = false, ?Participant $previousParticipant = null): Participant {
$event = new JoinRoomGuestEvent($room, $password, $passedPasswordProtection);
$this->dispatcher->dispatch(Room::EVENT_BEFORE_GUEST_CONNECT, $event);
$event = new BeforeGuestJoinedRoomEvent($room, $password, $passedPasswordProtection);
$this->dispatcher->dispatchTyped($event);
$legacyEvent = new JoinRoomGuestEvent($room, $password, $event->getPassedPasswordProtection());
$legacyEvent->setCancelJoin($event->getCancelJoin());
$this->dispatcher->dispatch(Room::EVENT_BEFORE_GUEST_CONNECT, $legacyEvent);
if ($event->getCancelJoin()) {
if ($legacyEvent->getCancelJoin()) {
throw new UnauthorizedException('Participant is not allowed to join');
}
if (!$event->getPassedPasswordProtection() && !$roomService->verifyPassword($room, $password)['result']) {
if (!$legacyEvent->getPassedPasswordProtection() && !$roomService->verifyPassword($room, $password)['result']) {
throw new InvalidPasswordException();
}
@ -429,9 +444,13 @@ class ParticipantService {
$this->attendeeMapper->update($attendee);
}
$this->dispatcher->dispatch(Room::EVENT_AFTER_GUEST_CONNECT, $event);
$participant = new Participant($room, $attendee, $session);
return new Participant($room, $attendee, $session);
$this->dispatcher->dispatch(Room::EVENT_AFTER_GUEST_CONNECT, $legacyEvent);
$event = new GuestJoinedRoomEvent($room, $participant);
$this->dispatcher->dispatchTyped($event);
return $participant;
}
/**
@ -781,6 +800,8 @@ class ParticipantService {
}
public function leaveRoomAsSession(Room $room, Participant $participant, bool $duplicatedParticipant = false): void {
$event = new BeforeSessionLeftRoomEvent($room, $participant, $duplicatedParticipant);
$this->dispatcher->dispatchTyped($event);
if ($duplicatedParticipant) {
$event = new DuplicatedParticipantEvent($room, $participant);
} else {
@ -801,6 +822,8 @@ class ParticipantService {
}
$this->dispatcher->dispatch(Room::EVENT_AFTER_ROOM_DISCONNECT, $event);
$event = new SessionLeftRoomEvent($room, $participant, $duplicatedParticipant);
$this->dispatcher->dispatchTyped($event);
if ($participant->getAttendee()->getParticipantType() === Participant::USER_SELF_JOINED
&& empty($this->sessionMapper->findByAttendeeId($participant->getAttendee()->getId()))) {

Loading…
Cancel
Save