]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Merge pull request #3053 from wallabag/api-bulk-add
authorNicolas LÅ“uillet <nicolas@loeuillet.org>
Fri, 5 May 2017 11:56:34 +0000 (13:56 +0200)
committerGitHub <noreply@github.com>
Fri, 5 May 2017 11:56:34 +0000 (13:56 +0200)
Added API endpoint to handle a list of URL

app/config/config.yml
src/Wallabag/ApiBundle/Controller/EntryRestController.php
src/Wallabag/CoreBundle/DependencyInjection/Configuration.php
src/Wallabag/CoreBundle/DependencyInjection/WallabagCoreExtension.php
tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php

index 4f4fb900e325e6950eaf405bf85492d550e75fd7..451809d640557c86c553d39663702c0fdf229ca3 100644 (file)
@@ -55,6 +55,7 @@ wallabag_core:
     list_mode: 0
     fetching_error_message: |
         wallabag can't retrieve contents for this article. Please <a href="http://doc.wallabag.org/en/master/user/errors_during_fetching.html#how-can-i-help-to-fix-that">troubleshoot this issue</a>.
+    api_limit_mass_actions: 10
 
 wallabag_user:
     registration_enabled: "%fosuser_registration%"
index 7590efbb16b5325102ccea35afdae450bd7581b6..dbff606547c1e59ee96447046bee322d44c7bf35 100644 (file)
@@ -5,6 +5,7 @@ namespace Wallabag\ApiBundle\Controller;
 use Hateoas\Configuration\Route;
 use Hateoas\Representation\Factory\PagerfantaFactory;
 use Nelmio\ApiDocBundle\Annotation\ApiDoc;
+use Symfony\Component\HttpKernel\Exception\HttpException;
 use Symfony\Component\HttpFoundation\Request;
 use Symfony\Component\HttpFoundation\JsonResponse;
 use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
@@ -44,9 +45,7 @@ class EntryRestController extends WallabagRestController
                 $results[$url] = $res instanceof Entry ? $res->getId() : false;
             }
 
-            $json = $this->get('serializer')->serialize($results, 'json');
-
-            return (new JsonResponse())->setJson($json);
+            return $this->sendResponse($results);
         }
 
         // let's see if it is a simple url?
@@ -62,9 +61,7 @@ class EntryRestController extends WallabagRestController
 
         $exists = $res instanceof Entry ? $res->getId() : false;
 
