diff options
Diffstat (limited to 'application/bookmark')
-rw-r--r-- | application/bookmark/BookmarkFileService.php | 47 | ||||
-rw-r--r-- | application/bookmark/BookmarkServiceInterface.php | 32 |
2 files changed, 53 insertions, 26 deletions
diff --git a/application/bookmark/BookmarkFileService.php b/application/bookmark/BookmarkFileService.php index eb7899bf..3ea98a45 100644 --- a/application/bookmark/BookmarkFileService.php +++ b/application/bookmark/BookmarkFileService.php | |||
@@ -97,13 +97,16 @@ class BookmarkFileService implements BookmarkServiceInterface | |||
97 | /** | 97 | /** |
98 | * @inheritDoc | 98 | * @inheritDoc |
99 | */ | 99 | */ |
100 | public function findByHash(string $hash): Bookmark | 100 | public function findByHash(string $hash, string $privateKey = null): Bookmark |
101 | { | 101 | { |
102 | $bookmark = $this->bookmarkFilter->filter(BookmarkFilter::$FILTER_HASH, $hash); | 102 | $bookmark = $this->bookmarkFilter->filter(BookmarkFilter::$FILTER_HASH, $hash); |
103 | // PHP 7.3 introduced array_key_first() to avoid this hack | 103 | // PHP 7.3 introduced array_key_first() to avoid this hack |
104 | $first = reset($bookmark); | 104 | $first = reset($bookmark); |
105 | if (! $this->isLoggedIn && $first->isPrivate()) { | 105 | if (!$this->isLoggedIn |
106 | throw new Exception('Not authorized'); | 106 | && $first->isPrivate() |
107 | && (empty($privateKey) || $privateKey !== $first->getAdditionalContentEntry('private_key')) | ||
108 | ) { | ||
109 | throw new BookmarkNotFoundException(); | ||
107 | } | 110 | } |
108 | 111 | ||
109 | return $first; | 112 | return $first; |
@@ -340,26 +343,42 @@ class BookmarkFileService implements BookmarkServiceInterface | |||
340 | /** | 343 | /** |
341 | * @inheritDoc | 344 | * @inheritDoc |
342 | */ | 345 | */ |
343 | public function days(): array | 346 | public function findByDate( |
344 | { | 347 | \DateTimeInterface $from, |
345 | $bookmarkDays = []; | 348 | \DateTimeInterface $to, |
346 | foreach ($this->search() as $bookmark) { | 349 | ?\DateTimeInterface &$previous, |
347 | $bookmarkDays[$bookmark->getCreated()->format('Ymd')] = 0; | 350 | ?\DateTimeInterface &$next |
351 | ): array { | ||
352 | $out = []; | ||
353 | $previous = null; | ||
354 | $next = null; | ||
355 | |||
356 | foreach ($this->search([], null, false, false, true) as $bookmark) { | ||
357 | if ($to < $bookmark->getCreated()) { | ||
358 | $next = $bookmark->getCreated(); | ||
359 | } else if ($from < $bookmark->getCreated() && $to > $bookmark->getCreated()) { | ||
360 | $out[] = $bookmark; | ||
361 | } else { | ||
362 | if ($previous !== null) { | ||
363 | break; | ||
364 | } | ||
365 | $previous = $bookmark->getCreated(); | ||
366 | } | ||
348 | } | 367 | } |
349 | $bookmarkDays = array_keys($bookmarkDays); | ||
350 | sort($bookmarkDays); | ||
351 | 368 | ||
352 | return array_map('strval', $bookmarkDays); | 369 | return $out; |
353 | } | 370 | } |
354 | 371 | ||
355 | /** | 372 | /** |
356 | * @inheritDoc | 373 | * @inheritDoc |
357 | */ | 374 | */ |
358 | public function filterDay(string $request) | 375 | public function getLatest(): ?Bookmark |
359 | { | 376 | { |
360 | $visibility = $this->isLoggedIn ? BookmarkFilter::$ALL : BookmarkFilter::$PUBLIC; | 377 | foreach ($this->search([], null, false, false, true) as $bookmark) { |
378 | return $bookmark; | ||
379 | } | ||
361 | 380 | ||
362 | return $this->bookmarkFilter->filter(BookmarkFilter::$FILTER_DAY, $request, false, $visibility); | 381 | return null; |
363 | } | 382 | } |
364 | 383 | ||
365 | /** | 384 | /** |
diff --git a/application/bookmark/BookmarkServiceInterface.php b/application/bookmark/BookmarkServiceInterface.php index 37a54d03..08cdbb4e 100644 --- a/application/bookmark/BookmarkServiceInterface.php +++ b/application/bookmark/BookmarkServiceInterface.php | |||
@@ -20,13 +20,14 @@ interface BookmarkServiceInterface | |||
20 | /** | 20 | /** |
21 | * Find a bookmark by hash | 21 | * Find a bookmark by hash |
22 | * | 22 | * |
23 | * @param string $hash | 23 | * @param string $hash Bookmark's hash |
24 | * @param string|null $privateKey Optional key used to access private links while logged out | ||
24 | * | 25 | * |
25 | * @return Bookmark | 26 | * @return Bookmark |
26 | * | 27 | * |
27 | * @throws \Exception | 28 | * @throws \Exception |
28 | */ | 29 | */ |
29 | public function findByHash(string $hash): Bookmark; | 30 | public function findByHash(string $hash, string $privateKey = null); |
30 | 31 | ||
31 | /** | 32 | /** |
32 | * @param $url | 33 | * @param $url |
@@ -155,22 +156,29 @@ interface BookmarkServiceInterface | |||
155 | public function bookmarksCountPerTag(array $filteringTags = [], ?string $visibility = null): array; | 156 | public function bookmarksCountPerTag(array $filteringTags = [], ?string $visibility = null): array; |
156 | 157 | ||
157 | /** | 158 | /** |
158 | * Returns the list of days containing articles (oldest first) | 159 | * Return a list of bookmark matching provided period of time. |
160 | * It also update directly previous and next date outside of given period found in the datastore. | ||
159 | * | 161 | * |
160 | * @return array containing days (in format YYYYMMDD). | 162 | * @param \DateTimeInterface $from Starting date. |
163 | * @param \DateTimeInterface $to Ending date. | ||
164 | * @param \DateTimeInterface|null $previous (by reference) updated with first created date found before $from. | ||
165 | * @param \DateTimeInterface|null $next (by reference) updated with first created date found after $to. | ||
166 | * | ||
167 | * @return array List of bookmarks matching provided period of time. | ||
161 | */ | 168 | */ |
162 | public function days(): array; | 169 | public function findByDate( |
170 | \DateTimeInterface $from, | ||
171 | \DateTimeInterface $to, | ||
172 | ?\DateTimeInterface &$previous, | ||
173 | ?\DateTimeInterface &$next | ||
174 | ): array; | ||
163 | 175 | ||
164 | /** | 176 | /** |
165 | * Returns the list of articles for a given day. | 177 | * Returns the latest bookmark by creation date. |
166 | * | ||
167 | * @param string $request day to filter. Format: YYYYMMDD. | ||
168 | * | 178 | * |
169 | * @return Bookmark[] list of shaare found. | 179 | * @return Bookmark|null Found Bookmark or null if the datastore is empty. |
170 | * | ||
171 | * @throws BookmarkNotFoundException | ||
172 | */ | 180 | */ |
173 | public function filterDay(string $request); | 181 | public function getLatest(): ?Bookmark; |
174 | 182 | ||
175 | /** | 183 | /** |
176 | * Creates the default database after a fresh install. | 184 | * Creates the default database after a fresh install. |