- Issue created by @aleix
So after reviewing what can be done with outbox service, and how can solve json:api issue #3300973 , something like this can be done for c2s posting implementation (very raw, and it needs DI and to do some activity type detection/negotiation:
public function outbox(Request $request, UserInterface $user, ActivitypubActorInterface $activitypub_actor) {
$status = 400;
$data = NULL;
$metadata = new CacheableMetadata();
if ($request->getMethod() == 'POST' /*&& !empty($actor)*/) {
// Add a way to get the note type related content type.
$this->entityTypeManager()->getStorage('activitypub_type')->loadMultiple()['note'];
if->getPlugin()['configuration'];
['target_entity_type_id']
['target_bundle']*/
$note_type = $this->entityTypeManager()->getStorage('activitypub_type')->loadMultiple()['note'];
if ($note_type) {
$payload = json_decode((string)$request->getContent(), TRUE);
$type_configuration = $note_type->getPlugin()['configuration'];
$values = ['type' => $type_configuration['target_bundle']];
foreach($type_configuration['field_mapping'] as $mapping) {
if ($mapping['field_name'] && $payload['object'][$mapping['property']]) {
$values[$mapping['field_name']] = $payload['object'][$mapping['property']];
}
}
$created_entity = $this->entityTypeManager()->getStorage($type_configuration['target_entity_type_id'])->create($values);
$created_entity->save();
$outbox = \Drupal::service('activitypub.outbox');
if (in_array(ActivityPubActivityInterface::PUBLIC_URL, $payload['to'])){
$visibility = ActivityPubActivityInterface::VISIBILITY_PUBLIC;
} elseif (in_array(ActivityPubActivityInterface::FOLLOWERS, $payload['to'])) {
$visibility = ActivityPubActivityInterface::VISIBILITY_FOLLOWERS;
} elseif (in_array(ActivityPubActivityInterface::VISIBILITY_UNLISTED, $payload['to'])) {
$visibility = ActivityPubActivityInterface::VISIBILITY_UNLISTED;
} else {
$visibility = ActivityPubActivityInterface::VISIBILITY_PRIVATE;
}
$outbox->createActivity($type_configuration['activity'], $created_entity, $activitypub_actor, $visibility, $payload['to'] ?? []);
}
}
try {
Active
1.0
Code