basic notify_push usage with session handling (rebased)
Signed-off-by: chandi Langecker <git@chandi.it>
This commit is contained in:
@@ -82,6 +82,11 @@ return [
|
||||
['name' => 'label#update', 'url' => '/labels/{labelId}', 'verb' => 'PUT'],
|
||||
['name' => 'label#delete', 'url' => '/labels/{labelId}', 'verb' => 'DELETE'],
|
||||
|
||||
// sessions
|
||||
['name' => 'Session#create', 'url' => '/session/create', 'verb' => 'PUT'],
|
||||
['name' => 'Session#sync', 'url' => '/session/sync', 'verb' => 'POST'],
|
||||
['name' => 'Session#close', 'url' => '/session/close', 'verb' => 'POST'],
|
||||
|
||||
// api
|
||||
['name' => 'board_api#index', 'url' => '/api/v{apiVersion}/boards', 'verb' => 'GET'],
|
||||
['name' => 'board_api#get', 'url' => '/api/v{apiVersion}/boards/{boardId}', 'verb' => 'GET'],
|
||||
|
||||
@@ -40,6 +40,8 @@ use OCA\Deck\Event\AclUpdatedEvent;
|
||||
use OCA\Deck\Event\CardCreatedEvent;
|
||||
use OCA\Deck\Event\CardDeletedEvent;
|
||||
use OCA\Deck\Event\CardUpdatedEvent;
|
||||
use OCA\Deck\Event\SessionClosedEvent;
|
||||
use OCA\Deck\Event\SessionCreatedEvent;
|
||||
use OCA\Deck\Listeners\BeforeTemplateRenderedListener;
|
||||
use OCA\Deck\Listeners\ParticipantCleanupListener;
|
||||
use OCA\Deck\Listeners\FullTextSearchEventListener;
|
||||
@@ -51,8 +53,10 @@ use OCA\Deck\Reference\CardReferenceProvider;
|
||||
use OCA\Deck\Search\CardCommentProvider;
|
||||
use OCA\Deck\Search\DeckProvider;
|
||||
use OCA\Deck\Service\PermissionService;
|
||||
use OCA\Deck\Service\SessionService;
|
||||
use OCA\Deck\Sharing\DeckShareProvider;
|
||||
use OCA\Deck\Sharing\Listener;
|
||||
use OCA\NotifyPush\Queue\IQueue;
|
||||
use OCP\AppFramework\App;
|
||||
use OCP\AppFramework\Bootstrap\IBootContext;
|
||||
use OCP\AppFramework\Bootstrap\IBootstrap;
|
||||
@@ -96,6 +100,7 @@ class Application extends App implements IBootstrap {
|
||||
$context->injectFn(Closure::fromCallable([$this, 'registerCommentsEventHandler']));
|
||||
$context->injectFn(Closure::fromCallable([$this, 'registerNotifications']));
|
||||
$context->injectFn(Closure::fromCallable([$this, 'registerCollaborationResources']));
|
||||
$context->injectFn(Closure::fromCallable([$this, 'registerSessionListener']));
|
||||
|
||||
$context->injectFn(function (IManager $shareManager) {
|
||||
$shareManager->registerShareProvider(DeckShareProvider::class);
|
||||
@@ -188,4 +193,33 @@ class Application extends App implements IBootstrap {
|
||||
Util::addScript('deck', 'deck-collections');
|
||||
});
|
||||
}
|
||||
|
||||
protected function registerSessionListener(IEventDispatcher $eventDispatcher): void {
|
||||
$container = $this->getContainer();
|
||||
|
||||
try {
|
||||
$queue = $container->get(IQueue::class);
|
||||
} catch (\Exception $e) {
|
||||
// most likely notify_push is not installed.
|
||||
return;
|
||||
}
|
||||
|
||||
// if SessionService is injected via function parameters, tests throw following error:
|
||||
// "OCA\Deck\NoPermissionException: Creating boards has been disabled for your account."
|
||||
// doing it this way it somehow works
|
||||
$sessionService = $container->get(SessionService::class);
|
||||
|
||||
|
||||
$eventDispatcher->addListener(SessionCreatedEvent::class, function (SessionCreatedEvent $event) use ($sessionService, $queue) {
|
||||
$sessionService->notifyAllSessions($queue, $event->getBoardId(), "DeckBoardUpdate", $event->getUserId(), [
|
||||
"id" => $event->getBoardId()
|
||||
]);
|
||||
});
|
||||
|
||||
$eventDispatcher->addListener(SessionClosedEvent::class, function (SessionClosedEvent $event) use ($sessionService, $queue) {
|
||||
$sessionService->notifyAllSessions($queue, $event->getBoardId(), "DeckBoardUpdate", $event->getUserId(), [
|
||||
"id" => $event->getBoardId()
|
||||
]);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
88
lib/Controller/SessionController.php
Normal file
88
lib/Controller/SessionController.php
Normal file
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2022, chandi Langecker (git@chandi.it)
|
||||
*
|
||||
* @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\Deck\Controller;
|
||||
|
||||
use OCA\Deck\Service\SessionService;
|
||||
use OCA\Deck\Service\PermissionService;
|
||||
use OCA\Deck\Db\BoardMapper;
|
||||
use OCP\AppFramework\Db\DoesNotExistException;
|
||||
use OCP\AppFramework\Http\DataResponse;
|
||||
use OCP\AppFramework\ApiController;
|
||||
use OCP\IRequest;
|
||||
use OCA\Deck\Db\Acl;
|
||||
|
||||
class SessionController extends ApiController {
|
||||
private SessionService $sessionService;
|
||||
private PermissionService $permissionService;
|
||||
private BoardMapper $boardMapper;
|
||||
|
||||
public function __construct($appName,
|
||||
IRequest $request,
|
||||
SessionService $sessionService,
|
||||
PermissionService $permissionService,
|
||||
BoardMapper $boardMapper
|
||||
) {
|
||||
parent::__construct($appName, $request);
|
||||
$this->sessionService = $sessionService;
|
||||
$this->permissionService = $permissionService;
|
||||
$this->boardMapper = $boardMapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* @NoAdminRequired
|
||||
*/
|
||||
public function create(int $boardId): DataResponse {
|
||||
$this->permissionService->checkPermission($this->boardMapper, $boardId, Acl::PERMISSION_READ);
|
||||
|
||||
$session = $this->sessionService->initSession($boardId);
|
||||
return new DataResponse([
|
||||
'token' => $session->getToken(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* notifies the server that the session is still active
|
||||
* @NoAdminRequired
|
||||
* @param $boardId
|
||||
*/
|
||||
public function sync($boardId, $token): DataResponse {
|
||||
$this->permissionService->checkPermission($this->boardMapper, $boardId, Acl::PERMISSION_READ);
|
||||
try {
|
||||
$this->sessionService->syncSession($boardId, $token);
|
||||
return new DataResponse([]);
|
||||
} catch (DoesNotExistException $e) {
|
||||
return new DataResponse([], 403);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* delete a session if existing
|
||||
* @NoAdminRequired
|
||||
* @param $boardId
|
||||
* @return bool
|
||||
*/
|
||||
public function close($boardId, $token) {
|
||||
$this->permissionService->checkPermission($this->boardMapper, $boardId, Acl::PERMISSION_READ);
|
||||
$this->sessionService->closeSession((int)$boardId, $token);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -44,6 +44,7 @@ class Board extends RelationalEntity {
|
||||
protected $users = [];
|
||||
protected $shared;
|
||||
protected $stacks = [];
|
||||
protected $activeSessions = [];
|
||||
protected $deletedAt = 0;
|
||||
protected $lastModified = 0;
|
||||
|
||||
@@ -59,6 +60,7 @@ class Board extends RelationalEntity {
|
||||
$this->addRelation('acl');
|
||||
$this->addRelation('shared');
|
||||
$this->addRelation('users');
|
||||
$this->addRelation('activeSessions');
|
||||
$this->addRelation('permissions');
|
||||
$this->addRelation('stacks');
|
||||
$this->addRelation('settings');
|
||||
|
||||
48
lib/Db/Session.php
Normal file
48
lib/Db/Session.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2022, chandi Langecker (git@chandi.it)
|
||||
*
|
||||
* @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\Deck\Db;
|
||||
|
||||
use OCP\AppFramework\Db\Entity;
|
||||
|
||||
class Session extends Entity implements \JsonSerializable {
|
||||
public $id;
|
||||
protected $userId;
|
||||
protected $token;
|
||||
protected $lastContact;
|
||||
protected $boardId;
|
||||
|
||||
public function __construct() {
|
||||
$this->addType('id', 'integer');
|
||||
$this->addType('boardId', 'integer');
|
||||
$this->addType('lastContact', 'integer');
|
||||
}
|
||||
|
||||
public function jsonSerialize(): array {
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'userId' => $this->userId,
|
||||
'token' => $this->token,
|
||||
'lastContact' => $this->lastContact,
|
||||
'boardId' => $this->boardId,
|
||||
];
|
||||
}
|
||||
}
|
||||
62
lib/Db/SessionMapper.php
Normal file
62
lib/Db/SessionMapper.php
Normal file
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2022, chandi Langecker (git@chandi.it)
|
||||
*
|
||||
* @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\Deck\Db;
|
||||
|
||||
use OCA\Deck\Service\SessionService;
|
||||
use OCP\AppFramework\Db\DoesNotExistException;
|
||||
use OCP\AppFramework\Db\QBMapper;
|
||||
use OCP\IDBConnection;
|
||||
|
||||
class SessionMapper extends QBMapper {
|
||||
public function __construct(IDBConnection $db) {
|
||||
parent::__construct($db, 'deck_sessions', Session::class);
|
||||
}
|
||||
|
||||
public function find(int $boardId, string $userId, string $token): Session {
|
||||
$qb = $this->db->getQueryBuilder();
|
||||
$result = $qb->select('*')
|
||||
->from($this->getTableName())
|
||||
->where($qb->expr()->eq('board_id', $qb->createNamedParameter($boardId)))
|
||||
->andWhere($qb->expr()->eq('user_id', $qb->createNamedParameter($userId)))
|
||||
->andWhere($qb->expr()->eq('token', $qb->createNamedParameter($token)))
|
||||
->andWhere($qb->expr()->gt('last_contact', $qb->createNamedParameter(time() - SessionService::SESSION_VALID_TIME)))
|
||||
->executeQuery();
|
||||
|
||||
$data = $result->fetch();
|
||||
$result->closeCursor();
|
||||
if ($data === false) {
|
||||
throw new DoesNotExistException('Session is invalid');
|
||||
}
|
||||
return Session::fromRow($data);
|
||||
}
|
||||
|
||||
public function findAllActive($boardId) {
|
||||
$qb = $this->db->getQueryBuilder();
|
||||
$qb->select('id', 'board_id', 'last_contact', 'user_id')
|
||||
->from($this->getTableName())
|
||||
->where($qb->expr()->eq('board_id', $qb->createNamedParameter($boardId)))
|
||||
->andWhere($qb->expr()->gt('last_contact', $qb->createNamedParameter(time() - SessionService::SESSION_VALID_TIME)))
|
||||
->executeQuery();
|
||||
|
||||
return $this->findEntities($qb);
|
||||
}
|
||||
}
|
||||
45
lib/Event/SessionClosedEvent.php
Normal file
45
lib/Event/SessionClosedEvent.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2022, chandi Langecker (git@chandi.it)
|
||||
*
|
||||
* @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/>.
|
||||
*
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
|
||||
namespace OCA\Deck\Event;
|
||||
|
||||
use OCP\EventDispatcher\Event;
|
||||
|
||||
class SessionClosedEvent extends Event {
|
||||
private $boardId;
|
||||
private $userId;
|
||||
|
||||
public function __construct($boardId, $userId) {
|
||||
parent::__construct();
|
||||
|
||||
$this->boardId = $boardId;
|
||||
$this->userId = $userId;
|
||||
}
|
||||
|
||||
public function getBoardId(): int {
|
||||
return $this->boardId;
|
||||
}
|
||||
public function getUserId(): string {
|
||||
return $this->userId;
|
||||
}
|
||||
}
|
||||
46
lib/Event/SessionCreatedEvent.php
Normal file
46
lib/Event/SessionCreatedEvent.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2022, chandi Langecker (git@chandi.it)
|
||||
*
|
||||
* @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/>.
|
||||
*
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
|
||||
namespace OCA\Deck\Event;
|
||||
|
||||
use OCP\EventDispatcher\Event;
|
||||
|
||||
class SessionCreatedEvent extends Event {
|
||||
private $boardId;
|
||||
private $userId;
|
||||
|
||||
public function __construct($boardId, $userId) {
|
||||
parent::__construct();
|
||||
|
||||
$this->boardId = $boardId;
|
||||
$this->userId = $userId;
|
||||
}
|
||||
|
||||
public function getBoardId(): int {
|
||||
return $this->boardId;
|
||||
}
|
||||
public function getUserId(): string {
|
||||
return $this->userId;
|
||||
}
|
||||
}
|
||||
65
lib/Migration/Version10900Date202206151724222.php
Normal file
65
lib/Migration/Version10900Date202206151724222.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2022, chandi Langecker (git@chandi.it)
|
||||
*
|
||||
* @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/>.
|
||||
*
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
|
||||
namespace OCA\Deck\Migration;
|
||||
|
||||
use Closure;
|
||||
use OCP\DB\ISchemaWrapper;
|
||||
use OCP\Migration\IOutput;
|
||||
use OCP\Migration\SimpleMigrationStep;
|
||||
|
||||
class Version10900Date202206151724222 extends SimpleMigrationStep {
|
||||
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
|
||||
$schema = $schemaClosure();
|
||||
|
||||
if (!$schema->hasTable('deck_sessions')) {
|
||||
$table = $schema->createTable('deck_sessions');
|
||||
$table->addColumn('id', 'integer', [
|
||||
'autoincrement' => true,
|
||||
'notnull' => true,
|
||||
'unsigned' => true,
|
||||
]);
|
||||
$table->addColumn('user_id', 'string', [
|
||||
'notnull' => false,
|
||||
'length' => 64,
|
||||
]);
|
||||
$table->addColumn('board_id', 'integer', [
|
||||
'notnull' => false,
|
||||
]);
|
||||
$table->addColumn('token', 'string', [
|
||||
'notnull' => true,
|
||||
'length' => 64,
|
||||
]);
|
||||
$table->addColumn('last_contact', 'integer', [
|
||||
'notnull' => true,
|
||||
'length' => 20,
|
||||
'unsigned' => true,
|
||||
]);
|
||||
$table->setPrimaryKey(['id']);
|
||||
$table->addIndex(['token'], 'rd_session_token_idx');
|
||||
$table->addIndex(['last_contact'], 'ts_lastcontact');
|
||||
}
|
||||
return $schema;
|
||||
}
|
||||
}
|
||||
@@ -34,6 +34,8 @@ use OCA\Deck\Db\CardMapper;
|
||||
use OCA\Deck\Db\ChangeHelper;
|
||||
use OCA\Deck\Db\IPermissionMapper;
|
||||
use OCA\Deck\Db\Label;
|
||||
use OCA\Deck\Db\Session;
|
||||
use OCA\Deck\Db\SessionMapper;
|
||||
use OCA\Deck\Db\Stack;
|
||||
use OCA\Deck\Db\StackMapper;
|
||||
use OCA\Deck\Event\AclCreatedEvent;
|
||||
@@ -81,6 +83,7 @@ class BoardService {
|
||||
private IURLGenerator $urlGenerator;
|
||||
private IDBConnection $connection;
|
||||
private BoardServiceValidator $boardServiceValidator;
|
||||
private SessionMapper $sessionMapper;
|
||||
|
||||
public function __construct(
|
||||
BoardMapper $boardMapper,
|
||||
@@ -101,6 +104,7 @@ class BoardService {
|
||||
IURLGenerator $urlGenerator,
|
||||
IDBConnection $connection,
|
||||
BoardServiceValidator $boardServiceValidator,
|
||||
SessionMapper $sessionMapper,
|
||||
?string $userId
|
||||
) {
|
||||
$this->boardMapper = $boardMapper;
|
||||
@@ -122,6 +126,7 @@ class BoardService {
|
||||
$this->urlGenerator = $urlGenerator;
|
||||
$this->connection = $connection;
|
||||
$this->boardServiceValidator = $boardServiceValidator;
|
||||
$this->sessionMapper = $sessionMapper;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -214,6 +219,7 @@ class BoardService {
|
||||
]);
|
||||
$this->enrichWithUsers($board);
|
||||
$this->enrichWithBoardSettings($board);
|
||||
$this->enrichWithActiveSessions($board);
|
||||
$this->boardsCache[$board->getId()] = $board;
|
||||
return $board;
|
||||
}
|
||||
@@ -448,6 +454,14 @@ class BoardService {
|
||||
$board->setSettings($settings);
|
||||
}
|
||||
|
||||
public function enrichWithActiveSessions(Board $board) {
|
||||
$sessions = $this->sessionMapper->findAllActive($board->getId());
|
||||
|
||||
$board->setActiveSessions(array_unique(array_map(function (Session $session) {
|
||||
return $session->getUserId();
|
||||
}, $sessions)));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $boardId
|
||||
* @param $type
|
||||
|
||||
95
lib/Service/SessionService.php
Normal file
95
lib/Service/SessionService.php
Normal file
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2022, chandi Langecker (git@chandi.it)
|
||||
*
|
||||
* @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\Deck\Service;
|
||||
|
||||
use OCA\Deck\Db\Session;
|
||||
use OCA\Deck\Db\SessionMapper;
|
||||
use OCA\Deck\Event\SessionCreatedEvent;
|
||||
use OCA\Deck\Event\SessionClosedEvent;
|
||||
use OCP\AppFramework\Db\DoesNotExistException;
|
||||
use OCP\AppFramework\Utility\ITimeFactory;
|
||||
use OCP\EventDispatcher\IEventDispatcher;
|
||||
use OCA\NotifyPush\Queue\IQueue;
|
||||
use OCP\Security\ISecureRandom;
|
||||
|
||||
class SessionService {
|
||||
public const SESSION_VALID_TIME = 30;
|
||||
|
||||
private SessionMapper $sessionMapper;
|
||||
private ITimeFactory $timeFactory;
|
||||
private string|null $userId;
|
||||
private IEventDispatcher $eventDispatcher;
|
||||
private ISecureRandom $secureRandom;
|
||||
|
||||
public function __construct(
|
||||
SessionMapper $sessionMapper,
|
||||
ISecureRandom $secureRandom,
|
||||
ITimeFactory $timeFactory,
|
||||
$userId,
|
||||
IEventDispatcher $eventDispatcher
|
||||
) {
|
||||
$this->sessionMapper = $sessionMapper;
|
||||
$this->secureRandom = $secureRandom;
|
||||
$this->timeFactory = $timeFactory;
|
||||
$this->userId = $userId;
|
||||
$this->eventDispatcher = $eventDispatcher;
|
||||
}
|
||||
|
||||
public function initSession($boardId): Session {
|
||||
$session = new Session();
|
||||
$session->setBoardId($boardId);
|
||||
$session->setUserId($this->userId);
|
||||
$session->setToken($this->secureRandom->generate(64));
|
||||
$session->setLastContact($this->timeFactory->getTime());
|
||||
|
||||
$session = $this->sessionMapper->insert($session);
|
||||
$this->eventDispatcher->dispatchTyped(new SessionCreatedEvent($boardId, $this->userId));
|
||||
return $session;
|
||||
}
|
||||
|
||||
public function syncSession(int $boardId, string $token) {
|
||||
$session = $this->sessionMapper->find($boardId, $this->userId, $token);
|
||||
$session->setLastContact($this->timeFactory->getTime());
|
||||
$this->sessionMapper->update($session);
|
||||
}
|
||||
|
||||
public function closeSession(int $boardId, string $token): void {
|
||||
try {
|
||||
$session = $this->sessionMapper->find($boardId, $this->userId, $token);
|
||||
$this->sessionMapper->delete($session);
|
||||
} catch (DoesNotExistException $e) {
|
||||
}
|
||||
$this->eventDispatcher->dispatchTyped(new SessionClosedEvent($boardId, $this->userId));
|
||||
}
|
||||
|
||||
public function notifyAllSessions(IQueue $queue, int $boardId, $event, $excludeUserId, $body) {
|
||||
$activeSessions = $this->sessionMapper->findAllActive($boardId);
|
||||
|
||||
foreach ($activeSessions as $session) {
|
||||
$queue->push("notify_custom", [
|
||||
'user' => $session->getUserId(),
|
||||
'message' => $event,
|
||||
'body' => $body
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -39,6 +39,7 @@
|
||||
"@nextcloud/initial-state": "^2.0.0",
|
||||
"@nextcloud/l10n": "^1.6.0",
|
||||
"@nextcloud/moment": "^1.2.1",
|
||||
"@nextcloud/notify_push": "^1.1.2",
|
||||
"@nextcloud/router": "^2.0.1",
|
||||
"@nextcloud/vue": "^7.3.0",
|
||||
"@nextcloud/vue-dashboard": "^2.0.1",
|
||||
|
||||
@@ -42,6 +42,7 @@
|
||||
<referencedClass name="OC\*" />
|
||||
<referencedClass name="OC" />
|
||||
<referencedClass name="OC\Security\CSP\ContentSecurityPolicyNonceManager" />
|
||||
<referencedClass name="OCA\NotifyPush\Queue\IQueue" />
|
||||
</errorLevel>
|
||||
</UndefinedClass>
|
||||
<UndefinedDocblockClass>
|
||||
|
||||
@@ -40,6 +40,8 @@
|
||||
</p>
|
||||
</div>
|
||||
<div class="board-actions">
|
||||
<SessionList v-if="isNotifyPushEnabled && board && board.activeSessions"
|
||||
:sessions="board.activeSessions" />
|
||||
<div v-if="searchQuery || true" class="deck-search">
|
||||
<input type="search"
|
||||
class="icon-search"
|
||||
@@ -224,6 +226,8 @@ import FilterIcon from 'vue-material-design-icons/Filter.vue'
|
||||
import FilterOffIcon from 'vue-material-design-icons/FilterOff.vue'
|
||||
import ArrowCollapseVerticalIcon from 'vue-material-design-icons/ArrowCollapseVertical.vue'
|
||||
import ArrowExpandVerticalIcon from 'vue-material-design-icons/ArrowExpandVertical.vue'
|
||||
import SessionList from './SessionList'
|
||||
import { isNotifyPushEnabled } from '../listeners'
|
||||
|
||||
export default {
|
||||
name: 'Controls',
|
||||
@@ -239,6 +243,7 @@ export default {
|
||||
FilterOffIcon,
|
||||
ArrowCollapseVerticalIcon,
|
||||
ArrowExpandVerticalIcon,
|
||||
SessionList,
|
||||
},
|
||||
mixins: [labelStyle],
|
||||
props: {
|
||||
@@ -286,6 +291,9 @@ export default {
|
||||
labelsSorted() {
|
||||
return [...this.board.labels].sort((a, b) => (a.title < b.title) ? -1 : 1)
|
||||
},
|
||||
isNotifyPushEnabled() {
|
||||
return isNotifyPushEnabled()
|
||||
},
|
||||
},
|
||||
watch: {
|
||||
board(current, previous) {
|
||||
|
||||
115
src/components/SessionList.vue
Normal file
115
src/components/SessionList.vue
Normal file
@@ -0,0 +1,115 @@
|
||||
<!--
|
||||
* @copyright Copyright (c) 2019 Julius Härtl <jus@bitgrid.net>
|
||||
* @copyright Copyright (c) 2022, chandi Langecker (git@chandi.it)
|
||||
*
|
||||
* @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/>.
|
||||
*
|
||||
* -->
|
||||
|
||||
<template>
|
||||
<div class="session-list">
|
||||
<button slot="trigger"
|
||||
v-tooltip.bottom="t('text', 'Active people')"
|
||||
class="avatar-list">
|
||||
<div v-for="session in sessionsVisible"
|
||||
:key="session"
|
||||
class="avatar-wrapper"
|
||||
:style="sessionAvatarStyle">
|
||||
<Avatar :user="session"
|
||||
:disable-menu="true"
|
||||
:show-user-status="false"
|
||||
:disable-tooltip="true"
|
||||
:size="size" />
|
||||
</div>
|
||||
</button>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import Tooltip from '@nextcloud/vue/dist/Directives/Tooltip'
|
||||
import Avatar from '@nextcloud/vue/dist/Components/Avatar'
|
||||
|
||||
export default {
|
||||
name: 'SessionList',
|
||||
components: {
|
||||
Avatar,
|
||||
},
|
||||
directives: {
|
||||
tooltip: Tooltip,
|
||||
},
|
||||
props: {
|
||||
sessions: {
|
||||
type: Array,
|
||||
default: () => { return [] },
|
||||
},
|
||||
size: {
|
||||
type: Number,
|
||||
default: () => 32,
|
||||
},
|
||||
},
|
||||
computed: {
|
||||
sessionsVisible() {
|
||||
if (!this.sessions) return []
|
||||
return this.sessions.slice(0, 5)
|
||||
},
|
||||
sessionAvatarStyle() {
|
||||
return {
|
||||
'--size': this.size + 'px',
|
||||
'--font-size': this.size / 2 + 'px',
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped lang="scss">
|
||||
.session-list {
|
||||
padding-left: 0.5em;
|
||||
padding-right: 0.5em;
|
||||
}
|
||||
.avatar-list {
|
||||
border: none;
|
||||
background-color: var(--color-main-background);
|
||||
padding: 0;
|
||||
margin: 0;
|
||||
padding-left: 6px;
|
||||
display: inline-flex;
|
||||
flex-direction: row-reverse;
|
||||
|
||||
&:focus {
|
||||
background-color: #eee;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
.avatar-wrapper {
|
||||
background-color: #b9b9b9;
|
||||
border-radius: 50%;
|
||||
border-width: 2px;
|
||||
border-style: solid;
|
||||
width: var(--size);
|
||||
height: var(--size);
|
||||
text-align: center;
|
||||
color: #ffffff;
|
||||
line-height: var(--size);
|
||||
font-size: var(--font-size);
|
||||
font-weight: normal;
|
||||
z-index: 1;
|
||||
overflow: hidden;
|
||||
box-sizing: content-box !important;
|
||||
margin-left: -8px;
|
||||
}
|
||||
</style>
|
||||
@@ -81,6 +81,8 @@ import Stack from './Stack.vue'
|
||||
import { NcEmptyContent } from '@nextcloud/vue'
|
||||
import GlobalSearchResults from '../search/GlobalSearchResults.vue'
|
||||
import { showError } from '../../helpers/errors.js'
|
||||
import { sessionApi } from '../../services/SessionApi'
|
||||
import { isNotifyPushEnabled } from '../../listeners'
|
||||
|
||||
export default {
|
||||
name: 'Board',
|
||||
@@ -128,13 +130,51 @@ export default {
|
||||
},
|
||||
},
|
||||
watch: {
|
||||
id: 'fetchData',
|
||||
id(newValue, oldValue) {
|
||||
if (oldValue) {
|
||||
// close old session
|
||||
sessionApi.closeSession(oldValue, this.token)
|
||||
this.token = null
|
||||
}
|
||||
// create new session
|
||||
this.ensureSession(newValue)
|
||||
|
||||
this.fetchData()
|
||||
},
|
||||
showArchived() {
|
||||
this.fetchData()
|
||||
},
|
||||
},
|
||||
created() {
|
||||
if (isNotifyPushEnabled()) {
|
||||
// create a session
|
||||
this.ensureSession()
|
||||
}
|
||||
|
||||
this.fetchData()
|
||||
|
||||
if (isNotifyPushEnabled()) {
|
||||
// regularly let the server know that we are still here
|
||||
this.sessionInterval = setInterval(() => {
|
||||
this.ensureSession()
|
||||
}, 25 * 1000)
|
||||
|
||||
// we don't get events pushed for sessions that have expired,
|
||||
// so we poll the list of sessions every minute when there
|
||||
// are other sessions active
|
||||
this.refreshInterval = setInterval(() => {
|
||||
if (this.board?.activeSessions?.length) {
|
||||
this.refreshData()
|
||||
}
|
||||
}, 60 * 1000)
|
||||
}
|
||||
},
|
||||
beforeDestroy() {
|
||||
if (isNotifyPushEnabled()) {
|
||||
sessionApi.closeSession(this.id, this.token)
|
||||
clearInterval(this.sessionInterval)
|
||||
clearInterval(this.refreshInterval)
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
async fetchData() {
|
||||
@@ -149,6 +189,32 @@ export default {
|
||||
this.loading = false
|
||||
},
|
||||
|
||||
async ensureSession(boardId = this.id) {
|
||||
if (this.token) {
|
||||
try {
|
||||
await sessionApi.syncSession(boardId, this.token)
|
||||
} catch (err) {
|
||||
// session probably expired, let's try again
|
||||
// with a fresh session
|
||||
this.token = null
|
||||
setTimeout(() => {
|
||||
this.ensureSession()
|
||||
}, 100)
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
const res = await sessionApi.createSession(boardId)
|
||||
this.token = res.token
|
||||
} catch (err) {
|
||||
showError(err)
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
async refreshData() {
|
||||
await this.$store.dispatch('refreshBoard', this.id)
|
||||
},
|
||||
|
||||
onDropStack({ removedIndex, addedIndex }) {
|
||||
this.$store.dispatch('orderStack', { stack: this.stacksByBoard[removedIndex], removedIndex, addedIndex })
|
||||
},
|
||||
|
||||
41
src/listeners.js
Normal file
41
src/listeners.js
Normal file
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* @copyright Copyright (c) 2022, chandi Langecker (git@chandi.it)
|
||||
*
|
||||
* @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/>.
|
||||
*
|
||||
*/
|
||||
|
||||
import { listen } from '@nextcloud/notify_push'
|
||||
import store from './store/main'
|
||||
|
||||
let hasPush = false
|
||||
|
||||
/**
|
||||
* is the notify_push app active and can
|
||||
* provide us with real time updates?
|
||||
*/
|
||||
export function isNotifyPushEnabled() {
|
||||
return hasPush
|
||||
}
|
||||
|
||||
hasPush = listen('DeckBoardUpdate', (name, body) => {
|
||||
const currentBoardId = store.state.currentBoard?.id
|
||||
|
||||
// only handle update event for the currently open board
|
||||
if (body.id !== currentBoardId) return
|
||||
|
||||
store.dispatch('refreshBoard', currentBoardId)
|
||||
})
|
||||
@@ -31,6 +31,7 @@ import { subscribe } from '@nextcloud/event-bus'
|
||||
import { Tooltip } from '@nextcloud/vue'
|
||||
import ClickOutside from 'vue-click-outside'
|
||||
import './models/index.js'
|
||||
import './listeners'
|
||||
|
||||
// the server snap.js conflicts with vertical scrolling so we disable it
|
||||
document.body.setAttribute('data-snap-ignore', 'true')
|
||||
|
||||
45
src/services/SessionApi.js
Normal file
45
src/services/SessionApi.js
Normal file
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* @copyright Copyright (c) 2022, chandi Langecker (git@chandi.it)
|
||||
*
|
||||
* @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/>.
|
||||
*
|
||||
*/
|
||||
|
||||
import axios from '@nextcloud/axios'
|
||||
import { generateUrl } from '@nextcloud/router'
|
||||
|
||||
export class SessionApi {
|
||||
|
||||
url(url) {
|
||||
url = `/apps/deck${url}`
|
||||
return generateUrl(url)
|
||||
}
|
||||
|
||||
async createSession(boardId) {
|
||||
return (await axios.put(this.url('/session/create'), { boardId })).data
|
||||
}
|
||||
|
||||
async syncSession(boardId, token) {
|
||||
return await axios.post(this.url('/session/sync'), { boardId, token })
|
||||
}
|
||||
|
||||
async closeSession(boardId, token) {
|
||||
return await axios.post(this.url('/session/close'), { boardId, token })
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export const sessionApi = new SessionApi()
|
||||
@@ -32,6 +32,7 @@ class BoardTest extends TestCase {
|
||||
'archived' => false,
|
||||
'users' => ['user1', 'user2'],
|
||||
'settings' => [],
|
||||
'activeSessions' => [],
|
||||
'ETag' => $board->getETag(),
|
||||
], $board->jsonSerialize());
|
||||
}
|
||||
@@ -55,6 +56,7 @@ class BoardTest extends TestCase {
|
||||
'archived' => false,
|
||||
'users' => ['user1', 'user2'],
|
||||
'settings' => [],
|
||||
'activeSessions' => [],
|
||||
'ETag' => $board->getETag(),
|
||||
], $board->jsonSerialize());
|
||||
}
|
||||
@@ -76,6 +78,7 @@ class BoardTest extends TestCase {
|
||||
'archived' => false,
|
||||
'users' => [],
|
||||
'settings' => [],
|
||||
'activeSessions' => [],
|
||||
'ETag' => $board->getETag(),
|
||||
], $board->jsonSerialize());
|
||||
}
|
||||
@@ -105,6 +108,7 @@ class BoardTest extends TestCase {
|
||||
'shared' => 1,
|
||||
'users' => [],
|
||||
'settings' => [],
|
||||
'activeSessions' => [],
|
||||
'ETag' => $board->getETag(),
|
||||
], $board->jsonSerialize());
|
||||
}
|
||||
|
||||
@@ -34,6 +34,8 @@ use OCA\Deck\Db\BoardMapper;
|
||||
use OCA\Deck\Db\CardMapper;
|
||||
use OCA\Deck\Db\ChangeHelper;
|
||||
use OCA\Deck\Db\LabelMapper;
|
||||
use OCA\Deck\Db\Session;
|
||||
use OCA\Deck\Db\SessionMapper;
|
||||
use OCA\Deck\Db\StackMapper;
|
||||
use OCA\Deck\Event\AclCreatedEvent;
|
||||
use OCA\Deck\Event\AclDeletedEvent;
|
||||
@@ -89,6 +91,8 @@ class BoardServiceTest extends TestCase {
|
||||
private $connection;
|
||||
/** @var BoardServiceValidator */
|
||||
private $boardServiceValidator;
|
||||
/** @var SessionMapper */
|
||||
private $sessionMapper;
|
||||
|
||||
public function setUp(): void {
|
||||
parent::setUp();
|
||||
@@ -110,6 +114,7 @@ class BoardServiceTest extends TestCase {
|
||||
$this->urlGenerator = $this->createMock(IURLGenerator::class);
|
||||
$this->connection = $this->createMock(IDBConnection::class);
|
||||
$this->boardServiceValidator = $this->createMock(BoardServiceValidator::class);
|
||||
$this->sessionMapper = $this->createMock(SessionMapper::class);
|
||||
|
||||
$this->service = new BoardService(
|
||||
$this->boardMapper,
|
||||
@@ -130,6 +135,7 @@ class BoardServiceTest extends TestCase {
|
||||
$this->urlGenerator,
|
||||
$this->connection,
|
||||
$this->boardServiceValidator,
|
||||
$this->sessionMapper,
|
||||
$this->userId
|
||||
);
|
||||
|
||||
@@ -172,6 +178,11 @@ class BoardServiceTest extends TestCase {
|
||||
->willReturn([
|
||||
'admin' => 'admin',
|
||||
]);
|
||||
$session = $this->createMock(Session::class);
|
||||
$this->sessionMapper->expects($this->once())
|
||||
->method('findAllActive')
|
||||
->with(1)
|
||||
->willReturn([$session]);
|
||||
$this->assertEquals($b1, $this->service->find(1));
|
||||
}
|
||||
|
||||
@@ -224,6 +235,9 @@ class BoardServiceTest extends TestCase {
|
||||
->willReturn([
|
||||
'admin' => 'admin',
|
||||
]);
|
||||
$this->sessionMapper->expects($this->once())
|
||||
->method('findAllActive')
|
||||
->willReturn([]);
|
||||
$b = $this->service->update(123, 'MyNewNameBoard', 'ffffff', false);
|
||||
|
||||
$this->assertEquals($b->getTitle(), 'MyNewNameBoard');
|
||||
@@ -244,6 +258,10 @@ class BoardServiceTest extends TestCase {
|
||||
->willReturn([
|
||||
'admin' => 'admin',
|
||||
]);
|
||||
$this->sessionMapper->expects($this->once())
|
||||
->method('findAllActive')
|
||||
->with(null)
|
||||
->willReturn([]);
|
||||
$boardDeleted = clone $board;
|
||||
$boardDeleted->setDeletedAt(1);
|
||||
$this->boardMapper->expects($this->once())
|
||||
|
||||
Reference in New Issue
Block a user