Skip to content

Commit

Permalink
Add etags to list all endpoints
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Härtl <jus@bitgrid.net>
  • Loading branch information
juliushaertl committed Sep 15, 2020
1 parent 8b346f1 commit 6840327
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
9 changes: 7 additions & 2 deletions lib/Controller/AttachmentApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

/**
Expand Down
10 changes: 9 additions & 1 deletion lib/Controller/BoardApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,24 @@ 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) {
throw new StatusException('Invalid If-Modified-Since header provided.');
}
$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;
}

/**
Expand Down
8 changes: 7 additions & 1 deletion lib/Controller/StackApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

/**
Expand Down
4 changes: 4 additions & 0 deletions lib/Db/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,8 @@ public function __construct() {
$this->addResolvable('createdBy');
$this->addRelation('extendedData');
}

public function getETag(): string {
return md5($this->getLastModified());
}
}

0 comments on commit 6840327

Please sign in to comment.