]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Add public filter/field in the API
authorJeremy Benoist <jeremy.benoist@gmail.com>
Sat, 10 Jun 2017 13:31:57 +0000 (15:31 +0200)
committerJeremy Benoist <jeremy.benoist@gmail.com>
Sat, 10 Jun 2017 13:31:57 +0000 (15:31 +0200)
Listing entries can now be filtered by “public”.
Creating or patching an entry can now set is to public or remove the public.
Entry response now include “is_public” boolean field

src/Wallabag/ApiBundle/Controller/EntryRestController.php
src/Wallabag/CoreBundle/Entity/Entry.php
src/Wallabag/CoreBundle/Repository/EntryRepository.php
tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php

index 09b73ccb4edb81c6393c9fa085f77726e1cf8ba7..ae9a93aa3cbea81a4d1a1dbf6b16de7ea8f859b6 100644 (file)
@@ -77,6 +77,7 @@ class EntryRestController extends WallabagRestController
      *          {"name"="perPage", "dataType"="integer", "required"=false, "format"="default'30'", "description"="results per page."},
      *          {"name"="tags", "dataType"="string", "required"=false, "format"="api,rest", "description"="a list of tags url encoded. Will returns entries that matches ALL tags."},
      *          {"name"="since", "dataType"="integer", "required"=false, "format"="default '0'", "description"="The timestamp since when you want entries updated."},
+     *          {"name"="public", "dataType"="integer", "required"=false, "format"="1 or 0, all entries by default", "description"="filter by entries with a public link"},
      *       }
      * )
      *
@@ -88,6 +89,7 @@ class EntryRestController extends WallabagRestController
 
         $isArchived = (null === $request->query->get('archive')) ? null : (bool) $request->query->get('archive');
         $isStarred = (null === $request->query->get('starred')) ? null : (bool) $request->query->get('starred');
+        $isPublic = (null === $request->query->get('public')) ? null : (bool) $request->query->get('public');
         $sort = $request->query->get('sort', 'created');
         $order = $request->query->get('order', 'desc');
         $page = (int) $request->query->get('page', 1);
@@ -96,9 +98,16 @@ class EntryRestController extends WallabagRestController
         $since = $request->query->get('since', 0);
 
         /** @var \Pagerfanta\Pagerfanta $pager */
-        $pager = $this->getDoctrine()
-            ->getRepository('WallabagCoreBundle:Entry')
-            ->findEntries($this->getUser()->getId(), $isArchived, $isStarred, $sort, $order, $since, $tags);
+        $pager = $this->get('wallabag_core.entry_repository')->findEntries(
+            $this->getUser()->getId(),
+            $isArchived,
+            $isStarred,
+            $isPublic,
+            $sort,
+            $order,
+            $since,
+            $tags
+        );
 
         $pager->setMaxPerPage($perPage);
         $pager->setCurrentPage($page);
@@ -111,6 +120,7 @@ class EntryRestController extends WallabagRestController
                 [
                     'archive' => $isArchived,
                     'starred' => $isStarred,
+                    'public' => $isPublic,
                     'sort' => $sort,
                     'order' => $order,
                     'page' => $page,
@@ -289,6 +299,7 @@ class EntryRestController extends WallabagRestController
      *          {"name"="preview_picture", "dataType"="string", "required"=false, "description"="Preview picture of the entry"},
      *          {"name"="published_at", "dataType"="datetime|integer", "format"="YYYY-MM-DDTHH:II:SS+TZ or a timestamp", "required"=false, "description"="Published date of the entry"},
      *          {"name"="authors", "dataType"="string", "format"="Name Firstname,author2,author3", "required"=false, "description"="Authors of the entry"},
+     *          {"name"="public", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="will generate a public link for the entry"},
      *       }
      * )
      *
@@ -332,6 +343,7 @@ class EntryRestController extends WallabagRestController
      *          {"name"="preview_picture", "dataType"="string", "required"=false, "description"="Preview picture of the entry"},
      *          {"name"="published_at", "dataType"="datetime|integer", "format"="YYYY-MM-DDTHH:II:SS+TZ or a timestamp", "required"=false, "description"="Published date of the entry"},
      *          {"name"="authors", "dataType"="string", "format"="Name Firstname,author2,author3", "required"=false, "description"="Authors of the entry"},
+     *          {"name"="public", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="will generate a public link for the entry"},
      *      }
      * )
      *
@@ -623,6 +635,7 @@ class EntryRestController extends WallabagRestController
         $tags = $request->request->get('tags', []);
         $isArchived = $request->request->get('archive');
         $isStarred = $request->request->get('starred');
+        $isPublic = $request->request->get('public');
         $content = $request->request->get('content');
         $language = $request->request->get('language');
         $picture = $request->request->get('preview_picture');
@@ -666,6 +679,14 @@ class EntryRestController extends WallabagRestController
             $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $tags);
         }
 
+        if (!is_null($isPublic)) {
+            $entry->cleanUid();
+
+            if (true === (bool) $isPublic && null === $entry->getUid()) {
+                $entry->generateUid();
+            }
+        }
+
         $em = $this->getDoctrine()->getManager();
         $em->persist($entry);
         $em->flush();
index 365030c7b8b79bcb85e3de039866d5fb57606fad..07d41ed8f8c65748586041eeb3118d26a6294605 100644 (file)
@@ -687,6 +687,10 @@ class Entry
     /**
      * Used in the entries filter so it's more explicit for the end user than the uid.
      *
+     * @VirtualProperty
+     * @SerializedName("is_public")
+     * @Groups({"entries_for_user"})
+     *
      * @return bool
      */
     public function isPublic()
