mirror of
https://github.com/nextcloud/spreed.git
synced 2025-12-18 05:20:50 +01:00
Split the API controller into 2
Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
parent
4836abdfef
commit
b274441c61
7 changed files with 408 additions and 337 deletions
|
|
@ -55,7 +55,7 @@ And in the works for the [coming versions](https://github.com/nextcloud/spreed/m
|
|||
<navigations>
|
||||
<navigation>
|
||||
<name>Video calls</name>
|
||||
<route>spreed.page.index</route>
|
||||
<route>spreed.Page.index</route>
|
||||
<order>3</order>
|
||||
</navigation>
|
||||
</navigations>
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@
|
|||
return [
|
||||
'routes' => [
|
||||
[
|
||||
'name' => 'page#index',
|
||||
'name' => 'Page#index',
|
||||
'url' => '/',
|
||||
'verb' => 'GET',
|
||||
],
|
||||
|
|
@ -46,25 +46,13 @@ return [
|
|||
],
|
||||
'ocs' => [
|
||||
[
|
||||
'name' => 'api#getRooms',
|
||||
'name' => 'Call#getRooms',
|
||||
'url' => '/api/{apiVersion}/room',
|
||||
'verb' => 'GET',
|
||||
'requirements' => ['apiVersion' => 'v1'],
|
||||
],
|
||||
[
|
||||
'name' => 'api#makePublic',
|
||||
'url' => '/api/{apiVersion}/room/public',
|
||||
'verb' => 'POST',
|
||||
'requirements' => ['apiVersion' => 'v1'],
|
||||
],
|
||||
[
|
||||
'name' => 'api#makePrivate',
|
||||
'url' => '/api/{apiVersion}/room/public',
|
||||
'verb' => 'DELETE',
|
||||
'requirements' => ['apiVersion' => 'v1'],
|
||||
],
|
||||
[
|
||||
'name' => 'api#getRoom',
|
||||
'name' => 'Call#getRoom',
|
||||
'url' => '/api/{apiVersion}/room/{token}',
|
||||
'verb' => 'GET',
|
||||
'requirements' => [
|
||||
|
|
@ -73,34 +61,7 @@ return [
|
|||
],
|
||||
],
|
||||
[
|
||||
'name' => 'api#renameRoom',
|
||||
'url' => '/api/{apiVersion}/room/{roomId}',
|
||||
'verb' => 'PUT',
|
||||
'requirements' => [
|
||||
'apiVersion' => 'v1',
|
||||
'roomId' => '\d+'
|
||||
],
|
||||
],
|
||||
[
|
||||
'name' => 'api#addParticipantToRoom',
|
||||
'url' => '/api/{apiVersion}/room/{roomId}',
|
||||
'verb' => 'POST',
|
||||
'requirements' => [
|
||||
'apiVersion' => 'v1',
|
||||
'roomId' => '\d+'
|
||||
],
|
||||
],
|
||||
[
|
||||
'name' => 'api#leaveRoom',
|
||||
'url' => '/api/{apiVersion}/room/{roomId}',
|
||||
'verb' => 'DELETE',
|
||||
'requirements' => [
|
||||
'apiVersion' => 'v1',
|
||||
'roomId' => '\d+'
|
||||
],
|
||||
],
|
||||
[
|
||||
'name' => 'api#getPeersInRoom',
|
||||
'name' => 'Call#getPeersInRoom',
|
||||
'url' => '/api/{apiVersion}/room/{token}/peers',
|
||||
'verb' => 'GET',
|
||||
'requirements' => [
|
||||
|
|
@ -109,7 +70,7 @@ return [
|
|||
],
|
||||
],
|
||||
[
|
||||
'name' => 'api#joinRoom',
|
||||
'name' => 'Call#joinRoom',
|
||||
'url' => '/api/{apiVersion}/room/{token}/join',
|
||||
'verb' => 'POST',
|
||||
'requirements' => [
|
||||
|
|
@ -118,7 +79,7 @@ return [
|
|||
],
|
||||
],
|
||||
[
|
||||
'name' => 'api#ping',
|
||||
'name' => 'Call#ping',
|
||||
'url' => '/api/{apiVersion}/ping',
|
||||
'verb' => 'POST',
|
||||
'requirements' => [
|
||||
|
|
@ -127,29 +88,69 @@ return [
|
|||
],
|
||||
],
|
||||
[
|
||||
'name' => 'api#leave',
|
||||
'name' => 'Call#leave',
|
||||
'url' => '/api/{apiVersion}/leave',
|
||||
'verb' => 'DELETE',
|
||||
'requirements' => ['apiVersion' => 'v1'],
|
||||
],
|
||||
|
||||
[
|
||||
'name' => 'api#createOneToOneRoom',
|
||||
'name' => 'Room#createOneToOneRoom',
|
||||
'url' => '/api/{apiVersion}/oneToOne',
|
||||
'verb' => 'PUT',
|
||||
'requirements' => ['apiVersion' => 'v1'],
|
||||
],
|
||||
[
|
||||
'name' => 'api#createGroupRoom',
|
||||
'name' => 'Room#createGroupRoom',
|
||||
'url' => '/api/{apiVersion}/group',
|
||||
'verb' => 'PUT',
|
||||
'requirements' => ['apiVersion' => 'v1'],
|
||||
],
|
||||
[
|
||||
'name' => 'api#createPublicRoom',
|
||||
'name' => 'Room#createPublicRoom',
|
||||
'url' => '/api/{apiVersion}/public',
|
||||
'verb' => 'PUT',
|
||||
'requirements' => ['apiVersion' => 'v1'],
|
||||
],
|
||||
[
|
||||
'name' => 'Room#renameRoom',
|
||||
'url' => '/api/{apiVersion}/room/{roomId}',
|
||||
'verb' => 'PUT',
|
||||
'requirements' => [
|
||||
'apiVersion' => 'v1',
|
||||
'roomId' => '\d+'
|
||||
],
|
||||
],
|
||||
[
|
||||
'name' => 'Room#makePublic',
|
||||
'url' => '/api/{apiVersion}/room/public',
|
||||
'verb' => 'POST',
|
||||
'requirements' => ['apiVersion' => 'v1'],
|
||||
],
|
||||
[
|
||||
'name' => 'Room#makePrivate',
|
||||
'url' => '/api/{apiVersion}/room/public',
|
||||
'verb' => 'DELETE',
|
||||
'requirements' => ['apiVersion' => 'v1'],
|
||||
],
|
||||
[
|
||||
'name' => 'Room#addParticipantToRoom',
|
||||
'url' => '/api/{apiVersion}/room/{roomId}',
|
||||
'verb' => 'POST',
|
||||
'requirements' => [
|
||||
'apiVersion' => 'v1',
|
||||
'roomId' => '\d+'
|
||||
],
|
||||
],
|
||||
[
|
||||
'name' => 'Room#leaveRoom',
|
||||
'url' => '/api/{apiVersion}/room/{roomId}',
|
||||
'verb' => 'DELETE',
|
||||
'requirements' => [
|
||||
'apiVersion' => 'v1',
|
||||
'roomId' => '\d+'
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
|
|
|
|||
|
|
@ -71,7 +71,7 @@ class CallProvider implements IProvider {
|
|||
}
|
||||
|
||||
$iconUrl = $this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'actions/video.svg'));
|
||||
$callUrl = $this->urlGenerator->linkToRouteAbsolute('spreed.page.index') . '?callUser=' . $uid;
|
||||
$callUrl = $this->urlGenerator->linkToRouteAbsolute('spreed.Page.index') . '?callUser=' . $uid;
|
||||
$action = $this->actionFactory->newLinkAction($iconUrl, $this->l10n->t('Video call'), $callUrl);
|
||||
$entry->addAction($action);
|
||||
}
|
||||
|
|
|
|||
346
lib/Controller/CallController.php
Normal file
346
lib/Controller/CallController.php
Normal file
|
|
@ -0,0 +1,346 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2016 Lukas Reschke <lukas@statuscode.ch>
|
||||
* @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
|
||||
*
|
||||
* @author Lukas Reschke <lukas@statuscode.ch>
|
||||
* @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\Spreed\Controller;
|
||||
|
||||
use OCA\Spreed\Exceptions\RoomNotFoundException;
|
||||
use OCA\Spreed\Manager;
|
||||
use OCA\Spreed\Room;
|
||||
use OCP\AppFramework\Http;
|
||||
use OCP\AppFramework\Http\DataResponse;
|
||||
use OCP\AppFramework\OCSController;
|
||||
use OCP\IL10N;
|
||||
use OCP\ILogger;
|
||||
use OCP\IRequest;
|
||||
use OCP\ISession;
|
||||
use OCP\IUser;
|
||||
use OCP\IUserManager;
|
||||
|
||||
class CallController extends OCSController {
|
||||
/** @var string */
|
||||
private $userId;
|
||||
/** @var IL10N */
|
||||
private $l10n;
|
||||
/** @var IUserManager */
|
||||
private $userManager;
|
||||
/** @var ISession */
|
||||
private $session;
|
||||
/** @var ILogger */
|
||||
private $logger;
|
||||
/** @var Manager */
|
||||
private $manager;
|
||||
|
||||
/**
|
||||
* @param string $appName
|
||||
* @param string $UserId
|
||||
* @param IRequest $request
|
||||
* @param IL10N $l10n
|
||||
* @param IUserManager $userManager
|
||||
* @param ISession $session
|
||||
* @param ILogger $logger
|
||||
* @param Manager $manager
|
||||
*/
|
||||
public function __construct($appName,
|
||||
$UserId,
|
||||
IRequest $request,
|
||||
IL10N $l10n,
|
||||
IUserManager $userManager,
|
||||
ISession $session,
|
||||
ILogger $logger,
|
||||
Manager $manager) {
|
||||
parent::__construct($appName, $request);
|
||||
$this->userId = $UserId;
|
||||
$this->l10n = $l10n;
|
||||
$this->userManager = $userManager;
|
||||
$this->session = $session;
|
||||
$this->logger = $logger;
|
||||
$this->manager = $manager;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all currently existent rooms which the user has joined
|
||||
*
|
||||
* @NoAdminRequired
|
||||
*
|
||||
* @return DataResponse
|
||||
*/
|
||||
public function getRooms() {
|
||||
$rooms = $this->manager->getRoomsForParticipant($this->userId);
|
||||
|
||||
$return = [];
|
||||
foreach ($rooms as $room) {
|
||||
try {
|
||||
$return[] = $this->formatRoom($room);
|
||||
} catch (RoomNotFoundException $e) {
|
||||
}
|
||||
}
|
||||
|
||||
return new DataResponse($return);
|
||||
}
|
||||
|
||||
/**
|
||||
* @PublicPage
|
||||
*
|
||||
* @param string $token
|
||||
* @return DataResponse
|
||||
*/
|
||||
public function getRoom($token) {
|
||||
try {
|
||||
$room = $this->manager->getRoomForParticipantByToken($token, $this->userId);
|
||||
return new DataResponse($this->formatRoom($room));
|
||||
} catch (RoomNotFoundException $e) {
|
||||
return new DataResponse([], Http::STATUS_NOT_FOUND);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Room $room
|
||||
* @return array
|
||||
* @throws RoomNotFoundException
|
||||
*/
|
||||
protected function formatRoom(Room $room) {
|
||||
// Sort by lastPing
|
||||
/** @var array[] $participants */
|
||||
$participants = $room->getParticipants();
|
||||
$sortParticipants = function(array $participant1, array $participant2) {
|
||||
if ($participant1['lastPing'] === $participant2['lastPing']) {
|
||||
return 0;
|
||||
}
|
||||
return ($participant1['lastPing'] > $participant2['lastPing']) ? -1 : 1;
|
||||
};
|
||||
uasort($participants['users'], $sortParticipants);
|
||||
uasort($participants['guests'], $sortParticipants);
|
||||
|
||||
$participantList = [];
|
||||
foreach ($participants['users'] as $participant => $lastPing) {
|
||||
$user = $this->userManager->get($participant);
|
||||
if ($user instanceof IUser) {
|
||||
$participantList[$participant] = $user->getDisplayName();
|
||||
}
|
||||
}
|
||||
|
||||
$roomData = [
|
||||
'id' => $room->getId(),
|
||||
'token' => $room->getToken(),
|
||||
'type' => $room->getType(),
|
||||
'name' => $room->getName(),
|
||||
'displayName' => $room->getName(),
|
||||
'isNameEditable' => $room->getType() !== Room::ONE_TO_ONE_CALL,
|
||||
'count' => $room->getNumberOfParticipants(time() - 30),
|
||||
'lastPing' => isset($participants['users'][$this->userId]['lastPing']) ? $participants['users'][$this->userId]['lastPing'] : 0,
|
||||
'sessionId' => isset($participants['users'][$this->userId]['sessionId']) ? $participants['users'][$this->userId]['sessionId'] : '0',
|
||||
'participants' => $participantList,
|
||||
];
|
||||
|
||||
$activeGuests = array_filter($participants['guests'], function($data) {
|
||||
return $data['lastPing'] > time() - 30;
|
||||
});
|
||||
|
||||
$numActiveGuests = count($activeGuests);
|
||||
if ($numActiveGuests !== count($participants['guests'])) {
|
||||
$room->cleanGuestParticipants();
|
||||
}
|
||||
|
||||
if ($this->userId !== null) {
|
||||
unset($participantList[$this->userId]);
|
||||
$numOtherParticipants = count($participantList);
|
||||
$numGuestParticipants = $numActiveGuests;
|
||||
} else {
|
||||
$numOtherParticipants = count($participantList);
|
||||
$numGuestParticipants = $numActiveGuests - 1;
|
||||
}
|
||||
|
||||
$guestString = '';
|
||||
switch ($room->getType()) {
|
||||
case Room::ONE_TO_ONE_CALL:
|
||||
// As name of the room use the name of the other person participating
|
||||
if ($numOtherParticipants === 1) {
|
||||
// Only one other participant
|
||||
reset($participantList);
|
||||
$roomData['name'] = key($participantList);
|
||||
$roomData['displayName'] = $participantList[$roomData['name']];
|
||||
} else {
|
||||
// Invalid user count, there must be exactly 2 users in each one2one room
|
||||
$this->logger->warning('one2one room found with invalid participant count. Leaving room for everyone', [
|
||||
'app' => 'spreed',
|
||||
]);
|
||||
$room->deleteRoom();
|
||||
}
|
||||
break;
|
||||
|
||||
/** @noinspection PhpMissingBreakStatementInspection */
|
||||
case Room::PUBLIC_CALL:
|
||||
if ($this->userId === null && $numGuestParticipants) {
|
||||
$guestString = $this->l10n->n('%n other guest', '%n other guests', $numGuestParticipants);
|
||||
} else if ($numGuestParticipants) {
|
||||
$guestString = $this->l10n->n('%n guest', '%n guests', $numGuestParticipants);
|
||||
}
|
||||
|
||||
// no break;
|
||||
|
||||
case Room::GROUP_CALL:
|
||||
if ($room->getName() === '') {
|
||||
// As name of the room use the names of the other participants
|
||||
if ($this->userId === null) {
|
||||
$participantList[] = $this->l10n->t('You');
|
||||
} else if ($numOtherParticipants === 0) {
|
||||
$participantList = [$this->l10n->t('You')];
|
||||
}
|
||||
|
||||
if ($guestString !== '') {
|
||||
$participantList[] = $guestString;
|
||||
}
|
||||
|
||||
$roomData['displayName'] = implode($this->l10n->t(', '), $participantList);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
// Invalid room type
|
||||
$this->logger->warning('Invalid room type found. Leaving room for everyone', [
|
||||
'app' => 'spreed',
|
||||
]);
|
||||
$room->deleteRoom();
|
||||
throw new RoomNotFoundException();
|
||||
}
|
||||
|
||||
$roomData['guestList'] = $guestString;
|
||||
|
||||
return $roomData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @PublicPage
|
||||
*
|
||||
* @param string $token
|
||||
* @return DataResponse
|
||||
*/
|
||||
public function getPeersInRoom($token) {
|
||||
try {
|
||||
$room = $this->manager->getRoomForParticipantByToken($token, $this->userId);
|
||||
} catch (RoomNotFoundException $e) {
|
||||
return new DataResponse([], Http::STATUS_NOT_FOUND);
|
||||
}
|
||||
|
||||
/** @var array[] $participants */
|
||||
$participants = $room->getParticipants(time() - 30);
|
||||
$result = [];
|
||||
foreach ($participants['users'] as $participant => $data) {
|
||||
if ($data['sessionId'] === '0') {
|
||||
// User left the room
|
||||
continue;
|
||||
}
|
||||
|
||||
$result[] = [
|
||||
'userId' => $participant,
|
||||
'token' => $token,
|
||||
'lastPing' => $data['lastPing'],
|
||||
'sessionId' => $data['sessionId'],
|
||||
];
|
||||
}
|
||||
|
||||
foreach ($participants['guests'] as $data) {
|
||||
$result[] = [
|
||||
'userId' => '',
|
||||
'token' => $token,
|
||||
'lastPing' => $data['lastPing'],
|
||||
'sessionId' => $data['sessionId'],
|
||||
];
|
||||
}
|
||||
|
||||
return new DataResponse($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @PublicPage
|
||||
*
|
||||
* @param string $token
|
||||
* @return DataResponse
|
||||
*/
|
||||
public function ping($token) {
|
||||
try {
|
||||
$room = $this->manager->getRoomForParticipantByToken($token, $this->userId);
|
||||
} catch (RoomNotFoundException $e) {
|
||||
return new DataResponse([], Http::STATUS_NOT_FOUND);
|
||||
}
|
||||
|
||||
$sessionId = $this->session->get('spreed-session');
|
||||
$room->ping($this->userId, $sessionId, time());
|
||||
|
||||
return new DataResponse();
|
||||
}
|
||||
|
||||
/**
|
||||
* @PublicPage
|
||||
* @UseSession
|
||||
*
|
||||
* @param string $token
|
||||
* @return DataResponse
|
||||
*/
|
||||
public function joinRoom($token) {
|
||||
try {
|
||||
$room = $this->manager->getRoomForParticipantByToken($token, $this->userId);
|
||||
} catch (RoomNotFoundException $e) {
|
||||
return new DataResponse([], Http::STATUS_NOT_FOUND);
|
||||
}
|
||||
|
||||
if ($this->userId !== null) {
|
||||
$sessionIds = $this->manager->getSessionIdsForUser($this->userId);
|
||||
$newSessionId = $room->enterRoomAsUser($this->userId);
|
||||
|
||||
if (!empty($sessionIds)) {
|
||||
$this->manager->deleteMessagesForSessionIds($sessionIds);
|
||||
}
|
||||
} else {
|
||||
$newSessionId = $room->enterRoomAsGuest();
|
||||
}
|
||||
|
||||
$this->session->set('spreed-session', $newSessionId);
|
||||
$room->ping($this->userId, $newSessionId, time());
|
||||
|
||||
return new DataResponse([
|
||||
'sessionId' => $newSessionId,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @PublicPage
|
||||
* @UseSession
|
||||
*
|
||||
* @return DataResponse
|
||||
*/
|
||||
public function leave() {
|
||||
if ($this->userId !== null) {
|
||||
$this->manager->disconnectUserFromAllRooms($this->userId);
|
||||
} else {
|
||||
$sessionId = $this->session->get('spreed-session');
|
||||
$this->manager->removeSessionFromAllRooms($sessionId);
|
||||
}
|
||||
|
||||
$this->session->remove('spreed-session');
|
||||
return new DataResponse();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -42,7 +42,7 @@ use OCP\Security\ISecureRandom;
|
|||
class PageController extends Controller {
|
||||
/** @var string */
|
||||
private $userId;
|
||||
/** @var ApiController */
|
||||
/** @var ManagementController */
|
||||
private $api;
|
||||
/** @var IL10N */
|
||||
private $l10n;
|
||||
|
|
@ -60,7 +60,7 @@ class PageController extends Controller {
|
|||
/**
|
||||
* @param string $appName
|
||||
* @param IRequest $request
|
||||
* @param ApiController $api
|
||||
* @param ManagementController $api
|
||||
* @param string $UserId
|
||||
* @param IL10N $l10n
|
||||
* @param ILogger $logger
|
||||
|
|
@ -71,7 +71,7 @@ class PageController extends Controller {
|
|||
*/
|
||||
public function __construct($appName,
|
||||
IRequest $request,
|
||||
ApiController $api,
|
||||
ManagementController $api,
|
||||
$UserId,
|
||||
IL10N $l10n,
|
||||
ILogger $logger,
|
||||
|
|
@ -132,7 +132,7 @@ class PageController extends Controller {
|
|||
$response = $this->api->createOneToOneRoom($callUser);
|
||||
if ($response->getStatus() !== Http::STATUS_NOT_FOUND) {
|
||||
$data = $response->getData();
|
||||
return new RedirectResponse($this->url->linkToRoute('spreed.pagecontroller.showCall', ['token' => $data['token']]));
|
||||
return new RedirectResponse($this->url->linkToRoute('spreed.Page.showCall', ['token' => $data['token']]));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -161,7 +161,7 @@ class PageController extends Controller {
|
|||
}
|
||||
} catch (RoomNotFoundException $e) {
|
||||
return new RedirectResponse($this->url->linkToRoute('core.login.showLoginForm', [
|
||||
'redirect_url' => $this->url->linkToRoute('spreed.page.index', ['token' => $token]),
|
||||
'redirect_url' => $this->url->linkToRoute('spreed.Page.index', ['token' => $token]),
|
||||
]));
|
||||
}
|
||||
|
||||
|
|
@ -190,13 +190,13 @@ class PageController extends Controller {
|
|||
if ($room->getType() !== Room::PUBLIC_CALL) {
|
||||
throw new RoomNotFoundException();
|
||||
}
|
||||
return new RedirectResponse($this->url->linkToRoute('spreed.page.index', ['token' => $token]));
|
||||
return new RedirectResponse($this->url->linkToRoute('spreed.Page.index', ['token' => $token]));
|
||||
} catch (RoomNotFoundException $e) {
|
||||
return new RedirectResponse($this->url->linkToRoute('core.login.showLoginForm', [
|
||||
'redirect_url' => $this->url->linkToRoute('spreed.page.index', ['token' => $token]),
|
||||
'redirect_url' => $this->url->linkToRoute('spreed.Page.index', ['token' => $token]),
|
||||
]));
|
||||
}
|
||||
}
|
||||
return new RedirectResponse($this->url->linkToRoute('spreed.page.index', ['token' => $token]));
|
||||
return new RedirectResponse($this->url->linkToRoute('spreed.Page.index', ['token' => $token]));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -32,27 +32,21 @@ use OCP\Activity\IManager as IActivityManager;
|
|||
use OCP\AppFramework\Http;
|
||||
use OCP\AppFramework\Http\DataResponse;
|
||||
use OCP\AppFramework\OCSController;
|
||||
use OCP\IL10N;
|
||||
use OCP\ILogger;
|
||||
use OCP\IRequest;
|
||||
use OCP\ISession;
|
||||
use OCP\IUser;
|
||||
use OCP\IUserManager;
|
||||
use OCP\IGroup;
|
||||
use OCP\IGroupManager;
|
||||
use OCP\Notification\IManager as INotificationManager;
|
||||
|
||||
class ApiController extends OCSController {
|
||||
class RoomController extends OCSController {
|
||||
/** @var string */
|
||||
private $userId;
|
||||
/** @var IL10N */
|
||||
private $l10n;
|
||||
/** @var IUserManager */
|
||||
private $userManager;
|
||||
/** @var IGroupManager */
|
||||
private $groupManager;
|
||||
/** @var ISession */
|
||||
private $session;
|
||||
/** @var ILogger */
|
||||
private $logger;
|
||||
/** @var Manager */
|
||||
|
|
@ -66,10 +60,8 @@ class ApiController extends OCSController {
|
|||
* @param string $appName
|
||||
* @param string $UserId
|
||||
* @param IRequest $request
|
||||
* @param IL10N $l10n
|
||||
* @param IUserManager $userManager
|
||||
* @param IGroupManager $groupManager
|
||||
* @param ISession $session
|
||||
* @param ILogger $logger
|
||||
* @param Manager $manager
|
||||
* @param INotificationManager $notificationManager
|
||||
|
|
@ -78,220 +70,22 @@ class ApiController extends OCSController {
|
|||
public function __construct($appName,
|
||||
$UserId,
|
||||
IRequest $request,
|
||||
IL10N $l10n,
|
||||
IUserManager $userManager,
|
||||
IGroupManager $groupManager,
|
||||
ISession $session,
|
||||
ILogger $logger,
|
||||
Manager $manager,
|
||||
INotificationManager $notificationManager,
|
||||
IActivityManager $activityManager) {
|
||||
parent::__construct($appName, $request);
|
||||
$this->userId = $UserId;
|
||||
$this->l10n = $l10n;
|
||||
$this->userManager = $userManager;
|
||||
$this->groupManager = $groupManager;
|
||||
$this->session = $session;
|
||||
$this->logger = $logger;
|
||||
$this->manager = $manager;
|
||||
$this->notificationManager = $notificationManager;
|
||||
$this->activityManager = $activityManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all currently existent rooms which the user has joined
|
||||
*
|
||||
* @NoAdminRequired
|
||||
*
|
||||
* @return DataResponse
|
||||
*/
|
||||
public function getRooms() {
|
||||
$rooms = $this->manager->getRoomsForParticipant($this->userId);
|
||||
|
||||
$return = [];
|
||||
foreach ($rooms as $room) {
|
||||
try {
|
||||
$return[] = $this->formatRoom($room);
|
||||
} catch (RoomNotFoundException $e) {
|
||||
}
|
||||
}
|
||||
|
||||
return new DataResponse($return);
|
||||
}
|
||||
|
||||
/**
|
||||
* @PublicPage
|
||||
*
|
||||
* @param string $token
|
||||
* @return DataResponse
|
||||
*/
|
||||
public function getRoom($token) {
|
||||
try {
|
||||
$room = $this->manager->getRoomForParticipantByToken($token, $this->userId);
|
||||
return new DataResponse($this->formatRoom($room));
|
||||
} catch (RoomNotFoundException $e) {
|
||||
return new DataResponse([], Http::STATUS_NOT_FOUND);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Room $room
|
||||
* @return array
|
||||
* @throws RoomNotFoundException
|
||||
*/
|
||||
protected function formatRoom(Room $room) {
|
||||
// Sort by lastPing
|
||||
/** @var array[] $participants */
|
||||
$participants = $room->getParticipants();
|
||||
$sortParticipants = function(array $participant1, array $participant2) {
|
||||
if ($participant1['lastPing'] === $participant2['lastPing']) {
|
||||
return 0;
|
||||
}
|
||||
return ($participant1['lastPing'] > $participant2['lastPing']) ? -1 : 1;
|
||||
};
|
||||
uasort($participants['users'], $sortParticipants);
|
||||
uasort($participants['guests'], $sortParticipants);
|
||||
|
||||
$participantList = [];
|
||||
foreach ($participants['users'] as $participant => $lastPing) {
|
||||
$user = $this->userManager->get($participant);
|
||||
if ($user instanceof IUser) {
|
||||
$participantList[$participant] = $user->getDisplayName();
|
||||
}
|
||||
}
|
||||
|
||||
$roomData = [
|
||||
'id' => $room->getId(),
|
||||
'token' => $room->getToken(),
|
||||
'type' => $room->getType(),
|
||||
'name' => $room->getName(),
|
||||
'displayName' => $room->getName(),
|
||||
'isNameEditable' => $room->getType() !== Room::ONE_TO_ONE_CALL,
|
||||
'count' => $room->getNumberOfParticipants(time() - 30),
|
||||
'lastPing' => isset($participants['users'][$this->userId]['lastPing']) ? $participants['users'][$this->userId]['lastPing'] : 0,
|
||||
'sessionId' => isset($participants['users'][$this->userId]['sessionId']) ? $participants['users'][$this->userId]['sessionId'] : '0',
|
||||
'participants' => $participantList,
|
||||
];
|
||||
|
||||
$activeGuests = array_filter($participants['guests'], function($data) {
|
||||
return $data['lastPing'] > time() - 30;
|
||||
});
|
||||
|
||||
$numActiveGuests = count($activeGuests);
|
||||
if ($numActiveGuests !== count($participants['guests'])) {
|
||||
$room->cleanGuestParticipants();
|
||||
}
|
||||
|
||||
if ($this->userId !== null) {
|
||||
unset($participantList[$this->userId]);
|
||||
$numOtherParticipants = count($participantList);
|
||||
$numGuestParticipants = $numActiveGuests;
|
||||
} else {
|
||||
$numOtherParticipants = count($participantList);
|
||||
$numGuestParticipants = $numActiveGuests - 1;
|
||||
}
|
||||
|
||||
$guestString = '';
|
||||
switch ($room->getType()) {
|
||||
case Room::ONE_TO_ONE_CALL:
|
||||
// As name of the room use the name of the other person participating
|
||||
if ($numOtherParticipants === 1) {
|
||||
// Only one other participant
|
||||
reset($participantList);
|
||||
$roomData['name'] = key($participantList);
|
||||
$roomData['displayName'] = $participantList[$roomData['name']];
|
||||
} else {
|
||||
// Invalid user count, there must be exactly 2 users in each one2one room
|
||||
$this->logger->warning('one2one room found with invalid participant count. Leaving room for everyone', [
|
||||
'app' => 'spreed',
|
||||
]);
|
||||
$room->deleteRoom();
|
||||
}
|
||||
break;
|
||||
|
||||
/** @noinspection PhpMissingBreakStatementInspection */
|
||||
case Room::PUBLIC_CALL:
|
||||
if ($this->userId === null && $numGuestParticipants) {
|
||||
$guestString = $this->l10n->n('%n other guest', '%n other guests', $numGuestParticipants);
|
||||
} else if ($numGuestParticipants) {
|
||||
$guestString = $this->l10n->n('%n guest', '%n guests', $numGuestParticipants);
|
||||
}
|
||||
|
||||
// no break;
|
||||
|
||||
case Room::GROUP_CALL:
|
||||
if ($room->getName() === '') {
|
||||
// As name of the room use the names of the other participants
|
||||
if ($this->userId === null) {
|
||||
$participantList[] = $this->l10n->t('You');
|
||||
} else if ($numOtherParticipants === 0) {
|
||||
$participantList = [$this->l10n->t('You')];
|
||||
}
|
||||
|
||||
if ($guestString !== '') {
|
||||
$participantList[] = $guestString;
|
||||
}
|
||||
|
||||
$roomData['displayName'] = implode($this->l10n->t(', '), $participantList);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
// Invalid room type
|
||||
$this->logger->warning('Invalid room type found. Leaving room for everyone', [
|
||||
'app' => 'spreed',
|
||||
]);
|
||||
$room->deleteRoom();
|
||||
throw new RoomNotFoundException();
|
||||
}
|
||||
|
||||
$roomData['guestList'] = $guestString;
|
||||
|
||||
return $roomData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @PublicPage
|
||||
*
|
||||
* @param string $token
|
||||
* @return DataResponse
|
||||
*/
|
||||
public function getPeersInRoom($token) {
|
||||
try {
|
||||
$room = $this->manager->getRoomForParticipantByToken($token, $this->userId);
|
||||
} catch (RoomNotFoundException $e) {
|
||||
return new DataResponse([], Http::STATUS_NOT_FOUND);
|
||||
}
|
||||
|
||||
/** @var array[] $participants */
|
||||
$participants = $room->getParticipants(time() - 30);
|
||||
$result = [];
|
||||
foreach ($participants['users'] as $participant => $data) {
|
||||
if ($data['sessionId'] === '0') {
|
||||
// User left the room
|
||||
continue;
|
||||
}
|
||||
|
||||
$result[] = [
|
||||
'userId' => $participant,
|
||||
'token' => $token,
|
||||
'lastPing' => $data['lastPing'],
|
||||
'sessionId' => $data['sessionId'],
|
||||
];
|
||||
}
|
||||
|
||||
foreach ($participants['guests'] as $data) {
|
||||
$result[] = [
|
||||
'userId' => '',
|
||||
'token' => $token,
|
||||
'lastPing' => $data['lastPing'],
|
||||
'sessionId' => $data['sessionId'],
|
||||
];
|
||||
}
|
||||
|
||||
return new DataResponse($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initiates a one-to-one video call from the current user to the recipient
|
||||
*
|
||||
|
|
@ -501,76 +295,6 @@ class ApiController extends OCSController {
|
|||
return new DataResponse();
|
||||
}
|
||||
|
||||
/**
|
||||
* @PublicPage
|
||||
*
|
||||
* @param string $token
|
||||
* @return DataResponse
|
||||
*/
|
||||
public function ping($token) {
|
||||
try {
|
||||
$room = $this->manager->getRoomForParticipantByToken($token, $this->userId);
|
||||
} catch (RoomNotFoundException $e) {
|
||||
return new DataResponse([], Http::STATUS_NOT_FOUND);
|
||||
}
|
||||
|
||||
$sessionId = $this->session->get('spreed-session');
|
||||
$room->ping($this->userId, $sessionId, time());
|
||||
|
||||
return new DataResponse();
|
||||
}
|
||||
|
||||
/**
|
||||
* @PublicPage
|
||||
* @UseSession
|
||||
*
|
||||
* @param string $token
|
||||
* @return DataResponse
|
||||
*/
|
||||
public function joinRoom($token) {
|
||||
try {
|
||||
$room = $this->manager->getRoomForParticipantByToken($token, $this->userId);
|
||||
} catch (RoomNotFoundException $e) {
|
||||
return new DataResponse([], Http::STATUS_NOT_FOUND);
|
||||
}
|
||||
|
||||
if ($this->userId !== null) {
|
||||
$sessionIds = $this->manager->getSessionIdsForUser($this->userId);
|
||||
$newSessionId = $room->enterRoomAsUser($this->userId);
|
||||
|
||||
if (!empty($sessionIds)) {
|
||||
$this->manager->deleteMessagesForSessionIds($sessionIds);
|
||||
}
|
||||
} else {
|
||||
$newSessionId = $room->enterRoomAsGuest();
|
||||
}
|
||||
|
||||
$this->session->set('spreed-session', $newSessionId);
|
||||
$room->ping($this->userId, $newSessionId, time());
|
||||
|
||||
return new DataResponse([
|
||||
'sessionId' => $newSessionId,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @PublicPage
|
||||
* @UseSession
|
||||
*
|
||||
* @return DataResponse
|
||||
*/
|
||||
public function leave() {
|
||||
if ($this->userId !== null) {
|
||||
$this->manager->disconnectUserFromAllRooms($this->userId);
|
||||
} else {
|
||||
$sessionId = $this->session->get('spreed-session');
|
||||
$this->manager->removeSessionFromAllRooms($sessionId);
|
||||
}
|
||||
|
||||
$this->session->remove('spreed-session');
|
||||
return new DataResponse();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param IUser $actor
|
||||
* @param IUser $user
|
||||
|
|
@ -92,7 +92,7 @@ class Notifier implements INotifier {
|
|||
$uid = $parameters[0];
|
||||
$notification
|
||||
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('spreed', 'app.svg')))
|
||||
->setLink($this->url->linkToRouteAbsolute('spreed.page.index') . '?token=' . $room->getToken());
|
||||
->setLink($this->url->linkToRouteAbsolute('spreed.Page.index') . '?token=' . $room->getToken());
|
||||
|
||||
if ($notification->getObjectType() === 'room') {
|
||||
$user = $this->userManager->get($uid);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue