]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/ApiBundle/Controller/WallabagRestController.php
Added a missing namespace
[github/wallabag/wallabag.git] / src / Wallabag / ApiBundle / Controller / WallabagRestController.php
CommitLineData
f8bf8952
NL
1<?php
2
769e19dc 3namespace Wallabag\ApiBundle\Controller;
f8bf8952 4
fcb1fba5 5use FOS\RestBundle\Controller\FOSRestController;
3f3a6087 6use Hateoas\Configuration\Route as HateoasRoute;
619cc453 7use Hateoas\Representation\Factory\PagerfantaFactory;
f8bf8952 8use Nelmio\ApiDocBundle\Annotation\ApiDoc;
351eb8d9 9use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
27f15aa4 10use Symfony\Component\HttpFoundation\Request;
60faee00 11use Symfony\Component\HttpFoundation\JsonResponse;
1d76102a 12use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
b0b893ea 13use Symfony\Component\Security\Core\Exception\AccessDeniedException;
be463487 14use Wallabag\CoreBundle\Entity\Entry;
653e8be4 15use Wallabag\CoreBundle\Entity\Tag;
351eb8d9 16use Wallabag\AnnotationBundle\Entity\Annotation;
d9b7768d 17use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
f8bf8952 18
fcb1fba5 19class WallabagRestController extends FOSRestController
f8bf8952 20{
77273253
NL
21 private function validateAuthentication()
22 {
18f8f32f 23 if (false === $this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) {
77273253
NL
24 throw new AccessDeniedException();
25 }
26 }
27
6273fefd 28 /**
3583cadf 29 * Check if an entry exist by url.
6273fefd
JB
30 *
31 * @ApiDoc(
32 * parameters={
f0abc22d
JB
33 * {"name"="url", "dataType"="string", "required"=true, "format"="An url", "description"="Url to check if it exists"},
34 * {"name"="urls", "dataType"="string", "required"=false, "format"="An array of urls (?urls[]=http...&urls[]=http...)", "description"="Urls (as an array) to check if it exists"}
6273fefd
JB
35 * }
36 * )
37 *
38 * @return JsonResponse
39 */
40 public function getEntriesExistsAction(Request $request)
41 {
42 $this->validateAuthentication();
43
f0abc22d
JB
44 $urls = $request->query->get('urls', []);
45
46 // handle multiple urls first
47 if (!empty($urls)) {
48 $results = [];
49 foreach ($urls as $url) {
50 $res = $this->getDoctrine()
51 ->getRepository('WallabagCoreBundle:Entry')
52 ->findByUrlAndUserId($url, $this->getUser()->getId());
53
54 $results[$url] = false === $res ? false : true;
55 }
56
57 $json = $this->get('serializer')->serialize($results, 'json');
58
59 return (new JsonResponse())->setJson($json);
60 }
61
62 // let's see if it is a simple url?
6273fefd
JB
63 $url = $request->query->get('url', '');
64
65 if (empty($url)) {
0b174d69 66 throw $this->createAccessDeniedException('URL is empty?, logged user id: '.$this->getUser()->getId());
6273fefd
JB
67 }
68
69 $res = $this->getDoctrine()
70 ->getRepository('WallabagCoreBundle:Entry')
71 ->findByUrlAndUserId($url, $this->getUser()->getId());
72
73 $exists = false === $res ? false : true;
74
75 $json = $this->get('serializer')->serialize(['exists' => $exists], 'json');
76
77 return (new JsonResponse())->setJson($json);
78 }
79
f8bf8952 80 /**
a8c90c5c 81 * Retrieve all entries. It could be filtered by many options.
f8bf8952
NL
82 *
83 * @ApiDoc(
a8c90c5c 84 * parameters={
189ef634
TC
85 * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0, all entries by default", "description"="filter by archived status."},
86 * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0, all entries by default", "description"="filter by starred status."},
a8c90c5c
NL
87 * {"name"="sort", "dataType"="string", "required"=false, "format"="'created' or 'updated', default 'created'", "description"="sort entries by date."},
88 * {"name"="order", "dataType"="string", "required"=false, "format"="'asc' or 'desc', default 'desc'", "description"="order of sort."},
89 * {"name"="page", "dataType"="integer", "required"=false, "format"="default '1'", "description"="what page you want."},
90 * {"name"="perPage", "dataType"="integer", "required"=false, "format"="default'30'", "description"="results per page."},
189ef634 91 * {"name"="tags", "dataType"="string", "required"=false, "format"="api,rest", "description"="a list of tags url encoded. Will returns entries that matches ALL tags."},
e5fb89e5 92 * {"name"="since", "dataType"="integer", "required"=false, "format"="default '0'", "description"="The timestamp since when you want entries updated."},
a8c90c5c 93 * }
f8bf8952 94 * )
4346a860 95 *
60faee00 96 * @return JsonResponse
f8bf8952 97 */
27f15aa4 98 public function getEntriesAction(Request $request)
f8bf8952 99 {
77273253
NL
100 $this->validateAuthentication();
101
0135c98b
NL
102 $isArchived = (null === $request->query->get('archive')) ? null : (bool) $request->query->get('archive');
103 $isStarred = (null === $request->query->get('starred')) ? null : (bool) $request->query->get('starred');
8ce32af6
JB
104 $sort = $request->query->get('sort', 'created');
105 $order = $request->query->get('order', 'desc');
106 $page = (int) $request->query->get('page', 1);
107 $perPage = (int) $request->query->get('perPage', 30);
28803f10 108 $tags = $request->query->get('tags', '');
c3f8b428 109 $since = $request->query->get('since', 0);
a8c90c5c 110
fc732227 111 $pager = $this->getDoctrine()
be463487 112 ->getRepository('WallabagCoreBundle:Entry')
28803f10 113 ->findEntries($this->getUser()->getId(), $isArchived, $isStarred, $sort, $order, $since, $tags);
a8c90c5c 114
6e22bd73
WD
115 $pager->setCurrentPage($page);
116 $pager->setMaxPerPage($perPage);
117
8ce32af6 118 $pagerfantaFactory = new PagerfantaFactory('page', 'perPage');
6e22bd73
WD
119 $paginatedCollection = $pagerfantaFactory->createRepresentation(
120 $pager,
3f3a6087 121 new HateoasRoute(
c3f8b428
JB
122 'api_get_entries',
123 [
124 'archive' => $isArchived,
125 'starred' => $isStarred,
126 'sort' => $sort,
127 'order' => $order,
128 'page' => $page,
129 'perPage' => $perPage,
130 'tags' => $tags,
131 'since' => $since,
132 ],
133 UrlGeneratorInterface::ABSOLUTE_URL
134 )
6e22bd73
WD
135 );
136
137 $json = $this->get('serializer')->serialize($paginatedCollection, 'json');
0f006880 138
60faee00 139 return (new JsonResponse())->setJson($json);
f8bf8952
NL
140 }
141
142 /**
4346a860 143 * Retrieve a single entry.
f8bf8952
NL
144 *
145 * @ApiDoc(
146 * requirements={
147 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
148 * }
149 * )
4346a860 150 *
60faee00 151 * @return JsonResponse
f8bf8952 152 */
be463487 153 public function getEntryAction(Entry $entry)
f8bf8952 154 {
77273253 155 $this->validateAuthentication();
fcb1fba5 156 $this->validateUserAccess($entry->getUser()->getId());
092ca707 157
aa4d6562 158 $json = $this->get('serializer')->serialize($entry, 'json');
0f006880 159
60faee00 160 return (new JsonResponse())->setJson($json);
f8bf8952
NL
161 }
162
3f3a6087
JB
163 /**
164 * Retrieve a single entry as a predefined format.
165 *
166 * @ApiDoc(
167 * requirements={
168 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
169 * }
170 * )
171 *
3f3a6087
JB
172 * @return Response
173 */
174 public function getEntryExportAction(Entry $entry, Request $request)
175 {
176 $this->validateAuthentication();
177 $this->validateUserAccess($entry->getUser()->getId());
178
179 return $this->get('wallabag_core.helper.entries_export')
180 ->setEntries($entry)
181 ->updateTitle('entry')
182 ->exportAs($request->attributes->get('_format'));
183 }
184
f8bf8952 185 /**
4346a860 186 * Create an entry.
f8bf8952
NL
187 *
188 * @ApiDoc(
a8c90c5c
NL
189 * parameters={
190 * {"name"="url", "dataType"="string", "required"=true, "format"="http://www.test.com/article.html", "description"="Url for the entry."},
191 * {"name"="title", "dataType"="string", "required"=false, "description"="Optional, we'll get the title from the page."},
192 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
189ef634
TC
193 * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="entry already starred"},
194 * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="entry already archived"},
a8c90c5c 195 * }
f8bf8952 196 * )
4346a860 197 *
60faee00 198 * @return JsonResponse
f8bf8952 199 */
843dbe51 200 public function postEntriesAction(Request $request)
f8bf8952 201 {
77273253
NL
202 $this->validateAuthentication();
203
c3235553 204 $url = $request->request->get('url');
51a15609 205 $title = $request->request->get('title');
873e3806
YE
206 $isArchived = $request->request->get('archive');
207 $isStarred = $request->request->get('starred');
c3235553 208
3107f92a
TC
209 $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($url, $this->getUser()->getId());
210
211 if (false === $entry) {
212 $entry = $this->get('wallabag_core.content_proxy')->updateEntry(
213 new Entry($this->getUser()),
214 $url
215 );
216 }
092ca707 217
51a15609
NL
218 if (!is_null($title)) {
219 $entry->setTitle($title);
220 }
221
0ca374e6
NL
222 $tags = $request->request->get('tags', '');
223 if (!empty($tags)) {
c2656f96 224 $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
0ca374e6 225 }
092ca707 226
bc2b947c
TC
227 if (!is_null($isStarred)) {
228 $entry->setStarred((bool) $isStarred);
229 }
816ad405 230
bc2b947c
TC
231 if (!is_null($isArchived)) {
232 $entry->setArchived((bool) $isArchived);
233 }
816ad405 234
843dbe51
NL
235 $em = $this->getDoctrine()->getManager();
236 $em->persist($entry);
816ad405 237
843dbe51
NL
238 $em->flush();
239
aa4d6562
NL
240 $json = $this->get('serializer')->serialize($entry, 'json');
241
60faee00 242 return (new JsonResponse())->setJson($json);
f8bf8952
NL
243 }
244
245 /**
4346a860 246 * Change several properties of an entry.
f8bf8952
NL
247 *
248 * @ApiDoc(
249 * requirements={
250 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
a8c90c5c
NL
251 * },
252 * parameters={
253 * {"name"="title", "dataType"="string", "required"=false},
254 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
189ef634
TC
255 * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="archived the entry."},
256 * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="starred the entry."},
1d147791 257 * }
f8bf8952 258 * )
4346a860 259 *
60faee00 260 * @return JsonResponse
f8bf8952 261 */
be463487 262 public function patchEntriesAction(Entry $entry, Request $request)
f8bf8952 263 {
77273253 264 $this->validateAuthentication();
fcb1fba5 265 $this->validateUserAccess($entry->getUser()->getId());
092ca707 266
8ce32af6 267 $title = $request->request->get('title');
614a0bfd
YE
268 $isArchived = $request->request->get('archive');
269 $isStarred = $request->request->get('starred');
2c093b03
NL
270
271 if (!is_null($title)) {
272 $entry->setTitle($title);
273 }
274
275 if (!is_null($isArchived)) {
189ef634 276 $entry->setArchived((bool) $isArchived);
2c093b03
NL
277 }
278
2c093b03 279 if (!is_null($isStarred)) {
189ef634 280 $entry->setStarred((bool) $isStarred);
2c093b03
NL
281 }
282
0ca374e6
NL
283 $tags = $request->request->get('tags', '');
284 if (!empty($tags)) {
c2656f96 285 $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
0ca374e6 286 }
092ca707 287
2c093b03 288 $em = $this->getDoctrine()->getManager();
2c093b03
NL
289 $em->flush();
290
0ca374e6
NL
291 $json = $this->get('serializer')->serialize($entry, 'json');
292
60faee00 293 return (new JsonResponse())->setJson($json);
f8bf8952
NL
294 }
295
296 /**
4346a860 297 * Delete **permanently** an entry.
f8bf8952
NL
298 *
299 * @ApiDoc(
a8c90c5c
NL
300 * requirements={
301 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
302 * }
f8bf8952 303 * )
4346a860 304 *
60faee00 305 * @return JsonResponse
f8bf8952 306 */
be463487 307 public function deleteEntriesAction(Entry $entry)
f8bf8952 308 {
77273253 309 $this->validateAuthentication();
fcb1fba5 310 $this->validateUserAccess($entry->getUser()->getId());
092ca707 311
42a90646 312 $em = $this->getDoctrine()->getManager();
1d147791 313 $em->remove($entry);
42a90646
NL
314 $em->flush();
315
1d147791
NL
316 $json = $this->get('serializer')->serialize($entry, 'json');
317
60faee00 318 return (new JsonResponse())->setJson($json);
f8bf8952
NL
319 }
320
321 /**
4346a860 322 * Retrieve all tags for an entry.
f8bf8952
NL
323 *
324 * @ApiDoc(
325 * requirements={
326 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
327 * }
328 * )
8eedc8cf 329 *
60faee00 330 * @return JsonResponse
f8bf8952 331 */
be463487 332 public function getEntriesTagsAction(Entry $entry)
7df80cb3 333 {
77273253 334 $this->validateAuthentication();
fcb1fba5 335 $this->validateUserAccess($entry->getUser()->getId());
092ca707 336
1bd12b62 337 $json = $this->get('serializer')->serialize($entry->getTags(), 'json');
0a018fe0 338
60faee00 339 return (new JsonResponse())->setJson($json);
f8bf8952
NL
340 }
341
342 /**
4346a860 343 * Add one or more tags to an entry.
f8bf8952
NL
344 *
345 * @ApiDoc(
346 * requirements={
347 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
a8c90c5c
NL
348 * },
349 * parameters={
350 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
351 * }
f8bf8952 352 * )
8eedc8cf 353 *
60faee00 354 * @return JsonResponse
f8bf8952 355 */
a36737f4 356 public function postEntriesTagsAction(Request $request, Entry $entry)
7df80cb3 357 {
77273253 358 $this->validateAuthentication();
fcb1fba5 359 $this->validateUserAccess($entry->getUser()->getId());
a36737f4 360
0ca374e6
NL
361 $tags = $request->request->get('tags', '');
362 if (!empty($tags)) {
c2656f96 363 $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
0ca374e6 364 }
092ca707 365
a36737f4
NL
366 $em = $this->getDoctrine()->getManager();
367 $em->persist($entry);
368 $em->flush();
369
370 $json = $this->get('serializer')->serialize($entry, 'json');
371
60faee00 372 return (new JsonResponse())->setJson($json);
f8bf8952
NL
373 }
374
375 /**
4346a860 376 * Permanently remove one tag for an entry.
f8bf8952
NL
377 *
378 * @ApiDoc(
379 * requirements={
769e19dc 380 * {"name"="tag", "dataType"="integer", "requirement"="\w+", "description"="The tag ID"},
f8bf8952
NL
381 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
382 * }
383 * )
8eedc8cf 384 *
60faee00 385 * @return JsonResponse
f8bf8952 386 */
653e8be4 387 public function deleteEntriesTagsAction(Entry $entry, Tag $tag)
f8bf8952 388 {
77273253 389 $this->validateAuthentication();
fcb1fba5 390 $this->validateUserAccess($entry->getUser()->getId());
092ca707
NL
391
392 $entry->removeTag($tag);
393 $em = $this->getDoctrine()->getManager();
394 $em->persist($entry);
395 $em->flush();
396
397 $json = $this->get('serializer')->serialize($entry, 'json');
398
60faee00 399 return (new JsonResponse())->setJson($json);
f8bf8952
NL
400 }
401
402 /**
4346a860 403 * Retrieve all tags.
f8bf8952 404 *
092ca707 405 * @ApiDoc()
8eedc8cf 406 *
60faee00 407 * @return JsonResponse
f8bf8952 408 */
092ca707 409 public function getTagsAction()
7df80cb3 410 {
77273253 411 $this->validateAuthentication();
fc732227
JB
412
413 $tags = $this->getDoctrine()
414 ->getRepository('WallabagCoreBundle:Tag')
28bb4890 415 ->findAllTags($this->getUser()->getId());
fc732227
JB
416
417 $json = $this->get('serializer')->serialize($tags, 'json');
092ca707 418
60faee00 419 return (new JsonResponse())->setJson($json);
f8bf8952
NL
420 }
421
f8bf8952 422 /**
4346a860 423 * Permanently remove one tag from **every** entry.
f8bf8952
NL
424 *
425 * @ApiDoc(
426 * requirements={
a0e1eafc 427 * {"name"="tag", "dataType"="string", "required"=true, "requirement"="\w+", "description"="Tag as a string"}
f8bf8952
NL
428 * }
429 * )
8eedc8cf 430 *
60faee00 431 * @return JsonResponse
f8bf8952 432 */
a0e1eafc 433 public function deleteTagLabelAction(Request $request)
f8bf8952 434 {
77273253 435 $this->validateAuthentication();
a0e1eafc
JB
436 $label = $request->request->get('tag', '');
437
438 $tag = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($label);
439
440 if (empty($tag)) {
441 throw $this->createNotFoundException('Tag not found');
442 }
fc732227
JB
443
444 $this->getDoctrine()
445 ->getRepository('WallabagCoreBundle:Entry')
446 ->removeTag($this->getUser()->getId(), $tag);
092ca707 447
ac8cf632
JB
448 $this->cleanOrphanTag($tag);
449
092ca707
NL
450 $json = $this->get('serializer')->serialize($tag, 'json');
451
60faee00 452 return (new JsonResponse())->setJson($json);
769e19dc 453 }
4da01f49
TC
454
455 /**
a0e1eafc 456 * Permanently remove some tags from **every** entry.
4da01f49
TC
457 *
458 * @ApiDoc(
459 * requirements={
a0e1eafc 460 * {"name"="tags", "dataType"="string", "required"=true, "format"="tag1,tag2", "description"="Tags as strings (comma splitted)"}
4da01f49
TC
461 * }
462 * )
463 *
60faee00 464 * @return JsonResponse
4da01f49 465 */
a0e1eafc 466 public function deleteTagsLabelAction(Request $request)
4da01f49
TC
467 {
468 $this->validateAuthentication();
4da01f49 469
a0e1eafc
JB
470 $tagsLabels = $request->request->get('tags', '');
471
472 $tags = [];
473
474 foreach (explode(',', $tagsLabels) as $tagLabel) {
475 $tagEntity = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($tagLabel);
476
477 if (!empty($tagEntity)) {
478 $tags[] = $tagEntity;
479 }
480 }
481
482 if (empty($tags)) {
483 throw $this->createNotFoundException('Tags not found');
484 }
485
4da01f49
TC
486 $this->getDoctrine()
487 ->getRepository('WallabagCoreBundle:Entry')
a0e1eafc 488 ->removeTags($this->getUser()->getId(), $tags);
4da01f49 489
ac8cf632
JB
490 $this->cleanOrphanTag($tags);
491
a0e1eafc 492 $json = $this->get('serializer')->serialize($tags, 'json');
4da01f49 493
60faee00 494 return (new JsonResponse())->setJson($json);
4da01f49
TC
495 }
496
497 /**
a0e1eafc 498 * Permanently remove one tag from **every** entry.
4da01f49
TC
499 *
500 * @ApiDoc(
501 * requirements={
a0e1eafc 502 * {"name"="tag", "dataType"="integer", "requirement"="\w+", "description"="The tag"}
4da01f49
TC
503 * }
504 * )
505 *
60faee00 506 * @return JsonResponse
4da01f49 507 */
a0e1eafc 508 public function deleteTagAction(Tag $tag)
4da01f49
TC
509 {
510 $this->validateAuthentication();
511
4da01f49
TC
512 $this->getDoctrine()
513 ->getRepository('WallabagCoreBundle:Entry')
a0e1eafc 514 ->removeTag($this->getUser()->getId(), $tag);
4da01f49 515
ac8cf632
JB
516 $this->cleanOrphanTag($tag);
517
a0e1eafc 518 $json = $this->get('serializer')->serialize($tag, 'json');
4da01f49 519
60faee00 520 return (new JsonResponse())->setJson($json);
4da01f49
TC
521 }
522
351eb8d9
TC
523 /**
524 * Retrieve annotations for an entry.
525 *
526 * @ApiDoc(
527 * requirements={
528 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
529 * }
530 * )
531 *
1eea248b 532 * @param Entry $entry
b1e92f8c 533 *
1eea248b 534 * @return JsonResponse
351eb8d9
TC
535 */
536 public function getAnnotationsAction(Entry $entry)
537 {
351eb8d9
TC
538 $this->validateAuthentication();
539
0c271b9e
TC
540 return $this->forward('WallabagApiBundle:WallabagRest:getAnnotations', [
541 'entry' => $entry,
542 ]);
351eb8d9
TC
543 }
544
545 /**
546 * Creates a new annotation.
547 *
1eea248b 548 * @param Request $request
b1e92f8c
TC
549 * @param Entry $entry
550 *
1eea248b 551 * @return JsonResponse
351eb8d9
TC
552 * @ApiDoc(
553 * requirements={
554 * {"name"="ranges", "dataType"="array", "requirement"="\w+", "description"="The range array for the annotation"},
555 * {"name"="quote", "dataType"="string", "required"=false, "description"="Optional, quote for the annotation"},
556 * {"name"="text", "dataType"="string", "required"=true, "description"=""},
557 * }
558 * )
351eb8d9
TC
559 */
560 public function postAnnotationAction(Request $request, Entry $entry)
561 {
562 $this->validateAuthentication();
563
0c271b9e 564 return $this->forward('WallabagApiBundle:WallabagRest:postAnnotation', [
1eea248b 565 'request' => $request,
b1e92f8c 566 'entry' => $entry,
1eea248b 567 ]);
351eb8d9
TC
568 }
569
570 /**
571 * Updates an annotation.
572 *
573 * @ApiDoc(
574 * requirements={
575 * {"name"="annotation", "dataType"="string", "requirement"="\w+", "description"="The annotation ID"}
576 * }
577 * )
578 *
579 * @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation")
580 *
1eea248b 581 * @param Annotation $annotation
b1e92f8c
TC
582 * @param Request $request
583 *
1eea248b 584 * @return JsonResponse
351eb8d9
TC
585 */
586 public function putAnnotationAction(Annotation $annotation, Request $request)
587 {
351eb8d9
TC
588 $this->validateAuthentication();
589
0c271b9e 590 return $this->forward('WallabagApiBundle:WallabagRest:putAnnotation', [
1eea248b 591 'annotation' => $annotation,
b1e92f8c 592 'request' => $request,
1eea248b 593 ]);
351eb8d9
TC
594 }
595
596 /**
597 * Removes an annotation.
598 *
599 * @ApiDoc(
600 * requirements={
601 * {"name"="annotation", "dataType"="string", "requirement"="\w+", "description"="The annotation ID"}
602 * }
603 * )
604 *
605 * @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation")
606 *
1eea248b 607 * @param Annotation $annotation
b1e92f8c 608 *
1eea248b 609 * @return JsonResponse
351eb8d9
TC
610 */
611 public function deleteAnnotationAction(Annotation $annotation)
612 {
351eb8d9
TC
613 $this->validateAuthentication();
614
0c271b9e 615 return $this->forward('WallabagApiBundle:WallabagRest:deleteAnnotation', [
1eea248b
TC
616 'annotation' => $annotation,
617 ]);
351eb8d9
TC
618 }
619
2b477030 620 /**
6f8310b4
TC
621 * Retrieve version number.
622 *
623 * @ApiDoc()
2b477030 624 *
60faee00 625 * @return JsonResponse
2b477030
V
626 */
627 public function getVersionAction()
628 {
629 $version = $this->container->getParameter('wallabag_core.version');
630
631 $json = $this->get('serializer')->serialize($version, 'json');
632
60faee00 633 return (new JsonResponse())->setJson($json);
2b477030 634 }
769e19dc 635
ac8cf632
JB
636 /**
637 * Remove orphan tag in case no entries are associated to it.
638 *
639 * @param Tag|array $tags
640 */
641 private function cleanOrphanTag($tags)
642 {
643 if (!is_array($tags)) {
644 $tags = [$tags];
645 }
646
647 $em = $this->getDoctrine()->getManager();
648
649 foreach ($tags as $tag) {
650 if (count($tag->getEntries()) === 0) {
651 $em->remove($tag);
652 }
653 }
654
655 $em->flush();
656 }
657
769e19dc
J
658 /**
659 * Validate that the first id is equal to the second one.
4346a860 660 * If not, throw exception. It means a user try to access information from an other user.
769e19dc 661 *
4346a860 662 * @param int $requestUserId User id from the requested source
769e19dc 663 */
fcb1fba5 664 private function validateUserAccess($requestUserId)
769e19dc 665 {
18f8f32f 666 $user = $this->get('security.token_storage')->getToken()->getUser();
fcb1fba5
NL
667 if ($requestUserId != $user->getId()) {
668 throw $this->createAccessDeniedException('Access forbidden. Entry user id: '.$requestUserId.', logged user id: '.$user->getId());
769e19dc
J
669 }
670 }
7df80cb3 671}