diff options
Diffstat (limited to 'src/Wallabag/ApiBundle/Controller/EntryRestController.php')
-rw-r--r-- | src/Wallabag/ApiBundle/Controller/EntryRestController.php | 68 |
1 files changed, 33 insertions, 35 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index 33b75665..06520af9 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php | |||
@@ -4,14 +4,12 @@ namespace Wallabag\ApiBundle\Controller; | |||
4 | 4 | ||
5 | use Hateoas\Configuration\Route; | 5 | use Hateoas\Configuration\Route; |
6 | use Hateoas\Representation\Factory\PagerfantaFactory; | 6 | use Hateoas\Representation\Factory\PagerfantaFactory; |
7 | use JMS\Serializer\SerializationContext; | ||
8 | use Nelmio\ApiDocBundle\Annotation\ApiDoc; | 7 | use Nelmio\ApiDocBundle\Annotation\ApiDoc; |
9 | use Symfony\Component\HttpFoundation\JsonResponse; | 8 | use Symfony\Component\HttpFoundation\JsonResponse; |
10 | use Symfony\Component\HttpFoundation\Request; | 9 | use Symfony\Component\HttpFoundation\Request; |
11 | use Symfony\Component\HttpFoundation\Response; | 10 | use Symfony\Component\HttpFoundation\Response; |
12 | use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; | 11 | use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
13 | use Symfony\Component\HttpKernel\Exception\HttpException; | 12 | use Symfony\Component\HttpKernel\Exception\HttpException; |
14 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | ||
15 | use Wallabag\CoreBundle\Entity\Entry; | 13 | use Wallabag\CoreBundle\Entity\Entry; |
16 | use Wallabag\CoreBundle\Entity\Tag; | 14 | use Wallabag\CoreBundle\Entity\Tag; |
17 | use Wallabag\CoreBundle\Event\EntryDeletedEvent; | 15 | use Wallabag\CoreBundle\Event\EntryDeletedEvent; |
@@ -29,8 +27,10 @@ class EntryRestController extends WallabagRestController | |||
29 | * @ApiDoc( | 27 | * @ApiDoc( |
30 | * parameters={ | 28 | * parameters={ |
31 | * {"name"="return_id", "dataType"="string", "required"=false, "format"="1 or 0", "description"="Set 1 if you want to retrieve ID in case entry(ies) exists, 0 by default"}, | 29 | * {"name"="return_id", "dataType"="string", "required"=false, "format"="1 or 0", "description"="Set 1 if you want to retrieve ID in case entry(ies) exists, 0 by default"}, |
32 | * {"name"="url", "dataType"="string", "required"=true, "format"="An url", "description"="Url to check if it exists"}, | 30 | * {"name"="url", "dataType"="string", "required"=true, "format"="An url", "description"="DEPRECATED, use hashed_url instead"}, |
33 | * {"name"="urls", "dataType"="string", "required"=false, "format"="An array of urls (?urls[]=http...&urls[]=http...)", "description"="Urls (as an array) to check if it exists"} | 31 | * {"name"="urls", "dataType"="string", "required"=false, "format"="An array of urls (?urls[]=http...&urls[]=http...)", "description"="DEPRECATED, use hashed_urls instead"}, |
32 | * {"name"="hashed_url", "dataType"="string", "required"=false, "format"="A hashed url", "description"="Hashed url using SHA1 to check if it exists"}, | ||
33 | * {"name"="hashed_urls", "dataType"="string", "required"=false, "format"="An array of hashed urls (?hashed_urls[]=xxx...&hashed_urls[]=xxx...)", "description"="An array of hashed urls using SHA1 to check if they exist"} | ||
34 | * } | 34 | * } |
35 | * ) | 35 | * ) |
36 | * | 36 | * |
@@ -39,17 +39,30 @@ class EntryRestController extends WallabagRestController | |||
39 | public function getEntriesExistsAction(Request $request) | 39 | public function getEntriesExistsAction(Request $request) |
40 | { | 40 | { |
41 | $this->validateAuthentication(); | 41 | $this->validateAuthentication(); |
42 | $repo = $this->getDoctrine()->getRepository('WallabagCoreBundle:Entry'); | ||
42 | 43 | ||
43 | $returnId = (null === $request->query->get('return_id')) ? false : (bool) $request->query->get('return_id'); | 44 | $returnId = (null === $request->query->get('return_id')) ? false : (bool) $request->query->get('return_id'); |
45 | |||
44 | $urls = $request->query->get('urls', []); | 46 | $urls = $request->query->get('urls', []); |
47 | $hashedUrls = $request->query->get('hashed_urls', []); | ||
45 | 48 | ||
46 | // handle multiple urls first | 49 | // handle multiple urls first |
50 | if (!empty($hashedUrls)) { | ||
51 | $results = []; | ||
52 | foreach ($hashedUrls as $hashedUrl) { | ||
53 | $res = $repo->findByHashedUrlAndUserId($hashedUrl, $this->getUser()->getId()); | ||
54 | |||
55 | $results[$hashedUrl] = $this->returnExistInformation($res, $returnId); | ||
56 | } | ||
57 | |||
58 | return $this->sendResponse($results); | ||
59 | } | ||
60 | |||
61 | // @deprecated, to be remove in 3.0 | ||
47 | if (!empty($urls)) { | 62 | if (!empty($urls)) { |
48 | $results = []; | 63 | $results = []; |
49 | foreach ($urls as $url) { | 64 | foreach ($urls as $url) { |
50 | $res = $this->getDoctrine() | 65 | $res = $repo->findByUrlAndUserId($url, $this->getUser()->getId()); |
51 | ->getRepository('WallabagCoreBundle:Entry') | ||
52 | ->findByUrlAndUserId($url, $this->getUser()->getId()); | ||
53 | 66 | ||
54 | $results[$url] = $this->returnExistInformation($res, $returnId); | 67 | $results[$url] = $this->returnExistInformation($res, $returnId); |
55 | } | 68 | } |
@@ -59,18 +72,21 @@ class EntryRestController extends WallabagRestController | |||
59 | 72 | ||
60 | // let's see if it is a simple url? | 73 | // let's see if it is a simple url? |
61 | $url = $request->query->get('url', ''); | 74 | $url = $request->query->get('url', ''); |
75 | $hashedUrl = $request->query->get('hashed_url', ''); | ||
62 | 76 | ||
63 | if (empty($url)) { | 77 | if (empty($url) && empty($hashedUrl)) { |
64 | throw $this->createAccessDeniedException('URL is empty?, logged user id: ' . $this->getUser()->getId()); | 78 | throw $this->createAccessDeniedException('URL is empty?, logged user id: ' . $this->getUser()->getId()); |
65 | } | 79 | } |
66 | 80 | ||
67 | $res = $this->getDoctrine() | 81 | $method = 'findByUrlAndUserId'; |
68 | ->getRepository('WallabagCoreBundle:Entry') | 82 | if (!empty($hashedUrl)) { |
69 | ->findByUrlAndUserId($url, $this->getUser()->getId()); | 83 | $method = 'findByHashedUrlAndUserId'; |
84 | $url = $hashedUrl; | ||
85 | } | ||
70 | 86 | ||
71 | $exists = $this->returnExistInformation($res, $returnId); | 87 | $res = $repo->$method($url, $this->getUser()->getId()); |
72 | 88 | ||
73 | return $this->sendResponse(['exists' => $exists]); | 89 | return $this->sendResponse(['exists' => $this->returnExistInformation($res, $returnId)]); |
74 | } | 90 | } |
75 | 91 | ||
76 | /** | 92 | /** |
@@ -80,7 +96,7 @@ class EntryRestController extends WallabagRestController | |||
80 | * parameters={ | 96 | * parameters={ |
81 | * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0, all entries by default", "description"="filter by archived status."}, | 97 | * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0, all entries by default", "description"="filter by archived status."}, |
82 | * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0, all entries by default", "description"="filter by starred status."}, | 98 | * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0, all entries by default", "description"="filter by starred status."}, |
83 | * {"name"="sort", "dataType"="string", "required"=false, "format"="'created' or 'updated', default 'created'", "description"="sort entries by date."}, | 99 | * {"name"="sort", "dataType"="string", "required"=false, "format"="'created' or 'updated' or 'archived', default 'created'", "description"="sort entries by date."}, |
84 | * {"name"="order", "dataType"="string", "required"=false, "format"="'asc' or 'desc', default 'desc'", "description"="order of sort."}, | 100 | * {"name"="order", "dataType"="string", "required"=false, "format"="'asc' or 'desc', default 'desc'", "description"="order of sort."}, |
85 | * {"name"="page", "dataType"="integer", "required"=false, "format"="default '1'", "description"="what page you want."}, | 101 | * {"name"="page", "dataType"="integer", "required"=false, "format"="default '1'", "description"="what page you want."}, |
86 | * {"name"="perPage", "dataType"="integer", "required"=false, "format"="default'30'", "description"="results per page."}, | 102 | * {"name"="perPage", "dataType"="integer", "required"=false, "format"="default'30'", "description"="results per page."}, |
@@ -141,7 +157,7 @@ class EntryRestController extends WallabagRestController | |||
141 | 'tags' => $tags, | 157 | 'tags' => $tags, |
142 | 'since' => $since, | 158 | 'since' => $since, |
143 | ], | 159 | ], |
144 | UrlGeneratorInterface::ABSOLUTE_URL | 160 | true |
145 | ) | 161 | ) |
146 | ); | 162 | ); |
147 | 163 | ||
@@ -363,7 +379,7 @@ class EntryRestController extends WallabagRestController | |||
363 | } | 379 | } |
364 | 380 | ||
365 | if (null !== $data['isArchived']) { | 381 | if (null !== $data['isArchived']) { |
366 | $entry->setArchived((bool) $data['isArchived']); | 382 | $entry->updateArchived((bool) $data['isArchived']); |
367 | } | 383 | } |
368 | 384 | ||
369 | if (null !== $data['isStarred']) { | 385 | if (null !== $data['isStarred']) { |
@@ -479,7 +495,7 @@ class EntryRestController extends WallabagRestController | |||
479 | } | 495 | } |
480 | 496 | ||
481 | if (null !== $data['isArchived']) { | 497 | if (null !== $data['isArchived']) { |
482 | $entry->setArchived((bool) $data['isArchived']); | 498 | $entry->updateArchived((bool) $data['isArchived']); |
483 | } | 499 | } |
484 | 500 | ||
485 | if (null !== $data['isStarred']) { | 501 | if (null !== $data['isStarred']) { |
@@ -787,24 +803,6 @@ class EntryRestController extends WallabagRestController | |||
787 | } | 803 | } |
788 | 804 | ||
789 | /** | 805 | /** |
790 | * Shortcut to send data serialized in json. | ||
791 | * | ||
792 | * @param mixed $data | ||
793 | * | ||
794 | * @return JsonResponse | ||
795 | */ | ||
796 | private function sendResponse($data) | ||
797 | { | ||
798 | // https://github.com/schmittjoh/JMSSerializerBundle/issues/293 | ||
799 | $context = new SerializationContext(); | ||
800 | $context->setSerializeNull(true); | ||
801 | |||
802 | $json = $this->get('jms_serializer')->serialize($data, 'json', $context); | ||
803 | |||
804 | return (new JsonResponse())->setJson($json); | ||
805 | } | ||
806 | |||
807 | /** | ||
808 | * Retrieve value from the request. | 806 | * Retrieve value from the request. |
809 | * Used for POST & PATCH on a an entry. | 807 | * Used for POST & PATCH on a an entry. |
810 | * | 808 | * |