Keep deleted boards for a while and delete with cron

Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
Julius Härtl
2017-05-17 22:01:37 +02:00
parent 247b4dde0c
commit 2c63bfb4b8
15 changed files with 182 additions and 53 deletions

View File

@@ -38,6 +38,14 @@
<type>boolean</type> <type>boolean</type>
<default>false</default> <default>false</default>
</field> </field>
<field>
<name>deleted_at</name>
<type>integer</type>
<default>0</default>
<length>8</length>
<notnull>false</notnull>
<unsigned>true</unsigned>
</field>
</declaration> </declaration>
</table> </table>
<table> <table>

View File

@@ -16,7 +16,7 @@
💥 This is still alpha software: it may not be stable enough for production! 💥 This is still alpha software: it may not be stable enough for production!
</description> </description>
<version>0.1.4.1</version> <version>0.1.4.2</version>
<licence>agpl</licence> <licence>agpl</licence>
<author>Julius Härtl</author> <author>Julius Härtl</author>
<namespace>Deck</namespace> <namespace>Deck</namespace>
@@ -30,6 +30,9 @@
<dependencies> <dependencies>
<nextcloud min-version="11" max-version="13" /> <nextcloud min-version="11" max-version="13" />
</dependencies> </dependencies>
<background-jobs>
<job>OCA\Deck\Cron\DeleteCron</job>
</background-jobs>
<repair-steps> <repair-steps>
<post-migration> <post-migration>
<step>OCA\Deck\Migration\UnknownUsers</step> <step>OCA\Deck\Migration\UnknownUsers</step>

View File

@@ -31,6 +31,7 @@ return [
['name' => 'board#read', 'url' => '/boards/{boardId}', 'verb' => 'GET'], ['name' => 'board#read', 'url' => '/boards/{boardId}', 'verb' => 'GET'],
['name' => 'board#update', 'url' => '/boards/{boardId}', 'verb' => 'PUT'], ['name' => 'board#update', 'url' => '/boards/{boardId}', 'verb' => 'PUT'],
['name' => 'board#delete', 'url' => '/boards/{boardId}', 'verb' => 'DELETE'], ['name' => 'board#delete', 'url' => '/boards/{boardId}', 'verb' => 'DELETE'],
['name' => 'board#deleteUndo', 'url' => '/boards/{boardId}/deleteUndo', 'verb' => 'POST'],
['name' => 'board#getUserPermissions', 'url' => '/boards/{boardId}/permissions', 'verb' => 'GET'], ['name' => 'board#getUserPermissions', 'url' => '/boards/{boardId}/permissions', 'verb' => 'GET'],
['name' => 'board#addAcl', 'url' => '/boards/{boardId}/acl', 'verb' => 'POST'], ['name' => 'board#addAcl', 'url' => '/boards/{boardId}/acl', 'verb' => 'POST'],
['name' => 'board#updateAcl', 'url' => '/boards/{boardId}/acl', 'verb' => 'PUT'], ['name' => 'board#updateAcl', 'url' => '/boards/{boardId}/acl', 'verb' => 'PUT'],

View File

@@ -705,6 +705,10 @@ button.button-inline:hover {
width: 50%; width: 50%;
} }
#boardlist tr.deleted td * {
opacity: 0.5;
}
#boardlist td form { #boardlist td form {
display: flex; display: flex;
width: 100%; width: 100%;
@@ -722,11 +726,17 @@ button.button-inline:hover {
width: 32px; width: 32px;
} }
#boardlist .popovermenu { #boardlist td .app-popover-menu-utils {
top: 9px; float: right;
right: -36px;
} }
#boardlist .popovermenu {
top: 33px;
right: -5px;
}
/** /**
* Board details * Board details
*/ */

View File

