[PLUGIN][ActivityPub] Introduce ActivitypubObject. Beware, inside the plugin, an Object can never be an Activity.

Many bug fixes and other major changes (interface changed, see EVENTS.md)
This commit is contained in:
2021-12-08 22:24:52 +00:00
parent b1227d36f1
commit 480a42cca5
10 changed files with 400 additions and 121 deletions

View File

@@ -31,6 +31,7 @@ declare(strict_types=1);
namespace Plugin\ActivityPub\Util\Model;
use ActivityPhp\Type;
use ActivityPhp\Type\AbstractObject;
use App\Core\DB\DB;
use App\Core\Event;
@@ -40,10 +41,15 @@ use App\Util\Exception\ClientException;
use App\Util\Exception\NoSuchActorException;
use DateTime;
use DateTimeInterface;
use Exception;
use InvalidArgumentException;
use Plugin\ActivityPub\ActivityPub;
use Plugin\ActivityPub\Entity\ActivitypubActivity;
use Plugin\ActivityPub\Util\Model;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
/**
* This class handles translation between JSON and ActivityPub Activities
@@ -60,72 +66,75 @@ class Activity extends Model
* @param string|AbstractObject $json
* @param array $options
* @return ActivitypubActivity
* @throws ClientException
* @throws NoSuchActorException
* @throws ClientExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ServerExceptionInterface
* @throws TransportExceptionInterface
*/
public static function fromJson(string|AbstractObject $json, array $options = []): ActivitypubActivity
{
$type_activity = is_string($json) ? self::jsonToType($json) : $json;
$source = $options['source'];
$activity_stream_two_verb_to_gs_verb = fn(string $verb): string => match ($verb) {
'Create' => 'create',
default => throw new ClientException('Invalid verb'),
};
$activity_stream_two_object_type_to_gs_table = fn(string $object): string => match ($object) {
'Note' => 'note',
default => throw new ClientException('Invalid verb'),
};
// Ditch known activities
$ap_act = ActivitypubActivity::getWithPK(['activity_uri' => $type_activity->get('id')]);
if (is_null($ap_act)) {
$actor = ActivityPub::getActorByUri($type_activity->get('actor'));
if (!$type_activity->has('object') || !$type_activity->get('object')->has('type')) {
throw new InvalidArgumentException('Activity Object or Activity Object Type is missing.');
if (!is_null($ap_act)) {
return $ap_act;
}
// Find Actor and Object
$actor = ActivityPub::getActorByUri($type_activity->get('actor'));
$type_object = $type_activity->get('object');
if (is_string($type_object)) { // Retrieve it
$type_object = ActivityPub::getObjectByUri($type_object, try_online: true);
} else { // Encapsulated, if we have it locally, prefer it
$type_object = ActivityPub::getObjectByUri($type_object->get('id'), try_online: false) ?? $type_object;
}
if (($type_object instanceof Type\AbstractObject)) { // It's a new object apparently
if (Event::handle('NewActivityPubActivity', [$actor, $type_activity, $type_object, &$ap_act]) !== Event::stop) {
return self::handle_core_activity($actor, $type_activity, $type_object, $ap_act);
}
// Store Object if new
$ap_act = ActivitypubActivity::getWithPK(['object_uri' => $type_activity->get('object')->get('id')]);
if (!is_null($ap_act)) {
$obj = $ap_act->getActivity()->getObject();
} else { // Object was already stored locally then
if (Event::handle('NewActivityPubActivityWithObject', [$actor, $type_activity, $type_object, &$ap_act]) !== Event::stop) {
return self::handle_core_activity($actor, $type_activity, $type_object, $ap_act);
}
}
return $ap_act;
}
private static function handle_core_activity(\App\Entity\Actor $actor, AbstractObject $type_activity, mixed $type_object, ?ActivitypubActivity &$ap_act): ActivitypubActivity
{
if ($type_activity->get('type') === 'Create' && $type_object->get('type') === 'Note') {
if ($type_object instanceof AbstractObject) {
$note = Note::fromJson($type_object, ['test_authority' => true, 'actor_uri' => $type_activity->get('actor'), 'actor' => $actor, 'actor_id' => $actor->getId()]);
} else {
$obj = null;
switch ($type_activity->get('object')->get('type')) {
case 'Note':
$obj = Note::fromJson($type_activity->get('object'), ['source' => $source, 'actor_uri' => $type_activity->get('actor'), 'actor_id' => $actor->getId()]);
break;
default:
if (!Event::handle('ActivityPubObject', [$type_activity->get('object')->get('type'), $type_activity->get('object'), &$obj])) {
throw new ClientException('Unsupported Object type.');
}
break;
if ($type_object instanceof \App\Entity\Note) {
$note = $type_object;
} else {
throw new Exception('dunno bro');
}
DB::persist($obj);
}
// Store Activity
$act = GSActivity::create([
'actor_id' => $actor->getId(),
'verb' => $activity_stream_two_verb_to_gs_verb($type_activity->get('type')),
'object_type' => $activity_stream_two_object_type_to_gs_table($type_activity->get('object')->get('type')),
'object_id' => $obj->getId(),
'is_local' => false,
'verb' => 'create',
'object_type' => 'note',
'object_id' => $note->getId(),
'created' => new DateTime($type_activity->get('published') ?? 'now'),
'source' => $source,
'source' => 'ActivityPub',
]);
DB::persist($act);
// Store ActivityPub Activity
$ap_act = ActivitypubActivity::create([
'activity_id' => $act->getId(),
'activity_uri' => $type_activity->get('id'),
'object_uri' => $type_activity->get('object')->get('id'),
'is_local' => false,
'created' => new DateTime($type_activity->get('published') ?? 'now'),
'modified' => new DateTime(),
]);
DB::persist($ap_act);
}
Event::handle('ActivityPubNewActivity', [&$ap_act, &$act, &$obj]);
return $ap_act;
}

View File

@@ -44,7 +44,6 @@ use App\Util\Exception\ServerException;
use App\Util\Formatting;
use App\Util\TemporaryFile;
use Component\Avatar\Avatar;
use Component\Avatar\Exception\NoAvatarException;
use DateTime;
use DateTimeInterface;
use Exception;
@@ -120,12 +119,12 @@ class Actor extends Model
}
// Avatar
if ($person->has('icon')) {
if ($person->has('icon') && !empty($person->get('icon'))) {
try {
// Retrieve media
$get_response = HTTPClient::get($person->get('icon')->get('url'));
$media = $get_response->getContent();
$mimetype = $get_response->getHeaders()['content-type'][0] ?? null;
$media = $get_response->getContent();
$mimetype = $get_response->getHeaders()['content-type'][0] ?? null;
unset($get_response);
// Only handle if it is an image
@@ -139,7 +138,7 @@ class Actor extends Model
// Delete current avatar if there's one
$avatar = DB::find('avatar', ['actor_id' => $actor->getId()]);
$avatar?->delete();
DB::wrapInTransaction(function() use ($attachment, $actor) {
DB::wrapInTransaction(function () use ($attachment, $actor) {
DB::persist($attachment);
DB::persist(\Component\Avatar\Entity\Avatar::create(['actor_id' => $actor->getId(), 'attachment_id' => $attachment->getId()]));
});
@@ -157,11 +156,10 @@ class Actor extends Model
$avatar->delete();
Event::handle('AvatarUpdate', [$actor->getId()]);
} catch (Exception) {
// No avatar set, so cannot delete
// No avatar set, so cannot delete
}
}
Event::handle('ActivityPubNewActor', [&$ap_actor, &$actor, &$apRSA]);
return $ap_actor;
}

View File

@@ -37,11 +37,13 @@ use App\Core\Event;
use App\Core\GSFile;
use App\Core\HTTPClient;
use App\Core\Router\Router;
use App\Entity\Actor;
use App\Entity\Language;
use App\Entity\Note as GSNote;
use App\Util\Common;
use App\Util\Exception\ClientException;
use App\Util\Exception\DuplicateFoundException;
use App\Util\Exception\NoSuchActorException;
use App\Util\Exception\ServerException;
use App\Util\Formatting;
use App\Util\TemporaryFile;
use Component\Attachment\Entity\ActorToAttachment;
@@ -51,9 +53,15 @@ use DateTimeInterface;
use Exception;
use InvalidArgumentException;
use Plugin\ActivityPub\ActivityPub;
use Plugin\ActivityPub\Entity\ActivitypubObject;
use Plugin\ActivityPub\Util\Model;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use function App\Core\I18n\_m;
use function is_null;
use function is_string;
/**
* This class handles translation between JSON and GSNotes
@@ -71,28 +79,52 @@ class Note extends Model
* @param string|AbstractObject $json
* @param array $options
* @return GSNote
* @throws Exception
* @throws ClientException
* @throws DuplicateFoundException
* @throws NoSuchActorException
* @throws ServerException
* @throws ClientExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ServerExceptionInterface
* @throws TransportExceptionInterface
*/
public static function fromJson(string|AbstractObject $json, array $options = []): GSNote
{
$source = $options['source'];
$actor_uri = $options['actor_uri'];
$actor_id = $options['actor_id'];
$type_note = \is_string($json) ? self::jsonToType($json) : $json;
$source = $options['source'] ?? 'ActivityPub';
$type_note = is_string($json) ? self::jsonToType($json) : $json;
$actor = null;
$actor_id = null;
if ($json instanceof AbstractObject
&& array_key_exists('test_authority', $options)
&& $options['test_authority']
&& array_key_exists('actor_uri', $options)
) {
$actor_uri = $options['actor_uri'];
if ($actor_uri !== $type_note->get('attributedTo')) {
if (parse_url($actor_uri)['host'] !== parse_url($type_note->get('attributedTo'))['host']) {
throw new Exception('You don\'t seem to have enough authority to create this note.');
}
} else {
$actor = $options['actor'] ?? null;
$actor_id = $options['actor_id'] ?? $actor?->getId();
}
}
if (\is_null($actor_uri) || $actor_uri !== $type_note->get('attributedTo')) {
$actor_id = ActivityPub::getActorByUri($type_note->get('attributedTo'))->getId();
if (is_null($actor_id)) {
$actor = ActivityPub::getActorByUri($type_note->get('attributedTo'));
$actor_id = $actor->getId();
}
$map = [
'is_local' => false,
'created' => new DateTime($type_note->get('published') ?? 'now'),
'content' => $type_note->get('content') ?? null,
'is_local' => false,
'created' => new DateTime($type_note->get('published') ?? 'now'),
'content' => $type_note->get('content') ?? null,
'rendered' => null,
'content_type' => 'text/html',
'language_id' => $type_note->get('contentLang') ?? null,
'url' => $type_note->get('url') ?? $type_note->get('id'),
'actor_id' => $actor_id,
'modified' => new DateTime(),
'source' => $source,
'language_id' => $type_note->get('contentLang') ?? null,
'url' => $type_note->get('url') ?? $type_note->get('id'),
'actor_id' => $actor_id,
'modified' => new DateTime(),
'source' => $source,
];
if ($map['content'] !== null) {
$mentions = [];
@@ -100,7 +132,7 @@ class Note extends Model
$map['content'],
$map['content_type'],
&$map['rendered'],
Actor::getById($actor_id),
$actor,
$map['language_id'],
&$mentions,
]);
@@ -108,7 +140,7 @@ class Note extends Model
$obj = new GSNote();
if (!\is_null($map['language_id'])) {
if (!is_null($map['language_id'])) {
$map['language_id'] = Language::getByLocale($map['language_id'])->getId();
} else {
$map['language_id'] = null;
@@ -148,7 +180,6 @@ class Note extends Model
DB::persist($obj);
// Need file and note ids for the next step
$obj->setUrl(Router::url('note_view', ['id' => $obj->getId()], Router::ABSOLUTE_URL));
Event::handle('ProcessNoteContent', [$obj, $obj->getContent(), $obj->getContentType(), $process_note_content_extra_args = []]);
if ($processed_attachments !== []) {
@@ -160,7 +191,20 @@ class Note extends Model
}
}
Event::handle('ActivityPubNewNote', [&$obj]);
$map = [
'object_uri' => $type_note->get('id'),
'object_type' => 'note',
'object_id' => $obj->getId(),
'created' => new DateTime($type_note->get('published') ?? 'now'),
'modified' => new DateTime(),
];
$ap_obj = new ActivitypubObject();
foreach ($map as $prop => $val) {
$set = Formatting::snakeCaseToCamelCase("set_{$prop}");
$ap_obj->{$set}($val);
}
DB::persist($ap_obj);
return $obj;
}
@@ -179,15 +223,15 @@ class Note extends Model
}
$attr = [
'@context' => 'https://www.w3.org/ns/activitystreams',
'type' => 'Note',
'id' => Router::url('note_view', ['id' => $object->getId()], Router::ABSOLUTE_URL),
'published' => $object->getCreated()->format(DateTimeInterface::RFC3339),
'@context' => 'https://www.w3.org/ns/activitystreams',
'type' => 'Note',
'id' => Router::url('note_view', ['id' => $object->getId()], Router::ABSOLUTE_URL),
'published' => $object->getCreated()->format(DateTimeInterface::RFC3339),
'attributedTo' => $object->getActor()->getUri(Router::ABSOLUTE_URL),
'to' => ['https://www.w3.org/ns/activitystreams#Public'], // TODO: implement proper scope address
'cc' => ['https://www.w3.org/ns/activitystreams#Public'],
'content' => $object->getRendered(),
'attachment' => [],
'to' => ['https://www.w3.org/ns/activitystreams#Public'], // TODO: implement proper scope address
'cc' => ['https://www.w3.org/ns/activitystreams#Public'],
'content' => $object->getRendered(),
'attachment' => [],
];
// Attachments