[ActivityPub][Inbox] Restore Create Note Functionality

Minor bug fixes
This commit is contained in:
2021-11-27 15:06:46 +00:00
parent 7145dba8af
commit 56526c9ba6
15 changed files with 138 additions and 148 deletions

View File

@@ -6,6 +6,7 @@ namespace Plugin\ActivityPub\Util\Model\AS2ToEntity;
use App\Core\DB\DB;
use App\Core\Event;
use App\Entity\Activity;
use App\Entity\Actor;
use App\Entity\Note;
use App\Util\Exception\ClientException;
@@ -35,58 +36,47 @@ abstract class AS2ToEntity
/**
* @throws ClientException
*/
public static function store(array $activity, ?string $source = null): array
public static function store(array $activity, ?string $source = null): ActivitypubActivity
{
$act = ActivitypubActivity::getWithPK(['activity_uri' => $activity['id']]);
if (\is_null($act)) {
$ap_act = ActivitypubActivity::getWithPK(['activity_uri' => $activity['id']]);
if (\is_null($ap_act)) {
$actor = ActivityPub::getActorByUri($activity['actor']);
$map = [
'activity_uri' => $activity['id'],
// Store Object
$obj = null;
switch ($activity['object']['type']) {
case 'Note':
$obj = AS2ToNote::translate($activity['object'], $source, $activity['actor'], $actor->getId());
break;
default:
if (!Event::handle('ActivityPubObject', [$activity['object']['type'], $activity['object'], &$obj])) {
throw new ClientException('Unsupported Object type.');
}
break;
}
DB::persist($obj);
// Store Activity
$act = Activity::create([
'actor_id' => $actor->getId(),
'verb' => self::activity_stream_two_verb_to_gs_verb($activity['type']),
'object_type' => self::activity_stream_two_object_type_to_gs_table($activity['object']['type']),
'object_id' => $obj->getId(),
'is_local' => false,
'created' => new DateTime($activity['published'] ?? 'now'),
'source' => $source,
]);
DB::persist($act);
// Store ActivityPub Activity
$ap_act = ActivitypubActivity::create([
'activity_id' => $act->getId(),
'activity_uri' => $activity['id'],
'object_uri' => $activity['object']['id'],
'is_local' => false,
'created' => new DateTime($activity['published'] ?? 'now'),
'modified' => new DateTime(),
'source' => $source,
];
$act = new ActivitypubActivity();
foreach ($map as $prop => $val) {
$set = Formatting::snakeCaseToCamelCase("set_{$prop}");
$act->{$set}($val);
}
$obj = null;
switch ($activity['object']['type']) {
case 'Note':
$obj = AS2ToNote::translate($activity['object'], $source, $activity['actor'], $act);
break;
default:
if (!Event::handle('ActivityPubObject', [$activity['object']['type'], $activity['object'], &$obj])) {
throw new ClientException('Unsupported Object type.');
}
break;
}
DB::persist($obj);
$act->setObjectId($obj->getId());
DB::persist($act);
} else {
$actor = Actor::getById($act->getActorId());
switch ($activity['object']['type']) {
case 'Note':
$obj = Note::getWithPK(['id' => $act->getObjectId()]);
break;
default:
if (!Event::handle('ActivityPubObject', [$activity['object']['type'], $activity['object'], &$obj])) {
throw new ClientException('Unsupported Object type.');
}
break;
}
]);
DB::persist($ap_act);
}
return [$actor, $act, $obj];
return $ap_act;
}
}

View File

@@ -6,6 +6,7 @@ namespace Plugin\ActivityPub\Util\Model\AS2ToEntity;
use App\Core\Event;
use App\Entity\Actor;
use App\Entity\Language;
use App\Entity\Note;
use App\Util\Formatting;
use DateTime;
@@ -18,11 +19,9 @@ abstract class AS2ToNote
/**
*@throws Exception
*/
public static function translate(array $object, ?string $source, ?string $actor_uri, ?ActivitypubActivity $act = null): Note
public static function translate(array $object, ?string $source, ?string $actor_uri = null, ?int $actor_id = null): Note
{
if (isset($actor_uri) && $actor_uri === $object['attributedTo']) {
$actor_id = $act->getActorId();
} else {
if (is_null($actor_uri) || $actor_uri !== $object['attributedTo']) {
$actor_id = ActivityPub::getActorByUri($object['attributedTo'])->getId();
}
$map = [
@@ -30,22 +29,32 @@ abstract class AS2ToNote
'created' => new DateTime($object['published'] ?? 'now'),
'content' => $object['content'] ?? null,
'content_type' => 'text/html',
'language_id' => $object['contentLang'] ?? null,
'url' => \array_key_exists('url', $object) ? $object['url'] : $object['id'],
'actor_id' => $actor_id,
'modified' => new DateTime(),
'source' => $source,
];
if ($map['content'] !== null) {
$mentions = [];
Event::handle('RenderNoteContent', [
$map['content'],
$map['content_type'],
&$map['rendered'],
Actor::getById($actor_id),
null, // TODO reply to
$map['language_id'],
&$mentions,
]);
}
$obj = new Note();
if (!is_null($map['language_id'])) {
$map['language_id'] = Language::getFromLocale($map['language_id'])->getId();
} else {
$map['language_id'] = null;
}
foreach ($map as $prop => $val) {
$set = Formatting::snakeCaseToCamelCase("set_{$prop}");
$obj->{$set}($val);