Avatar.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. // {{{ License
  3. // This file is part of GNU social - https://www.gnu.org/software/social
  4. //
  5. // GNU social is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // GNU social is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  17. // }}}
  18. namespace Component\Avatar;
  19. use App\Core\Cache;
  20. use App\Core\DB\DB;
  21. use App\Core\Event;
  22. use App\Core\GSFile;
  23. use App\Core\Modules\Component;
  24. use App\Core\Router\Router;
  25. use App\Util\Common;
  26. use Component\Avatar\Controller as C;
  27. use Component\Avatar\Exception\NoAvatarException;
  28. use Symfony\Component\HttpFoundation\Request;
  29. class Avatar extends Component
  30. {
  31. public function onAddRoute($r): bool
  32. {
  33. $r->connect('avatar', '/{gsactor_id<\d+>}/avatar/{size<full|big|medium|small>?full}', [Controller\Avatar::class, 'avatar_view']);
  34. $r->connect('settings_avatar', '/settings/avatar', [Controller\Avatar::class, 'settings_avatar']);
  35. return Event::next;
  36. }
  37. public function onPopulateProfileSettingsTabs(Request $request, &$tabs): bool
  38. {
  39. // TODO avatar template shouldn't be on settings folder
  40. $tabs[] = [
  41. 'title' => 'Avatar',
  42. 'desc' => 'Change your avatar.',
  43. 'controller' => C\Avatar::settings_avatar($request),
  44. ];
  45. return Event::next;
  46. }
  47. public function onStartTwigPopulateVars(array &$vars): bool
  48. {
  49. if (Common::user() != null) {
  50. $vars['user_avatar'] = self::getAvatarUrl();
  51. }
  52. return Event::next;
  53. }
  54. public function onGetAvatarUrl(int $gsactor_id, ?string &$url): bool
  55. {
  56. $url = self::getAvatarUrl($gsactor_id);
  57. return Event::next;
  58. }
  59. public function onAvatarUpdate(int $gsactor_id): bool
  60. {
  61. Cache::delete('avatar-' . $gsactor_id);
  62. Cache::delete('avatar-url-' . $gsactor_id);
  63. Cache::delete('avatar-file-info-' . $gsactor_id);
  64. return Event::next;
  65. }
  66. // UTILS ----------------------------------
  67. /**
  68. * Get the avatar associated with the given GSActor id
  69. */
  70. public static function getAvatar(?int $gsactor_id = null): Entity\Avatar
  71. {
  72. $gsactor_id = $gsactor_id ?: Common::userId();
  73. return GSFile::error(NoAvatarException::class,
  74. $gsactor_id,
  75. Cache::get("avatar-{$gsactor_id}",
  76. function () use ($gsactor_id) {
  77. return DB::dql('select a from Component\Avatar\Entity\Avatar a ' .
  78. 'where a.gsactor_id = :gsactor_id',
  79. ['gsactor_id' => $gsactor_id]);
  80. }));
  81. }
  82. /**
  83. * Get the cached avatar associated with the given GSActor id, or the current user if not given
  84. */
  85. public static function getAvatarUrl(?int $gsactor_id = null, string $size = 'full'): string
  86. {
  87. $gsactor_id = $gsactor_id ?: Common::userId();
  88. return Cache::get("avatar-url-{$gsactor_id}", function () use ($gsactor_id) {
  89. return Router::url('avatar', ['gsactor_id' => $gsactor_id, 'size' => 'full']);
  90. });
  91. }
  92. /**
  93. * Get the cached avatar file info associated with the given GSActor id
  94. *
  95. * Returns the avatar file's hash, mimetype, title and path.
  96. * Ensures exactly one cached value exists
  97. */
  98. public static function getAvatarFileInfo(int $gsactor_id): array
  99. {
  100. $res = Cache::get("avatar-file-info-{$gsactor_id}",
  101. function () use ($gsactor_id) {
  102. return DB::dql('select f.id, f.filename, a.filename title, f.mimetype ' .
  103. 'from App\Entity\Attachment f ' .
  104. 'join Component\Avatar\Entity\Avatar a with f.id = a.attachment_id ' .
  105. 'where a.gsactor_id = :gsactor_id',
  106. ['gsactor_id' => $gsactor_id]);
  107. }
  108. );
  109. if ($res === []) { // Avatar not found
  110. $filepath = INSTALLDIR . '/public/assets/default-avatar.svg';
  111. return [
  112. 'id' => null,
  113. 'filepath' => $filepath,
  114. 'mimetype' => 'image/svg+xml',
  115. 'filename' => null,
  116. 'title' => 'default_avatar.svg',
  117. ];
  118. } else {
  119. $res = $res[0]; // A user must always only have one avatar.
  120. $res['filepath'] = DB::findOneBy('attachment', ['id' => $res['id']])->getPath();
  121. return $res;
  122. }
  123. }
  124. }