Keep deleted boards for a while and delete with cron
Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
@@ -108,6 +108,14 @@ class BoardController extends Controller {
|
||||
public function delete($boardId) {
|
||||
return $this->boardService->delete($boardId);
|
||||
}
|
||||
/**
|
||||
* @NoAdminRequired
|
||||
* @param $boardId
|
||||
* @return \OCP\AppFramework\Db\Entity
|
||||
*/
|
||||
public function deleteUndo($boardId) {
|
||||
return $this->boardService->deleteUndo($boardId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @NoAdminRequired
|
||||
|
||||
49
lib/Cron/DeleteCron.php
Normal file
49
lib/Cron/DeleteCron.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2017 Julius Härtl <jus@bitgrid.net>
|
||||
*
|
||||
* @author Julius Härtl <jus@bitgrid.net>
|
||||
*
|
||||
* @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/>.
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: jus
|
||||
* Date: 16.05.17
|
||||
* Time: 12:34
|
||||
*/
|
||||
|
||||
namespace OCA\Deck\Cron;
|
||||
|
||||
use OC\BackgroundJob\Job;
|
||||
use OCA\Deck\Db\BoardMapper;
|
||||
|
||||
class DeleteCron extends Job {
|
||||
|
||||
public function __construct(BoardMapper $boardMapper) {
|
||||
$this->boardMapper = $boardMapper;
|
||||
}
|
||||
|
||||
protected function run($argument) {
|
||||
$boards = $this->boardMapper->findToDelete();
|
||||
foreach ($boards as $board) {
|
||||
$this->boardMapper->delete($board);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -36,11 +36,13 @@ class Board extends RelationalEntity implements JsonSerializable {
|
||||
protected $acl = [];
|
||||
protected $permissions = [];
|
||||
protected $shared;
|
||||
protected $deletedAt;
|
||||
|
||||
public function __construct() {
|
||||
$this->addType('id', 'integer');
|
||||
$this->addType('shared', 'integer');
|
||||
$this->addType('archived', 'boolean');
|
||||
$this->addType('deletedAt', 'integer');
|
||||
$this->addRelation('labels');
|
||||
$this->addRelation('acl');
|
||||
$this->addRelation('shared');
|
||||
|
||||
@@ -59,7 +59,7 @@ class BoardMapper extends DeckMapper implements IPermissionMapper {
|
||||
* @return \OCP\AppFramework\Db\Entity if not found
|
||||
*/
|
||||
public function find($id, $withLabels = false, $withAcl = false) {
|
||||
$sql = 'SELECT id, title, owner, color, archived FROM `*PREFIX*deck_boards` ' .
|
||||
$sql = 'SELECT id, title, owner, color, archived, deleted_at FROM `*PREFIX*deck_boards` ' .
|
||||
'WHERE `id` = ?';
|
||||
$board = $this->findEntity($sql, [$id]);
|
||||
|
||||
@@ -87,8 +87,8 @@ class BoardMapper extends DeckMapper implements IPermissionMapper {
|
||||
* @return array
|
||||
*/
|
||||
public function findAllByUser($userId, $limit = null, $offset = null) {
|
||||
$sql = 'SELECT id, title, owner, color, archived, 0 as shared FROM `*PREFIX*deck_boards` WHERE owner = ? UNION ' .
|
||||
'SELECT boards.id, title, owner, color, archived, 1 as shared FROM `*PREFIX*deck_boards` as boards ' .
|
||||
$sql = 'SELECT id, title, owner, color, archived, deleted_at, 0 as shared FROM `*PREFIX*deck_boards` WHERE owner = ? UNION ' .
|
||||
'SELECT boards.id, title, owner, color, archived, deleted_at, 1 as shared FROM `*PREFIX*deck_boards` as boards ' .
|
||||
'JOIN `*PREFIX*deck_board_acl` as acl ON boards.id=acl.board_id WHERE acl.participant=? AND acl.type=? AND boards.owner != ?';
|
||||
$entries = $this->findEntities($sql, [$userId, $userId, Acl::PERMISSION_TYPE_USER, $userId], $limit, $offset);
|
||||
/* @var Board $entry */
|
||||
@@ -112,7 +112,7 @@ class BoardMapper extends DeckMapper implements IPermissionMapper {
|
||||
if (count($groups) <= 0) {
|
||||
return [];
|
||||
}
|
||||
$sql = 'SELECT boards.id, title, owner, color, archived, 2 as shared FROM `*PREFIX*deck_boards` as boards ' .
|
||||
$sql = 'SELECT boards.id, title, owner, color, archived, deleted_at, 2 as shared FROM `*PREFIX*deck_boards` as boards ' .
|
||||
'INNER JOIN `*PREFIX*deck_board_acl` as acl ON boards.id=acl.board_id WHERE owner != ? AND type=? AND (';
|
||||
for ($i = 0; $i < count($groups); $i++) {
|
||||
$sql .= 'acl.participant = ? ';
|
||||
@@ -135,6 +135,15 @@ class BoardMapper extends DeckMapper implements IPermissionMapper {
|
||||
return $this->findEntities($sql, []);
|
||||
}
|
||||
|
||||
public function findToDelete() {
|
||||
// add buffer of 5 min
|
||||
$timeLimit = time()-(60*5);
|
||||
$sql = 'SELECT id, title, owner, color, archived, deleted_at FROM `*PREFIX*deck_boards` ' .
|
||||
'WHERE `deleted_at` > 0 AND `deleted_at` < ?';
|
||||
\OC::$server->getLogger()->error($sql);
|
||||
return $this->findEntities($sql, [$timeLimit]);
|
||||
}
|
||||
|
||||
public function delete(/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
|
||||
\OCP\AppFramework\Db\Entity $entity) {
|
||||
// delete acl
|
||||
|
||||
@@ -107,6 +107,23 @@ class BoardService {
|
||||
return $board->getArchived();
|
||||
}
|
||||
|
||||
public function isDeleted($mapper, $id) {
|
||||
try {
|
||||
if ($mapper instanceof IPermissionMapper) {
|
||||
$boardId = $mapper->findBoardId($id);
|
||||
} else {
|
||||
$boardId = $id;
|
||||
}
|
||||
if ($boardId === null) {
|
||||
return false;
|
||||
}
|
||||
} catch (DoesNotExistException $exception) {
|
||||
return false;
|
||||
}
|
||||
$board = $this->find($boardId);
|
||||
return $board->getDeletedAt() > 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function create($title, $userId, $color) {
|
||||
@@ -139,7 +156,17 @@ class BoardService {
|
||||
|
||||
public function delete($id) {
|
||||
$this->permissionService->checkPermission($this->boardMapper, $id, Acl::PERMISSION_READ);
|
||||
return $this->boardMapper->delete($this->find($id));
|
||||
$board = $this->find($id);
|
||||
$board->setDeletedAt(time());
|
||||
$this->boardMapper->update($board);
|
||||
return $board;
|
||||
}
|
||||
|
||||
public function deleteUndo($id) {
|
||||
$this->permissionService->checkPermission($this->boardMapper, $id, Acl::PERMISSION_READ);
|
||||
$board = $this->find($id);
|
||||
$board->setDeletedAt(0);
|
||||
$this->boardMapper->update($board);
|
||||
}
|
||||
|
||||
public function update($id, $title, $color, $archived) {
|
||||
|
||||
Reference in New Issue
Block a user