mirror of
https://github.com/wallabag/wallabag.git
synced 2025-02-17 03:05:19 +00:00
Little refacto and send 400 on reaching urls limit
This commit is contained in:
parent
efd351c98f
commit
72db15ca5d
2 changed files with 97 additions and 106 deletions
|
@ -5,7 +5,7 @@ namespace Wallabag\ApiBundle\Controller;
|
||||||
use Hateoas\Configuration\Route;
|
use Hateoas\Configuration\Route;
|
||||||
use Hateoas\Representation\Factory\PagerfantaFactory;
|
use Hateoas\Representation\Factory\PagerfantaFactory;
|
||||||
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
|
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
|
||||||
use Symfony\Component\Config\Definition\Exception\Exception;
|
use Symfony\Component\HttpKernel\Exception\HttpException;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
||||||
|
@ -45,9 +45,7 @@ class EntryRestController extends WallabagRestController
|
||||||
$results[$url] = $res instanceof Entry ? $res->getId() : false;
|
$results[$url] = $res instanceof Entry ? $res->getId() : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$json = $this->get('serializer')->serialize($results, 'json');
|
return $this->sendResponse($results);
|
||||||
|
|
||||||
return (new JsonResponse())->setJson($json);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// let's see if it is a simple url?
|
// let's see if it is a simple url?
|
||||||
|
@ -63,9 +61,7 @@ class EntryRestController extends WallabagRestController
|
||||||
|
|
||||||
$exists = $res instanceof Entry ? $res->getId() : false;
|
$exists = $res instanceof Entry ? $res->getId() : false;
|
||||||
|
|
||||||
$json = $this->get('serializer')->serialize(['exists' => $exists], 'json');
|
return $this->sendResponse(['exists' => $exists]);
|
||||||
|
|
||||||
return (new JsonResponse())->setJson($json);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -125,9 +121,7 @@ class EntryRestController extends WallabagRestController
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
$json = $this->get('serializer')->serialize($paginatedCollection, 'json');
|
return $this->sendResponse($paginatedCollection);
|
||||||
|
|
||||||
return (new JsonResponse())->setJson($json);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -146,9 +140,7 @@ class EntryRestController extends WallabagRestController
|
||||||
$this->validateAuthentication();
|
$this->validateAuthentication();
|
||||||
$this->validateUserAccess($entry->getUser()->getId());
|
$this->validateUserAccess($entry->getUser()->getId());
|
||||||
|
|
||||||
$json = $this->get('serializer')->serialize($entry, 'json');
|
return $this->sendResponse($entry);
|
||||||
|
|
||||||
return (new JsonResponse())->setJson($json);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -189,35 +181,35 @@ class EntryRestController extends WallabagRestController
|
||||||
$this->validateAuthentication();
|
$this->validateAuthentication();
|
||||||
|
|
||||||
$urls = json_decode($request->query->get('urls', []));
|
$urls = json_decode($request->query->get('urls', []));
|
||||||
|
|
||||||
|
if (empty($urls)) {
|
||||||
|
return $this->sendResponse([]);
|
||||||
|
}
|
||||||
|
|
||||||
$results = [];
|
$results = [];
|
||||||
|
|
||||||
// handle multiple urls
|
// handle multiple urls
|
||||||
if (!empty($urls)) {
|
foreach ($urls as $key => $url) {
|
||||||
$results = [];
|
$entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId(
|
||||||
foreach ($urls as $key => $url) {
|
$url,
|
||||||
$entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId(
|
$this->getUser()->getId()
|
||||||
$url,
|
);
|
||||||
$this->getUser()->getId()
|
|
||||||
);
|
|
||||||
|
|
||||||
$results[$key]['url'] = $url;
|
$results[$key]['url'] = $url;
|
||||||
|
|
||||||
if (false !== $entry) {
|
if (false !== $entry) {
|
||||||
$em = $this->getDoctrine()->getManager();
|
$em = $this->getDoctrine()->getManager();
|
||||||
$em->remove($entry);
|
$em->remove($entry);
|
||||||
$em->flush();
|
$em->flush();
|
||||||
|
|
||||||
// entry deleted, dispatch event about it!
|
// entry deleted, dispatch event about it!
|
||||||
$this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry));
|
$this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry));
|
||||||
}
|
|
||||||
|
|
||||||
$results[$key]['entry'] = $entry instanceof Entry ? true : false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$results[$key]['entry'] = $entry instanceof Entry ? true : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$json = $this->get('serializer')->serialize($results, 'json');
|
return $this->sendResponse($results);
|
||||||
|
|
||||||
return (new JsonResponse())->setJson($json);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -231,7 +223,7 @@ class EntryRestController extends WallabagRestController
|
||||||
*
|
*
|
||||||
* @return JsonResponse
|
* @return JsonResponse
|
||||||
*
|
*
|
||||||
* @throws Symfony\Component\Config\Definition\Exception\Exception When limit is reached
|
* @throws HttpException When limit is reached
|
||||||
*/
|
*/
|
||||||
public function postEntriesListAction(Request $request)
|
public function postEntriesListAction(Request $request)
|
||||||
{
|
{
|
||||||
|
@ -243,7 +235,7 @@ class EntryRestController extends WallabagRestController
|
||||||
$limit = $this->container->getParameter('wallabag_core.api_limit_mass_actions');
|
$limit = $this->container->getParameter('wallabag_core.api_limit_mass_actions');
|
||||||
|
|
||||||
if (count($urls) > $limit) {
|
if (count($urls) > $limit) {
|
||||||
throw new Exception('API limit reached');
|
throw new HttpException(400, 'API limit reached');
|
||||||
}
|
}
|
||||||
|
|
||||||
// handle multiple urls
|
// handle multiple urls
|
||||||
|
@ -274,9 +266,7 @@ class EntryRestController extends WallabagRestController
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$json = $this->get('serializer')->serialize($results, 'json');
|
return $this->sendResponse($results);
|
||||||
|
|
||||||
return (new JsonResponse())->setJson($json);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -336,9 +326,7 @@ class EntryRestController extends WallabagRestController
|
||||||
// entry saved, dispatch event about it!
|
// entry saved, dispatch event about it!
|
||||||
$this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry));
|
$this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry));
|
||||||
|
|
||||||
$json = $this->get('serializer')->serialize($entry, 'json');
|
return $this->sendResponse($entry);
|
||||||
|
|
||||||
return (new JsonResponse())->setJson($json);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -387,9 +375,7 @@ class EntryRestController extends WallabagRestController
|
||||||
$em = $this->getDoctrine()->getManager();
|
$em = $this->getDoctrine()->getManager();
|
||||||
$em->flush();
|
$em->flush();
|
||||||
|
|
||||||
$json = $this->get('serializer')->serialize($entry, 'json');
|
return $this->sendResponse($entry);
|
||||||
|
|
||||||
return (new JsonResponse())->setJson($json);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -432,9 +418,7 @@ class EntryRestController extends WallabagRestController
|
||||||
// entry saved, dispatch event about it!
|
// entry saved, dispatch event about it!
|
||||||
$this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry));
|
$this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry));
|
||||||
|
|
||||||
$json = $this->get('serializer')->serialize($entry, 'json');
|
return $this->sendResponse($entry);
|
||||||
|
|
||||||
return (new JsonResponse())->setJson($json);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -460,9 +444,7 @@ class EntryRestController extends WallabagRestController
|
||||||
// entry deleted, dispatch event about it!
|
// entry deleted, dispatch event about it!
|
||||||
$this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry));
|
$this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry));
|
||||||
|
|
||||||
$json = $this->get('serializer')->serialize($entry, 'json');
|
return $this->sendResponse($entry);
|
||||||
|
|
||||||
return (new JsonResponse())->setJson($json);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -481,9 +463,7 @@ class EntryRestController extends WallabagRestController
|
||||||
$this->validateAuthentication();
|
$this->validateAuthentication();
|
||||||
$this->validateUserAccess($entry->getUser()->getId());
|
$this->validateUserAccess($entry->getUser()->getId());
|
||||||
|
|
||||||
$json = $this->get('serializer')->serialize($entry->getTags(), 'json');
|
return $this->sendResponse($entry->getTags());
|
||||||
|
|
||||||
return (new JsonResponse())->setJson($json);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -514,9 +494,7 @@ class EntryRestController extends WallabagRestController
|
||||||
$em->persist($entry);
|
$em->persist($entry);
|
||||||
$em->flush();
|
$em->flush();
|
||||||
|
|
||||||
$json = $this->get('serializer')->serialize($entry, 'json');
|
return $this->sendResponse($entry);
|
||||||
|
|
||||||
return (new JsonResponse())->setJson($json);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -541,9 +519,7 @@ class EntryRestController extends WallabagRestController
|
||||||
$em->persist($entry);
|
$em->persist($entry);
|
||||||
$em->flush();
|
$em->flush();
|
||||||
|
|
||||||
$json = $this->get('serializer')->serialize($entry, 'json');
|
return $this->sendResponse($entry);
|
||||||
|
|
||||||
return (new JsonResponse())->setJson($json);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -562,45 +538,46 @@ class EntryRestController extends WallabagRestController
|
||||||
$this->validateAuthentication();
|
$this->validateAuthentication();
|
||||||
|
|
||||||
$list = json_decode($request->query->get('list', []));
|
$list = json_decode($request->query->get('list', []));
|
||||||
$results = [];
|
|
||||||
|
if (empty($list)) {
|
||||||
|
return $this->sendResponse([]);
|
||||||
|
}
|
||||||
|
|
||||||
// handle multiple urls
|
// handle multiple urls
|
||||||
if (!empty($list)) {
|
$results = [];
|
||||||
foreach ($list as $key => $element) {
|
|
||||||
$entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId(
|
|
||||||
$element->url,
|
|
||||||
$this->getUser()->getId()
|
|
||||||
);
|
|
||||||
|
|
||||||
$results[$key]['url'] = $element->url;
|
foreach ($list as $key => $element) {
|
||||||
$results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false;
|
$entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId(
|
||||||
|
$element->url,
|
||||||
|
$this->getUser()->getId()
|
||||||
|
);
|
||||||
|
|
||||||
$tags = $element->tags;
|
$results[$key]['url'] = $element->url;
|
||||||
|
$results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false;
|
||||||
|
|
||||||
if (false !== $entry && !(empty($tags))) {
|
$tags = $element->tags;
|
||||||
$tags = explode(',', $tags);
|
|
||||||
foreach ($tags as $label) {
|
|
||||||
$label = trim($label);
|
|
||||||
|
|
||||||
$tag = $this->getDoctrine()
|
if (false !== $entry && !(empty($tags))) {
|
||||||
->getRepository('WallabagCoreBundle:Tag')
|
$tags = explode(',', $tags);
|
||||||
->findOneByLabel($label);
|
foreach ($tags as $label) {
|
||||||
|
$label = trim($label);
|
||||||
|
|
||||||
if (false !== $tag) {
|
$tag = $this->getDoctrine()
|
||||||
$entry->removeTag($tag);
|
->getRepository('WallabagCoreBundle:Tag')
|
||||||
}
|
->findOneByLabel($label);
|
||||||
|
|
||||||
|
if (false !== $tag) {
|
||||||
|
$entry->removeTag($tag);
|
||||||
}
|
}
|
||||||
|
|
||||||
$em = $this->getDoctrine()->getManager();
|
|
||||||
$em->persist($entry);
|
|
||||||
$em->flush();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$em = $this->getDoctrine()->getManager();
|
||||||
|
$em->persist($entry);
|
||||||
|
$em->flush();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$json = $this->get('serializer')->serialize($results, 'json');
|
return $this->sendResponse($results);
|
||||||
|
|
||||||
return (new JsonResponse())->setJson($json);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -619,32 +596,47 @@ class EntryRestController extends WallabagRestController
|
||||||
$this->validateAuthentication();
|
$this->validateAuthentication();
|
||||||
|
|
||||||
$list = json_decode($request->query->get('list', []));
|
$list = json_decode($request->query->get('list', []));
|
||||||
|
|
||||||
|
if (empty($list)) {
|
||||||
|
return $this->sendResponse([]);
|
||||||
|
}
|
||||||
|
|
||||||
$results = [];
|
$results = [];
|
||||||
|
|
||||||
// handle multiple urls
|
// handle multiple urls
|
||||||
if (!empty($list)) {
|
foreach ($list as $key => $element) {
|
||||||
foreach ($list as $key => $element) {
|
$entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId(
|
||||||
$entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId(
|
$element->url,
|
||||||
$element->url,
|
$this->getUser()->getId()
|
||||||
$this->getUser()->getId()
|
);
|
||||||
);
|
|
||||||
|
|
||||||
$results[$key]['url'] = $element->url;
|
$results[$key]['url'] = $element->url;
|
||||||
$results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false;
|
$results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false;
|
||||||
|
|
||||||
$tags = $element->tags;
|
$tags = $element->tags;
|
||||||
|
|
||||||
if (false !== $entry && !(empty($tags))) {
|
if (false !== $entry && !(empty($tags))) {
|
||||||
$this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
|
$this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
|
||||||
|
|
||||||
$em = $this->getDoctrine()->getManager();
|
$em = $this->getDoctrine()->getManager();
|
||||||
$em->persist($entry);
|
$em->persist($entry);
|
||||||
$em->flush();
|
$em->flush();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$json = $this->get('serializer')->serialize($results, 'json');
|
return $this->sendResponse($results);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Shortcut to send data serialized in json.
|
||||||
|
*
|
||||||
|
* @param mixed $data
|
||||||
|
*
|
||||||
|
* @return JsonResponse
|
||||||
|
*/
|
||||||
|
private function sendResponse($data)
|
||||||
|
{
|
||||||
|
$json = $this->get('serializer')->serialize($data, 'json');
|
||||||
|
|
||||||
return (new JsonResponse())->setJson($json);
|
return (new JsonResponse())->setJson($json);
|
||||||
}
|
}
|
||||||
|
|
|
@ -810,10 +810,6 @@ class EntryRestControllerTest extends WallabagApiTestCase
|
||||||
$this->assertEquals('http://0.0.0.0/entry3', $content[1]['url']);
|
$this->assertEquals('http://0.0.0.0/entry3', $content[1]['url']);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @expectedException Symfony\Component\Config\Definition\Exception\Exception
|
|
||||||
* @expectedExceptionMessage API limit reached
|
|
||||||
*/
|
|
||||||
public function testLimitBulkAction()
|
public function testLimitBulkAction()
|
||||||
{
|
{
|
||||||
$list = [
|
$list = [
|
||||||
|
@ -831,5 +827,8 @@ class EntryRestControllerTest extends WallabagApiTestCase
|
||||||
];
|
];
|
||||||
|
|
||||||
$this->client->request('POST', '/api/entries/lists?urls='.json_encode($list));
|
$this->client->request('POST', '/api/entries/lists?urls='.json_encode($list));
|
||||||
|
|
||||||
|
$this->assertEquals(400, $this->client->getResponse()->getStatusCode());
|
||||||
|
$this->assertContains('API limit reached', $this->client->getResponse()->getContent());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue