Oomox.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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;
  22. use App\Core\Event;
  23. use App\Core\Modules\Plugin;
  24. use App\Core\Router;
  25. use App\Entity\LocalUser;
  26. use App\Util\Common;
  27. use App\Util\Exception\NotFoundException;
  28. use App\Util\Exception\RedirectException;
  29. use App\Util\Exception\ServerException;
  30. use EventResult;
  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(Router $r): EventResult
  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 'person/settings.html.twig'
  57. *
  58. * @param SettingsTabsType $tabs
  59. * @throws \App\Util\Exception\NoLoggedInUser
  60. * @throws RedirectException
  61. * @throws ServerException
  62. */
  63. public function onPopulateSettingsTabs(Request $request, string $section, array &$tabs): EventResult
  64. {
  65. if ($section === 'colours') {
  66. $tabs[] = [
  67. 'title' => 'Light theme colours',
  68. 'desc' => 'Change the theme colours.',
  69. 'id' => 'settings-light-theme-colours-details',
  70. 'controller' => C\Oomox::oomoxSettingsLight($request),
  71. ];
  72. $tabs[] = [
  73. 'title' => 'Dark theme colours',
  74. 'desc' => 'Change the theme colours.',
  75. 'id' => 'settings-dark-theme-colours-details',
  76. 'controller' => C\Oomox::oomoxSettingsDark($request),
  77. ];
  78. }
  79. return Event::next;
  80. }
  81. /**
  82. * Returns Oomox cache key for the given user.
  83. */
  84. public static function cacheKey(LocalUser $user): string
  85. {
  86. return "oomox-css-{$user->getId()}";
  87. }
  88. /**
  89. * Returns Entity\Oomox if it already exists
  90. */
  91. public static function getEntity(LocalUser $user): ?Entity\Oomox
  92. {
  93. try {
  94. return Cache::get(self::cacheKey($user), static fn () => DB::findOneBy('oomox', ['actor_id' => $user->getId()]));
  95. } catch (NotFoundException $e) {
  96. return null;
  97. }
  98. }
  99. /**
  100. * Adds to array $styles the generated CSS according to user settings, if any are present.
  101. */
  102. public function onEndShowStyles(array &$styles, string $route): EventResult
  103. {
  104. $user = Common::user();
  105. if ($user && Cache::get(self::cacheKey($user), static fn () => self::getEntity($user))) {
  106. $styles[] = Router::url('oomox_css');
  107. }
  108. return Event::next;
  109. }
  110. }