]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - src/Wallabag/ApiBundle/Controller/WallabagRestController.php
Merge pull request #2301 from wallabag/fix-rss-feeds
[github/wallabag/wallabag.git] / src / Wallabag / ApiBundle / Controller / WallabagRestController.php
index 4fae4b0a8a07842eeb2ed53cd87a056ac641e117..fb7c6c1f14a0ff90f0601241f043c7047b89edb7 100644 (file)
@@ -7,7 +7,7 @@ use Hateoas\Configuration\Route;
 use Hateoas\Representation\Factory\PagerfantaFactory;
 use Nelmio\ApiDocBundle\Annotation\ApiDoc;
 use Symfony\Component\HttpFoundation\Request;
-use Symfony\Component\HttpFoundation\Response;
+use Symfony\Component\HttpFoundation\JsonResponse;
 use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
 use Symfony\Component\Security\Core\Exception\AccessDeniedException;
 use Wallabag\CoreBundle\Entity\Entry;
@@ -38,7 +38,7 @@ class WallabagRestController extends FOSRestController
      *       }
      * )
      *
-     * @return Response
+     * @return JsonResponse
      */
     public function getEntriesAction(Request $request)
     {
@@ -51,10 +51,11 @@ class WallabagRestController extends FOSRestController
         $page = (int) $request->query->get('page', 1);
         $perPage = (int) $request->query->get('perPage', 30);
         $since = $request->query->get('since', 0);
+        $tags = $request->query->get('tags', '');
 
         $pager = $this->getDoctrine()
             ->getRepository('WallabagCoreBundle:Entry')
-            ->findEntries($this->getUser()->getId(), $isArchived, $isStarred, $sort, $order, $since);
+            ->findEntries($this->getUser()->getId(), $isArchived, $isStarred, $sort, $order, $since, $tags);
 
         $pager->setCurrentPage($page);
         $pager->setMaxPerPage($perPage);
@@ -67,7 +68,7 @@ class WallabagRestController extends FOSRestController
 
         $json = $this->get('serializer')->serialize($paginatedCollection, 'json');
 
-        return $this->renderJsonResponse($json);
+        return (new JsonResponse())->setJson($json);
     }
 
     /**
@@ -79,7 +80,7 @@ class WallabagRestController extends FOSRestController
      *      }
      * )
      *
-     * @return Response
+     * @return JsonResponse
      */
     public function getEntryAction(Entry $entry)
     {
@@ -88,7 +89,7 @@ class WallabagRestController extends FOSRestController
 
         $json = $this->get('serializer')->serialize($entry, 'json');
 
-        return $this->renderJsonResponse($json);
+        return (new JsonResponse())->setJson($json);
     }
 
     /**
@@ -104,7 +105,7 @@ class WallabagRestController extends FOSRestController
      *       }
      * )
      *
-     * @return Response
+     * @return JsonResponse
      */
     public function postEntriesAction(Request $request)
     {
@@ -148,7 +149,7 @@ class WallabagRestController extends FOSRestController
 
         $json = $this->get('serializer')->serialize($entry, 'json');
 
-        return $this->renderJsonResponse($json);
+        return (new JsonResponse())->setJson($json);
     }
 
     /**
@@ -166,7 +167,7 @@ class WallabagRestController extends FOSRestController
      *      }
      * )
      *
-     * @return Response
+     * @return JsonResponse
      */
     public function patchEntriesAction(Entry $entry, Request $request)
     {
@@ -199,7 +200,7 @@ class WallabagRestController extends FOSRestController
 
         $json = $this->get('serializer')->serialize($entry, 'json');
 
-        return $this->renderJsonResponse($json);
+        return (new JsonResponse())->setJson($json);
     }
 
     /**
@@ -211,7 +212,7 @@ class WallabagRestController extends FOSRestController
      *      }
      * )
      *
-     * @return Response
+     * @return JsonResponse
      */
     public function deleteEntriesAction(Entry $entry)
     {
@@ -224,7 +225,7 @@ class WallabagRestController extends FOSRestController
 
         $json = $this->get('serializer')->serialize($entry, 'json');
 
-        return $this->renderJsonResponse($json);
+        return (new JsonResponse())->setJson($json);
     }
 
     /**
@@ -236,7 +237,7 @@ class WallabagRestController extends FOSRestController
      *      }
      * )
      *
-     * @return Response
+     * @return JsonResponse
      */
     public function getEntriesTagsAction(Entry $entry)
     {
@@ -245,7 +246,7 @@ class WallabagRestController extends FOSRestController
 
         $json = $this->get('serializer')->serialize($entry->getTags(), 'json');
 
-        return $this->renderJsonResponse($json);
+        return (new JsonResponse())->setJson($json);
     }
 
     /**
@@ -260,7 +261,7 @@ class WallabagRestController extends FOSRestController
      *       }
      * )
      *
-     * @return Response
+     * @return JsonResponse
      */
     public function postEntriesTagsAction(Request $request, Entry $entry)
     {
@@ -278,7 +279,7 @@ class WallabagRestController extends FOSRestController
 
         $json = $this->get('serializer')->serialize($entry, 'json');
 
-        return $this->renderJsonResponse($json);
+        return (new JsonResponse())->setJson($json);
     }
 
     /**
@@ -291,7 +292,7 @@ class WallabagRestController extends FOSRestController
      *      }
      * )
      *
-     * @return Response
+     * @return JsonResponse
      */
     public function deleteEntriesTagsAction(Entry $entry, Tag $tag)
     {
@@ -305,7 +306,7 @@ class WallabagRestController extends FOSRestController
 
         $json = $this->get('serializer')->serialize($entry, 'json');
 
-        return $this->renderJsonResponse($json);
+        return (new JsonResponse())->setJson($json);
     }
 
     /**
@@ -313,7 +314,7 @@ class WallabagRestController extends FOSRestController
      *
      * @ApiDoc()
      *
-     * @return Response
+     * @return JsonResponse
      */
     public function getTagsAction()
     {
@@ -321,11 +322,84 @@ class WallabagRestController extends FOSRestController
 
         $tags = $this->getDoctrine()
             ->getRepository('WallabagCoreBundle:Tag')
-            ->findAllTags($this->getUser()->getId());
+            ->findAllTags($this->getUser()->getId())
+            ->getQuery()
+            ->getResult();
 
         $json = $this->get('serializer')->serialize($tags, 'json');
 
-        return $this->renderJsonResponse($json);
+        return (new JsonResponse())->setJson($json);
+    }
+
+    /**
+     * Permanently remove one tag from **every** entry.
+     *
+     * @ApiDoc(
+     *      requirements={
+     *          {"name"="tag", "dataType"="string", "required"=true, "requirement"="\w+", "description"="Tag as a string"}
+     *      }
+     * )
+     *
+     * @return JsonResponse
+     */
+    public function deleteTagLabelAction(Request $request)
+    {
+        $this->validateAuthentication();
+        $label = $request->request->get('tag', '');
+
+        $tag = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($label);
+
+        if (empty($tag)) {
+            throw $this->createNotFoundException('Tag not found');
+        }
+
+        $this->getDoctrine()
+            ->getRepository('WallabagCoreBundle:Entry')
+            ->removeTag($this->getUser()->getId(), $tag);
+
+        $json = $this->get('serializer')->serialize($tag, 'json');
+
+        return (new JsonResponse())->setJson($json);
+    }
+
+    /**
+     * Permanently remove some tags from **every** entry.
+     *
+     * @ApiDoc(
+     *      requirements={
+     *          {"name"="tags", "dataType"="string", "required"=true, "format"="tag1,tag2", "description"="Tags as strings (comma splitted)"}
+     *      }
+     * )
+     *
+     * @return JsonResponse
+     */
+    public function deleteTagsLabelAction(Request $request)
+    {
+        $this->validateAuthentication();
+
+        $tagsLabels = $request->request->get('tags', '');
+
+        $tags = [];
+
+        foreach (explode(',', $tagsLabels) as $tagLabel) {
+            $tagEntity = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($tagLabel);
+
+            if (!empty($tagEntity)) {
+                $tags[] = $tagEntity;
+            }
+        }
+
+        if (empty($tags)) {
+            throw $this->createNotFoundException('Tags not found');
+        }
+
+        $this->getDoctrine()
+            ->getRepository('WallabagCoreBundle:Entry')
+            ->removeTags($this->getUser()->getId(), $tags);
+
+        $json = $this->get('serializer')->serialize($tags, 'json');
+
+        return (new JsonResponse())->setJson($json);
     }
 
     /**
@@ -337,7 +411,7 @@ class WallabagRestController extends FOSRestController
      *      }
      * )
      *
-     * @return Response
+     * @return JsonResponse
      */
     public function deleteTagAction(Tag $tag)
     {
@@ -349,14 +423,15 @@ class WallabagRestController extends FOSRestController
 
         $json = $this->get('serializer')->serialize($tag, 'json');
 
-        return $this->renderJsonResponse($json);
+        return (new JsonResponse())->setJson($json);
     }
+
     /**
      * Retrieve version number.
      *
      * @ApiDoc()
      *
-     * @return Response
+     * @return JsonResponse
      */
     public function getVersionAction()
     {
@@ -364,7 +439,7 @@ class WallabagRestController extends FOSRestController
 
         $json = $this->get('serializer')->serialize($version, 'json');
 
-        return $this->renderJsonResponse($json);
+        return (new JsonResponse())->setJson($json);
     }
 
     /**
@@ -380,17 +455,4 @@ class WallabagRestController extends FOSRestController
             throw $this->createAccessDeniedException('Access forbidden. Entry user id: '.$requestUserId.', logged user id: '.$user->getId());
         }
     }
-
-    /**
-     * Send a JSON Response.
-     * We don't use the Symfony JsonRespone, because it takes an array as parameter instead of a JSON string.
-     *
-     * @param string $json
-     *
-     * @return Response
-     */
-    private function renderJsonResponse($json)
-    {
-        return new Response($json, 200, ['application/json']);
-    }
 }