LeftPanel.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. declare(strict_types = 1);
  3. // {{{ License
  4. // This file is part of GNU social - https://www.gnu.org/software/social
  5. //
  6. // GNU social is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // GNU social is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  18. // }}}
  19. namespace Component\LeftPanel;
  20. use App\Core\Cache;
  21. use App\Core\DB;
  22. use App\Core\Event;
  23. use function App\Core\I18n\_m;
  24. use App\Core\Modules\Component;
  25. use App\Core\Router;
  26. use App\Entity\Actor;
  27. use App\Entity\Feed;
  28. use App\Util\Exception\ClientException;
  29. use App\Util\Exception\NotFoundException;
  30. use Component\LeftPanel\Controller as C;
  31. use EventResult;
  32. class LeftPanel extends Component
  33. {
  34. public function onAddRoute(Router $r): EventResult
  35. {
  36. $r->connect('edit_feeds', '/edit-feeds', C\EditFeeds::class);
  37. return Event::next;
  38. }
  39. /**
  40. * @param array<string, string> $route_params
  41. *
  42. * @throws \App\Util\Exception\DuplicateFoundException
  43. * @throws \App\Util\Exception\ServerException
  44. * @throws ClientException
  45. */
  46. public function onAppendFeed(Actor $actor, string $title, string $route, array $route_params): EventResult
  47. {
  48. $cache_key = Feed::cacheKey($actor);
  49. $feeds = Feed::getFeeds($actor);
  50. $ordering = end($feeds)->getOrdering();
  51. $url = Router::url($route, $route_params);
  52. try {
  53. $feed = DB::findOneBy('feed', ['actor_id' => $actor->getId(), 'url' => $url]);
  54. throw new ClientException(_m(
  55. 'Cannot add feed with url "{url}" because it already exists with title "{title}"',
  56. ['{url}' => $url, '{title}' => $feed->getTitle()],
  57. ));
  58. } catch (NotFoundException) {
  59. DB::persist(Feed::create([
  60. 'actor_id' => $actor->getId(),
  61. 'url' => $url,
  62. 'route' => $route,
  63. 'title' => $title,
  64. 'ordering' => $ordering + 1,
  65. ]));
  66. DB::flush();
  67. Cache::delete($cache_key);
  68. return Event::stop;
  69. }
  70. }
  71. }