From 684032781a8d55a6fdc89627e4c46d90ed2d3665 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Tue, 15 Sep 2020 22:47:54 +0200 Subject: [PATCH] Add etags to list all endpoints MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- lib/Controller/AttachmentApiController.php | 9 +++++++-- lib/Controller/BoardApiController.php | 10 +++++++++- lib/Controller/StackApiController.php | 8 +++++++- lib/Db/Attachment.php | 4 ++++ 4 files changed, 27 insertions(+), 4 deletions(-) diff --git a/lib/Controller/AttachmentApiController.php b/lib/Controller/AttachmentApiController.php index 54741613b..558716723 100644 --- a/lib/Controller/AttachmentApiController.php +++ b/lib/Controller/AttachmentApiController.php @@ -22,6 +22,7 @@ */ namespace OCA\Deck\Controller; +use OCA\Deck\Db\Attachment; use OCP\AppFramework\ApiController; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; @@ -43,8 +44,12 @@ public function __construct($appName, IRequest $request, AttachmentService $atta * */ public function getAll() { - $attachment = $this->attachmentService->findAll($this->request->getParam('cardId'), true); - return new DataResponse($attachment, HTTP::STATUS_OK); + $attachments = $this->attachmentService->findAll($this->request->getParam('cardId'), true); + $response = new DataResponse($attachments, HTTP::STATUS_OK); + $response->setETag(md5(json_encode(array_map(function(Attachment $attachment) { + return $attachment->getId() . '-' . $attachment->getETag(); + }, $attachments)))); + return $response; } /** diff --git a/lib/Controller/BoardApiController.php b/lib/Controller/BoardApiController.php index d7afc6dcc..fb3eb3990 100644 --- a/lib/Controller/BoardApiController.php +++ b/lib/Controller/BoardApiController.php @@ -63,8 +63,12 @@ public function __construct($appName, IRequest $request, BoardService $service, */ public function index($details = null) { $modified = $this->request->getHeader('If-Modified-Since'); + $etag = null; if ($modified === null || $modified === '') { $boards = $this->boardService->findAll(0, $details); + $etag = md5(json_encode(array_map(function(Board $board) { + return $board->getId() . '-' . $board->getETag(); + }, $boards))); } else { $date = Util::parseHTTPDate($modified); if (!$date) { @@ -72,7 +76,11 @@ public function index($details = null) { } $boards = $this->boardService->findAll($date->getTimestamp(), $details); } - return new DataResponse($boards, HTTP::STATUS_OK); + $response = new DataResponse($boards, HTTP::STATUS_OK); + if($etag) { + $response->setETag($etag); + } + return $response; } /** diff --git a/lib/Controller/StackApiController.php b/lib/Controller/StackApiController.php index d39bd7f5b..16707d7b8 100644 --- a/lib/Controller/StackApiController.php +++ b/lib/Controller/StackApiController.php @@ -24,6 +24,7 @@ namespace OCA\Deck\Controller; +use OCA\Deck\Db\Stack; use OCA\Deck\StatusException; use OCP\AppFramework\ApiController; use OCP\AppFramework\Http; @@ -71,7 +72,12 @@ public function index() { $since = $date->getTimestamp(); } $stacks = $this->stackService->findAll($this->request->getParam('boardId'), $since); - return new DataResponse($stacks, HTTP::STATUS_OK); + $response = new DataResponse($stacks, HTTP::STATUS_OK); + $etag = md5(json_encode(array_map(function(Stack $stack) { + return $stack->getId() . '-' . $stack->getETag(); + }, $stacks))); + $response->setETag($etag); + return $response; } /** diff --git a/lib/Db/Attachment.php b/lib/Db/Attachment.php index 23a07fcb2..89bc0aff2 100644 --- a/lib/Db/Attachment.php +++ b/lib/Db/Attachment.php @@ -42,4 +42,8 @@ public function __construct() { $this->addResolvable('createdBy'); $this->addRelation('extendedData'); } + + public function getETag(): string { + return md5($this->getLastModified()); + } }