[FreeNetwork] Initial multi-protocol support

This commit is contained in:
2021-12-02 04:25:58 +00:00
parent dbaee08038
commit d044039272
6 changed files with 307 additions and 53 deletions

View File

@@ -0,0 +1,180 @@
<?php
declare(strict_types = 1);
// {{{ License
// 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
*
* @author Diogo Peralta Cordeiro <@diogo.site
* @copyright 2021 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
namespace Component\FreeNetwork\Entity;
use App\Core\Cache;
use App\Core\DB\DB;
use App\Core\Entity;
use function App\Core\I18n\_m;
use App\Core\Log;
use App\Entity\Actor;
use Component\FreeNetwork\Util\Discovery;
use DateTimeInterface;
use Exception;
use Plugin\ActivityPub\Util\DiscoveryHints;
use Plugin\ActivityPub\Util\Explorer;
/**
* Table Definition for free_network_actor_protocol
*
* @copyright 2021 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
class FreeNetworkActorProtocol extends Entity
{
// {{{ Autocode
// @codeCoverageIgnoreStart;
private int $actor_id;
private ?string $protocol;
private ?string $addr;
private DateTimeInterface $created;
private DateTimeInterface $modified;
public function getActorId(): int
{
return $this->actor_id;
}
public function setActorId(int $actor_id): self
{
$this->actor_id = $actor_id;
return $this;
}
public function setProtocol(?string $protocol): self
{
$this->protocol = $protocol;
return $this;
}
public function getProtocol(): ?string
{
return $this->protocol;
}
public function setAddr(string $addr): self
{
$this->addr = $addr;
return $this;
}
public function getAddr(): string
{
return $this->addr;
}
public function getCreated(): DateTimeInterface
{
return $this->created;
}
public function setCreated(DateTimeInterface $created): self
{
$this->created = $created;
return $this;
}
public function getModified(): DateTimeInterface
{
return $this->modified;
}
public function setModified(DateTimeInterface $modified): self
{
$this->modified = $modified;
return $this;
}
// @codeCoverageIgnoreEnd
// }}} Autocode
public static function protocolSucceeded(string $protocol, int|Actor $actor_id, string $addr): void
{
$actor_id = is_int($actor_id) ? $actor_id : $actor_id->getId();
$attributed_protocol = self::getWithPK(['actor_id' => $actor_id]);
if (is_null($attributed_protocol)) {
$attributed_protocol = self::create([
'actor_id' => $actor_id,
'protocol' => $protocol,
'addr' => Discovery::normalize($addr),
]);
} else {
$attributed_protocol->setProtocol($protocol);
}
DB::wrapInTransaction(fn() => DB::persist($attributed_protocol));
}
public static function canIActor(string $protocol, int|Actor $actor_id): bool
{
$actor_id = is_int($actor_id) ? $actor_id : $actor_id->getId();
$attributed_protocol = self::getWithPK(['actor_id' => $actor_id])?->getProtocol();
if (is_null($attributed_protocol)) {
// If it is not attributed, you can go ahead.
return true;
} else {
// If it is attributed, you can on the condition that you're assigned to it.
return $attributed_protocol === $protocol;
}
}
public static function canIAddr(string $protocol, string $target): bool
{
// Normalize $addr, i.e. add 'acct:' if missing
$addr = Discovery::normalize($target);
$attributed_protocol = self::getWithPK(['addr' => $addr])?->getProtocol();
if (is_null($attributed_protocol)) {
// If it is not attributed, you can go ahead.
return true;
} else {
// If it is attributed, you can on the condition that you're assigned to it.
return $attributed_protocol === $protocol;
}
}
public static function schemaDef(): array
{
return [
'name' => 'free_network_actor_protocol',
'fields' => [
'actor_id' => ['type' => 'int', 'not null' => true],
'protocol' => ['type' => 'varchar', 'length' => 32, 'description' => 'the protocol plugin that should handle federation of this actor'],
'addr' => ['type' => 'text', 'not null' => true, 'description' => 'webfinger acct'],
'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'],
],
'primary key' => ['actor_id'],
'foreign keys' => [
'activitypub_actor_actor_id_fkey' => ['actor', ['actor_id' => 'id']],
],
];
}
}

View File

@@ -22,8 +22,11 @@ declare(strict_types = 1);
namespace Component\FreeNetwork;
use App\Core\Event;
use App\Core\GSFile;
use App\Core\HTTPClient;
use App\Entity\Activity;
use Plugin\ActivityPub\Entity\ActivitypubActor;
use XML_XRD;
use function App\Core\I18n\_m;
use App\Core\Log;
use App\Core\Modules\Component;
@@ -304,7 +307,7 @@ class FreeNetwork extends Component
// In the regexp below we need to match / _before_ URL_REGEX_VALID_PATH_CHARS because it otherwise gets merged
// with the TLD before (but / is in URL_REGEX_VALID_PATH_CHARS anyway, it's just its positioning that is important)
$result = preg_match_all(
'/(?:^|\s+)' . preg_quote($preMention, '/') . '(' . URL_REGEX_DOMAIN_NAME . '(?:\/[' . URL_REGEX_VALID_PATH_CHARS . ']*)*)/',
'/' . Nickname::BEFORE_MENTIONS . preg_quote($preMention, '/') . '(' . URL_REGEX_DOMAIN_NAME . '(?:\/[' . URL_REGEX_VALID_PATH_CHARS . ']*)*)/',
$text,
$wmatches,
PREG_OFFSET_CAPTURE
@@ -362,46 +365,72 @@ class FreeNetwork extends Component
foreach (self::extractUrlMentions($text) as $wmatch) {
[$target, $pos] = $wmatch;
$schemes = ['https', 'http'];
foreach ($schemes as $scheme) {
$url = "$scheme://$target";
if (Common::isValidHttpUrl($url)) {
// This means $resource is a valid url
Log::info("Checking actor address '$url'");
$actor = null;
$resource_parts = parse_url($url);
// TODO: Use URLMatcher
if ($resource_parts['host'] === $_ENV['SOCIAL_DOMAIN']) { // XXX: Common::config('site', 'server')) {
$str = $resource_parts['path'];
// actor_view_nickname
$renick = '/\/@(' . Nickname::DISPLAY_FMT . ')\/?/m';
// actor_view_id
$reuri = '/\/actor\/(\d+)\/?/m';
if (preg_match_all($renick, $str, $matches, PREG_SET_ORDER, 0) === 1) {
$actor = LocalUser::getWithPK(['nickname' => $matches[0][1]])->getActor();
} elseif (preg_match_all($reuri, $str, $matches, PREG_SET_ORDER, 0) === 1) {
$actor = Actor::getById((int) $matches[0][1]);
} else {
Log::error('Unexpected behaviour onEndFindMentions at FreeNetwork');
throw new ServerException('Unexpected behaviour onEndFindMentions at FreeNetwork');
}
$url = "https://$target";
if (Common::isValidHttpUrl($url)) {
// This means $resource is a valid url
$resource_parts = parse_url($url);
// TODO: Use URLMatcher
if ($resource_parts['host'] === $_ENV['SOCIAL_DOMAIN']) { // XXX: Common::config('site', 'server')) {
$str = $resource_parts['path'];
// actor_view_nickname
$renick = '/\/@(' . Nickname::DISPLAY_FMT . ')\/?/m';
// actor_view_id
$reuri = '/\/actor\/(\d+)\/?/m';
if (preg_match_all($renick, $str, $matches, PREG_SET_ORDER, 0) === 1) {
$actor = LocalUser::getWithPK(['nickname' => $matches[0][1]])->getActor();
} elseif (preg_match_all($reuri, $str, $matches, PREG_SET_ORDER, 0) === 1) {
$actor = Actor::getById((int) $matches[0][1]);
} else {
Event::handle('FreeNetworkFindUrlMentions', [$url, &$actor]);
if (is_null($actor)) {
continue;
}
Log::error('Unexpected behaviour onEndFindMentions at FreeNetwork');
throw new ServerException('Unexpected behaviour onEndFindMentions at FreeNetwork');
}
$displayName = $actor->getFullname() ?? $actor->getNickname() ?? $target; // TODO: we could do getBestName() or getFullname() here
$matches[$pos] = ['mentioned' => [$actor],
'type' => 'mention',
'text' => $displayName,
'position' => $pos,
'length' => mb_strlen($target),
'url' => $actor->getUri()
];
} else {
break;
Log::info("Checking actor address '$url'");
$link = new XML_XRD_Element_Link(
Discovery::LRDD_REL,
'https://' . parse_url($url, PHP_URL_HOST) . '/.well-known/webfinger?resource={uri}',
Discovery::JRD_MIMETYPE,
true // isTemplate
);
$xrd_uri = Discovery::applyTemplate($link->template, $url);
$response = HTTPClient::get($xrd_uri, ['headers' => ['Accept' => $link->type]]);
if ($response->getStatusCode() !== 200) {
continue;
}
$xrd = new XML_XRD();
switch (GSFile::mimetypeBare($response->getHeaders()['content-type'][0])) {
case Discovery::JRD_MIMETYPE_OLD:
case Discovery::JRD_MIMETYPE:
$type = 'json';
break;
case Discovery::XRD_MIMETYPE:
$type = 'xml';
break;
default:
// fall back to letting XML_XRD auto-detect
Log::debug('No recognized content-type header for resource descriptor body on ' . $xrd_uri);
$type = null;
}
$xrd->loadString($response->getContent(), $type);
$actor = null;
Event::handle('FreeNetworkFoundXrd', [$xrd, &$actor]);
if (is_null($actor)) {
continue;
}
}
$displayName = $actor->getFullname() ?? $actor->getNickname() ?? $target; // TODO: we could do getBestName() or getFullname() here
$matches[$pos] = [
'mentioned' => [$actor],
'type' => 'mention',
'text' => $displayName,
'position' => $pos,
'length' => mb_strlen($target),
'url' => $actor->getUri()
];
}
}
@@ -425,9 +454,12 @@ class FreeNetwork extends Component
{
$protocols = [];
Event::handle('AddFreeNetworkProtocol', [&$protocols]);
$delivered = [];
foreach ($protocols as $protocol) {
$protocol::freeNetworkDistribute($sender, $activity, $targets, $reason);
$protocol::freeNetworkDistribute($sender, $activity, $targets, $reason, $delivered);
}
$failed_targets = array_udiff($targets, $delivered, function(Actor $a, Actor $b):int {return $a->getId() <=> $b->getId();});
// TODO: Implement failed queues
return false;
}