aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2020-08-29 11:45:08 +0200
committerArthurHoaro <arthur@hoa.ro>2020-08-29 11:45:08 +0200
commitb06fc28aa32f477e1785cd998385fdb490bc5ebf (patch)
tree65daa380574c5452a3e3c5679ea438115b4fb3bb
parentbea062149ebcb4663861edb1cc0a32faf85b273f (diff)
downloadShaarli-b06fc28aa32f477e1785cd998385fdb490bc5ebf.tar.gz
Shaarli-b06fc28aa32f477e1785cd998385fdb490bc5ebf.tar.zst
Shaarli-b06fc28aa32f477e1785cd998385fdb490bc5ebf.zip
REST API: allow override of creation and update dates
Note that if they're not provided, default behaviour will apply: creation and update dates will be autogenerated, and not empty. Fixes #1223
-rw-r--r--application/api/ApiUtils.php11
-rw-r--r--application/api/controllers/ApiController.php3
-rw-r--r--application/api/controllers/Links.php4
-rw-r--r--tests/api/controllers/links/PostLinkTest.php8
4 files changed, 18 insertions, 8 deletions
diff --git a/application/api/ApiUtils.php b/application/api/ApiUtils.php
index faebb8f5..4a6326f0 100644
--- a/application/api/ApiUtils.php
+++ b/application/api/ApiUtils.php
@@ -94,7 +94,7 @@ class ApiUtils
94 * 94 *
95 * @return Bookmark instance. 95 * @return Bookmark instance.
96 */ 96 */
97 public static function buildLinkFromRequest($input, $defaultPrivate) 97 public static function buildBookmarkFromRequest($input, $defaultPrivate): Bookmark
98 { 98 {
99 $bookmark = new Bookmark(); 99 $bookmark = new Bookmark();
100 $url = ! empty($input['url']) ? cleanup_url($input['url']) : ''; 100 $url = ! empty($input['url']) ? cleanup_url($input['url']) : '';
@@ -110,6 +110,15 @@ class ApiUtils
110 $bookmark->setTags(! empty($input['tags']) ? $input['tags'] : []); 110 $bookmark->setTags(! empty($input['tags']) ? $input['tags'] : []);
111 $bookmark->setPrivate($private); 111 $bookmark->setPrivate($private);
112 112
113 $created = \DateTime::createFromFormat(\DateTime::ATOM, $input['created'] ?? '');
114 if ($created instanceof \DateTimeInterface) {
115 $bookmark->setCreated($created);
116 }
117 $updated = \DateTime::createFromFormat(\DateTime::ATOM, $input['updated'] ?? '');
118 if ($updated instanceof \DateTimeInterface) {
119 $bookmark->setUpdated($updated);
120 }
121
113 return $bookmark; 122 return $bookmark;
114 } 123 }
115 124
diff --git a/application/api/controllers/ApiController.php b/application/api/controllers/ApiController.php
index c4b3d0c3..88a845eb 100644
--- a/application/api/controllers/ApiController.php
+++ b/application/api/controllers/ApiController.php
@@ -4,6 +4,7 @@ namespace Shaarli\Api\Controllers;
4 4
5use Shaarli\Bookmark\BookmarkServiceInterface; 5use Shaarli\Bookmark\BookmarkServiceInterface;
6use Shaarli\Config\ConfigManager; 6use Shaarli\Config\ConfigManager;
7use Shaarli\History;
7use Slim\Container; 8use Slim\Container;
8 9
9/** 10/**
@@ -31,7 +32,7 @@ abstract class ApiController
31 protected $bookmarkService; 32 protected $bookmarkService;
32 33
33 /** 34 /**
34 * @var HistoryController 35 * @var History
35 */ 36 */
36 protected $history; 37 protected $history;
37 38
diff --git a/application/api/controllers/Links.php b/application/api/controllers/Links.php
index 29247950..778097fd 100644
--- a/application/api/controllers/Links.php
+++ b/application/api/controllers/Links.php
@@ -116,7 +116,7 @@ class Links extends ApiController
116 public function postLink($request, $response) 116 public function postLink($request, $response)
117 { 117 {
118 $data = $request->getParsedBody(); 118 $data = $request->getParsedBody();
119 $bookmark = ApiUtils::buildLinkFromRequest($data, $this->conf->get('privacy.default_private_links')); 119 $bookmark = ApiUtils::buildBookmarkFromRequest($data, $this->conf->get('privacy.default_private_links'));
120 // duplicate by URL, return 409 Conflict 120 // duplicate by URL, return 409 Conflict
121 if (! empty($bookmark->getUrl()) 121 if (! empty($bookmark->getUrl())
122 && ! empty($dup = $this->bookmarkService->findByUrl($bookmark->getUrl())) 122 && ! empty($dup = $this->bookmarkService->findByUrl($bookmark->getUrl()))
@@ -155,7 +155,7 @@ class Links extends ApiController
155 $index = index_url($this->ci['environment']); 155 $index = index_url($this->ci['environment']);
156 $data = $request->getParsedBody(); 156 $data = $request->getParsedBody();
157 157
158 $requestBookmark = ApiUtils::buildLinkFromRequest($data, $this->conf->get('privacy.default_private_links')); 158 $requestBookmark = ApiUtils::buildBookmarkFromRequest($data, $this->conf->get('privacy.default_private_links'));
159 // duplicate URL on a different link, return 409 Conflict 159 // duplicate URL on a different link, return 409 Conflict
160 if (! empty($requestBookmark->getUrl()) 160 if (! empty($requestBookmark->getUrl())
161 && ! empty($dup = $this->bookmarkService->findByUrl($requestBookmark->getUrl())) 161 && ! empty($dup = $this->bookmarkService->findByUrl($requestBookmark->getUrl()))
diff --git a/tests/api/controllers/links/PostLinkTest.php b/tests/api/controllers/links/PostLinkTest.php
index 4e791a04..f969fe1c 100644
--- a/tests/api/controllers/links/PostLinkTest.php
+++ b/tests/api/controllers/links/PostLinkTest.php
@@ -160,6 +160,8 @@ class PostLinkTest extends TestCase
160 'description' => 'shaare description', 160 'description' => 'shaare description',
161 'tags' => ['one', 'two'], 161 'tags' => ['one', 'two'],
162 'private' => true, 162 'private' => true,
163 'created' => '2015-05-05T12:30:00+03:00',
164 'updated' => '2016-06-05T14:32:10+03:00',
163 ]; 165 ];
164 $env = Environment::mock([ 166 $env = Environment::mock([
165 'REQUEST_METHOD' => 'POST', 167 'REQUEST_METHOD' => 'POST',
@@ -181,10 +183,8 @@ class PostLinkTest extends TestCase
181 $this->assertEquals($link['description'], $data['description']); 183 $this->assertEquals($link['description'], $data['description']);
182 $this->assertEquals($link['tags'], $data['tags']); 184 $this->assertEquals($link['tags'], $data['tags']);
183 $this->assertEquals(true, $data['private']); 185 $this->assertEquals(true, $data['private']);
184 $this->assertTrue( 186 $this->assertSame($link['created'], $data['created']);
185 new \DateTime('2 seconds ago') < \DateTime::createFromFormat(\DateTime::ATOM, $data['created']) 187 $this->assertSame($link['updated'], $data['updated']);
186 );
187 $this->assertEquals('', $data['updated']);
188 } 188 }
189 189
190 /** 190 /**