Merge pull request #8727 from MrPetovan/task/8676-ap-attachments
[ActivityPub] Add support for more attachments structures
This commit is contained in:
commit
6665eb76f9
3 changed files with 98 additions and 59 deletions
|
@ -94,6 +94,19 @@ class Processor
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($activity['attachments'] as $attach) {
|
foreach ($activity['attachments'] as $attach) {
|
||||||
|
switch ($attach['type']) {
|
||||||
|
case 'link':
|
||||||
|
// Only one [attachment] tag is allowed
|
||||||
|
$existingAttachmentPos = strpos($item['body'], '[attachment');
|
||||||
|
if ($existingAttachmentPos !== false) {
|
||||||
|
$linkTitle = $attach['title'] ?: $attach['url'];
|
||||||
|
// Additional link attachments are prepended before the existing [attachment] tag
|
||||||
|
$item['body'] = substr_replace($item['body'], "\n[bookmark=" . $attach['url'] . ']' . $linkTitle . "[/bookmark]\n", $existingAttachmentPos, 0);
|
||||||
|
} else {
|
||||||
|
$item['body'] .= "\n[attachment type='link' url='" . $attach['url'] . "' title='" . ($attach['title'] ?? '') . "' image='" . ($attach['image'] ?? '') . "']" . ($attach['desc'] ?? '') . '[/attachment]';
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
$filetype = strtolower(substr($attach['mediaType'], 0, strpos($attach['mediaType'], '/')));
|
$filetype = strtolower(substr($attach['mediaType'], 0, strpos($attach['mediaType'], '/')));
|
||||||
if ($filetype == 'image') {
|
if ($filetype == 'image') {
|
||||||
if (!empty($activity['source']) && strpos($activity['source'], $attach['url'])) {
|
if (!empty($activity['source']) && strpos($activity['source'], $attach['url'])) {
|
||||||
|
@ -123,10 +136,9 @@ class Processor
|
||||||
} else {
|
} else {
|
||||||
$item["attach"] = '';
|
$item["attach"] = '';
|
||||||
}
|
}
|
||||||
if (!isset($attach['length'])) {
|
|
||||||
$attach['length'] = "0";
|
$item["attach"] .= '[attach]href="' . $attach['url'] . '" length="' . ($attach['length'] ?? '0') . '" type="' . $attach['mediaType'] . '" title="' . ($attach['name'] ?? '') . '"[/attach]';
|
||||||
}
|
}
|
||||||
$item["attach"] .= '[attach]href="'.$attach['url'].'" length="'.$attach['length'].'" type="'.$attach['mediaType'].'" title="'.($attach['name'] ?? '') .'"[/attach]';
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -821,14 +821,10 @@ class Receiver
|
||||||
*
|
*
|
||||||
* @return array with tags in a simplified format
|
* @return array with tags in a simplified format
|
||||||
*/
|
*/
|
||||||
private static function processTags($tags)
|
private static function processTags(array $tags)
|
||||||
{
|
{
|
||||||
$taglist = [];
|
$taglist = [];
|
||||||
|
|
||||||
if (empty($tags)) {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($tags as $tag) {
|
foreach ($tags as $tag) {
|
||||||
if (empty($tag)) {
|
if (empty($tag)) {
|
||||||
continue;
|
continue;
|
||||||
|
@ -854,17 +850,13 @@ class Receiver
|
||||||
/**
|
/**
|
||||||
* Convert emojis from JSON-LD format into a simplified format
|
* Convert emojis from JSON-LD format into a simplified format
|
||||||
*
|
*
|
||||||
* @param $emojis
|
* @param array $emojis
|
||||||
* @return array with emojis in a simplified format
|
* @return array with emojis in a simplified format
|
||||||
*/
|
*/
|
||||||
private static function processEmojis($emojis)
|
private static function processEmojis(array $emojis)
|
||||||
{
|
{
|
||||||
$emojilist = [];
|
$emojilist = [];
|
||||||
|
|
||||||
if (empty($emojis)) {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($emojis as $emoji) {
|
foreach ($emojis as $emoji) {
|
||||||
if (empty($emoji) || (JsonLD::fetchElement($emoji, '@type') != 'toot:Emoji') || empty($emoji['as:icon'])) {
|
if (empty($emoji) || (JsonLD::fetchElement($emoji, '@type') != 'toot:Emoji') || empty($emoji['as:icon'])) {
|
||||||
continue;
|
continue;
|
||||||
|
@ -876,6 +868,7 @@ class Receiver
|
||||||
|
|
||||||
$emojilist[] = $element;
|
$emojilist[] = $element;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $emojilist;
|
return $emojilist;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -886,24 +879,62 @@ class Receiver
|
||||||
*
|
*
|
||||||
* @return array with attachmants in a simplified format
|
* @return array with attachmants in a simplified format
|
||||||
*/
|
*/
|
||||||
private static function processAttachments($attachments)
|
private static function processAttachments(array $attachments)
|
||||||
{
|
{
|
||||||
$attachlist = [];
|
$attachlist = [];
|
||||||
|
|
||||||
if (empty($attachments)) {
|
// Removes empty values
|
||||||
return [];
|
$attachments = array_filter($attachments);
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($attachments as $attachment) {
|
foreach ($attachments as $attachment) {
|
||||||
if (empty($attachment)) {
|
switch (JsonLD::fetchElement($attachment, '@type')) {
|
||||||
|
case 'as:Page':
|
||||||
|
$pageUrl = null;
|
||||||
|
$pageImage = null;
|
||||||
|
|
||||||
|
$urls = JsonLD::fetchElementArray($attachment, 'as:url');
|
||||||
|
foreach ($urls as $url) {
|
||||||
|
// Single scalar URL case
|
||||||
|
if (is_string($url)) {
|
||||||
|
$pageUrl = $url;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$attachlist[] = ['type' => str_replace('as:', '', JsonLD::fetchElement($attachment, '@type')),
|
$href = JsonLD::fetchElement($url, 'as:href', '@id');
|
||||||
|
$mediaType = JsonLD::fetchElement($url, 'as:mediaType', '@value');
|
||||||
|
if (Strings::startsWith($mediaType, 'image')) {
|
||||||
|
$pageImage = $href;
|
||||||
|
} else {
|
||||||
|
$pageUrl = $href;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$attachlist[] = [
|
||||||
|
'type' => 'link',
|
||||||
|
'title' => JsonLD::fetchElement($attachment, 'as:name', '@value'),
|
||||||
|
'desc' => JsonLD::fetchElement($attachment, 'as:summary', '@value'),
|
||||||
|
'url' => $pageUrl,
|
||||||
|
'image' => $pageImage,
|
||||||
|
];
|
||||||
|
break;
|
||||||
|
case 'as:Link':
|
||||||
|
$attachlist[] = [
|
||||||
|
'type' => str_replace('as:', '', JsonLD::fetchElement($attachment, '@type')),
|
||||||
'mediaType' => JsonLD::fetchElement($attachment, 'as:mediaType', '@value'),
|
'mediaType' => JsonLD::fetchElement($attachment, 'as:mediaType', '@value'),
|
||||||
'name' => JsonLD::fetchElement($attachment, 'as:name', '@value'),
|
'name' => JsonLD::fetchElement($attachment, 'as:name', '@value'),
|
||||||
'url' => JsonLD::fetchElement($attachment, 'as:url', '@id')];
|
'url' => JsonLD::fetchElement($attachment, 'as:href', '@id')
|
||||||
|
];
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
$attachlist[] = [
|
||||||
|
'type' => str_replace('as:', '', JsonLD::fetchElement($attachment, '@type')),
|
||||||
|
'mediaType' => JsonLD::fetchElement($attachment, 'as:mediaType', '@value'),
|
||||||
|
'name' => JsonLD::fetchElement($attachment, 'as:name', '@value'),
|
||||||
|
'url' => JsonLD::fetchElement($attachment, 'as:url', '@id')
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return $attachlist;
|
return $attachlist;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1086,9 +1117,9 @@ class Receiver
|
||||||
$object_data['latitude'] = JsonLD::fetchElement($object_data, 'latitude', '@value');
|
$object_data['latitude'] = JsonLD::fetchElement($object_data, 'latitude', '@value');
|
||||||
$object_data['longitude'] = JsonLD::fetchElement($object, 'as:location', 'as:longitude', '@type', 'as:Place');
|
$object_data['longitude'] = JsonLD::fetchElement($object, 'as:location', 'as:longitude', '@type', 'as:Place');
|
||||||
$object_data['longitude'] = JsonLD::fetchElement($object_data, 'longitude', '@value');
|
$object_data['longitude'] = JsonLD::fetchElement($object_data, 'longitude', '@value');
|
||||||
$object_data['attachments'] = self::processAttachments(JsonLD::fetchElementArray($object, 'as:attachment'));
|
$object_data['attachments'] = self::processAttachments(JsonLD::fetchElementArray($object, 'as:attachment') ?? []);
|
||||||
$object_data['tags'] = self::processTags(JsonLD::fetchElementArray($object, 'as:tag'));
|
$object_data['tags'] = self::processTags(JsonLD::fetchElementArray($object, 'as:tag') ?? []);
|
||||||
$object_data['emojis'] = self::processEmojis(JsonLD::fetchElementArray($object, 'as:tag', 'toot:Emoji'));
|
$object_data['emojis'] = self::processEmojis(JsonLD::fetchElementArray($object, 'as:tag', 'toot:Emoji') ?? []);
|
||||||
$object_data['generator'] = JsonLD::fetchElement($object, 'as:generator', 'as:name', '@type', 'as:Application');
|
$object_data['generator'] = JsonLD::fetchElement($object, 'as:generator', 'as:name', '@type', 'as:Application');
|
||||||
$object_data['generator'] = JsonLD::fetchElement($object_data, 'generator', '@value');
|
$object_data['generator'] = JsonLD::fetchElement($object_data, 'generator', '@value');
|
||||||
$object_data['alternate-url'] = JsonLD::fetchElement($object, 'as:url', '@id');
|
$object_data['alternate-url'] = JsonLD::fetchElement($object, 'as:url', '@id');
|
||||||
|
|
|
@ -175,10 +175,6 @@ class JsonLD
|
||||||
*/
|
*/
|
||||||
public static function fetchElementArray($array, $element, $key = null)
|
public static function fetchElementArray($array, $element, $key = null)
|
||||||
{
|
{
|
||||||
if (empty($array)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!isset($array[$element])) {
|
if (!isset($array[$element])) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue