ActivitypubActor.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. * @category ActivityPub
  24. *
  25. * @author Diogo Peralta Cordeiro <@diogo.site>
  26. * @copyright 2018-2019, 2021 Free Software Foundation, Inc http://www.fsf.org
  27. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  28. */
  29. namespace Plugin\ActivityPub\Entity;
  30. use App\Core\Cache;
  31. use App\Core\DB;
  32. use App\Core\Entity;
  33. use function App\Core\I18n\_m;
  34. use App\Core\Log;
  35. use App\Entity\Actor;
  36. use Component\FreeNetwork\Util\Discovery;
  37. use DateTimeInterface;
  38. use Exception;
  39. use Plugin\ActivityPub\Util\DiscoveryHints;
  40. use Plugin\ActivityPub\Util\Explorer;
  41. use XML_XRD;
  42. /**
  43. * Table Definition for activitypub_actor
  44. *
  45. * @copyright 2018-2019, 2021 Free Software Foundation, Inc http://www.fsf.org
  46. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  47. */
  48. class ActivitypubActor extends Entity
  49. {
  50. // {{{ Autocode
  51. // @codeCoverageIgnoreStart
  52. private string $uri;
  53. private int $actor_id;
  54. private string $inbox_uri;
  55. private ?string $inbox_shared_uri = null;
  56. private ?string $url = null;
  57. private DateTimeInterface $created;
  58. private DateTimeInterface $modified;
  59. public function setUri(string $uri): self
  60. {
  61. $this->uri = $uri;
  62. return $this;
  63. }
  64. public function getUri(): string
  65. {
  66. return $this->uri;
  67. }
  68. public function setActorId(int $actor_id): self
  69. {
  70. $this->actor_id = $actor_id;
  71. return $this;
  72. }
  73. public function getActorId(): int
  74. {
  75. return $this->actor_id;
  76. }
  77. public function setInboxUri(string $inbox_uri): self
  78. {
  79. $this->inbox_uri = $inbox_uri;
  80. return $this;
  81. }
  82. public function getInboxUri(): string
  83. {
  84. return $this->inbox_uri;
  85. }
  86. public function setInboxSharedUri(?string $inbox_shared_uri): self
  87. {
  88. $this->inbox_shared_uri = $inbox_shared_uri;
  89. return $this;
  90. }
  91. public function getInboxSharedUri(): ?string
  92. {
  93. return $this->inbox_shared_uri;
  94. }
  95. public function setUrl(?string $url): self
  96. {
  97. $this->url = $url;
  98. return $this;
  99. }
  100. public function getUrl(): ?string
  101. {
  102. return $this->url;
  103. }
  104. public function setCreated(DateTimeInterface $created): self
  105. {
  106. $this->created = $created;
  107. return $this;
  108. }
  109. public function getCreated(): DateTimeInterface
  110. {
  111. return $this->created;
  112. }
  113. public function setModified(DateTimeInterface $modified): self
  114. {
  115. $this->modified = $modified;
  116. return $this;
  117. }
  118. public function getModified(): DateTimeInterface
  119. {
  120. return $this->modified;
  121. }
  122. // @codeCoverageIgnoreEnd
  123. // }}} Autocode
  124. public function getActor(): Actor
  125. {
  126. return Actor::getById($this->getActorId());
  127. }
  128. /**
  129. * Look up, and if necessary create, an Activitypub_profile for the remote
  130. * entity with the given WebFinger address.
  131. * This should never return null -- you will either get an object or
  132. * an exception will be thrown.
  133. *
  134. * @param string $addr WebFinger address
  135. *
  136. * @throws Exception on error conditions
  137. */
  138. public static function getByAddr(string $addr): Actor
  139. {
  140. // Normalize $addr, i.e. add 'acct:' if missing
  141. $addr = Discovery::normalize($addr);
  142. // Try the cache
  143. $uri = Cache::get(sprintf('ActivitypubActor-webfinger-%s', urlencode($addr)), fn () => false);
  144. if ($uri !== false) {
  145. if (\is_null($uri)) {
  146. // TRANS: Exception.
  147. throw new Exception(_m('Not a valid WebFinger address (via cache).'));
  148. }
  149. try {
  150. return DB::wrapInTransaction(fn () => Explorer::getOneFromUri($uri));
  151. } catch (Exception $e) {
  152. Log::error(sprintf(__METHOD__ . ': WebFinger address cache inconsistent with database, did not find Activitypub_profile uri==%s', $uri), [$e]);
  153. Cache::set(sprintf('ActivitypubActor-webfinger-%s', urlencode($addr)), false);
  154. }
  155. }
  156. // Now, try some discovery
  157. $disco = new Discovery();
  158. try {
  159. $xrd = $disco->lookup($addr);
  160. } catch (Exception $e) {
  161. // Save negative cache entry so we don't waste time looking it up again.
  162. // @todo FIXME: Distinguish temporary failures?
  163. Cache::set(sprintf('ActivitypubActor-webfinger-%s', urlencode($addr)), null);
  164. // TRANS: Exception.
  165. throw new Exception(_m('Not a valid WebFinger address: ' . $e->getMessage()));
  166. }
  167. return self::fromXrd($addr, $xrd);
  168. }
  169. public static function fromXrd(string $addr, XML_XRD $xrd): Actor
  170. {
  171. $hints = array_merge(
  172. ['webfinger' => $addr],
  173. DiscoveryHints::fromXRD($xrd),
  174. );
  175. if (\array_key_exists('activitypub', $hints)) {
  176. $uri = $hints['activitypub'];
  177. try {
  178. LOG::info("Discovery on acct:{$addr} with URI:{$uri}");
  179. $actor = DB::wrapInTransaction(fn () => Explorer::getOneFromUri($hints['activitypub']));
  180. Cache::set(sprintf('ActivitypubActor-webfinger-%s', urlencode($addr)), $hints['activitypub']);
  181. return $actor;
  182. } catch (Exception $e) {
  183. Log::warning("Failed creating profile from URI:'{$uri}', error:" . $e->getMessage());
  184. throw $e;
  185. // keep looking
  186. //
  187. // @todo FIXME: This means an error discovering from profile page
  188. // may give us a corrupt entry using the webfinger URI, which
  189. // will obscure the correct page-keyed profile later on.
  190. }
  191. }
  192. // XXX: try hcard
  193. // XXX: try FOAF
  194. // TRANS: Exception. %s is a WebFinger address.
  195. throw new Exception(sprintf(_m('Could not find a valid profile for "%s".'), $addr));
  196. }
  197. /**
  198. * @param ActivitypubActor $ap_actor
  199. *
  200. * @throws Exception
  201. */
  202. public static function update_profile(
  203. self &$ap_actor,
  204. Actor &$actor,
  205. ActivitypubRsa &$activitypub_rsa,
  206. string $res,
  207. ): void {
  208. \Plugin\ActivityPub\Util\Model\Actor::fromJson($res, ['objects' => [
  209. 'ActivitypubActor' => &$ap_actor,
  210. 'Actor' => &$actor,
  211. 'ActivitypubRsa' => &$activitypub_rsa,
  212. ]]);
  213. }
  214. public static function schemaDef(): array
  215. {
  216. return [
  217. 'name' => 'activitypub_actor',
  218. 'fields' => [
  219. 'uri' => ['type' => 'text', 'not null' => true],
  220. 'actor_id' => ['type' => 'int', 'not null' => true],
  221. 'inbox_uri' => ['type' => 'text', 'not null' => true],
  222. 'inbox_shared_uri' => ['type' => 'text'],
  223. 'url' => ['type' => 'text'],
  224. 'created' => ['type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was created'],
  225. 'modified' => ['type' => 'timestamp', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'],
  226. ],
  227. 'primary key' => ['uri'],
  228. 'unique keys' => [
  229. 'activitypub_actor_id_ukey' => ['actor_id'],
  230. ],
  231. 'foreign keys' => [
  232. 'activitypub_actor_actor_id_fkey' => ['actor', ['actor_id' => 'id']],
  233. ],
  234. ];
  235. }
  236. }