diff options
Diffstat (limited to 'src/Wallabag/ApiBundle')
-rw-r--r-- | src/Wallabag/ApiBundle/Controller/EntryRestController.php | 150 | ||||
-rw-r--r-- | src/Wallabag/ApiBundle/Controller/UserRestController.php | 25 | ||||
-rw-r--r-- | src/Wallabag/ApiBundle/Entity/Client.php | 23 |
3 files changed, 116 insertions, 82 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index 93c8157e..09b73ccb 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php | |||
@@ -299,65 +299,18 @@ class EntryRestController extends WallabagRestController | |||
299 | $this->validateAuthentication(); | 299 | $this->validateAuthentication(); |
300 | 300 | ||
301 | $url = $request->request->get('url'); | 301 | $url = $request->request->get('url'); |
302 | $title = $request->request->get('title'); | ||
303 | $tags = $request->request->get('tags', []); | ||
304 | $isArchived = $request->request->get('archive'); | ||
305 | $isStarred = $request->request->get('starred'); | ||
306 | $content = $request->request->get('content'); | ||
307 | $language = $request->request->get('language'); | ||
308 | $picture = $request->request->get('preview_picture'); | ||
309 | $publishedAt = $request->request->get('published_at'); | ||
310 | $authors = $request->request->get('authors', ''); | ||
311 | 302 | ||
312 | $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($url, $this->getUser()->getId()); | 303 | $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( |
304 | $url, | ||
305 | $this->getUser()->getId() | ||
306 | ); | ||
313 | 307 | ||
314 | if (false === $entry) { | 308 | if (false === $entry) { |
315 | $entry = new Entry($this->getUser()); | 309 | $entry = new Entry($this->getUser()); |
316 | } | ||
317 | |||
318 | try { | ||
319 | $this->get('wallabag_core.content_proxy')->updateEntry( | ||
320 | $entry, | ||
321 | $url, | ||
322 | [ | ||
323 | 'title' => $title, | ||
324 | 'html' => $content, | ||
325 | 'url' => $url, | ||
326 | 'language' => $language, | ||
327 | 'date' => $publishedAt, | ||
328 | // faking the preview picture | ||
329 | 'open_graph' => [ | ||
330 | 'og_image' => $picture, | ||
331 | ], | ||
332 | 'authors' => explode(',', $authors), | ||
333 | ] | ||
334 | ); | ||
335 | } catch (\Exception $e) { | ||
336 | $this->get('logger')->error('Error while saving an entry', [ | ||
337 | 'exception' => $e, | ||
338 | 'entry' => $entry, | ||
339 | ]); | ||
340 | $entry->setUrl($url); | 310 | $entry->setUrl($url); |
341 | } | 311 | } |
342 | 312 | ||
343 | if (!empty($tags)) { | 313 | $this->upsertEntry($entry, $request); |
344 | $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $tags); | ||
345 | } | ||
346 | |||
347 | if (!is_null($isStarred)) { | ||
348 | $entry->setStarred((bool) $isStarred); | ||
349 | } | ||
350 | |||
351 | if (!is_null($isArchived)) { | ||
352 | $entry->setArchived((bool) $isArchived); | ||
353 | } | ||
354 | |||
355 | $em = $this->getDoctrine()->getManager(); | ||
356 | $em->persist($entry); | ||
357 | $em->flush(); | ||
358 | |||
359 | // entry saved, dispatch event about it! | ||
360 | $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); | ||
361 | 314 | ||
362 | return $this->sendResponse($entry); | 315 | return $this->sendResponse($entry); |
363 | } | 316 | } |
@@ -374,6 +327,11 @@ class EntryRestController extends WallabagRestController | |||
374 | * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."}, | 327 | * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."}, |
375 | * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="archived the entry."}, | 328 | * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="archived the entry."}, |
376 | * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="starred the entry."}, | 329 | * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="starred the entry."}, |
330 | * {"name"="content", "dataType"="string", "required"=false, "description"="Content of the entry"}, | ||
331 | * {"name"="language", "dataType"="string", "required"=false, "description"="Language of the entry"}, | ||
332 | * {"name"="preview_picture", "dataType"="string", "required"=false, "description"="Preview picture of the entry"}, | ||
333 | * {"name"="published_at", "dataType"="datetime|integer", "format"="YYYY-MM-DDTHH:II:SS+TZ or a timestamp", "required"=false, "description"="Published date of the entry"}, | ||
334 | * {"name"="authors", "dataType"="string", "format"="Name Firstname,author2,author3", "required"=false, "description"="Authors of the entry"}, | ||
377 | * } | 335 | * } |
378 | * ) | 336 | * ) |
379 | * | 337 | * |
@@ -384,29 +342,7 @@ class EntryRestController extends WallabagRestController | |||
384 | $this->validateAuthentication(); | 342 | $this->validateAuthentication(); |
385 | $this->validateUserAccess($entry->getUser()->getId()); | 343 | $this->validateUserAccess($entry->getUser()->getId()); |
386 | 344 | ||
387 | $title = $request->request->get('title'); | 345 | $this->upsertEntry($entry, $request, true); |
388 | $isArchived = $request->request->get('archive'); | ||
389 | $isStarred = $request->request->get('starred'); | ||
390 | |||
391 | if (!is_null($title)) { | ||
392 | $entry->setTitle($title); | ||
393 | } | ||
394 | |||
395 | if (!is_null($isArchived)) { | ||
396 | $entry->setArchived((bool) $isArchived); | ||
397 | } | ||
398 | |||
399 | if (!is_null($isStarred)) { | ||
400 | $entry->setStarred((bool) $isStarred); | ||
401 | } | ||
402 | |||
403 | $tags = $request->request->get('tags', ''); | ||
404 | if (!empty($tags)) { | ||
405 | $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $tags); | ||
406 | } | ||
407 | |||
408 | $em = $this->getDoctrine()->getManager(); | ||
409 | $em->flush(); | ||
410 | 346 | ||
411 | return $this->sendResponse($entry); | 347 | return $this->sendResponse($entry); |
412 | } | 348 | } |
@@ -673,4 +609,68 @@ class EntryRestController extends WallabagRestController | |||
673 | 609 | ||
674 | return (new JsonResponse())->setJson($json); | 610 | return (new JsonResponse())->setJson($json); |
675 | } | 611 | } |
612 | |||
613 | /** | ||
614 | * Update or Insert a new entry. | ||
615 | * | ||
616 | * @param Entry $entry | ||
617 | * @param Request $request | ||
618 | * @param bool $disableContentUpdate If we don't want the content to be update by fetching the url (used when patching instead of posting) | ||
619 | */ | ||
620 | private function upsertEntry(Entry $entry, Request $request, $disableContentUpdate = false) | ||
621 | { | ||
622 | $title = $request->request->get('title'); | ||
623 | $tags = $request->request->get('tags', []); | ||
624 | $isArchived = $request->request->get('archive'); | ||
625 | $isStarred = $request->request->get('starred'); | ||
626 | $content = $request->request->get('content'); | ||
627 | $language = $request->request->get('language'); | ||
628 | $picture = $request->request->get('preview_picture'); | ||
629 | $publishedAt = $request->request->get('published_at'); | ||
630 | $authors = $request->request->get('authors', ''); | ||
631 | |||
632 | try { | ||
633 | $this->get('wallabag_core.content_proxy')->updateEntry( | ||
634 | $entry, | ||
635 | $entry->getUrl(), | ||
636 | [ | ||
637 | 'title' => !empty($title) ? $title : $entry->getTitle(), | ||
638 | 'html' => !empty($content) ? $content : $entry->getContent(), | ||
639 | 'url' => $entry->getUrl(), | ||
640 | 'language' => !empty($language) ? $language : $entry->getLanguage(), | ||
641 | 'date' => !empty($publishedAt) ? $publishedAt : $entry->getPublishedAt(), | ||
642 | // faking the open graph preview picture | ||
643 | 'open_graph' => [ | ||
644 | 'og_image' => !empty($picture) ? $picture : $entry->getPreviewPicture(), | ||
645 | ], | ||
646 | 'authors' => is_string($authors) ? explode(',', $authors) : $entry->getPublishedBy(), | ||
647 | ], | ||
648 | $disableContentUpdate | ||
649 | ); | ||
650 | } catch (\Exception $e) { | ||
651 | $this->get('logger')->error('Error while saving an entry', [ | ||
652 | 'exception' => $e, | ||
653 | 'entry' => $entry, | ||
654 | ]); | ||
655 | } | ||
656 | |||
657 | if (!is_null($isArchived)) { | ||
658 | $entry->setArchived((bool) $isArchived); | ||
659 | } | ||
660 | |||
661 | if (!is_null($isStarred)) { | ||
662 | $entry->setStarred((bool) $isStarred); | ||
663 | } | ||
664 | |||
665 | if (!empty($tags)) { | ||
666 | $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $tags); | ||
667 | } | ||
668 | |||
669 | $em = $this->getDoctrine()->getManager(); | ||
670 | $em->persist($entry); | ||
671 | $em->flush(); | ||
672 | |||
673 | // entry saved, dispatch event about it! | ||
674 | $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); | ||
675 | } | ||
676 | } | 676 | } |
diff --git a/src/Wallabag/ApiBundle/Controller/UserRestController.php b/src/Wallabag/ApiBundle/Controller/UserRestController.php index 8f675b8d..7471f5f6 100644 --- a/src/Wallabag/ApiBundle/Controller/UserRestController.php +++ b/src/Wallabag/ApiBundle/Controller/UserRestController.php | |||
@@ -9,6 +9,7 @@ use Nelmio\ApiDocBundle\Annotation\ApiDoc; | |||
9 | use Symfony\Component\HttpFoundation\Request; | 9 | use Symfony\Component\HttpFoundation\Request; |
10 | use Symfony\Component\HttpFoundation\JsonResponse; | 10 | use Symfony\Component\HttpFoundation\JsonResponse; |
11 | use Wallabag\UserBundle\Entity\User; | 11 | use Wallabag\UserBundle\Entity\User; |
12 | use Wallabag\ApiBundle\Entity\Client; | ||
12 | 13 | ||
13 | class UserRestController extends WallabagRestController | 14 | class UserRestController extends WallabagRestController |
14 | { | 15 | { |
@@ -27,13 +28,14 @@ class UserRestController extends WallabagRestController | |||
27 | } | 28 | } |
28 | 29 | ||
29 | /** | 30 | /** |
30 | * Register an user. | 31 | * Register an user and create a client. |
31 | * | 32 | * |
32 | * @ApiDoc( | 33 | * @ApiDoc( |
33 | * requirements={ | 34 | * requirements={ |
34 | * {"name"="username", "dataType"="string", "required"=true, "description"="The user's username"}, | 35 | * {"name"="username", "dataType"="string", "required"=true, "description"="The user's username"}, |
35 | * {"name"="password", "dataType"="string", "required"=true, "description"="The user's password"}, | 36 | * {"name"="password", "dataType"="string", "required"=true, "description"="The user's password"}, |
36 | * {"name"="email", "dataType"="string", "required"=true, "description"="The user's email"} | 37 | * {"name"="email", "dataType"="string", "required"=true, "description"="The user's email"}, |
38 | * {"name"="client_name", "dataType"="string", "required"=true, "description"="The client name (to be used by your app)"} | ||
37 | * } | 39 | * } |
38 | * ) | 40 | * ) |
39 | * | 41 | * |
@@ -97,29 +99,38 @@ class UserRestController extends WallabagRestController | |||
97 | ->setStatusCode(JsonResponse::HTTP_BAD_REQUEST); | 99 | ->setStatusCode(JsonResponse::HTTP_BAD_REQUEST); |
98 | } | 100 | } |
99 | 101 | ||
102 | // create a default client | ||
103 | $client = new Client($user); | ||
104 | $client->setName($request->request->get('client_name', 'Default client')); | ||
105 | |||
106 | $this->getDoctrine()->getManager()->persist($client); | ||
107 | |||
108 | $user->addClient($client); | ||
109 | |||
100 | $userManager->updateUser($user); | 110 | $userManager->updateUser($user); |
101 | 111 | ||
102 | // dispatch a created event so the associated config will be created | 112 | // dispatch a created event so the associated config will be created |
103 | $event = new UserEvent($user, $request); | 113 | $event = new UserEvent($user, $request); |
104 | $this->get('event_dispatcher')->dispatch(FOSUserEvents::USER_CREATED, $event); | 114 | $this->get('event_dispatcher')->dispatch(FOSUserEvents::USER_CREATED, $event); |
105 | 115 | ||
106 | return $this->sendUser($user, JsonResponse::HTTP_CREATED); | 116 | return $this->sendUser($user, 'user_api_with_client', JsonResponse::HTTP_CREATED); |
107 | } | 117 | } |
108 | 118 | ||
109 | /** | 119 | /** |
110 | * Send user response. | 120 | * Send user response. |
111 | * | 121 | * |
112 | * @param User $user | 122 | * @param User $user |
113 | * @param int $status HTTP Status code to send | 123 | * @param string $group Used to define with serialized group might be used |
124 | * @param int $status HTTP Status code to send | ||
114 | * | 125 | * |
115 | * @return JsonResponse | 126 | * @return JsonResponse |
116 | */ | 127 | */ |
117 | private function sendUser(User $user, $status = JsonResponse::HTTP_OK) | 128 | private function sendUser(User $user, $group = 'user_api', $status = JsonResponse::HTTP_OK) |
118 | { | 129 | { |
119 | $json = $this->get('serializer')->serialize( | 130 | $json = $this->get('serializer')->serialize( |
120 | $user, | 131 | $user, |
121 | 'json', | 132 | 'json', |
122 | SerializationContext::create()->setGroups(['user_api']) | 133 | SerializationContext::create()->setGroups([$group]) |
123 | ); | 134 | ); |
124 | 135 | ||
125 | return (new JsonResponse()) | 136 | return (new JsonResponse()) |
diff --git a/src/Wallabag/ApiBundle/Entity/Client.php b/src/Wallabag/ApiBundle/Entity/Client.php index 9ed9f980..c15fd3fa 100644 --- a/src/Wallabag/ApiBundle/Entity/Client.php +++ b/src/Wallabag/ApiBundle/Entity/Client.php | |||
@@ -5,6 +5,9 @@ namespace Wallabag\ApiBundle\Entity; | |||
5 | use Doctrine\ORM\Mapping as ORM; | 5 | use Doctrine\ORM\Mapping as ORM; |
6 | use FOS\OAuthServerBundle\Entity\Client as BaseClient; | 6 | use FOS\OAuthServerBundle\Entity\Client as BaseClient; |
7 | use Wallabag\UserBundle\Entity\User; | 7 | use Wallabag\UserBundle\Entity\User; |
8 | use JMS\Serializer\Annotation\Groups; | ||
9 | use JMS\Serializer\Annotation\SerializedName; | ||
10 | use JMS\Serializer\Annotation\VirtualProperty; | ||
8 | 11 | ||
9 | /** | 12 | /** |
10 | * @ORM\Table("oauth2_clients") | 13 | * @ORM\Table("oauth2_clients") |
@@ -23,6 +26,8 @@ class Client extends BaseClient | |||
23 | * @var string | 26 | * @var string |
24 | * | 27 | * |
25 | * @ORM\Column(name="name", type="text", nullable=false) | 28 | * @ORM\Column(name="name", type="text", nullable=false) |
29 | * | ||
30 | * @Groups({"user_api_with_client"}) | ||
26 | */ | 31 | */ |
27 | protected $name; | 32 | protected $name; |
28 | 33 | ||
@@ -37,6 +42,14 @@ class Client extends BaseClient | |||
37 | protected $accessTokens; | 42 | protected $accessTokens; |
38 | 43 | ||
39 | /** | 44 | /** |
45 | * @var string | ||
46 | * | ||
47 | * @SerializedName("client_secret") | ||
48 | * @Groups({"user_api_with_client"}) | ||
49 | */ | ||
50 | protected $secret; | ||
51 | |||
52 | /** | ||
40 | * @ORM\ManyToOne(targetEntity="Wallabag\UserBundle\Entity\User", inversedBy="clients") | 53 | * @ORM\ManyToOne(targetEntity="Wallabag\UserBundle\Entity\User", inversedBy="clients") |
41 | */ | 54 | */ |
42 | private $user; | 55 | private $user; |
@@ -78,4 +91,14 @@ class Client extends BaseClient | |||
78 | { | 91 | { |
79 | return $this->user; | 92 | return $this->user; |
80 | } | 93 | } |
94 | |||
95 | /** | ||
96 | * @VirtualProperty | ||
97 | * @SerializedName("client_id") | ||
98 | * @Groups({"user_api_with_client"}) | ||
99 | */ | ||
100 | public function getClientId() | ||
101 | { | ||
102 | return $this->getId().'_'.$this->getRandomId(); | ||
103 | } | ||
81 | } | 104 | } |