From: Jérémy Benoist Date: Tue, 28 May 2019 12:18:33 +0000 (+0200) Subject: Merge pull request #3944 from shtrom/always-hash-exists-url X-Git-Url: https://git.immae.eu/?a=commitdiff_plain;h=2cbee36a0184786644470a84fdd8c720cfcac58e;hp=-c;p=github%2Fwallabag%2Fwallabag.git Merge pull request #3944 from shtrom/always-hash-exists-url Always hash exists url --- 2cbee36a0184786644470a84fdd8c720cfcac58e diff --combined src/Wallabag/ApiBundle/Controller/EntryRestController.php index d9d99c85,bdd02129..aaacdcdc --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php @@@ -14,6 -14,7 +14,7 @@@ use Wallabag\CoreBundle\Entity\Entry use Wallabag\CoreBundle\Entity\Tag; use Wallabag\CoreBundle\Event\EntryDeletedEvent; use Wallabag\CoreBundle\Event\EntrySavedEvent; + use Wallabag\CoreBundle\Helper\UrlHasher; class EntryRestController extends WallabagRestController { @@@ -43,50 -44,45 +44,45 @@@ $returnId = (null === $request->query->get('return_id')) ? false : (bool) $request->query->get('return_id'); - $urls = $request->query->get('urls', []); $hashedUrls = $request->query->get('hashed_urls', []); + $hashedUrl = $request->query->get('hashed_url', ''); + if (!empty($hashedUrl)) { + $hashedUrls[] = $hashedUrl; + } - // handle multiple urls first - if (!empty($hashedUrls)) { - $results = []; - foreach ($hashedUrls as $hashedUrl) { - $res = $repo->findByHashedUrlAndUserId($hashedUrl, $this->getUser()->getId()); - - $results[$hashedUrl] = $this->returnExistInformation($res, $returnId); - } + $urls = $request->query->get('urls', []); + $url = $request->query->get('url', ''); + if (!empty($url)) { + $urls[] = $url; + } - return $this->sendResponse($results); + $urlHashMap = []; + foreach ($urls as $urlToHash) { + $urlHash = UrlHasher::hashUrl($urlToHash); + $hashedUrls[] = $urlHash; + $urlHashMap[$urlHash] = $urlToHash; } - // @deprecated, to be remove in 3.0 - if (!empty($urls)) { - $results = []; - foreach ($urls as $url) { - $res = $repo->findByUrlAndUserId($url, $this->getUser()->getId()); + if (empty($hashedUrls)) { + throw $this->createAccessDeniedException('URL is empty?, logged user id: ' . $this->getUser()->getId()); + } - $results[$url] = $this->returnExistInformation($res, $returnId); - } + $results = []; + foreach ($hashedUrls as $hashedUrlToSearch) { + $res = $repo->findByHashedUrlAndUserId($hashedUrlToSearch, $this->getUser()->getId()); - return $this->sendResponse($results); + $results[$hashedUrlToSearch] = $this->returnExistInformation($res, $returnId); } - // let's see if it is a simple url? - $url = $request->query->get('url', ''); - $hashedUrl = $request->query->get('hashed_url', ''); + $results = $this->replaceUrlHashes($results, $urlHashMap); - if (empty($url) && empty($hashedUrl)) { - throw $this->createAccessDeniedException('URL is empty?, logged user id: ' . $this->getUser()->getId()); - } + if (!empty($url) || !empty($hashedUrl)) { + $hu = array_keys($results)[0]; - $method = 'findByUrlAndUserId'; - if (!empty($hashedUrl)) { - $method = 'findByHashedUrlAndUserId'; - $url = $hashedUrl; + return $this->sendResponse(['exists' => $results[$hu]]); } - $res = $repo->$method($url, $this->getUser()->getId()); - - return $this->sendResponse(['exists' => $this->returnExistInformation($res, $returnId)]); + return $this->sendResponse($results); } /** @@@ -369,7 -365,9 +365,7 @@@ 'language' => !empty($data['language']) ? $data['language'] : $entry->getLanguage(), 'date' => !empty($data['publishedAt']) ? $data['publishedAt'] : $entry->getPublishedAt(), // faking the open graph preview picture - 'open_graph' => [ - 'og_image' => !empty($data['picture']) ? $data['picture'] : $entry->getPreviewPicture(), - ], + 'image' => !empty($data['picture']) ? $data['picture'] : $entry->getPreviewPicture(), 'authors' => \is_string($data['authors']) ? explode(',', $data['authors']) : $entry->getPublishedBy(), ] ); @@@ -804,6 -802,24 +800,24 @@@ return $this->sendResponse($results); } + /** + * Replace the hashedUrl keys in $results with the unhashed URL from the + * request, as recorded in $urlHashMap. + */ + private function replaceUrlHashes(array $results, array $urlHashMap) + { + $newResults = []; + foreach ($results as $hash => $res) { + if (isset($urlHashMap[$hash])) { + $newResults[$urlHashMap[$hash]] = $res; + } else { + $newResults[$hash] = $res; + } + } + + return $newResults; + } + /** * Retrieve value from the request. * Used for POST & PATCH on a an entry. @@@ -832,8 -848,8 +846,8 @@@ /** * Return information about the entry if it exist and depending on the id or not. * - * @param Entry|null $entry - * @param bool $returnId + * @param Entry|bool|null $entry + * @param bool $returnId * * @return bool|int */