index 6972e974eeeb1853d28472f5be3ebf1b881ea9fa..9bda4e15e81fcfd8d7e33635607fefa7db01c723 100644 (file)
@@ -135,6 +135,7 @@ class EntryRepository extends EntityRepository
      * @param int    $userId
      * @param bool   $isArchived
      * @param bool   $isStarred
+     * @param bool   $isPublic
      * @param string $sort
      * @param string $order
      * @param int    $since
@@ -142,18 +143,22 @@ class EntryRepository extends EntityRepository
      *
      * @return array
      */
-    public function findEntries($userId, $isArchived = null, $isStarred = null, $sort = 'created', $order = 'ASC', $since = 0, $tags = '')
+    public function findEntries($userId, $isArchived = null, $isStarred = null, $isPublic = null, $sort = 'created', $order = 'ASC', $since = 0, $tags = '')
     {
         $qb = $this->createQueryBuilder('e')
             ->leftJoin('e.tags', 't')
             ->where('e.user =:userId')->setParameter('userId', $userId);
 
         if (null !== $isArchived) {
-            $qb->andWhere('e.isArchived =:isArchived')->setParameter('isArchived', (bool) $isArchived);
+            $qb->andWhere('e.isArchived = :isArchived')->setParameter('isArchived', (bool) $isArchived);
         }
 
         if (null !== $isStarred) {
-            $qb->andWhere('e.isStarred =:isStarred')->setParameter('isStarred', (bool) $isStarred);
+            $qb->andWhere('e.isStarred = :isStarred')->setParameter('isStarred', (bool) $isStarred);
+        }
+
+        if (null !== $isPublic) {
+            $qb->andWhere('e.uid IS '.(true === $isPublic ? 'NOT' : '').' NULL');
         }
 
         if ($since > 0) {
index 4aa60e90d265592e73b9745c7143d6d73d6117a1..067aed2c8dd97bffc6300c9b17ea9f5b75ced15c 100644 (file)
@@ -128,6 +128,7 @@ class EntryRestControllerTest extends WallabagApiTestCase
             'perPage' => 2,
             'tags' => 'foo',
             'since' => 1443274283,
+            'public' => 0,
         ]);
 
         $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
@@ -154,6 +155,53 @@ class EntryRestControllerTest extends WallabagApiTestCase
             $this->assertContains('order=asc', $content['_links'][$link]['href']);
             $this->assertContains('tags=foo', $content['_links'][$link]['href']);
             $this->assertContains('since=1443274283', $content['_links'][$link]['href']);
+            $this->assertContains('public=0', $content['_links'][$link]['href']);
+        }
+
+        $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type'));
+    }
+
+    public function testGetEntriesPublicOnly()
+    {
+        $entry = $this->client->getContainer()
+            ->get('doctrine.orm.entity_manager')
+            ->getRepository('WallabagCoreBundle:Entry')
+            ->findOneByUser(1);
+
+        if (!$entry) {
+            $this->markTestSkipped('No content found in db.');
+        }
+
+        // generate at least one public entry
+        $entry->generateUid();
+
+        $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
+        $em->persist($entry);
+        $em->flush();
+
+        $this->client->request('GET', '/api/entries', [
+            'public' => 1,
+        ]);
+
+        $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
+
+        $content = json_decode($this->client->getResponse()->getContent(), true);
+
+        $this->assertGreaterThanOrEqual(1, count($content));
+        $this->assertArrayHasKey('items', $content['_embedded']);
+        $this->assertGreaterThanOrEqual(1, $content['total']);
+        $this->assertEquals(1, $content['page']);
+        $this->assertEquals(30, $content['limit']);
+        $this->assertGreaterThanOrEqual(1, $content['pages']);
+
+        $this->assertArrayHasKey('_links', $content);
+        $this->assertArrayHasKey('self', $content['_links']);
+        $this->assertArrayHasKey('first', $content['_links']);
+        $this->assertArrayHasKey('last', $content['_links']);
+
+        foreach (['self', 'first', 'last'] as $link) {
+            $this->assertArrayHasKey('href', $content['_links'][$link]);
+            $this->assertContains('public=1', $content['_links'][$link]['href']);
         }
 
         $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type'));
@@ -348,6 +396,7 @@ class EntryRestControllerTest extends WallabagApiTestCase
             'language' => 'de',
             'published_at' => '2016-09-08T11:55:58+0200',
             'authors' => 'bob,helen',
+            'public' => 1,
         ]);
 
         $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
@@ -367,6 +416,7 @@ class EntryRestControllerTest extends WallabagApiTestCase
         $this->assertCount(2, $content['published_by']);
         $this->assertContains('bob', $content['published_by']);
         $this->assertContains('helen', $content['published_by']);
+        $this->assertTrue($content['is_public'], 'A public link has been generated for that entry');
     }
 
     public function testPostSameEntry()
@@ -481,6 +531,7 @@ class EntryRestControllerTest extends WallabagApiTestCase
             'preview_picture' => 'http://preview.io/picture.jpg',
             'authors' => 'bob,sponge',
             'content' => 'awesome',
+            'public' => 0,
         ]);
 
         $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
@@ -497,6 +548,7 @@ class EntryRestControllerTest extends WallabagApiTestCase
         $this->assertContains('sponge', $content['published_by']);
         $this->assertContains('bob', $content['published_by']);
         $this->assertEquals('awesome', $content['content']);
+        $this->assertFalse($content['is_public'], 'Entry is no more shared');
     }
 
     public function testPatchEntryWithoutQuotes()