upstream V3 development https://www.gnusocial.rocks/v3
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

177 lines
5.2 KiB

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