-        $json = $this->get('serializer')->serialize(['exists' => $exists], 'json');
-
-        return (new JsonResponse())->setJson($json);
+        return $this->sendResponse(['exists' => $exists]);
     }
 
     /**
@@ -124,9 +121,7 @@ class EntryRestController extends WallabagRestController
             )
         );
 
-        $json = $this->get('serializer')->serialize($paginatedCollection, 'json');
-
-        return (new JsonResponse())->setJson($json);
+        return $this->sendResponse($paginatedCollection);
     }
 
     /**
@@ -145,9 +140,7 @@ class EntryRestController extends WallabagRestController
         $this->validateAuthentication();
         $this->validateUserAccess($entry->getUser()->getId());
 
-        $json = $this->get('serializer')->serialize($entry, 'json');
-
-        return (new JsonResponse())->setJson($json);
+        return $this->sendResponse($entry);
     }
 
     /**
@@ -172,6 +165,110 @@ class EntryRestController extends WallabagRestController
             ->exportAs($request->attributes->get('_format'));
     }
 
+    /**
+     * Handles an entries list and delete URL.
+     *
+     * @ApiDoc(
+     *       parameters={
+     *          {"name"="urls", "dataType"="string", "required"=true, "format"="A JSON array of urls [{'url': 'http://...'}, {'url': 'http://...'}]", "description"="Urls (as an array) to delete."}
+     *       }
+     * )
+     *
+     * @return JsonResponse
+     */
+    public function deleteEntriesListAction(Request $request)
+    {
+        $this->validateAuthentication();
+
+        $urls = json_decode($request->query->get('urls', []));
+
+        if (empty($urls)) {
+            return $this->sendResponse([]);
+        }
+
+        $results = [];
+
+        // handle multiple urls
+        foreach ($urls as $key => $url) {
+            $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId(
+                $url,
+                $this->getUser()->getId()
+            );
+
+            $results[$key]['url'] = $url;
+
+            if (false !== $entry) {
+                $em = $this->getDoctrine()->getManager();
+                $em->remove($entry);
+                $em->flush();
+
+                // entry deleted, dispatch event about it!
+                $this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry));
+            }
+
+            $results[$key]['entry'] = $entry instanceof Entry ? true : false;
+        }
+
+        return $this->sendResponse($results);
+    }
+
+    /**
+     * Handles an entries list and create URL.
+     *
+     * @ApiDoc(
+     *       parameters={
+     *          {"name"="urls", "dataType"="string", "required"=true, "format"="A JSON array of urls [{'url': 'http://...'}, {'url': 'http://...'}]", "description"="Urls (as an array) to create."}
+     *       }
+     * )
+     *
+     * @return JsonResponse
+     *
+     * @throws HttpException When limit is reached
+     */
+    public function postEntriesListAction(Request $request)
+    {
+        $this->validateAuthentication();
+
+        $urls = json_decode($request->query->get('urls', []));
+        $results = [];
+
+        $limit = $this->container->getParameter('wallabag_core.api_limit_mass_actions');
+
+        if (count($urls) > $limit) {
+            throw new HttpException(400, 'API limit reached');
+        }
+
+        // handle multiple urls
+        if (!empty($urls)) {
+            foreach ($urls as $key => $url) {
+                $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId(
+                    $url,
+                    $this->getUser()->getId()
+                );
+
+                $results[$key]['url'] = $url;
+
+                if (false === $entry) {
+                    $entry = $this->get('wallabag_core.content_proxy')->updateEntry(
+                        new Entry($this->getUser()),
+                        $url
+                    );
+                }
+
+                $em = $this->getDoctrine()->getManager();
+                $em->persist($entry);
+                $em->flush();
+
+                $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false;
+
+                // entry saved, dispatch event about it!
+                $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry));
+            }
+        }
+
+        return $this->sendResponse($results);
+    }
+
     /**
      * Create an entry.
      *
@@ -229,9 +326,7 @@ class EntryRestController extends WallabagRestController
         // entry saved, dispatch event about it!
         $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry));
 
-        $json = $this->get('serializer')->serialize($entry, 'json');
-
-        return (new JsonResponse())->setJson($json);
+        return $this->sendResponse($entry);
     }
 
     /**
@@ -280,9 +375,7 @@ class EntryRestController extends WallabagRestController
         $em = $this->getDoctrine()->getManager();
         $em->flush();
 
-        $json = $this->get('serializer')->serialize($entry, 'json');
-
-        return (new JsonResponse())->setJson($json);
+        return $this->sendResponse($entry);
     }
 
     /**
@@ -325,9 +418,7 @@ class EntryRestController extends WallabagRestController
         // entry saved, dispatch event about it!
         $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry));
 
-        $json = $this->get('serializer')->serialize($entry, 'json');
-
-        return (new JsonResponse())->setJson($json);
+        return $this->sendResponse($entry);
     }
 
     /**
@@ -353,9 +444,7 @@ class EntryRestController extends WallabagRestController
         // entry deleted, dispatch event about it!
         $this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry));
 
-        $json = $this->get('serializer')->serialize($entry, 'json');
-
-        return (new JsonResponse())->setJson($json);
+        return $this->sendResponse($entry);
     }
 
     /**
@@ -374,9 +463,7 @@ class EntryRestController extends WallabagRestController
         $this->validateAuthentication();
         $this->validateUserAccess($entry->getUser()->getId());
 
-        $json = $this->get('serializer')->serialize($entry->getTags(), 'json');
-
-        return (new JsonResponse())->setJson($json);
+        return $this->sendResponse($entry->getTags());
     }
 
     /**
@@ -407,9 +494,7 @@ class EntryRestController extends WallabagRestController
         $em->persist($entry);
         $em->flush();
 
-        $json = $this->get('serializer')->serialize($entry, 'json');
-
-        return (new JsonResponse())->setJson($json);
+        return $this->sendResponse($entry);
     }
 
     /**
@@ -434,9 +519,7 @@ class EntryRestController extends WallabagRestController
         $em->persist($entry);
         $em->flush();
 
-        $json = $this->get('serializer')->serialize($entry, 'json');
-
-        return (new JsonResponse())->setJson($json);
+        return $this->sendResponse($entry);
     }
 
     /**
@@ -455,45 +538,46 @@ class EntryRestController extends WallabagRestController
         $this->validateAuthentication();
 
         $list = json_decode($request->query->get('list', []));
-        $results = [];
+
+        if (empty($list)) {
+            return $this->sendResponse([]);
+        }
 
         // handle multiple urls
-        if (!empty($list)) {
-            foreach ($list as $key => $element) {
-                $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId(
-                    $element->url,
-                    $this->getUser()->getId()
-                );
+        $results = [];
 
-                $results[$key]['url'] = $element->url;
-                $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false;
+        foreach ($list as $key => $element) {
+            $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 = explode(',', $tags);
-                    foreach ($tags as $label) {
-                        $label = trim($label);
+            $tags = $element->tags;
 
-                        $tag = $this->getDoctrine()
-                            ->getRepository('WallabagCoreBundle:Tag')
-                            ->findOneByLabel($label);
+            if (false !== $entry && !(empty($tags))) {
+                $tags = explode(',', $tags);
+                foreach ($tags as $label) {
+                    $label = trim($label);
 
-                        if (false !== $tag) {
-                            $entry->removeTag($tag);
-                        }
-                    }
+                    $tag = $this->getDoctrine()
+                        ->getRepository('WallabagCoreBundle:Tag')
+                        ->findOneByLabel($label);
 
-                    $em = $this->getDoctrine()->getManager();
-                    $em->persist($entry);
-                    $em->flush();
+                    if (false !== $tag) {
+                        $entry->removeTag($tag);
+                    }
                 }
+
+                $em = $this->getDoctrine()->getManager();
+                $em->persist($entry);
+                $em->flush();
             }
         }
 
-        $json = $this->get('serializer')->serialize($results, 'json');
-
-        return (new JsonResponse())->setJson($json);
+        return $this->sendResponse($results);
     }
 
     /**
@@ -512,32 +596,47 @@ class EntryRestController extends WallabagRestController
         $this->validateAuthentication();
 
         $list = json_decode($request->query->get('list', []));
+
+        if (empty($list)) {
+            return $this->sendResponse([]);
+        }
+
         $results = [];
 
         // handle multiple urls
-        if (!empty($list)) {
-            foreach ($list as $key => $element) {
-                $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId(
-                    $element->url,
-                    $this->getUser()->getId()
-                );
+        foreach ($list as $key => $element) {
+            $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId(
+                $element->url,
+                $this->getUser()->getId()
+            );
 
-                $results[$key]['url'] = $element->url;
-                $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false;
+            $results[$key]['url'] = $element->url;
+            $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false;
 
-                $tags = $element->tags;
+            $tags = $element->tags;
 
-                if (false !== $entry && !(empty($tags))) {
-                    $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
+            if (false !== $entry && !(empty($tags))) {
+                $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
 
-                    $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);
+    }
+
+    /**
+     * 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);
     }
index 006a18c397a6ceea925ef29dcd5ea254c528eb52..75b37729f38eb601c093bd9b786602c63b4c5291 100644 (file)
@@ -47,6 +47,9 @@ class Configuration implements ConfigurationInterface
                 ->scalarNode('list_mode')
                     ->defaultValue(1)
                 ->end()
+                ->scalarNode('api_limit_mass_actions')
+                    ->defaultValue(10)
+                ->end()
             ->end()
         ;
 
index aa9ee339adebd24a1e7d9282eb6b70714396e740..c075c19fd993c48d6c77ee88bf34f545d8d3e336 100644 (file)
@@ -26,6 +26,7 @@ class WallabagCoreExtension extends Extension
         $container->setParameter('wallabag_core.action_mark_as_read', $config['action_mark_as_read']);
         $container->setParameter('wallabag_core.list_mode', $config['list_mode']);
         $container->setParameter('wallabag_core.fetching_error_message', $config['fetching_error_message']);
+        $container->setParameter('wallabag_core.api_limit_mass_actions', $config['api_limit_mass_actions']);
 
         $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
         $loader->load('services.yml');
index e6ffd6641a319f6575f2ce41fbbd6729e09ecc1f..f0173cefce92dfdafa9c523192a7ead9be8fb242 100644 (file)
@@ -766,20 +766,69 @@ class EntryRestControllerTest extends WallabagApiTestCase
             ],
         ];
 
-        $this->client->request('DELETE', '/api/entries/tags/list?list='.json_encode($list));
+        $this->client->request('DELETE', '/api/entries/tags/list?list=' . json_encode($list));
+    }
+
+
+    public function testPostEntriesListAction()
+    {
+        $list = [
+            'http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html',
+            'http://0.0.0.0/entry2',
+        ];
+
+        $this->client->request('POST', '/api/entries/lists?urls='.json_encode($list));
 
         $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
 
         $content = json_decode($this->client->getResponse()->getContent(), true);
 
         $this->assertInternalType('int', $content[0]['entry']);
-        $this->assertEquals('http://0.0.0.0/entry4', $content[0]['url']);
+        $this->assertEquals('http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html', $content[0]['url']);
 
-        $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager')
-            ->getRepository('WallabagCoreBundle:Entry')
-            ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
+        $this->assertInternalType('int', $content[1]['entry']);
+        $this->assertEquals('http://0.0.0.0/entry2', $content[1]['url']);
+    }
 
-        $tags = $entry->getTags();
-        $this->assertCount(2, $tags);
+    public function testDeleteEntriesListAction()
+    {
+        $list = [
+            'http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html',
+            'http://0.0.0.0/entry3',
+        ];
+
+        $this->client->request('DELETE', '/api/entries/list?urls='.json_encode($list));
+
+        $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
+
+        $content = json_decode($this->client->getResponse()->getContent(), true);
+
+        $this->assertTrue($content[0]['entry']);
+        $this->assertEquals('http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html', $content[0]['url']);
+
+        $this->assertFalse($content[1]['entry']);
+        $this->assertEquals('http://0.0.0.0/entry3', $content[1]['url']);
+    }
+
+    public function testLimitBulkAction()
+    {
+        $list = [
+            'http://0.0.0.0/entry1',
+            'http://0.0.0.0/entry1',
+            'http://0.0.0.0/entry1',
+            'http://0.0.0.0/entry1',
+            'http://0.0.0.0/entry1',
+            'http://0.0.0.0/entry1',
+            'http://0.0.0.0/entry1',
+            'http://0.0.0.0/entry1',
+            'http://0.0.0.0/entry1',
+            'http://0.0.0.0/entry1',
+            'http://0.0.0.0/entry1',
+        ];
+
+        $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());
     }
 }