Oomox.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 Plugin\Oomox;
  20. use App\Core\Cache;
  21. use App\Core\DB\DB;
  22. use App\Core\Event;
  23. use App\Core\Modules\Plugin;
  24. use App\Core\Router\RouteLoader;
  25. use App\Core\Router\Router;
  26. use App\Entity\LocalUser;
  27. use App\Util\Common;
  28. use App\Util\Exception\NotFoundException;
  29. use App\Util\Exception\RedirectException;
  30. use App\Util\Exception\ServerException;
  31. use Plugin\Oomox\Controller as C;
  32. use Symfony\Component\HttpFoundation\Request;
  33. /**
  34. * Profile Colour plugin main class
  35. *
  36. * @package GNUsocial
  37. * @category Oomox
  38. *
  39. * @author Eliseu Amaro <mail@eliseuama.ro>
  40. * @copyright 2020 Free Software Foundation, Inc http://www.fsf.org
  41. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  42. */
  43. class Oomox extends Plugin
  44. {
  45. /**
  46. * Maps Routes to their respective Controllers
  47. */
  48. public function onAddRoute(RouteLoader $r): bool
  49. {
  50. $r->connect('oomox_settings', 'settings/oomox', [Controller\Oomox::class, 'oomoxSettings']);
  51. $r->connect('oomox_css', 'plugins/oomox/colours', [Controller\Oomox::class, 'oomoxCSS']);
  52. return Event::next;
  53. }
  54. /**
  55. * Populates an additional profile user panel section
  56. * Used in templates/settings/base.html.twig
  57. *
  58. * @throws \App\Util\Exception\NoLoggedInUser
  59. * @throws RedirectException
  60. * @throws ServerException
  61. */
  62. public function onPopulateSettingsTabs(Request $request, string $section, array &$tabs): bool
  63. {
  64. if ($section === 'colours') {
  65. $tabs[] = [
  66. 'title' => 'Light theme colours',
  67. 'desc' => 'Change the theme colours.',
  68. 'id' => 'settings-light-theme-colours-details',
  69. 'controller' => C\Oomox::oomoxSettingsLight($request),
  70. ];
  71. $tabs[] = [
  72. 'title' => 'Dark theme colours',
  73. 'desc' => 'Change the theme colours.',
  74. 'id' => 'settings-dark-theme-colours-details',
  75. 'controller' => C\Oomox::oomoxSettingsDark($request),
  76. ];
  77. }
  78. return Event::next;
  79. }
  80. /**
  81. * Returns Oomox cache key for the given user.
  82. */
  83. public static function cacheKey(LocalUser $user): string
  84. {
  85. return "oomox-css-{$user->getId()}";
  86. }
  87. /**
  88. * Returns Entity\Oomox if it already exists
  89. */
  90. public static function getEntity(LocalUser $user): ?Entity\Oomox
  91. {
  92. try {
  93. return Cache::get(self::cacheKey($user), static fn () => DB::findOneBy('oomox', ['actor_id' => $user->getId()]));
  94. } catch (NotFoundException $e) {
  95. return null;
  96. }
  97. }
  98. /**
  99. * Adds to array $styles the generated CSS according to user settings, if any are present.
  100. */
  101. public function onEndShowStyles(array &$styles, string $route): bool
  102. {
  103. $user = Common::user();
  104. if ($user && Cache::get(self::cacheKey($user), static fn () => self::getEntity($user))) {
  105. $styles[] = Router::url('oomox_css');
  106. }
  107. return Event::next;
  108. }
  109. }