. * */ 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 = 92; 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; } /** * @throws DoesNotExistException */ 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 ]); } } }