AttachmentEmbed.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. /**
  20. * OembedPlugin implementation for GNU social
  21. *
  22. * @package GNUsocial
  23. *
  24. * @author Stephen Paul Weber
  25. * @author Mikael Nordfeldth
  26. * @author Diogo Cordeiro <diogo@fc.up.pt>
  27. * @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
  28. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  29. */
  30. namespace Plugin\Embed\Entity;
  31. use App\Core\DB\DB;
  32. use App\Core\Entity;
  33. use Component\Attachment\Entity\Attachment;
  34. use DateTimeInterface;
  35. /**
  36. * Table Definition for attachment_embed
  37. *
  38. * @author Hugo Sales <hugo@hsal.es>
  39. * @copyright 2019, 2021 Free Software Foundation, Inc http://www.fsf.org
  40. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  41. */
  42. class AttachmentEmbed extends Entity
  43. {
  44. // {{{ Autocode
  45. // @codeCoverageIgnoreStart
  46. private int $link_id;
  47. private int $attachment_id;
  48. private ?string $title;
  49. private ?string $description;
  50. private ?string $provider_name;
  51. private ?string $provider_url;
  52. private ?string $author_name;
  53. private ?string $author_url;
  54. private ?string $thumbnail_url;
  55. private DateTimeInterface $modified;
  56. public function setLinkId(int $link_id): self
  57. {
  58. $this->link_id = $link_id;
  59. return $this;
  60. }
  61. public function getLinkId(): int
  62. {
  63. return $this->link_id;
  64. }
  65. public function getAttachmentId(): int
  66. {
  67. return $this->attachment_id;
  68. }
  69. public function setAttachmentId(int $attachment_id): void
  70. {
  71. $this->attachment_id = $attachment_id;
  72. }
  73. public function setProviderName(?string $provider_name): self
  74. {
  75. $this->provider_name = $provider_name;
  76. return $this;
  77. }
  78. public function getProviderName(): ?string
  79. {
  80. return $this->provider_name;
  81. }
  82. public function setProviderUrl(?string $provider_url): self
  83. {
  84. $this->provider_url = $provider_url;
  85. return $this;
  86. }
  87. public function getProviderUrl(): ?string
  88. {
  89. return $this->provider_url;
  90. }
  91. public function setDescription(?string $description): self
  92. {
  93. $this->description = $description;
  94. return $this;
  95. }
  96. public function getDescription(): ?string
  97. {
  98. return $this->description;
  99. }
  100. public function setTitle(?string $title): self
  101. {
  102. $this->title = $title;
  103. return $this;
  104. }
  105. public function getTitle(): ?string
  106. {
  107. return $this->title;
  108. }
  109. public function setAuthorName(?string $author_name): self
  110. {
  111. $this->author_name = $author_name;
  112. return $this;
  113. }
  114. public function getAuthorName(): ?string
  115. {
  116. return $this->author_name;
  117. }
  118. public function setAuthorUrl(?string $author_url): self
  119. {
  120. $this->author_url = $author_url;
  121. return $this;
  122. }
  123. public function getAuthorUrl(): ?string
  124. {
  125. return $this->author_url;
  126. }
  127. public function setThumbnailUrl(?string $thumbnail_url): self
  128. {
  129. $this->thumbnail_url = $thumbnail_url;
  130. return $this;
  131. }
  132. public function getThumbnailUrl(): ?string
  133. {
  134. return $this->thumbnail_url;
  135. }
  136. public function setModified(DateTimeInterface $modified): self
  137. {
  138. $this->modified = $modified;
  139. return $this;
  140. }
  141. public function getModified(): DateTimeInterface
  142. {
  143. return $this->modified;
  144. }
  145. // @codeCoverageIgnoreEnd
  146. // }}} Autocode
  147. /**
  148. * Generate the Embed thumbnail HTML attributes
  149. *
  150. * @return mixed[] ['class' => string, 'has_attachment' => bool, 'height' => int|null, 'width' => int|null]
  151. */
  152. public function getImageHTMLAttributes(): array
  153. {
  154. $attr = ['class' => 'u-photo embed'];
  155. $attachment = DB::find('attachment', ['id' => $this->getAttachmentId()]);
  156. $thumbnail = $attachment->getThumbnail('medium');
  157. if (\is_null($attachment) || \is_null($attachment->getWidth()) || \is_null($attachment->getHeight())) {
  158. $attr['has_attachment'] = false;
  159. } elseif (!\is_null($thumbnail)) {
  160. $attr['has_attachment'] = true;
  161. $attr['width'] = $thumbnail->getWidth();
  162. $attr['height'] = $thumbnail->getHeight();
  163. }
  164. return $attr;
  165. }
  166. public static function schemaDef()
  167. {
  168. return [
  169. 'name' => 'attachment_embed',
  170. 'fields' => [
  171. 'link_id' => ['type' => 'int', 'not null' => true, 'description' => 'Embed for that URL/file'],
  172. 'attachment_id' => ['type' => 'int', 'not null' => true, 'description' => 'Attachment relation, used to show previews'],
  173. 'provider_name' => ['type' => 'text', 'description' => 'Name of this Embed provider'],
  174. 'provider_url' => ['type' => 'text', 'description' => 'URL of this Embed provider'],
  175. 'title' => ['type' => 'text', 'description' => 'Title of Embed resource when available'],
  176. 'description' => ['type' => 'text', 'description' => 'Description of Embed resource when available'],
  177. 'author_name' => ['type' => 'text', 'description' => 'Author name for this Embed resource'],
  178. 'author_url' => ['type' => 'text', 'description' => 'Author URL for this Embed resource'],
  179. 'thumbnail_url' => ['type' => 'text', 'description' => 'URL for this Embed resource when applicable (image)'],
  180. 'modified' => ['type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'],
  181. ],
  182. 'primary key' => ['link_id'],
  183. 'foreign keys' => [
  184. 'attachment_embed_link_id_fkey' => ['link', ['link_id' => 'id']],
  185. 'attachment_embed_attachment_id_fkey' => ['attachment', ['attachment_id' => 'id']],
  186. ],
  187. ];
  188. }
  189. }