Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(federation): Expose local token with the invite so UI can render proxy avatars #11777

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/Controller/AEnvironmentAwareController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
namespace OCA\Talk\Controller;

use OC\AppFramework\Http\Dispatcher;
use OCA\Talk\Model\Invitation;
use OCA\Talk\Participant;
use OCA\Talk\Room;
use OCP\AppFramework\OCSController;
Expand All @@ -36,6 +37,7 @@ abstract class AEnvironmentAwareController extends OCSController {
protected int $apiVersion = 1;
protected ?Room $room = null;
protected ?Participant $participant = null;
protected ?Invitation $invitation = null;

public function setAPIVersion(int $apiVersion): void {
$this->apiVersion = $apiVersion;
Expand All @@ -61,6 +63,14 @@ public function getParticipant(): ?Participant {
return $this->participant;
}

public function setInvitation(Invitation $invitation): void {
$this->invitation = $invitation;
}

public function getInvitation(): ?Invitation {
return $this->invitation;
}

/**
* Following the logic of {@see Dispatcher::executeController}
* @return string Either 'json' or 'xml'
Expand Down
7 changes: 6 additions & 1 deletion lib/Controller/AvatarController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

use InvalidArgumentException;
use OCA\Talk\Exceptions\CannotReachRemoteException;
use OCA\Talk\Middleware\Attribute\AllowWithoutParticipantWhenPendingInvitation;
use OCA\Talk\Middleware\Attribute\FederationSupported;
use OCA\Talk\Middleware\Attribute\RequireLoggedInParticipant;
use OCA\Talk\Middleware\Attribute\RequireModeratorParticipant;
Expand Down Expand Up @@ -143,13 +144,14 @@ public function emojiAvatar(string $emoji, ?string $color): DataResponse {
#[FederationSupported]
#[PublicPage]
#[NoCSRFRequired]
#[AllowWithoutParticipantWhenPendingInvitation]
#[RequireParticipantOrLoggedInAndListedConversation]
public function getAvatar(bool $darkTheme = false): FileDisplayResponse {
if ($this->room->getRemoteServer() !== '') {
/** @var \OCA\Talk\Federation\Proxy\TalkV1\Controller\AvatarController $proxy */
$proxy = \OCP\Server::get(\OCA\Talk\Federation\Proxy\TalkV1\Controller\AvatarController::class);
try {
return $proxy->getAvatar($this->room, $this->participant, $darkTheme);
return $proxy->getAvatar($this->room, $this->participant, $this->invitation, $darkTheme);
} catch (CannotReachRemoteException) {
// Falling back to a local "globe" avatar for indicating the federation
}
Expand All @@ -172,6 +174,7 @@ public function getAvatar(bool $darkTheme = false): FileDisplayResponse {
#[FederationSupported]
#[PublicPage]
#[NoCSRFRequired]
#[AllowWithoutParticipantWhenPendingInvitation]
#[RequireParticipantOrLoggedInAndListedConversation]
public function getAvatarDark(): FileDisplayResponse {
return $this->getAvatar(true);
Expand All @@ -193,6 +196,7 @@ public function getAvatarDark(): FileDisplayResponse {
#[OpenAPI(scope: OpenAPI::SCOPE_FEDERATION)]
#[PublicPage]
#[NoCSRFRequired]
#[AllowWithoutParticipantWhenPendingInvitation]
#[RequireLoggedInParticipant]
public function getUserProxyAvatar(int $size, string $cloudId, bool $darkTheme = false): FileDisplayResponse {
try {
Expand Down Expand Up @@ -252,6 +256,7 @@ public function getUserProxyAvatar(int $size, string $cloudId, bool $darkTheme =
#[OpenAPI(scope: OpenAPI::SCOPE_FEDERATION)]
#[PublicPage]
#[NoCSRFRequired]
#[AllowWithoutParticipantWhenPendingInvitation]
#[RequireLoggedInParticipant]
public function getUserProxyAvatarDark(int $size, string $cloudId): FileDisplayResponse {
return $this->getUserProxyAvatar($size, $cloudId, true);
Expand Down
2 changes: 1 addition & 1 deletion lib/Controller/FederationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ public function getShares(): DataResponse {
* @return TalkFederationInvite|null
*/
protected function enrichInvite(Invitation $invitation): ?array {

try {
$room = $this->talkManager->getRoomById($invitation->getLocalRoomId());
} catch (RoomNotFoundException) {
Expand All @@ -189,6 +188,7 @@ protected function enrichInvite(Invitation $invitation): ?array {

$federationInvite = $invitation->jsonSerialize();
$federationInvite['roomName'] = $room->getName();
$federationInvite['localToken'] = $room->getToken();
return $federationInvite;
}
}
11 changes: 8 additions & 3 deletions lib/Federation/Proxy/TalkV1/Controller/AvatarController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

use OCA\Talk\Exceptions\CannotReachRemoteException;
use OCA\Talk\Federation\Proxy\TalkV1\ProxyRequest;
use OCA\Talk\Model\Invitation;
use OCA\Talk\Participant;
use OCA\Talk\ResponseDefinitions;
use OCA\Talk\Room;
Expand All @@ -53,10 +54,14 @@ public function __construct(
*
* 200: Room avatar returned
*/
public function getAvatar(Room $room, Participant $participant, bool $darkTheme): FileDisplayResponse {
public function getAvatar(Room $room, ?Participant $participant, ?Invitation $invitation, bool $darkTheme): FileDisplayResponse {
if ($participant === null && $invitation === null) {
throw new CannotReachRemoteException('Must receive either participant or invitation');
}

$proxy = $this->proxy->get(
$participant->getAttendee()->getInvitedCloudId(),
$participant->getAttendee()->getAccessToken(),
$participant ? $participant->getAttendee()->getInvitedCloudId() : $invitation->getLocalCloudId(),
$participant ? $participant->getAttendee()->getAccessToken() : $invitation->getAccessToken(),
$room->getRemoteServer() . '/ocs/v2.php/apps/spreed/api/v1/room/' . $room->getRemoteToken() . '/avatar' . ($darkTheme ? '/dark' : ''),
);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2024 Joas Schilling <coding@schilljs.com>
*
* @author 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\Middleware\Attribute;

use Attribute;
use OCA\Talk\Middleware\InjectionMiddleware;

/**
* @see InjectionMiddleware::getRoomByInvite()
*/
#[Attribute(Attribute::TARGET_METHOD)]
class AllowWithoutParticipantWhenPendingInvitation extends RequireRoom {
}
41 changes: 41 additions & 0 deletions lib/Middleware/InjectionMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use OCA\Talk\Exceptions\RoomNotFoundException;
use OCA\Talk\Federation\Authenticator;
use OCA\Talk\Manager;
use OCA\Talk\Middleware\Attribute\AllowWithoutParticipantWhenPendingInvitation;
use OCA\Talk\Middleware\Attribute\FederationSupported;
use OCA\Talk\Middleware\Attribute\RequireAuthenticatedParticipant;
use OCA\Talk\Middleware\Attribute\RequireLoggedInModeratorParticipant;
Expand All @@ -46,12 +47,14 @@
use OCA\Talk\Middleware\Exceptions\NotAModeratorException;
use OCA\Talk\Middleware\Exceptions\ReadOnlyException;
use OCA\Talk\Model\Attendee;
use OCA\Talk\Model\InvitationMapper;
use OCA\Talk\Participant;
use OCA\Talk\Room;
use OCA\Talk\Service\ParticipantService;
use OCA\Talk\TalkSession;
use OCA\Talk\Webinary;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\BruteForceProtection;
use OCP\AppFramework\Http\RedirectResponse;
Expand All @@ -74,6 +77,7 @@ public function __construct(
protected ICloudIdManager $cloudIdManager,
protected IThrottler $throttler,
protected IURLGenerator $url,
protected InvitationMapper $invitationMapper,
protected Authenticator $federationAuthenticator,
protected ?string $userId,
) {
Expand All @@ -98,6 +102,15 @@ public function beforeController(Controller $controller, string $methodName): vo
$apiVersion = $this->request->getParam('apiVersion');
$controller->setAPIVersion((int) substr($apiVersion, 1));

if (!empty($reflectionMethod->getAttributes(AllowWithoutParticipantWhenPendingInvitation::class))) {
try {
$this->getRoomByInvite($controller);
return;
} catch (RoomNotFoundException|ParticipantNotFoundException) {
// Falling back to bellow checks
}
}

if (!empty($reflectionMethod->getAttributes(RequireAuthenticatedParticipant::class))) {
$this->getLoggedInOrGuest($controller, false, requireFederationWhenNotLoggedIn: true);
}
Expand Down Expand Up @@ -232,6 +245,34 @@ protected function getLoggedInOrGuest(AEnvironmentAwareController $controller, b
}
}

/**
* @param AEnvironmentAwareController $controller
* @throws RoomNotFoundException
* @throws ParticipantNotFoundException
*/
protected function getRoomByInvite(AEnvironmentAwareController $controller): void {
if ($this->userId === null) {
throw new ParticipantNotFoundException('No user available');
}

$room = $controller->getRoom();
if (!$room instanceof Room) {
$token = $this->request->getParam('token');
$room = $this->manager->getRoomByToken($token);
$controller->setRoom($room);
}

$participant = $controller->getParticipant();
if (!$participant instanceof Participant) {
try {
$invitation = $this->invitationMapper->getInvitationsForUserByLocalRoom($room, $this->userId);
$controller->setInvitation($invitation);
} catch (DoesNotExistException $e) {
throw new ParticipantNotFoundException('No invite available', $e->getCode(), $e);
}
}
}

/**
* @param AEnvironmentAwareController $controller
* @throws FederationUnsupportedFeatureException
Expand Down
3 changes: 1 addition & 2 deletions lib/Model/Invitation.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,13 @@ public function __construct() {
}

/**
* @return array{id: int, localRoomId: int, localCloudId: string, remoteAttendeeId: int, remoteServerUrl: string, remoteToken: string, state: int, userId: string, inviterCloudId: string, inviterDisplayName: string}
* @return array{id: int, localCloudId: string, remoteAttendeeId: int, remoteServerUrl: string, remoteToken: string, state: int, userId: string, inviterCloudId: string, inviterDisplayName: string}
*/
public function jsonSerialize(): array {
return [
'id' => $this->getId(),
'userId' => $this->getUserId(),
'state' => $this->getState(),
'localRoomId' => $this->getLocalRoomId(),
'localCloudId' => $this->getLocalCloudId(),
'remoteServerUrl' => $this->getRemoteServerUrl(),
'remoteToken' => $this->getRemoteToken(),
Expand Down
14 changes: 14 additions & 0 deletions lib/Model/InvitationMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,20 @@ public function getInvitationsForUser(IUser $user): array {
return $this->findEntities($qb);
}

/**
* @throws DoesNotExistException
*/
public function getInvitationsForUserByLocalRoom(Room $room, string $userId): Invitation {
$query = $this->db->getQueryBuilder();

$query->select('*')
->from($this->getTableName())
->where($query->expr()->eq('user_id', $query->createNamedParameter($userId)))
->andWhere($query->expr()->eq('local_room_id', $query->createNamedParameter($room->getId())));

return $this->findEntity($query);
}

public function countInvitationsForLocalRoom(Room $room): int {
$qb = $this->db->getQueryBuilder();

Expand Down
2 changes: 1 addition & 1 deletion lib/ResponseDefinitions.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
* id: int,
* state: int,
* localCloudId: string,
* localRoomId: int,
* localToken: string,
* remoteAttendeeId: int,
* remoteServerUrl: string,
* remoteToken: string,
Expand Down
7 changes: 3 additions & 4 deletions openapi-federation.json
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@
"id",
"state",
"localCloudId",
"localRoomId",
"localToken",
"remoteAttendeeId",
"remoteServerUrl",
"remoteToken",
Expand All @@ -337,9 +337,8 @@
"localCloudId": {
"type": "string"
},
"localRoomId": {
"type": "integer",
"format": "int64"
"localToken": {
"type": "string"
},
"remoteAttendeeId": {
"type": "integer",
Expand Down
7 changes: 3 additions & 4 deletions openapi-full.json
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@
"id",
"state",
"localCloudId",
"localRoomId",
"localToken",
"remoteAttendeeId",
"remoteServerUrl",
"remoteToken",
Expand All @@ -538,9 +538,8 @@
"localCloudId": {
"type": "string"
},
"localRoomId": {
"type": "integer",
"format": "int64"
"localToken": {
"type": "string"
},
"remoteAttendeeId": {
"type": "integer",
Expand Down
2 changes: 1 addition & 1 deletion src/components/ConversationIcon.vue
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export default {
if (this.item.isDummyConversation) {
// Prevent a 404 when trying to load an avatar before the conversation data is actually loaded
// Also used in new conversation / invitation handler dialog
const isFed = this.item.isFederatedConversation && 'icon-conversation-federation'
const isFed = this.item.remoteServer && 'icon-conversation-federation'
const type = this.item.type === CONVERSATION.TYPE.PUBLIC ? 'icon-conversation-public' : 'icon-conversation-group'
const theme = isDarkTheme ? 'dark' : 'bright'
return `${isFed || type} icon--dummy icon--${theme}`
Expand Down
Loading
Loading