Skip to content

Commit

Permalink
Merge pull request #4473 from nextcloud/backport/4339/stable26
Browse files Browse the repository at this point in the history
  • Loading branch information
juliushaertl committed Jul 11, 2023
2 parents edd0222 + 16aae73 commit 6552e69
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 17 deletions.
33 changes: 16 additions & 17 deletions lib/Db/SessionMapper.php
Expand Up @@ -99,27 +99,26 @@ public function findAllInactive() {
return $this->findEntities($qb);
}

public function deleteInactiveWithoutSteps($documentId = -1) {
$qb = $this->db->getQueryBuilder();
$qb->select('session_id')
->from('text_steps');
public function deleteInactiveWithoutSteps(?int $documentId = null): int {
$selectSubQuery = $this->db->getQueryBuilder();
$selectSubQuery->select('s.id')
->from('text_sessions', 's')
->leftJoin('s', 'text_steps', 'st', $selectSubQuery->expr()->eq('st.session_id', 's.id'))
->where($selectSubQuery->expr()->lt('last_contact', $selectSubQuery->createParameter('lastContact')))
->andWhere($selectSubQuery->expr()->isNull('st.id'));
if ($documentId !== null) {
$qb->where($qb->expr()->eq('document_id', $qb->createNamedParameter($documentId)));
$selectSubQuery->andWhere($selectSubQuery->expr()->eq('s.document_id', $selectSubQuery->createParameter('documentId')));
}
$result = $qb
->groupBy('session_id')
->execute();
$activeSessions = $result->fetchAll(\PDO::FETCH_COLUMN);
$result->closeCursor();

$qb = $this->db->getQueryBuilder();
$qb->delete($this->getTableName());
$qb->where($qb->expr()->lt('last_contact', $qb->createNamedParameter(time() - SessionService::SESSION_VALID_TIME)));
if ($documentId !== null) {
$qb->andWhere($qb->expr()->eq('document_id', $qb->createNamedParameter($documentId)));
}
$qb->andWhere($qb->expr()->notIn('id', $qb->createNamedParameter($activeSessions, IQueryBuilder::PARAM_INT_ARRAY)));
return $qb->execute();
$qb->delete($this->getTableName())
->where($qb->expr()->in('id', $qb->createFunction($selectSubQuery->getSQL())));
$qb->setParameters([
'lastContact' => time() - SessionService::SESSION_VALID_TIME,
'documentId' => $documentId,
]);

return $qb->executeStatement();
}

public function deleteByDocumentId($documentId) {
Expand Down
99 changes: 99 additions & 0 deletions tests/unit/Db/SessionMapperTest.php
@@ -0,0 +1,99 @@
<?php

namespace OCA\Text\Db;

/**
* @group DB
*/
class SessionMapperTest extends \Test\TestCase {
private SessionMapper $sessionMapper;
private StepMapper $stepMapper;

public function setUp(): void {
parent::setUp();
$this->sessionMapper = \OCP\Server::get(SessionMapper::class);
$this->stepMapper = \OCP\Server::get(StepMapper::class);
}

public function testDeleteInactiveWithoutSteps() {
$this->sessionMapper->deleteByDocumentId(1);
$this->sessionMapper->deleteByDocumentId(2);
$this->sessionMapper->insert(Session::fromParams([
'userId' => 'admin',
'documentId' => 1,
'lastContact' => 1337,
'token' => uniqid(),
'color' => '00ff00',
]));
$this->sessionMapper->insert(Session::fromParams([
'userId' => 'admin',
'documentId' => 2,
'lastContact' => 1337,
'token' => uniqid(),
'color' => '00ff00',
]));
$this->sessionMapper->deleteInactiveWithoutSteps(1);
self::assertCount(0, $this->sessionMapper->findAll(1));
self::assertCount(1, $this->sessionMapper->findAll(2));
$this->sessionMapper->deleteInactiveWithoutSteps();
self::assertCount(0, $this->sessionMapper->findAll(2));
}

public function testDeleteInactiveWithoutStepsKeep() {
$this->stepMapper->deleteAll(1);
$this->sessionMapper->deleteByDocumentId(1);
$this->sessionMapper->deleteByDocumentId(2);

$s1 = $this->sessionMapper->insert(Session::fromParams([
'userId' => 'admin',
'documentId' => 1,
'lastContact' => 1337,
'token' => uniqid(),
'color' => '00ff00',
]));
$s2 = $this->sessionMapper->insert(Session::fromParams([
'userId' => 'admin',
'documentId' => 2,
'lastContact' => 1337,
'token' => uniqid(),
'color' => '00ff00',
]));
$this->stepMapper->insert(Step::fromParams([
'sessionId' => $s1->getId(),
'documentId' => 1,
'data' => 'YJSDATA',
'version' => 1,
]));
self::assertCount(1, $this->sessionMapper->findAll(1));

$this->sessionMapper->deleteInactiveWithoutSteps(1);
self::assertCount(1, $this->sessionMapper->findAll(1));
self::assertCount(1, $this->sessionMapper->findAll(2));

$this->sessionMapper->deleteInactiveWithoutSteps();
self::assertCount(1, $this->sessionMapper->findAll(1));
self::assertCount(0, $this->sessionMapper->findAll(2));
}

public function testDeleteInactiveWithoutStepsMultiple() {
$this->sessionMapper->deleteByDocumentId(1);

$count = 1010;
for ($i = 0;$i < $count;$i++) {
$this->sessionMapper->insert(Session::fromParams([
'userId' => 'admin',
'documentId' => 1,
'lastContact' => 1337,
'token' => uniqid(),
'color' => '00ff00',
]));
}

self::assertCount($count, $this->sessionMapper->findAll(1));

$deleted = $this->sessionMapper->deleteInactiveWithoutSteps();
self::assertEquals($count, $deleted);

self::assertCount(0, $this->sessionMapper->findAll(1));
}
}

0 comments on commit 6552e69

Please sign in to comment.