[PLUGIN][ActivityPub] Simplify DB usage
This commit is contained in:
@@ -34,6 +34,7 @@ namespace Plugin\ActivityPub\Util\Model;
|
||||
|
||||
use ActivityPhp\Type;
|
||||
use ActivityPhp\Type\AbstractObject;
|
||||
use App\Core\DB\DB;
|
||||
use App\Core\Event;
|
||||
use App\Core\Router\Router;
|
||||
use App\Entity\Activity as GSActivity;
|
||||
@@ -79,7 +80,7 @@ class Activity extends Model
|
||||
|
||||
// Ditch known activities
|
||||
if ($type_activity->has('id')) { // We can't dereference a transient activity
|
||||
$ap_act = ActivitypubActivity::getByPK(['activity_uri' => $type_activity->get('id')]);
|
||||
$ap_act = DB::findOneBy(ActivitypubActivity::class, ['activity_uri' => $type_activity->get('id')], return_null: true);
|
||||
if (!\is_null($ap_act)) {
|
||||
return $ap_act;
|
||||
}
|
||||
|
@@ -104,49 +104,46 @@ class Actor extends Model
|
||||
}
|
||||
|
||||
// Actor
|
||||
$actor_map = [
|
||||
'nickname' => $object->get('preferredUsername'),
|
||||
'fullname' => !empty($object->get('name')) ? $object->get('name') : null,
|
||||
'created' => new DateTime($object->get('published') ?? 'now'),
|
||||
'bio' => $object->get('summary'),
|
||||
'is_local' => false, // duh!
|
||||
'type' => self::$_as2_actor_type_to_gs_actor_type[$object->get('type')],
|
||||
'roles' => $roles,
|
||||
'modified' => new DateTime(),
|
||||
];
|
||||
|
||||
$actor = $options['objects']['Actor'] ?? new GSActor();
|
||||
|
||||
foreach ($actor_map as $prop => $val) {
|
||||
$set = Formatting::snakeCaseToCamelCase("set_{$prop}");
|
||||
$actor->{$set}($val);
|
||||
}
|
||||
|
||||
if (!isset($options['objects']['Actor'])) {
|
||||
DB::wrapInTransaction(fn () => DB::persist($actor));
|
||||
if (isset($options['objects']['Actor'])) {
|
||||
$actor = $options['objects']['Actor'];
|
||||
} else {
|
||||
$actor_map = [
|
||||
'nickname' => $object->get('preferredUsername'),
|
||||
'fullname' => !empty($object->get('name')) ? $object->get('name') : null,
|
||||
'created' => new DateTime($object->get('published') ?? 'now'),
|
||||
'bio' => $object->get('summary'),
|
||||
'is_local' => false, // duh!
|
||||
'type' => self::$_as2_actor_type_to_gs_actor_type[$object->get('type')],
|
||||
'roles' => $roles,
|
||||
'modified' => new DateTime(),
|
||||
];
|
||||
$actor = GSActor::create($actor_map);
|
||||
DB::persist($actor);
|
||||
}
|
||||
|
||||
// ActivityPub Actor
|
||||
$ap_actor = ActivitypubActor::create([
|
||||
'inbox_uri' => $object->get('inbox'),
|
||||
'inbox_shared_uri' => ($object->has('endpoints') && isset($object->get('endpoints')['sharedInbox'])) ? $object->get('endpoints')['sharedInbox'] : null,
|
||||
'uri' => $object->get('id'),
|
||||
'actor_id' => $actor->getId(),
|
||||
'url' => $object->get('url') ?? null,
|
||||
], $options['objects']['ActivitypubActor'] ?? null);
|
||||
|
||||
if (!isset($options['objects']['ActivitypubActor'])) {
|
||||
DB::wrapInTransaction(fn () => DB::persist($ap_actor));
|
||||
if (isset($options['objects']['ActivitypubActor'])) {
|
||||
$ap_actor = $options['objects']['ActivitypubActor'];
|
||||
} else {
|
||||
$ap_actor = ActivitypubActor::create([
|
||||
'inbox_uri' => $object->get('inbox'),
|
||||
'inbox_shared_uri' => ($object->has('endpoints') && isset($object->get('endpoints')['sharedInbox'])) ? $object->get('endpoints')['sharedInbox'] : null,
|
||||
'uri' => $object->get('id'),
|
||||
'actor_id' => $actor->getId(),
|
||||
'url' => $object->get('url') ?? null,
|
||||
], $options['objects']['ActivitypubActor'] ?? null);
|
||||
DB::persist($ap_actor);
|
||||
}
|
||||
|
||||
// Public Key
|
||||
$apRSA = ActivitypubRsa::create([
|
||||
'actor_id' => $actor->getID(),
|
||||
'public_key' => ($object->has('publicKey') && isset($object->get('publicKey')['publicKeyPem'])) ? $object->get('publicKey')['publicKeyPem'] : null,
|
||||
], $options['objects']['ActivitypubRsa'] ?? null);
|
||||
|
||||
if (!isset($options['objects']['ActivitypubRsa'])) {
|
||||
DB::wrapInTransaction(fn () => DB::persist($apRSA));
|
||||
if (isset($options['objects']['ActivitypubRsa'])) {
|
||||
$apRSA = $options['objects']['ActivitypubRsa'];
|
||||
} else {
|
||||
$apRSA = ActivitypubRsa::create([
|
||||
'actor_id' => $actor->getID(),
|
||||
'public_key' => ($object->has('publicKey') && isset($object->get('publicKey')['publicKeyPem'])) ? $object->get('publicKey')['publicKeyPem'] : null,
|
||||
], $options['objects']['ActivitypubRsa'] ?? null);
|
||||
DB::persist($apRSA);
|
||||
}
|
||||
|
||||
// Avatar
|
||||
@@ -170,14 +167,14 @@ class Actor extends Model
|
||||
if (!\is_null($avatar = DB::findOneBy(\Component\Avatar\Entity\Avatar::class, ['actor_id' => $actor->getId()], return_null: true))) {
|
||||
$avatar->delete();
|
||||
}
|
||||
DB::wrapInTransaction(function () use ($attachment, $actor, $object) {
|
||||
|
||||
DB::persist($attachment);
|
||||
DB::persist(\Component\Avatar\Entity\Avatar::create([
|
||||
'actor_id' => $actor->getId(),
|
||||
'attachment_id' => $attachment->getId(),
|
||||
'title' => $object->get('icon')->get('name') ?? null,
|
||||
]));
|
||||
});
|
||||
|
||||
Event::handle('AvatarUpdate', [$actor->getId()]);
|
||||
}
|
||||
}
|
||||
@@ -216,7 +213,7 @@ class Actor extends Model
|
||||
$uri = $object->getUri(Router::ABSOLUTE_URL);
|
||||
$attr = [
|
||||
'@context' => 'https://www.w3.org/ns/activitystreams',
|
||||
'type' => ($object->getType() === GSActor::GROUP) ? (LocalGroup::getByPK(['actor_id' => $object->getId()])->getType() === 'organisation' ? 'Organization' : 'Group'): self::$_gs_actor_type_to_as2_actor_type[$object->getType()],
|
||||
'type' => ($object->getType() === GSActor::GROUP) ? (DB::findOneBy(LocalGroup::class, ['actor_id' => $object->getId()], return_null: true)?->getType() === 'organisation' ? 'Organization' : 'Group'): self::$_gs_actor_type_to_as2_actor_type[$object->getType()],
|
||||
'id' => $uri,
|
||||
'inbox' => Router::url('activitypub_actor_inbox', ['gsactor_id' => $object->getId()], Router::ABSOLUTE_URL),
|
||||
'outbox' => Router::url('activitypub_actor_outbox', ['gsactor_id' => $object->getId()], Router::ABSOLUTE_URL),
|
||||
|
@@ -188,7 +188,7 @@ class Note extends Model
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
$actor = ActivityPub::getActorByUri($target);
|
||||
$actor = ActivityPub::getActorByUri($target);
|
||||
$object_mentions_ids[$actor->getId()] = $target;
|
||||
// If $to is a group and note is unlisted, set note's scope as Group
|
||||
if ($actor->isGroup() && $map['scope'] === 'unlisted') {
|
||||
@@ -209,18 +209,14 @@ class Note extends Model
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
$actor = ActivityPub::getActorByUri($target);
|
||||
$actor = ActivityPub::getActorByUri($target);
|
||||
$object_mentions_ids[$actor->getId()] = $target;
|
||||
} catch (Exception $e) {
|
||||
Log::debug('ActivityPub->Model->Note->fromJson->getActorByUri', [$e]);
|
||||
}
|
||||
}
|
||||
|
||||
$obj = new GSNote();
|
||||
foreach ($map as $prop => $val) {
|
||||
$set = Formatting::snakeCaseToCamelCase("set_{$prop}");
|
||||
$obj->{$set}($val);
|
||||
}
|
||||
$obj = GSNote::create($map);
|
||||
|
||||
// Attachments
|
||||
$processed_attachments = [];
|
||||
@@ -235,7 +231,7 @@ class Note extends Model
|
||||
// Create an attachment for this
|
||||
$temp_file = new TemporaryFile();
|
||||
$temp_file->write($media);
|
||||
$filesize = $temp_file->getSize();
|
||||
$filesize = $temp_file->getSize();
|
||||
$max_file_size = Common::getUploadLimit();
|
||||
if ($max_file_size < $filesize) {
|
||||
throw new ClientException(_m('No file may be larger than {quota} bytes and the file you sent was {size} bytes. '
|
||||
@@ -258,7 +254,7 @@ class Note extends Model
|
||||
case 'Mention':
|
||||
case 'Group':
|
||||
try {
|
||||
$actor = ActivityPub::getActorByUri($ap_tag->get('href'));
|
||||
$actor = ActivityPub::getActorByUri($ap_tag->get('href'));
|
||||
$object_mentions_ids[$actor->getId()] = $ap_tag->get('href');
|
||||
} catch (Exception $e) {
|
||||
Log::debug('ActivityPub->Model->Note->fromJson->getActorByUri', [$e]);
|
||||
@@ -276,8 +272,8 @@ class Note extends Model
|
||||
}
|
||||
break;
|
||||
case 'Hashtag':
|
||||
$match = ltrim($ap_tag->get('name'), '#');
|
||||
$tag = Tag::extract($match);
|
||||
$match = ltrim($ap_tag->get('name'), '#');
|
||||
$tag = Tag::extract($match);
|
||||
$canonical_tag = $ap_tag->get('canonical') ?? Tag::canonicalTag($tag, \is_null($lang_id = $obj->getLanguageId()) ? null : Language::getById($lang_id)->getLocale());
|
||||
DB::persist(NoteTag::create([
|
||||
'tag' => $tag,
|
||||
@@ -418,7 +414,7 @@ class Note extends Model
|
||||
'type' => 'Document',
|
||||
'mediaType' => $attachment->getMimetype(),
|
||||
'url' => $attachment->getUrl(note: $object, type: Router::ABSOLUTE_URL),
|
||||
'name' => AttachmentToNote::getByPK(['attachment_id' => $attachment->getId(), 'note_id' => $object->getId()])->getTitle(),
|
||||
'name' => DB::findOneBy(AttachmentToNote::class, ['attachment_id' => $attachment->getId(), 'note_id' => $object->getId()], return_null: true)?->getTitle(),
|
||||
'width' => $attachment->getWidth(),
|
||||
'height' => $attachment->getHeight(),
|
||||
];
|
||||
|
Reference in New Issue
Block a user