LeftPanel.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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\DB;
  22. use App\Core\Event;
  23. use function App\Core\I18n\_m;
  24. use App\Core\Modules\Component;
  25. use App\Core\Router\RouteLoader;
  26. use App\Core\Router\Router;
  27. use App\Entity\Actor;
  28. use App\Entity\Feed;
  29. use App\Util\Exception\ClientException;
  30. use App\Util\Exception\NotFoundException;
  31. use Component\LeftPanel\Controller as C;
  32. class LeftPanel extends Component
  33. {
  34. public function onAddRoute(RouteLoader $r): bool
  35. {
  36. $r->connect('edit_feeds', '/edit-feeds', C\EditFeeds::class);
  37. return Event::next;
  38. }
  39. public function onAppendFeed(Actor $actor, string $title, string $route, array $route_params)
  40. {
  41. $cache_key = Feed::cacheKey($actor);
  42. $feeds = Feed::getFeeds($actor);
  43. $ordering = end($feeds)->getOrdering();
  44. $url = Router::url($route, $route_params);
  45. try {
  46. $feed = DB::findOneBy('feed', ['actor_id' => $actor->getId(), 'url' => $url]);
  47. throw new ClientException(_m(
  48. 'Cannot add feed with url "{url}" because it already exists with title "{title}"',
  49. ['{url}' => $url, '{title}' => $feed->getTitle()],
  50. ));
  51. } catch (NotFoundException) {
  52. DB::persist(Feed::create([
  53. 'actor_id' => $actor->getId(),
  54. 'url' => $url,
  55. 'route' => $route,
  56. 'title' => $title,
  57. 'ordering' => $ordering + 1,
  58. ]));
  59. DB::flush();
  60. Cache::delete($cache_key);
  61. return Event::stop;
  62. }
  63. }
  64. /**
  65. * Output our dedicated stylesheet
  66. *
  67. * @param array $styles stylesheets path
  68. *
  69. * @return bool hook value; true means continue processing, false means stop
  70. */
  71. public function onEndShowStyles(array &$styles, string $route): bool
  72. {
  73. $styles[] = 'components/LeftPanel/assets/css/view.css';
  74. return Event::next;
  75. }
  76. }