FreeNetworkActorProtocol.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. * ActivityPub implementation for GNU social
  21. *
  22. * @package GNUsocial
  23. *
  24. * @author Diogo Peralta Cordeiro <@diogo.site
  25. * @copyright 2021 Free Software Foundation, Inc http://www.fsf.org
  26. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  27. */
  28. namespace Component\FreeNetwork\Entity;
  29. use App\Core\DB\DB;
  30. use App\Core\Entity;
  31. use App\Entity\Actor;
  32. use Component\FreeNetwork\Util\Discovery;
  33. use DateTimeInterface;
  34. /**
  35. * Table Definition for free_network_actor_protocol
  36. *
  37. * @copyright 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 FreeNetworkActorProtocol extends Entity
  41. {
  42. // {{{ Autocode
  43. // @codeCoverageIgnoreStart
  44. private int $actor_id;
  45. private ?string $protocol = null;
  46. private string $addr;
  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 setProtocol(?string $protocol): self
  59. {
  60. $this->protocol = \is_null($protocol) ? null : mb_substr($protocol, 0, 32);
  61. return $this;
  62. }
  63. public function getProtocol(): ?string
  64. {
  65. return $this->protocol;
  66. }
  67. public function setAddr(string $addr): self
  68. {
  69. $this->addr = $addr;
  70. return $this;
  71. }
  72. public function getAddr(): string
  73. {
  74. return $this->addr;
  75. }
  76. public function setCreated(DateTimeInterface $created): self
  77. {
  78. $this->created = $created;
  79. return $this;
  80. }
  81. public function getCreated(): DateTimeInterface
  82. {
  83. return $this->created;
  84. }
  85. public function setModified(DateTimeInterface $modified): self
  86. {
  87. $this->modified = $modified;
  88. return $this;
  89. }
  90. public function getModified(): DateTimeInterface
  91. {
  92. return $this->modified;
  93. }
  94. // @codeCoverageIgnoreEnd
  95. // }}} Autocode
  96. public static function protocolSucceeded(string $protocol, int|Actor $actor_id, string $addr): void
  97. {
  98. $actor_id = \is_int($actor_id) ? $actor_id : $actor_id->getId();
  99. $attributed_protocol = self::getByPK(['actor_id' => $actor_id]);
  100. if (\is_null($attributed_protocol)) {
  101. $attributed_protocol = self::create([
  102. 'actor_id' => $actor_id,
  103. 'protocol' => $protocol,
  104. 'addr' => Discovery::normalize($addr),
  105. ]);
  106. } else {
  107. $attributed_protocol->setProtocol($protocol);
  108. }
  109. DB::wrapInTransaction(fn () => DB::persist($attributed_protocol));
  110. }
  111. public static function canIActor(string $protocol, int|Actor $actor_id): bool
  112. {
  113. $actor_id = \is_int($actor_id) ? $actor_id : $actor_id->getId();
  114. $attributed_protocol = self::getByPK(['actor_id' => $actor_id])?->getProtocol();
  115. if (\is_null($attributed_protocol)) {
  116. // If it is not attributed, you can go ahead.
  117. return true;
  118. } else {
  119. // If it is attributed, you can on the condition that you're assigned to it.
  120. return $attributed_protocol === $protocol;
  121. }
  122. }
  123. public static function canIAddr(string $protocol, string $target): bool
  124. {
  125. // Normalize $addr, i.e. add 'acct:' if missing
  126. $addr = Discovery::normalize($target);
  127. $attributed_protocol = self::getByPK(['addr' => $addr])?->getProtocol();
  128. if (\is_null($attributed_protocol)) {
  129. // If it is not attributed, you can go ahead.
  130. return true;
  131. } else {
  132. // If it is attributed, you can on the condition that you're assigned to it.
  133. return $attributed_protocol === $protocol;
  134. }
  135. }
  136. public static function schemaDef(): array
  137. {
  138. return [
  139. 'name' => 'free_network_actor_protocol',
  140. 'fields' => [
  141. 'actor_id' => ['type' => 'int', 'not null' => true],
  142. 'protocol' => ['type' => 'varchar', 'length' => 32, 'description' => 'the protocol plugin that should handle federation of this actor'],
  143. 'addr' => ['type' => 'text', 'not null' => true, 'description' => 'webfinger acct'],
  144. 'created' => ['type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was created'],
  145. 'modified' => ['type' => 'timestamp', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'],
  146. ],
  147. 'primary key' => ['actor_id'],
  148. 'foreign keys' => [
  149. 'activitypub_actor_actor_id_fkey' => ['actor', ['actor_id' => 'id']],
  150. ],
  151. ];
  152. }
  153. }