]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - src/Wallabag/ApiBundle/Controller/EntryRestController.php
Added API endpoint to handle a list of URL and to add/delete tags
[github/wallabag/wallabag.git] / src / Wallabag / ApiBundle / Controller / EntryRestController.php
index c5bf1df819e4183a53eb2de8fc8acb86b8eaf8b7..fc46e782efb6cb29c5a6843c455e2d99b65ab3aa 100644 (file)
@@ -41,7 +41,7 @@ class EntryRestController extends WallabagRestController
                     ->getRepository('WallabagCoreBundle:Entry')
                     ->findByUrlAndUserId($url, $this->getUser()->getId());
 
-                $results[$url] = false === $res ? false : true;
+                $results[$url] = $res instanceof Entry ? $res->getId() : false;
             }
 
             $json = $this->get('serializer')->serialize($results, 'json');
@@ -60,7 +60,7 @@ class EntryRestController extends WallabagRestController
             ->getRepository('WallabagCoreBundle:Entry')
             ->findByUrlAndUserId($url, $this->getUser()->getId());
 
-        $exists = false === $res ? false : true;
+        $exists = $res instanceof Entry ? $res->getId() : false;
 
         $json = $this->get('serializer')->serialize(['exists' => $exists], 'json');
 
@@ -285,6 +285,51 @@ class EntryRestController extends WallabagRestController
         return (new JsonResponse())->setJson($json);
     }
 
+    /**
+     * Reload an entry.
+     * An empty response with HTTP Status 304 will be send if we weren't able to update the content (because it hasn't changed or we got an error).
+     *
+     * @ApiDoc(
+     *      requirements={
+     *          {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
+     *      }
+     * )
+     *
+     * @return JsonResponse
+     */
+    public function patchEntriesReloadAction(Entry $entry)
+    {
+        $this->validateAuthentication();
+        $this->validateUserAccess($entry->getUser()->getId());
+
+        try {
+            $entry = $this->get('wallabag_core.content_proxy')->updateEntry($entry, $entry->getUrl());
+        } catch (\Exception $e) {
+            $this->get('logger')->error('Error while saving an entry', [
+                'exception' => $e,
+                'entry' => $entry,
+            ]);
+
+            return new JsonResponse([], 304);
+        }
+
+        // if refreshing entry failed, don't save it
+        if ($this->getParameter('wallabag_core.fetching_error_message') === $entry->getContent()) {
+            return new JsonResponse([], 304);
+        }
+
+        $em = $this->getDoctrine()->getManager();
+        $em->persist($entry);
+        $em->flush();
+
+        // 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);
+    }
+
     /**
      * Delete **permanently** an entry.
      *
@@ -393,4 +438,72 @@ class EntryRestController extends WallabagRestController
 
         return (new JsonResponse())->setJson($json);
     }
+
+    /**
+     * Handles an entries list and add / delete to them some tags.
+     *
+     * @ApiDoc(
+     *       parameters={
+     *          {"name"="list", "dataType"="string", "required"=true, "format"="A JSON array of urls [{'url': 'http://...','tags': 'tag1, tag2','action': 'delete'}, {'url': 'http://...','tags': 'tag1, tag2','action': 'add'}]", "description"="Urls (as an array) to handle."}
+     *       }
+     * )
+     *
+     * @return JsonResponse
+     */
+    public function postEntriesTagsListAction(Request $request)
+    {
+        $this->validateAuthentication();
+
+        $list = json_decode($request->query->get('list', []));
+        $results = [];
+
+        // 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;
+                $results[$key]['action'] = $element->action;
+                $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false;
+
+                $tags = $element->tags;
+
+                if (false !== $entry && !(empty($tags))) {
+                    switch ($element->action) {
+                        case 'delete':
+                            $tags = explode(',', $tags);
+                            foreach ($tags as $label) {
+                                $label = trim($label);
+
+                                $tag = $this->getDoctrine()
+                                    ->getRepository('WallabagCoreBundle:Tag')
+                                    ->findOneByLabel($label);
+
+                                if (false !== $tag) {
+                                    $entry->removeTag($tag);
+                                }
+                            }
+
+                            break;
+                        case 'add':
+                            $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
+
+                            break;
+                    }
+
+                    $em = $this->getDoctrine()->getManager();
+                    $em->persist($entry);
+                    $em->flush();
+                }
+            }
+        }
+
+        $json = $this->get('serializer')->serialize($results, 'json');
+
+        return (new JsonResponse())->setJson($json);
+    }
 }