2021-10-05 01:00:58 +09:00
|
|
|
<?php
|
2021-10-10 17:26:18 +09:00
|
|
|
|
2021-12-26 18:48:16 +09:00
|
|
|
declare(strict_types = 1);
|
2021-10-05 01:00:58 +09:00
|
|
|
|
2021-12-04 13:07:08 +09:00
|
|
|
// {{{ License
|
2021-10-05 01:00:58 +09:00
|
|
|
// This file is part of GNU social - https://www.gnu.org/software/social
|
|
|
|
//
|
|
|
|
// GNU social is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// GNU social is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
// }}}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ActivityPub implementation for GNU social
|
|
|
|
*
|
|
|
|
* @package GNUsocial
|
2021-12-04 13:07:08 +09:00
|
|
|
* @category ActivityPub
|
2021-12-26 18:48:16 +09:00
|
|
|
*
|
2021-12-04 13:07:08 +09:00
|
|
|
* @author Diogo Peralta Cordeiro <@diogo.site>
|
|
|
|
* @copyright 2018-2019, 2021 Free Software Foundation, Inc http://www.fsf.org
|
2021-10-05 01:00:58 +09:00
|
|
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Plugin\ActivityPub\Entity;
|
|
|
|
|
2021-10-27 12:14:01 +09:00
|
|
|
use App\Core\Cache;
|
2022-03-27 23:19:09 +09:00
|
|
|
use App\Core\DB;
|
2021-10-05 01:00:58 +09:00
|
|
|
use App\Core\Entity;
|
2021-12-26 18:48:16 +09:00
|
|
|
use function App\Core\I18n\_m;
|
2021-10-27 12:14:01 +09:00
|
|
|
use App\Core\Log;
|
2021-12-05 12:11:08 +09:00
|
|
|
use App\Entity\Actor;
|
2021-10-27 12:14:01 +09:00
|
|
|
use Component\FreeNetwork\Util\Discovery;
|
2021-10-05 01:00:58 +09:00
|
|
|
use DateTimeInterface;
|
2021-10-27 12:14:01 +09:00
|
|
|
use Exception;
|
|
|
|
use Plugin\ActivityPub\Util\DiscoveryHints;
|
|
|
|
use Plugin\ActivityPub\Util\Explorer;
|
2021-12-04 13:07:08 +09:00
|
|
|
use XML_XRD;
|
2021-10-05 01:00:58 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Table Definition for activitypub_actor
|
|
|
|
*
|
2021-12-04 13:07:08 +09:00
|
|
|
* @copyright 2018-2019, 2021 Free Software Foundation, Inc http://www.fsf.org
|
2021-10-05 01:00:58 +09:00
|
|
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
|
|
|
*/
|
|
|
|
class ActivitypubActor extends Entity
|
|
|
|
{
|
|
|
|
// {{{ Autocode
|
|
|
|
// @codeCoverageIgnoreStart
|
|
|
|
private string $uri;
|
|
|
|
private int $actor_id;
|
|
|
|
private string $inbox_uri;
|
2021-12-27 06:32:09 +09:00
|
|
|
private ?string $inbox_shared_uri = null;
|
|
|
|
private ?string $url = null;
|
2021-10-10 17:26:18 +09:00
|
|
|
private DateTimeInterface $created;
|
|
|
|
private DateTimeInterface $modified;
|
2021-10-05 01:00:58 +09:00
|
|
|
|
2021-10-19 21:48:50 +09:00
|
|
|
public function setUri(string $uri): self
|
2021-10-05 01:00:58 +09:00
|
|
|
{
|
|
|
|
$this->uri = $uri;
|
2021-10-19 21:48:50 +09:00
|
|
|
return $this;
|
2021-10-05 01:00:58 +09:00
|
|
|
}
|
|
|
|
|
2021-12-27 00:12:06 +09:00
|
|
|
public function getUri(): string
|
2021-12-02 12:33:40 +09:00
|
|
|
{
|
2021-12-27 00:12:06 +09:00
|
|
|
return $this->uri;
|
2021-12-02 12:33:40 +09:00
|
|
|
}
|
|
|
|
|
2021-12-27 00:12:06 +09:00
|
|
|
public function setActorId(int $actor_id): self
|
2021-12-02 12:33:40 +09:00
|
|
|
{
|
2021-12-27 00:12:06 +09:00
|
|
|
$this->actor_id = $actor_id;
|
2021-12-02 12:33:40 +09:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2021-10-05 01:00:58 +09:00
|
|
|
public function getActorId(): int
|
|
|
|
{
|
|
|
|
return $this->actor_id;
|
|
|
|
}
|
|
|
|
|
2021-12-27 00:12:06 +09:00
|
|
|
public function setInboxUri(string $inbox_uri): self
|
2021-10-05 01:00:58 +09:00
|
|
|
{
|
2021-12-27 00:12:06 +09:00
|
|
|
$this->inbox_uri = $inbox_uri;
|
2021-10-19 21:48:50 +09:00
|
|
|
return $this;
|
2021-10-05 01:00:58 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getInboxUri(): string
|
|
|
|
{
|
|
|
|
return $this->inbox_uri;
|
|
|
|
}
|
|
|
|
|
2021-12-27 00:12:06 +09:00
|
|
|
public function setInboxSharedUri(?string $inbox_shared_uri): self
|
2021-10-05 01:00:58 +09:00
|
|
|
{
|
2021-12-27 00:12:06 +09:00
|
|
|
$this->inbox_shared_uri = $inbox_shared_uri;
|
2021-10-19 21:48:50 +09:00
|
|
|
return $this;
|
2021-10-05 01:00:58 +09:00
|
|
|
}
|
|
|
|
|
2021-11-30 08:58:42 +09:00
|
|
|
public function getInboxSharedUri(): ?string
|
2021-10-05 01:00:58 +09:00
|
|
|
{
|
|
|
|
return $this->inbox_shared_uri;
|
|
|
|
}
|
|
|
|
|
2021-12-27 00:12:06 +09:00
|
|
|
public function setUrl(?string $url): self
|
2021-10-05 01:00:58 +09:00
|
|
|
{
|
2021-12-27 00:12:06 +09:00
|
|
|
$this->url = $url;
|
2021-10-19 21:48:50 +09:00
|
|
|
return $this;
|
2021-10-05 01:00:58 +09:00
|
|
|
}
|
|
|
|
|
2021-12-27 00:12:06 +09:00
|
|
|
public function getUrl(): ?string
|
2021-10-05 01:00:58 +09:00
|
|
|
{
|
2021-12-27 00:12:06 +09:00
|
|
|
return $this->url;
|
2021-10-05 01:00:58 +09:00
|
|
|
}
|
|
|
|
|
2021-10-19 21:48:50 +09:00
|
|
|
public function setCreated(DateTimeInterface $created): self
|
2021-10-05 01:00:58 +09:00
|
|
|
{
|
|
|
|
$this->created = $created;
|
2021-10-19 21:48:50 +09:00
|
|
|
return $this;
|
2021-10-05 01:00:58 +09:00
|
|
|
}
|
|
|
|
|
2021-12-27 00:12:06 +09:00
|
|
|
public function getCreated(): DateTimeInterface
|
2021-10-05 01:00:58 +09:00
|
|
|
{
|
2021-12-27 00:12:06 +09:00
|
|
|
return $this->created;
|
2021-10-05 01:00:58 +09:00
|
|
|
}
|
|
|
|
|
2021-10-19 21:48:50 +09:00
|
|
|
public function setModified(DateTimeInterface $modified): self
|
2021-10-05 01:00:58 +09:00
|
|
|
{
|
|
|
|
$this->modified = $modified;
|
2021-10-19 21:48:50 +09:00
|
|
|
return $this;
|
2021-10-05 01:00:58 +09:00
|
|
|
}
|
2021-12-27 00:12:06 +09:00
|
|
|
|
|
|
|
public function getModified(): DateTimeInterface
|
|
|
|
{
|
|
|
|
return $this->modified;
|
|
|
|
}
|
|
|
|
|
2021-10-05 01:00:58 +09:00
|
|
|
// @codeCoverageIgnoreEnd
|
|
|
|
// }}} Autocode
|
|
|
|
|
2022-02-25 10:05:28 +09:00
|
|
|
public function getActor(): Actor
|
|
|
|
{
|
|
|
|
return Actor::getById($this->getActorId());
|
|
|
|
}
|
|
|
|
|
2021-10-27 12:14:01 +09:00
|
|
|
/**
|
|
|
|
* Look up, and if necessary create, an Activitypub_profile for the remote
|
|
|
|
* entity with the given WebFinger address.
|
|
|
|
* This should never return null -- you will either get an object or
|
|
|
|
* an exception will be thrown.
|
|
|
|
*
|
|
|
|
* @param string $addr WebFinger address
|
|
|
|
*
|
|
|
|
* @throws Exception on error conditions
|
|
|
|
*/
|
2022-02-25 10:05:28 +09:00
|
|
|
public static function getByAddr(string $addr): Actor
|
2021-10-27 12:14:01 +09:00
|
|
|
{
|
|
|
|
// Normalize $addr, i.e. add 'acct:' if missing
|
|
|
|
$addr = Discovery::normalize($addr);
|
|
|
|
|
|
|
|
// Try the cache
|
2021-12-26 18:48:16 +09:00
|
|
|
$uri = Cache::get(sprintf('ActivitypubActor-webfinger-%s', urlencode($addr)), fn () => false);
|
2021-10-27 12:14:01 +09:00
|
|
|
|
|
|
|
if ($uri !== false) {
|
2021-12-26 18:48:16 +09:00
|
|
|
if (\is_null($uri)) {
|
2021-10-27 12:14:01 +09:00
|
|
|
// TRANS: Exception.
|
|
|
|
throw new Exception(_m('Not a valid WebFinger address (via cache).'));
|
|
|
|
}
|
|
|
|
try {
|
2022-02-25 10:05:28 +09:00
|
|
|
return DB::wrapInTransaction(fn () => Explorer::getOneFromUri($uri));
|
2021-10-27 12:14:01 +09:00
|
|
|
} catch (Exception $e) {
|
2022-02-25 10:05:28 +09:00
|
|
|
Log::error(sprintf(__METHOD__ . ': WebFinger address cache inconsistent with database, did not find Activitypub_profile uri==%s', $uri), [$e]);
|
2021-10-27 12:14:01 +09:00
|
|
|
Cache::set(sprintf('ActivitypubActor-webfinger-%s', urlencode($addr)), false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now, try some discovery
|
|
|
|
|
|
|
|
$disco = new Discovery();
|
|
|
|
|
|
|
|
try {
|
|
|
|
$xrd = $disco->lookup($addr);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
// Save negative cache entry so we don't waste time looking it up again.
|
|
|
|
// @todo FIXME: Distinguish temporary failures?
|
|
|
|
Cache::set(sprintf('ActivitypubActor-webfinger-%s', urlencode($addr)), null);
|
|
|
|
// TRANS: Exception.
|
|
|
|
throw new Exception(_m('Not a valid WebFinger address: ' . $e->getMessage()));
|
|
|
|
}
|
|
|
|
|
2021-12-02 13:25:58 +09:00
|
|
|
return self::fromXrd($addr, $xrd);
|
|
|
|
}
|
|
|
|
|
2022-02-25 10:05:28 +09:00
|
|
|
public static function fromXrd(string $addr, XML_XRD $xrd): Actor
|
2021-12-02 13:25:58 +09:00
|
|
|
{
|
2021-10-27 12:14:01 +09:00
|
|
|
$hints = array_merge(
|
|
|
|
['webfinger' => $addr],
|
|
|
|
DiscoveryHints::fromXRD($xrd),
|
|
|
|
);
|
|
|
|
|
2021-12-26 18:48:16 +09:00
|
|
|
if (\array_key_exists('activitypub', $hints)) {
|
2021-10-27 12:14:01 +09:00
|
|
|
$uri = $hints['activitypub'];
|
|
|
|
try {
|
|
|
|
LOG::info("Discovery on acct:{$addr} with URI:{$uri}");
|
2022-02-25 10:05:28 +09:00
|
|
|
$actor = DB::wrapInTransaction(fn () => Explorer::getOneFromUri($hints['activitypub']));
|
|
|
|
Cache::set(sprintf('ActivitypubActor-webfinger-%s', urlencode($addr)), $hints['activitypub']);
|
|
|
|
return $actor;
|
2021-10-27 12:14:01 +09:00
|
|
|
} catch (Exception $e) {
|
|
|
|
Log::warning("Failed creating profile from URI:'{$uri}', error:" . $e->getMessage());
|
|
|
|
throw $e;
|
|
|
|
// keep looking
|
|
|
|
//
|
|
|
|
// @todo FIXME: This means an error discovering from profile page
|
|
|
|
// may give us a corrupt entry using the webfinger URI, which
|
|
|
|
// will obscure the correct page-keyed profile later on.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX: try hcard
|
|
|
|
// XXX: try FOAF
|
|
|
|
|
|
|
|
// TRANS: Exception. %s is a WebFinger address.
|
|
|
|
throw new Exception(sprintf(_m('Could not find a valid profile for "%s".'), $addr));
|
|
|
|
}
|
|
|
|
|
2021-12-05 12:11:08 +09:00
|
|
|
/**
|
|
|
|
* @param ActivitypubActor $ap_actor
|
2021-12-26 18:48:16 +09:00
|
|
|
*
|
2021-12-05 12:11:08 +09:00
|
|
|
* @throws Exception
|
|
|
|
*/
|
2022-03-02 06:20:24 +09:00
|
|
|
public static function update_profile(
|
|
|
|
self &$ap_actor,
|
|
|
|
Actor &$actor,
|
|
|
|
ActivitypubRsa &$activitypub_rsa,
|
|
|
|
string $res,
|
|
|
|
): void {
|
|
|
|
\Plugin\ActivityPub\Util\Model\Actor::fromJson($res, ['objects' => [
|
|
|
|
'ActivitypubActor' => &$ap_actor,
|
|
|
|
'Actor' => &$actor,
|
|
|
|
'ActivitypubRsa' => &$activitypub_rsa,
|
|
|
|
]]);
|
2021-12-05 12:11:08 +09:00
|
|
|
}
|
|
|
|
|
2021-10-19 21:48:50 +09:00
|
|
|
public static function schemaDef(): array
|
2021-10-05 01:00:58 +09:00
|
|
|
{
|
|
|
|
return [
|
2021-12-26 18:48:16 +09:00
|
|
|
'name' => 'activitypub_actor',
|
2021-10-05 01:00:58 +09:00
|
|
|
'fields' => [
|
2021-12-26 18:48:16 +09:00
|
|
|
'uri' => ['type' => 'text', 'not null' => true],
|
|
|
|
'actor_id' => ['type' => 'int', 'not null' => true],
|
|
|
|
'inbox_uri' => ['type' => 'text', 'not null' => true],
|
2021-10-05 01:00:58 +09:00
|
|
|
'inbox_shared_uri' => ['type' => 'text'],
|
2021-12-26 18:48:16 +09:00
|
|
|
'url' => ['type' => 'text'],
|
|
|
|
'created' => ['type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was created'],
|
|
|
|
'modified' => ['type' => 'timestamp', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'],
|
2021-10-05 01:00:58 +09:00
|
|
|
],
|
2022-02-25 10:05:28 +09:00
|
|
|
'primary key' => ['uri'],
|
2022-02-19 02:48:06 +09:00
|
|
|
'unique keys' => [
|
|
|
|
'activitypub_actor_id_ukey' => ['actor_id'],
|
|
|
|
],
|
2021-10-05 01:00:58 +09:00
|
|
|
'foreign keys' => [
|
|
|
|
'activitypub_actor_actor_id_fkey' => ['actor', ['actor_id' => 'id']],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|