@@ -37,9 +37,10 @@ app.config(function ($provide, $routeProvider, $interpolateProvider, $httpProvid
$stateProvider $stateProvider
.state('list', { .state('list', {
url: "/", url: "/:filter",
templateUrl: "/boardlist.mainView.html", templateUrl: "/boardlist.mainView.html",
controller: 'ListController', controller: 'ListController',
reloadOnSearch: false,
params: { params: {
filter: { value: '', dynamic: true } filter: { value: '', dynamic: true }
} }

View File

@@ -114,7 +114,6 @@ app.controller('BoardController', function ($rootScope, $scope, $stateParams, St
// Handle initial Loading // Handle initial Loading
BoardService.fetchOne($scope.id).then(function (data) { BoardService.fetchOne($scope.id).then(function (data) {
BoardService.getPermissions();
$scope.statusservice.releaseWaiting(); $scope.statusservice.releaseWaiting();
}, function (error) { }, function (error) {
$scope.statusservice.setError('Error occured', error); $scope.statusservice.setError('Error occured', error);

View File

@@ -21,7 +21,7 @@
* *
*/ */
app.controller('ListController', function ($scope, $location, $filter, BoardService, $element, $timeout, $stateParams) { app.controller('ListController', function ($scope, $location, $filter, BoardService, $element, $timeout, $stateParams, $state) {
$scope.boards = []; $scope.boards = [];
$scope.newBoard = {}; $scope.newBoard = {};
$scope.status = { $scope.status = {
@@ -58,7 +58,7 @@ app.controller('ListController', function ($scope, $location, $filter, BoardServ
} else { } else {
$scope.boardservice.sorted = $filter('cardFilter')($scope.boardservice.sorted, {archived: false}); $scope.boardservice.sorted = $filter('cardFilter')($scope.boardservice.sorted, {archived: false});
} }
$scope.boardservice.sorted = $filter('orderBy')($scope.boardservice.sorted, 'title'); $scope.boardservice.sorted = $filter('orderBy')($scope.boardservice.sorted, ['deletedAt', 'title']);
}; };
$scope.$state = $stateParams; $scope.$state = $stateParams;
@@ -71,6 +71,13 @@ app.controller('ListController', function ($scope, $location, $filter, BoardServ
$scope.newBoard.color = color; $scope.newBoard.color = color;
}; };
$scope.gotoBoard = function(board) {
if(board.deletedAt > 0) {
return false;
}
return $state.go('board', {boardId: board.id});
};
$scope.boardCreate = function() { $scope.boardCreate = function() {
if(!$scope.newBoard.title || !$scope.newBoard.color) { if(!$scope.newBoard.title || !$scope.newBoard.color) {
$scope.status.addBoard=false; $scope.status.addBoard=false;
@@ -109,24 +116,15 @@ app.controller('ListController', function ($scope, $location, $filter, BoardServ
}; };
$scope.boardDelete = function(board) { $scope.boardDelete = function(board) {
var boardId = board.id; BoardService.delete(board.id).then(function (data) {
$scope.status.deleteUndo[boardId] = 10; $scope.filterData();
$scope.boardDeleteCountdown = function () { });
if($scope.status.deleteUndo[boardId] > 0) {
$scope.status.deleteUndo[boardId]--;
$timeout($scope.boardDeleteCountdown, 1000);
}
if($scope.status.deleteUndo[boardId] === 0) {
BoardService.delete(board.id).then(function (data) {
$scope.filterData();
});
}
};
$timeout($scope.boardDeleteCountdown, 1000);
}; };
$scope.boardDeleteUndo = function (board) { $scope.boardDeleteUndo = function (board) {
delete $scope.status.deleteUndo[board.id]; BoardService.deleteUndo(board.id).then(function (data) {
$scope.filterData();
})
}; };
}); });

View File

@@ -26,6 +26,33 @@ app.factory('BoardService', function(ApiService, $http, $q){
}; };
BoardService.prototype = angular.copy(ApiService.prototype); BoardService.prototype = angular.copy(ApiService.prototype);
BoardService.prototype.delete = function (id) {
var deferred = $q.defer();
var self = this;
$http.delete(this.baseUrl + '/' + id).then(function (response) {
self.data[id].deletedAt = response.data.deletedAt;
deferred.resolve(response.data);
}, function (error) {
deferred.reject('Deleting ' + self.endpoint + ' failed');
});
return deferred.promise;
};
BoardService.prototype.deleteUndo = function (id) {
var deferred = $q.defer();
var self = this;
var _id = id;
$http.post(this.baseUrl + '/' + id + '/deleteUndo').then(function (response) {
self.data[_id].deletedAt = 0;
console.log(self.data[_id]);
deferred.resolve(response.data);
}, function (error) {
deferred.reject('Deleting ' + self.endpoint + ' failed');
});
return deferred.promise;
};
BoardService.prototype.searchUsers = function (search) { BoardService.prototype.searchUsers = function (search) {
var deferred = $q.defer(); var deferred = $q.defer();
var self = this; var self = this;
@@ -151,17 +178,6 @@ app.factory('BoardService', function(ApiService, $http, $q){
return deferred.promise; return deferred.promise;
}; };
BoardService.prototype.getPermissions = function() {
var board = this.getCurrent();
var deferred = $q.defer();
$http.get(this.baseUrl + '/' + board.id + '/permissions').then(function (response) {
board.permissions = response.data;
deferred.resolve(response.data);
}, function (error) {
deferred.reject('Error fetching board permissions ' + board);
});
};
BoardService.prototype.canRead = function() { BoardService.prototype.canRead = function() {
if(!this.getCurrent() || !this.getCurrent().permissions) { if(!this.getCurrent() || !this.getCurrent().permissions) {
return false; return false;

View File

@@ -108,6 +108,14 @@ class BoardController extends Controller {
public function delete($boardId) { public function delete($boardId) {
return $this->boardService->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 * @NoAdminRequired

49
lib/Cron/DeleteCron.php Normal file
View 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);
}
}
}

View File

@@ -36,11 +36,13 @@ class Board extends RelationalEntity implements JsonSerializable {
protected $acl = []; protected $acl = [];
protected $permissions = []; protected $permissions = [];
protected $shared; protected $shared;
protected $deletedAt;
public function __construct() { public function __construct() {
$this->addType('id', 'integer'); $this->addType('id', 'integer');
$this->addType('shared', 'integer'); $this->addType('shared', 'integer');
$this->addType('archived', 'boolean'); $this->addType('archived', 'boolean');
$this->addType('deletedAt', 'integer');
$this->addRelation('labels'); $this->addRelation('labels');
$this->addRelation('acl'); $this->addRelation('acl');
$this->addRelation('shared'); $this->addRelation('shared');

View File

@@ -59,7 +59,7 @@ class BoardMapper extends DeckMapper implements IPermissionMapper {
* @return \OCP\AppFramework\Db\Entity if not found * @return \OCP\AppFramework\Db\Entity if not found
*/ */
public function find($id, $withLabels = false, $withAcl = false) { 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` = ?'; 'WHERE `id` = ?';
$board = $this->findEntity($sql, [$id]); $board = $this->findEntity($sql, [$id]);
@@ -87,8 +87,8 @@ class BoardMapper extends DeckMapper implements IPermissionMapper {
* @return array * @return array
*/ */
public function findAllByUser($userId, $limit = null, $offset = null) { 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 ' . $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, 1 as shared FROM `*PREFIX*deck_boards` as boards ' . '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 != ?'; '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); $entries = $this->findEntities($sql, [$userId, $userId, Acl::PERMISSION_TYPE_USER, $userId], $limit, $offset);
/* @var Board $entry */ /* @var Board $entry */
@@ -112,7 +112,7 @@ class BoardMapper extends DeckMapper implements IPermissionMapper {
if (count($groups) <= 0) { if (count($groups) <= 0) {
return []; 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 ('; '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++) { for ($i = 0; $i < count($groups); $i++) {
$sql .= 'acl.participant = ? '; $sql .= 'acl.participant = ? ';
@@ -135,6 +135,15 @@ class BoardMapper extends DeckMapper implements IPermissionMapper {
return $this->findEntities($sql, []); 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 */ public function delete(/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
\OCP\AppFramework\Db\Entity $entity) { \OCP\AppFramework\Db\Entity $entity) {
// delete acl // delete acl

View File

@@ -107,6 +107,23 @@ class BoardService {
return $board->getArchived(); 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) { public function create($title, $userId, $color) {
@@ -139,7 +156,17 @@ class BoardService {
public function delete($id) { public function delete($id) {
$this->permissionService->checkPermission($this->boardMapper, $id, Acl::PERMISSION_READ); $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) { public function update($id, $title, $color, $archived) {

View File

@@ -9,12 +9,12 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<tr data-ng-repeat="b in boardservice.sorted"> <tr data-ng-repeat="b in boardservice.sorted" ng-class="{deleted: b.deletedAt > 0}">
<td ui-sref="board({boardId: b.id})"> <td ng-click="gotoBoard(b)">
<span class="board-bullet" <span class="board-bullet"
style="background-color:#{{b.color}};"> </span> style="background-color:#{{b.color}};"> </span>
</td> </td>
<td ui-sref="board({boardId: b.id})"><a href="#/board/{{b.id}}">{{ b.title }}</a></td> <td ng-click="gotoBoard(b)">{{ b.title }}</td>
<td> <td>
<div id="assigned-users"> <div id="assigned-users">
<div class="avatardiv" avatar displayname="{{ b.owner.uid }}" title="{{ b.owner.displayname }}"></div> <div class="avatardiv" avatar displayname="{{ b.owner.uid }}" title="{{ b.owner.displayname }}"></div>
@@ -22,8 +22,9 @@
</div> </div>
</td> </td>
<td> <td>
<div class="app-popover-menu-utils"> <div class="hint"></div>
<button class="icon icon-more"></button> <div class="app-popover-menu-utils" ng-if="b.archived && b.deletedAt == 0">
<button class="icon icon-more" title="More actions"></button>
<div class="popovermenu bubble hidden"> <div class="popovermenu bubble hidden">
<ul> <ul>
<li ng-if="boardservice.canManage(b) && !b.archived" ng-click="boardArchive(b)"> <li ng-if="boardservice.canManage(b) && !b.archived" ng-click="boardArchive(b)">
@@ -45,6 +46,9 @@
</ul> </ul>
</div> </div>
</div> </div>
<div class="app-popover-menu-utils" ng-if="b.archived && b.deletedAt > 0">
<button class="icon icon-history" ng-click="boardDeleteUndo(b)" title="Undo board deletion - Otherwise the board will be deleted during the next cronjob run."></button>
</div>
</td> </td>
</tr> </tr>
<tr ng-if="status.filter === '' && !status.addBoard" ng-click="status.addBoard=!status.addBoard"> <tr ng-if="status.filter === '' && !status.addBoard" ng-click="status.addBoard=!status.addBoard">

View File

@@ -1,7 +1,7 @@
<ul class="with-icon"> <ul class="with-icon">
<li ng-class="{active: status.filter === '' && !boardservice.getCurrent()}"><a ui-sref="list({ filter: ''})" class="icon-deck"><?php p($l->t('All Boards')); ?></a></li> <li ng-class="{active: status.filter === '' && !boardservice.getCurrent()}"><a ui-sref="list({ filter: ''})" class="icon-deck"><?php p($l->t('All Boards')); ?></a></li>
<li ng-class="{active: status.filter === 'archived'}"><a ui-sref="list({ filter: 'archived' })" class="icon-archive"><?php p($l->t('Archived boards')); ?></a></li> <li ng-class="{active: status.filter === 'archived' || (boardservice.getCurrent() && boardservice.getCurrent().archived)}"><a ui-sref="list({ filter: 'archived' })" class="icon-archive"><?php p($l->t('Archived boards')); ?></a></li>
<li ng-class="{active: status.filter === 'shared'}"><a ui-sref="list({ filter: 'shared' })" class="icon-share"><?php p($l->t('Shared boards')); ?></a></li> <li ng-class="{active: status.filter === 'shared'}"><a ui-sref="list({ filter: 'shared' })" class="icon-share"><?php p($l->t('Shared boards')); ?></a></li>
<li class="with-icon with-menu" ng-class="{active: b.id === boardservice.getCurrent().id}" data-ng-repeat="b in boardservice.sidebar"> <li class="with-icon with-menu" ng-class="{active: b.id === boardservice.getCurrent().id}" data-ng-repeat="b in boardservice.sidebar">
@@ -9,22 +9,16 @@
<a href="#!/board/{{b.id}}/" ng-if="!b.status.edit">{{ b.title }}</a> <a href="#!/board/{{b.id}}/" ng-if="!b.status.edit">{{ b.title }}</a>
<div class="app-navigation-entry-utils" ng-show="!b.status.edit" style="position:absolute;"> <div class="app-navigation-entry-utils" ng-show="!b.status.edit" style="position:absolute;">
<ul> <ul>
<li class="app-navigation-entry-utils-counter board-delete-undo" ng-show="status.deleteUndo[b.id]" ng-click="boardDeleteUndo(b)" title="Click to undo">Deleting in {{ status.deleteUndo[b.id] }}s &nbsp; X</li>
<li class="app-navigation-entry-utils-menu-share svg" ng-show="b.shared>0"><i class="icon icon-share" title="<?php p($l->t('Shared with you')); ?>"> </i></li> <li class="app-navigation-entry-utils-menu-share svg" ng-show="b.shared>0"><i class="icon icon-share" title="<?php p($l->t('Shared with you')); ?>"> </i></li>
<li class="app-navigation-entry-utils-menu-button svg" ng-show="!status.deleteUndo[b.id]"><button class="icon-more"></button></li> <li class="app-navigation-entry-utils-menu-button svg" ng-show="!status.deleteUndo[b.id]"><button class="icon-more"></button></li>
</ul> </ul>
</div> </div>
<div class="app-navigation-entry-menu app-navigation-noclose" ng-show="!b.status.edit"> <div class="app-navigation-entry-menu app-navigation-noclose" ng-show="!b.status.edit">
<ul> <ul>
<li ng-show="b.owner.uid===user"><button class="icon-rename svg" title="<?php p($l->t('edit')); ?>" ng-click="b.status.edit=true"></button></li> <li ng-show="boardservice.canManage(b)"><button class="icon-rename svg" title="<?php p($l->t('Edit board')); ?>" ng-click="b.status.edit=true"></button></li>
<li ng-show="b.owner.uid===user"><button class="icon-delete svg" title="<?php p($l->t('delete')); ?>" ng-click="boardDelete(b)"></button></li> <li ng-show="boardservice.canManage(b)"><button class="icon-archive svg" title="<?php p($l->t('Move board to archive')); ?>" ng-click="boardArchive(b)"></button></li>
<li ng-show="b.owner.uid===user"><button class="icon-archive svg" title="<?php p($l->t('Move board to archive')); ?>" ng-click="boardArchive(b)"></button></li>
</ul> </ul>
</div> </div>
<div class="app-navigation-entry-deleted" ng-show="false">
<div class="app-navigation-entry-deleted-description">Deleted X</div>
<button class="app-navigation-entry-deleted-button icon-history svg" title="Undo"></button>
</div>
<div class="app-navigation-entry-edit" ng-show="b.status.edit"> <div class="app-navigation-entry-edit" ng-show="b.status.edit">
<form ng-disabled="isAddingList" class="ng-pristine ng-valid" ng-submit="boardUpdate(b)"> <form ng-disabled="isAddingList" class="ng-pristine ng-valid" ng-submit="boardUpdate(b)">
<input id="newTitle" class="edit ng-valid ng-empty" type="text" autofocus-on-insert ng-model="b.title" maxlength="100"> <input id="newTitle" class="edit ng-valid ng-empty" type="text" autofocus-on-insert ng-model="b.title" maxlength="100">