Add attachment indicator

Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
Julius Härtl
2018-06-12 17:14:54 +02:00
parent ee5a54a575
commit 0137b882c5
7 changed files with 64 additions and 12 deletions

View File

@@ -33,6 +33,8 @@ use OCA\Deck\InvalidAttachmentType;
use OCA\Deck\NoPermissionException;
use OCA\Deck\NotFoundException;
use OCP\AppFramework\Http\Response;
use OCP\ICache;
use OCP\ICacheFactory;
class AttachmentService {
@@ -44,6 +46,8 @@ class AttachmentService {
/** @var IAttachmentService[] */
private $services = [];
private $application;
/** @var ICache */
private $cache;
/**
* AttachmentService constructor.
@@ -54,12 +58,14 @@ class AttachmentService {
* @param $userId
* @throws \OCP\AppFramework\QueryException
*/
public function __construct(AttachmentMapper $attachmentMapper, CardMapper $cardMapper, PermissionService $permissionService, Application $application, $userId) {
public function __construct(AttachmentMapper $attachmentMapper, CardMapper $cardMapper, PermissionService $permissionService, Application $application, ICacheFactory $cacheFactory, $userId) {
$this->attachmentMapper = $attachmentMapper;
$this->cardMapper = $cardMapper;
$this->permissionService = $permissionService;
$this->userId = $userId;
$this->application = $application;
$this->cache = $cacheFactory->createDistributed('deck-card-attachments-');
// Register shipped attachment services
// TODO: move this to a plugin based approach once we have different types of attachments
@@ -92,10 +98,13 @@ class AttachmentService {
* @return array
* @throws \OCA\Deck\NoPermissionException
*/
public function findAll($cardId) {
public function findAll($cardId, $withDeleted = false) {
$this->permissionService->checkPermission($this->cardMapper, $cardId, Acl::PERMISSION_READ);
$attachments = $this->attachmentMapper->findAll($cardId);
if ($withDeleted) {
$attachments = array_merge($attachments, $this->attachmentMapper->findToDelete($cardId, false));
}
foreach ($attachments as &$attachment) {
try {
$service = $this->getService($attachment->getType());
@@ -107,9 +116,19 @@ class AttachmentService {
return $attachments;
}
public function count($cardId) {
$count = $this->cache->get('card-' . $cardId);
if (!$count) {
$count = count($this->attachmentMapper->findAll($cardId));
$this->cache->set('card-' . $cardId, $count);
}
return $count;
}
public function create($cardId, $type, $data) {
$this->permissionService->checkPermission($this->cardMapper, $cardId, Acl::PERMISSION_EDIT);
$this->cache->clear('card-' . $cardId);
$attachment = new Attachment();
$attachment->setCardId($cardId);
$attachment->setType($type);
@@ -171,6 +190,9 @@ class AttachmentService {
*/
public function update($cardId, $attachmentId, $data) {
$this->permissionService->checkPermission($this->cardMapper, $cardId, Acl::PERMISSION_EDIT);
$this->cache->clear('card-' . $cardId);
$attachment = $this->attachmentMapper->find($attachmentId);
$attachment->setData($data);
try {
@@ -196,6 +218,8 @@ class AttachmentService {
public function delete($cardId, $attachmentId) {
$this->permissionService->checkPermission($this->cardMapper, $cardId, Acl::PERMISSION_EDIT);
$this->cache->clear('card-' . $cardId);
$attachment = $this->attachmentMapper->find($attachmentId);
try {
$service = $this->getService($attachment->getType());
@@ -213,6 +237,8 @@ class AttachmentService {
public function restore($cardId, $attachmentId) {
$this->permissionService->checkPermission($this->cardMapper, $cardId, Acl::PERMISSION_EDIT);
$this->cache->clear('card-' . $cardId);
$attachment = $this->attachmentMapper->find($attachmentId);
try {
$service = $this->getService($attachment->getType());

View File

@@ -54,7 +54,7 @@ class CardService {
$this->permissionService->checkPermission($this->cardMapper, $cardId, Acl::PERMISSION_READ);
$card = $this->cardMapper->find($cardId);
$assignedUsers = $this->assignedUsersMapper->find($card->getId());
$attachments = $this->attachmentService->findAll($cardId);
$attachments = $this->attachmentService->findAll($cardId, true);
$card->setAssignedUsers($assignedUsers);
$card->setAttachments($attachments);
return $card;

View File

@@ -30,6 +30,8 @@ use OCA\Deck\Db\AssignedUsersMapper;
use OCA\Deck\Db\Stack;
use OCA\Deck\Db\StackMapper;
use OCA\Deck\StatusException;
use OCP\ICache;
use OCP\ICacheFactory;
class StackService {
@@ -40,6 +42,7 @@ class StackService {
private $permissionService;
private $boardService;
private $assignedUsersMapper;
private $attachmentService;
public function __construct(
StackMapper $stackMapper,
@@ -47,7 +50,8 @@ class StackService {
LabelMapper $labelMapper,
PermissionService $permissionService,
BoardService $boardService,
AssignedUsersMapper $assignedUsersMapper
AssignedUsersMapper $assignedUsersMapper,
AttachmentService $attachmentService
) {
$this->stackMapper = $stackMapper;
$this->cardMapper = $cardMapper;
@@ -55,6 +59,7 @@ class StackService {
$this->permissionService = $permissionService;
$this->boardService = $boardService;
$this->assignedUsersMapper = $assignedUsersMapper;
$this->attachmentService = $attachmentService;
}
public function findAll($boardId) {
@@ -69,6 +74,7 @@ class StackService {
if (array_key_exists($card->id, $labels)) {
$cards[$cardIndex]->setLabels($labels[$card->id]);
}
$card->setAttachments($this->attachmentService->count($card->getId()));
}
$stacks[$stackIndex]->setCards($cards);
}