Skip to content

Commit

Permalink
Keep deleted boards for a while and delete with cron
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Härtl <[email protected]>
  • Loading branch information
juliusknorr committed Jun 8, 2017
1 parent 247b4dd commit 2c63bfb
Show file tree
Hide file tree
Showing 15 changed files with 181 additions and 52 deletions.
8 changes: 8 additions & 0 deletions appinfo/database.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@
<type>boolean</type>
<default>false</default>
</field>
<field>
<name>deleted_at</name>
<type>integer</type>
<default>0</default>
<length>8</length>
<notnull>false</notnull>
<unsigned>true</unsigned>
</field>
</declaration>
</table>
<table>
Expand Down
5 changes: 4 additions & 1 deletion appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
💥 This is still alpha software: it may not be stable enough for production!

</description>
<version>0.1.4.1</version>
<version>0.1.4.2</version>
<licence>agpl</licence>
<author>Julius Härtl</author>
<namespace>Deck</namespace>
Expand All @@ -30,6 +30,9 @@
<dependencies>
<nextcloud min-version="11" max-version="13" />
</dependencies>
<background-jobs>
<job>OCA\Deck\Cron\DeleteCron</job>
</background-jobs>
<repair-steps>
<post-migration>
<step>OCA\Deck\Migration\UnknownUsers</step>
Expand Down
1 change: 1 addition & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
['name' => 'board#read', 'url' => '/boards/{boardId}', 'verb' => 'GET'],
['name' => 'board#update', 'url' => '/boards/{boardId}', 'verb' => 'PUT'],
['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#addAcl', 'url' => '/boards/{boardId}/acl', 'verb' => 'POST'],
['name' => 'board#updateAcl', 'url' => '/boards/{boardId}/acl', 'verb' => 'PUT'],
Expand Down
14 changes: 12 additions & 2 deletions css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,10 @@ button.button-inline:hover {
width: 50%;
}

#boardlist tr.deleted td * {
opacity: 0.5;
}

#boardlist td form {
display: flex;
width: 100%;
Expand All @@ -722,11 +726,17 @@ button.button-inline:hover {
width: 32px;
}

#boardlist td .app-popover-menu-utils {
float: right;
}

#boardlist .popovermenu {
top: 9px;
right: -36px;
top: 33px;
right: -5px;
}



/**
* Board details
*/
Expand Down
3 changes: 2 additions & 1 deletion js/app/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ app.config(function ($provide, $routeProvider, $interpolateProvider, $httpProvid

$stateProvider
.state('list', {
url: "/",
url: "/:filter",
templateUrl: "/boardlist.mainView.html",
controller: 'ListController',
reloadOnSearch: false,
params: {
filter: { value: '', dynamic: true }
}
Expand Down
1 change: 0 additions & 1 deletion js/controller/BoardController.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ app.controller('BoardController', function ($rootScope, $scope, $stateParams, St

// Handle initial Loading
BoardService.fetchOne($scope.id).then(function (data) {
BoardService.getPermissions();
$scope.statusservice.releaseWaiting();
}, function (error) {
$scope.statusservice.setError('Error occured', error);
Expand Down
32 changes: 15 additions & 17 deletions js/controller/ListController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.newBoard = {};
$scope.status = {
Expand Down Expand Up @@ -58,7 +58,7 @@ app.controller('ListController', function ($scope, $location, $filter, BoardServ
} else {
$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;
Expand All @@ -71,6 +71,13 @@ app.controller('ListController', function ($scope, $location, $filter, BoardServ
$scope.newBoard.color = color;
};

$scope.gotoBoard = function(board) {
if(board.deletedAt > 0) {
return false;
}
return $state.go('board', {boardId: board.id});
};

$scope.boardCreate = function() {
if(!$scope.newBoard.title || !$scope.newBoard.color) {
$scope.status.addBoard=false;
Expand Down Expand Up @@ -109,24 +116,15 @@ app.controller('ListController', function ($scope, $location, $filter, BoardServ
};

$scope.boardDelete = function(board) {
var boardId = board.id;
$scope.status.deleteUndo[boardId] = 10;
$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);
BoardService.delete(board.id).then(function (data) {
$scope.filterData();
});
};

$scope.boardDeleteUndo = function (board) {
delete $scope.status.deleteUndo[board.id];
BoardService.deleteUndo(board.id).then(function (data) {
$scope.filterData();
})
};

});
Expand Down
38 changes: 27 additions & 11 deletions js/service/BoardService.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,33 @@ app.factory('BoardService', function(ApiService, $http, $q){
};
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) {
var deferred = $q.defer();
var self = this;
Expand Down Expand Up @@ -151,17 +178,6 @@ app.factory('BoardService', function(ApiService, $http, $q){
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() {
if(!this.getCurrent() || !this.getCurrent().permissions) {
return false;
Expand Down
8 changes: 8 additions & 0 deletions lib/Controller/BoardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ public function update($id, $title, $color, $archived) {
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
Expand Down
49 changes: 49 additions & 0 deletions lib/Cron/DeleteCron.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* @copyright Copyright (c) 2017 Julius Härtl <[email protected]>
*
* @author Julius Härtl <[email protected]>
*
* @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);
}
}

}
2 changes: 2 additions & 0 deletions lib/Db/Board.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
17 changes: 13 additions & 4 deletions lib/Db/BoardMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function __construct(
* @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]);

Expand Down Expand Up @@ -87,8 +87,8 @@ public function find($id, $withLabels = false, $withAcl = false) {
* @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 */
Expand All @@ -112,7 +112,7 @@ public function findAllByGroups($userId, $groups, $limit = null, $offset = null)
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 = ? ';
Expand All @@ -135,6 +135,15 @@ public function findAll() {
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
Expand Down
29 changes: 28 additions & 1 deletion lib/Service/BoardService.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,23 @@ public function isArchived($mapper, $id) {
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) {
Expand Down Expand Up @@ -139,7 +156,17 @@ public function create($title, $userId, $color) {

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) {
Expand Down
Loading

0 comments on commit 2c63bfb

Please sign in to comment.