AttachmentEmbed.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 $provider_name = null;
  49. private ?string $provider_url = null;
  50. private ?string $title = null;
  51. private ?string $description = null;
  52. private ?string $author_name = null;
  53. private ?string $author_url = null;
  54. private ?string $thumbnail_url = null;
  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 setAttachmentId(int $attachment_id): self
  66. {
  67. $this->attachment_id = $attachment_id;
  68. return $this;
  69. }
  70. public function getAttachmentId(): int
  71. {
  72. return $this->attachment_id;
  73. }
  74. public function setProviderName(?string $provider_name): self
  75. {
  76. $this->provider_name = $provider_name;
  77. return $this;
  78. }
  79. public function getProviderName(): ?string
  80. {
  81. return $this->provider_name;
  82. }
  83. public function setProviderUrl(?string $provider_url): self
  84. {
  85. $this->provider_url = $provider_url;
  86. return $this;
  87. }
  88. public function getProviderUrl(): ?string
  89. {
  90. return $this->provider_url;
  91. }
  92. public function setTitle(?string $title): self
  93. {
  94. $this->title = $title;
  95. return $this;
  96. }
  97. public function getTitle(): ?string
  98. {
  99. return $this->title;
  100. }
  101. public function setDescription(?string $description): self
  102. {
  103. $this->description = $description;
  104. return $this;
  105. }
  106. public function getDescription(): ?string
  107. {
  108. return $this->description;
  109. }
  110. public function setAuthorName(?string $author_name): self
  111. {
  112. $this->author_name = $author_name;
  113. return $this;
  114. }
  115. public function getAuthorName(): ?string
  116. {
  117. return $this->author_name;
  118. }
  119. public function setAuthorUrl(?string $author_url): self
  120. {
  121. $this->author_url = $author_url;
  122. return $this;
  123. }
  124. public function getAuthorUrl(): ?string
  125. {
  126. return $this->author_url;
  127. }
  128. public function setThumbnailUrl(?string $thumbnail_url): self
  129. {
  130. $this->thumbnail_url = $thumbnail_url;
  131. return $this;
  132. }
  133. public function getThumbnailUrl(): ?string
  134. {
  135. return $this->thumbnail_url;
  136. }
  137. public function setModified(DateTimeInterface $modified): self
  138. {
  139. $this->modified = $modified;
  140. return $this;
  141. }
  142. public function getModified(): DateTimeInterface
  143. {
  144. return $this->modified;
  145. }
  146. // @codeCoverageIgnoreEnd
  147. // }}} Autocode
  148. /**
  149. * Generate the Embed thumbnail HTML attributes
  150. *
  151. * @return mixed[] ['class' => string, 'has_attachment' => bool, 'height' => int|null, 'width' => int|null]
  152. */
  153. public function getImageHTMLAttributes(): array
  154. {
  155. $attr = ['class' => 'u-photo embed'];
  156. $attachment = DB::find('attachment', ['id' => $this->getAttachmentId()]);
  157. $thumbnail = $attachment->getThumbnail('medium');
  158. if (\is_null($attachment) || \is_null($attachment->getWidth()) || \is_null($attachment->getHeight())) {
  159. $attr['has_attachment'] = false;
  160. } elseif (!\is_null($thumbnail)) {
  161. $attr['has_attachment'] = true;
  162. $attr['width'] = $thumbnail->getWidth();
  163. $attr['height'] = $thumbnail->getHeight();
  164. }
  165. return $attr;
  166. }
  167. public static function schemaDef()
  168. {
  169. return [
  170. 'name' => 'attachment_embed',
  171. 'fields' => [
  172. 'link_id' => ['type' => 'int', 'not null' => true, 'description' => 'Embed for that URL/file'],
  173. 'attachment_id' => ['type' => 'int', 'not null' => true, 'description' => 'Attachment relation, used to show previews'],
  174. 'provider_name' => ['type' => 'text', 'description' => 'Name of this Embed provider'],
  175. 'provider_url' => ['type' => 'text', 'description' => 'URL of this Embed provider'],
  176. 'title' => ['type' => 'text', 'description' => 'Title of Embed resource when available'],
  177. 'description' => ['type' => 'text', 'description' => 'Description of Embed resource when available'],
  178. 'author_name' => ['type' => 'text', 'description' => 'Author name for this Embed resource'],
  179. 'author_url' => ['type' => 'text', 'description' => 'Author URL for this Embed resource'],
  180. 'thumbnail_url' => ['type' => 'text', 'description' => 'URL for this Embed resource when applicable (image)'],
  181. 'modified' => ['type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'],
  182. ],
  183. 'primary key' => ['link_id'],
  184. 'foreign keys' => [
  185. 'attachment_embed_link_id_fkey' => ['link', ['link_id' => 'id']],
  186. 'attachment_embed_attachment_id_fkey' => ['attachment', ['attachment_id' => 'id']],
  187. ],
  188. ];
  189. }
  190. }