]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Add ability to patch an entry with more fields
authorJeremy Benoist <jeremy.benoist@gmail.com>
Fri, 2 Jun 2017 18:52:49 +0000 (20:52 +0200)
committerJeremy Benoist <jeremy.benoist@gmail.com>
Fri, 2 Jun 2017 18:52:49 +0000 (20:52 +0200)
Like when we create an entry, we can now patch an entry with new fields:
- content
- language
- preview_picture
- published_at
- authors

src/Wallabag/ApiBundle/Controller/EntryRestController.php
src/Wallabag/CoreBundle/Helper/ContentProxy.php
tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php

index 93c8157e758a934905663846516fccc27b46e50b..7135e6160a87c8e73c0467a8addf14cc688f1359 100644 (file)
@@ -374,6 +374,11 @@ class EntryRestController extends WallabagRestController
      *          {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
      *          {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="archived the entry."},
      *          {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="starred the entry."},
+     *          {"name"="content", "dataType"="string", "required"=false, "description"="Content of the entry"},
+     *          {"name"="language", "dataType"="string", "required"=false, "description"="Language of the entry"},
+     *          {"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"},
      *      }
      * )
      *
@@ -385,11 +390,39 @@ class EntryRestController extends WallabagRestController
         $this->validateUserAccess($entry->getUser()->getId());
 
         $title = $request->request->get('title');
+        $tags = $request->request->get('tags', '');
         $isArchived = $request->request->get('archive');
         $isStarred = $request->request->get('starred');
+        $content = $request->request->get('content');
+        $language = $request->request->get('language');
+        $picture = $request->request->get('preview_picture');
+        $publishedAt = $request->request->get('published_at');
+        $authors = $request->request->get('authors', '');
 
-        if (!is_null($title)) {
-            $entry->setTitle($title);
+        try {
+            $this->get('wallabag_core.content_proxy')->updateEntry(
+                $entry,
+                $entry->getUrl(),
+                [
+                    'title' => !empty($title) ? $title : $entry->getTitle(),
+                    'html' => !empty($content) ? $content : $entry->getContent(),
+                    'url' => $entry->getUrl(),
+                    'language' => $language,
+                    'date' => $publishedAt,
+                    // faking the preview picture
+                    'open_graph' => [
+                        'og_image' => $picture,
+                    ],
+                    'authors' => is_string($authors) ? explode(',', $authors) : [],
+                ],
+                // we don't want the content to be update by fetching the url
+                true
+            );
+        } catch (\Exception $e) {
+            $this->get('logger')->error('Error while saving an entry', [
+                'exception' => $e,
+                'entry' => $entry,
+            ]);
         }
 
         if (!is_null($isArchived)) {
@@ -400,7 +433,6 @@ class EntryRestController extends WallabagRestController
             $entry->setStarred((bool) $isStarred);
         }
 
-        $tags = $request->request->get('tags', '');
         if (!empty($tags)) {
             $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $tags);
         }
index bfaa197695941af57dde87e751f95d1a1d0cc30c..d5820e6633e2250f1a53c7554046f5fc238a781f 100644 (file)
@@ -105,7 +105,7 @@ class ContentProxy
             }
         }
 
-        if (!empty($content['authors'])) {
+        if (!empty($content['authors']) && is_array($content['authors'])) {
             $entry->setPublishedBy($content['authors']);
         }
 
index 0968cfafc9d4132cb3e0c0adfe55a15dffab389c..74ec34b17bcbba81c2525c3941e038502b2ffa9b 100644 (file)
@@ -477,6 +477,10 @@ class EntryRestControllerTest extends WallabagApiTestCase
             'tags' => 'new tag '.uniqid(),
             'starred' => '1',
             'archive' => '0',
+            'language' => 'de_DE',
+            'preview_picture' => 'http://preview.io/picture.jpg',
+            'authors' => 'bob,sponge',
+            'content' => 'awesome',
         ]);
 
         $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
@@ -488,6 +492,11 @@ class EntryRestControllerTest extends WallabagApiTestCase
         $this->assertEquals('New awesome title', $content['title']);
         $this->assertGreaterThan($nbTags, count($content['tags']));
         $this->assertEquals(1, $content['user_id']);
+        $this->assertEquals('de_DE', $content['language']);
+        $this->assertEquals('http://preview.io/picture.jpg', $content['preview_picture']);
+        $this->assertContains('sponge', $content['published_by']);
+        $this->assertContains('bob', $content['published_by']);
+        $this->assertEquals('awesome', $content['content']);
     }
 
     public function testPatchEntryWithoutQuotes()
@@ -509,6 +518,7 @@ class EntryRestControllerTest extends WallabagApiTestCase
             'tags' => 'new tag '.uniqid(),
             'starred' => 1,
             'archive' => 0,
+            'authors' => ['bob', 'sponge'],
         ]);
 
         $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
@@ -519,6 +529,7 @@ class EntryRestControllerTest extends WallabagApiTestCase
         $this->assertEquals($entry->getUrl(), $content['url']);
         $this->assertEquals('New awesome title', $content['title']);
         $this->assertGreaterThan($nbTags, count($content['tags']));
+        $this->assertTrue(empty($content['published_by']), 'Authors were not saved because of an array instead of a string');
     }
 
     public function testGetTagsEntry()