Skip to content
Snippets Groups Projects
Commit ca4509e0 authored by Kristof De Jaeger's avatar Kristof De Jaeger
Browse files

Issue #3175472 by swentel: Support for other media types (audio / video)

parent 9aaad695
No related branches found
No related tags found
No related merge requests found
......@@ -60,11 +60,12 @@ when enabling this module, which are the 'Accept', 'Follow', 'Delete', 'Undo'
and 'Inbox reply' types. A "Note" example is also installed with basic
configuration but is not enabled yet since this depends on your setup.
6. Create your own configuration
- Select 'Type plugin', only dynamic types is currently available
- Select 'Type plugin'. Public message is the plugin that allows mapping
content fields with AP properties.
- Select the content type which you want to map
- Select the activity (See footnotes underneath)
- Select the object (use Note if you do not want to populate a title)
- Map properties to the content type fields
- Map properties to the content type fields.
7. When saved, go to the content type you selected. You can now select the
entity which will process this node in the 'ActivityPub outbox' fieldset.
......
......@@ -77,7 +77,11 @@ class DynamicTypes extends TypePluginBase {
'summary' => 'Summary',
'object' => 'Like/Announce/Follow target',
'inReplyTo' => 'Reply link',
'attachment' => 'Image attachment',
// 'attachment' was originally reserved for 'images' only, but attachments
// can also contain other media types.
'attachment' => ['label' => 'Image attachment', 'property' => 'attachment'],
'attachment_2' => ['label' => 'Video attachment', 'property' => 'attachment'],
'attachment_3' => ['label' => 'Audio attachment', 'property' => 'attachment'],
];
}
......@@ -106,7 +110,8 @@ class DynamicTypes extends TypePluginBase {
];
$properties = $this->getProperties($this->getConfiguration()['object']);
foreach ($properties as $property => $label) {
foreach ($properties as $property => $info) {
$label = is_array($info) ? $info['label'] : $info;
$form['field_mapping'][] = [
'property' => [
'#type' => 'value',
......@@ -138,8 +143,20 @@ class DynamicTypes extends TypePluginBase {
if (!empty($mapping['field_name'])) {
if ($entity->hasField($mapping['field_name']) && ($value = $entity->get($mapping['field_name'])->getValue())) {
$field_type = $entity->get($mapping['field_name'])->getFieldDefinition()->getType();
if ($v = $this->getValue($mapping['property'], $value, $field_type)) {
$object[$mapping['property']] = $v;
$property = $mapping['property'];
// 'attachment' was originally reserved for 'images' only, but
// attachments can also contain other media types like video and
// audio.
if (str_contains($property, 'attachment_')) {
$property = 'attachment';
}
if ($v = $this->getValue($property, $value, $field_type)) {
if (!isset($object[$property])) {
$object[$property] = $v;
}
else {
$object[$property] = array_merge($v, $object[$property]);
}
}
}
}
......
......@@ -251,10 +251,11 @@ abstract class TypePluginBase extends PluginBase implements TypePluginInterface,
/** @var \Drupal\file\FileInterface $file */
$file = $this->entityTypeManager->getStorage('file')->load($v['target_id']);
}
$type = $this->getMediaType($file);
$return[] = (object) [
'type' => 'Image',
'type' => $type,
'mediaType' => $file->getMimeType(),
'url' => $this->getFileUrl($file),
'url' => $this->getFileUrl($file, $type),
];
}
break;
......@@ -263,20 +264,54 @@ abstract class TypePluginBase extends PluginBase implements TypePluginInterface,
return $return;
}
/**
* Get the media type.
*
* @param \Drupal\file\FileInterface $file
*
* @return string
*/
protected function getMediaType(FileInterface $file) {
$type = 'Document';
switch ($file->getMimeType()) {
case 'image/jpg':
case 'image/jpeg':
case 'image/webp':
case 'image/png':
case 'image/gif':
$type = 'Image';
break;
case 'video/mpeg':
case 'video/mp4':
case 'video/quicktime':
case 'video/ogg':
$type = 'Video';
break;
case 'audio/ogg':
case 'audio/mp3':
$type = 'Audio';
break;
}
return $type;
}
/**
* Gets the file URL.
*
* @param \Drupal\file\FileInterface $file
* @param string $type
*
* @return string
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
protected function getFileUrl(FileInterface $file) {
protected function getFileUrl(FileInterface $file, string $type) {
$config = $this->configFactory->get('activitypub.settings');
$attachment_content_style = $config->get('attachment_content_style');
if (!empty($attachment_content_style)) {
if (!empty($attachment_content_style) && $type == 'Image') {
/** @var \Drupal\image\ImageStyleInterface $image_style */
$storage = $this->entityTypeManager->getStorage('image_style');
$image_style = $storage->load($attachment_content_style);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment