[COMPONENT][Notification] Make logic more generic and robust

Fixed various bugs

Some important concepts to bear in mind:

* Notification: Associated with activities, won't be reconstructed
together with objects, can be thought of as transient

* Attention: Associated with objects, will be reconstructed with them, can
be thought as persistent

* Notifications and Attentions have no direct implications.

* Mentions are a specific form of attentions in notes, leads to the creation of Attentions.

Finally,

Potential PHP issue detected and reported: https://github.com/php/php-src/issues/8199
`static::method()` from a non static context (such as a class method) calls `__call`, rather than
the expected `__callStatic`. Can be fixed by using `(static fn() => static::method())()`, but the
usage of the magic method is strictly unnecessary in this case.
This commit is contained in:
2022-03-13 18:23:19 +00:00
parent e1cceac150
commit 888c3798b7
32 changed files with 438 additions and 494 deletions

View File

@@ -44,6 +44,7 @@ use App\Util\Exception\NotFoundException;
use App\Util\Exception\NotImplementedException;
use DateTimeInterface;
use InvalidArgumentException;
use const JSON_UNESCAPED_SLASHES;
use Plugin\ActivityPub\ActivityPub;
use Plugin\ActivityPub\Entity\ActivitypubActivity;
use Plugin\ActivityPub\Util\Explorer;
@@ -145,7 +146,7 @@ class Activity extends Model
*
* @throws ClientException
*/
public static function toJson(mixed $object, int $options = \JSON_UNESCAPED_SLASHES): string
public static function toJson(mixed $object, int $options = JSON_UNESCAPED_SLASHES): string
{
if ($object::class !== GSActivity::class) {
throw new InvalidArgumentException('First argument type must be an Activity.');
@@ -169,10 +170,16 @@ class Activity extends Model
'actor' => $object->getActor()->getUri(Router::ABSOLUTE_URL),
];
$attr['to'] = [];
$attr['cc'] = ['https://www.w3.org/ns/activitystreams#Public'];
foreach ($object->getAttentionTargets() as $target) {
$attr['cc'][] = $target->getUri();
}
// Get object or Tombstone
try {
$object = $object->getObject(); // Throws NotFoundException
$attr['object'] = ($attr['type'] === 'Create') ? self::jsonToType(Model::toJson($object)) : ActivityPub::getUriByObject($object);
$child = $object->getObject(); // Throws NotFoundException
$attr['object'] = ($attr['type'] === 'Create') ? self::jsonToType(Model::toJson($child)) : ActivityPub::getUriByObject($child);
} catch (NotFoundException) {
// It seems this object was deleted, refer to it as a Tombstone
$uri = match ($object->getObjectType()) {
@@ -180,18 +187,7 @@ class Activity extends Model
'actor' => Router::url('actor_view_id', ['id' => $object->getObjectId()], type: Router::ABSOLUTE_URL),
default => throw new NotImplementedException(),
};
$attr['object'] = Type::create('Tombstone', [
'id' => $uri,
]);
}
// If embedded non tombstone Object
if (!\is_string($attr['object']) && $attr['object']->get('type') !== 'Tombstone') {
// Little special case
if ($attr['type'] === 'Create' && ($attr['object']->get('type') === 'Note' || $attr['object']->get('type') === 'Page')) {
$attr['to'] = $attr['object']->get('to') ?? [];
$attr['cc'] = $attr['object']->get('cc') ?? [];
}
$attr['object'] = Type::create('Tombstone', ['id' => $uri]);
}
if (!\is_string($attr['object'])) {