fix(federation): Remove notifications when the invite or room is invalid/not found

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2023-10-11 07:26:12 +02:00
parent 2f0b22f49d
commit 4659794b6f
No known key found for this signature in database
GPG key ID: 74434EFE0D2E2205
5 changed files with 34 additions and 8 deletions

View file

@ -113,17 +113,22 @@ class FederationController extends OCSController {
throw new UnauthorizedException();
}
$invitations = $this->federationManager->getRemoteRoomShares($user);
return new DataResponse(array_map(function (Invitation $invitation) {
/** @var TalkFederationInvite[] $data */
$data = array_filter(array_map(function (Invitation $invitation) {
$data = $invitation->asArray();
try {
$room = $this->talkManager->getRoomById($invitation->getRoomId());
$data['remote_token'] = $room->getRemoteToken();
$data['remote_server'] = $room->getRemoteServer();
} catch (RoomNotFoundException $exception) {
} catch (RoomNotFoundException) {
return null;
}
return $data;
}, $invitations));
return new DataResponse($data);
}
}

View file

@ -130,6 +130,13 @@ class FederationManager {
$this->invitationMapper->delete($invitation);
}
/**
* @throws DoesNotExistException
*/
public function getRemoteShareById(int $shareId): Invitation {
return $this->invitationMapper->getInvitationById($shareId);
}
/**
* @throws UnauthorizedException
* @throws DoesNotExistException

View file

@ -31,6 +31,7 @@ use OCA\Talk\Chat\MessageParser;
use OCA\Talk\Config;
use OCA\Talk\Exceptions\ParticipantNotFoundException;
use OCA\Talk\Exceptions\RoomNotFoundException;
use OCA\Talk\Federation\FederationManager;
use OCA\Talk\GuestManager;
use OCA\Talk\Manager;
use OCA\Talk\Model\Attendee;
@ -45,7 +46,6 @@ use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Comments\ICommentsManager;
use OCP\Comments\NotFoundException;
use OCP\Files\IRootFolder;
use OCP\HintException;
use OCP\IGroupManager;
use OCP\IL10N;
use OCP\IURLGenerator;
@ -90,6 +90,7 @@ class Notifier implements INotifier {
protected Definitions $definitions,
protected AddressHandler $addressHandler,
protected BotServerMapper $botServerMapper,
protected FederationManager $federationManager,
) {
$this->commentManager = $commentManager;
}
@ -395,12 +396,20 @@ class Notifier implements INotifier {
return $notification;
}
/**
* @throws HintException
*/
protected function parseRemoteInvitationMessage(INotification $notification, IL10N $l): INotification {
$subjectParameters = $notification->getSubjectParameters();
try {
$invite = $this->federationManager->getRemoteShareById((int) $notification->getObjectId());
if ($invite->getUserId() !== $notification->getUser()) {
throw new AlreadyProcessedException();
}
$this->manager->getRoomById($invite->getRoomId());
} catch (RoomNotFoundException $e) {
// Room does not exist
throw new AlreadyProcessedException();
}
[$sharedById, $sharedByServer] = $this->addressHandler->splitUserRemote($subjectParameters['sharedByFederatedId']);
$message = $l->t('{user1} shared room {roomName} on {remoteServer} with you');

View file

@ -98,8 +98,8 @@ namespace OCA\Talk;
* access_token: string,
* id: int,
* remote_id: string,
* remote_server: ?string,
* remote_token: ?string,
* remote_server: string,
* remote_token: string,
* room_id: int,
* user_id: string,
* }

View file

@ -27,6 +27,7 @@ use OCA\Talk\Chat\MessageParser;
use OCA\Talk\Config;
use OCA\Talk\Exceptions\ParticipantNotFoundException;
use OCA\Talk\Exceptions\RoomNotFoundException;
use OCA\Talk\Federation\FederationManager;
use OCA\Talk\GuestManager;
use OCA\Talk\Manager;
use OCA\Talk\Model\Attendee;
@ -95,6 +96,8 @@ class NotifierTest extends TestCase {
protected $addressHandler;
/** @var BotServerMapper|MockObject */
protected $botServerMapper;
/** @var FederationManager|MockObject */
protected $federationManager;
public function setUp(): void {
parent::setUp();
@ -118,6 +121,7 @@ class NotifierTest extends TestCase {
$this->definitions = $this->createMock(Definitions::class);
$this->addressHandler = $this->createMock(AddressHandler::class);
$this->botServerMapper = $this->createMock(BotServerMapper::class);
$this->federationManager = $this->createMock(FederationManager::class);
$this->notifier = new Notifier(
$this->lFactory,
@ -139,6 +143,7 @@ class NotifierTest extends TestCase {
$this->definitions,
$this->addressHandler,
$this->botServerMapper,
$this->federationManager,
);
}