Avatar.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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\Avatar\Entity;
  20. use App\Core\Cache;
  21. use App\Core\DB\DB;
  22. use App\Core\Entity;
  23. use App\Core\Event;
  24. use App\Core\Router\Router;
  25. use App\Util\Common;
  26. use Component\Attachment\Entity\Attachment;
  27. use Component\Attachment\Entity\AttachmentThumbnail;
  28. use DateTimeInterface;
  29. /**
  30. * Entity for user's avatar
  31. *
  32. * @category DB
  33. * @package GNUsocial
  34. *
  35. * @author Zach Copley <zach@status.net>
  36. * @copyright 2010 StatusNet Inc.
  37. * @author Mikael Nordfeldth <mmn@hethane.se>
  38. * @copyright 2009-2014 Free Software Foundation, Inc http://www.fsf.org
  39. * @author Hugo Sales <hugo@hsal.es>
  40. * @copyright 2020-2021 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 Avatar extends Entity
  44. {
  45. // {{{ Autocode
  46. // @codeCoverageIgnoreStart
  47. private int $actor_id;
  48. private int $attachment_id;
  49. private ?string $title = null;
  50. private DateTimeInterface $created;
  51. private DateTimeInterface $modified;
  52. public function setActorId(int $actor_id): self
  53. {
  54. $this->actor_id = $actor_id;
  55. return $this;
  56. }
  57. public function getActorId(): int
  58. {
  59. return $this->actor_id;
  60. }
  61. public function setAttachmentId(int $attachment_id): self
  62. {
  63. $this->attachment_id = $attachment_id;
  64. return $this;
  65. }
  66. public function getAttachmentId(): int
  67. {
  68. return $this->attachment_id;
  69. }
  70. public function setTitle(?string $title): self
  71. {
  72. $this->title = \is_null($title) ? null : mb_substr($title, 0, 191);
  73. return $this;
  74. }
  75. public function getTitle(): ?string
  76. {
  77. return $this->title;
  78. }
  79. public function setCreated(DateTimeInterface $created): self
  80. {
  81. $this->created = $created;
  82. return $this;
  83. }
  84. public function getCreated(): DateTimeInterface
  85. {
  86. return $this->created;
  87. }
  88. public function setModified(DateTimeInterface $modified): self
  89. {
  90. $this->modified = $modified;
  91. return $this;
  92. }
  93. public function getModified(): DateTimeInterface
  94. {
  95. return $this->modified;
  96. }
  97. // @codeCoverageIgnoreEnd
  98. // }}} Autocode
  99. private ?Attachment $attachment = null;
  100. public function getUrl(string $size = 'medium', int $type = Router::ABSOLUTE_PATH): string
  101. {
  102. $actor_id = $this->getActorId();
  103. return Cache::get("avatar-url-{$actor_id}-{$size}-{$type}", fn () => Router::url('avatar_actor', ['actor_id' => $actor_id, 'size' => $size], $type));
  104. }
  105. public function getAttachment(): Attachment
  106. {
  107. $this->attachment ??= DB::findOneBy('attachment', ['id' => $this->getAttachmentId()]);
  108. return $this->attachment;
  109. }
  110. public function getAttachmentThumbnail(string $size): ?AttachmentThumbnail
  111. {
  112. return AttachmentThumbnail::getOrCreate($this->getAttachment(), $size);
  113. }
  114. public static function getFilePathStatic(string $filename): string
  115. {
  116. return Common::config('avatar', 'dir') . $filename;
  117. }
  118. public function getPath(): string
  119. {
  120. return Common::config('avatar', 'dir') . $this->getAttachment()->getFileName();
  121. }
  122. /**
  123. * Delete this avatar and kill corresponding attachment
  124. */
  125. public function delete(): bool
  126. {
  127. $actor_id = $this->getActorId();
  128. $attachment = $this->getAttachment();
  129. DB::wrapInTransaction(fn () => DB::removeBy(static::class, ['actor_id' => $actor_id]));
  130. $attachment->kill();
  131. Event::handle('AvatarUpdate', [$actor_id]);
  132. return true;
  133. }
  134. public static function schemaDef(): array
  135. {
  136. return [
  137. 'name' => 'avatar',
  138. 'fields' => [
  139. 'actor_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'Actor.id', 'multiplicity' => 'one to one', 'not null' => true, 'description' => 'foreign key to actor table'],
  140. 'attachment_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'Attachment.id', 'multiplicity' => 'one to one', 'not null' => true, 'description' => 'foreign key to attachment table'],
  141. 'title' => ['type' => 'varchar', 'length' => 191, 'description' => 'file name of resource when available'],
  142. 'created' => ['type' => 'datetime', 'not null' => true, 'description' => 'date this record was created', 'default' => 'CURRENT_TIMESTAMP'],
  143. 'modified' => ['type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified', 'default' => 'CURRENT_TIMESTAMP'],
  144. ],
  145. 'primary key' => ['actor_id'],
  146. 'indexes' => [
  147. 'avatar_attachment_id_idx' => ['attachment_id'],
  148. ],
  149. ];
  150. }
  151. }