mirror of
https://github.com/friendica/friendica
synced 2025-04-27 08:30:10 +00:00
[ActivityPub] Add support for more attachments structures
- Add support for type: Page into [attachment], type: Link using href attribute - Ensure Receiver::process* parameter is an array - Remove superfluous eptiness check in JsonLD::fetchElementArray
This commit is contained in:
parent
59bd6fd908
commit
01e9beffc2
3 changed files with 90 additions and 59 deletions
|
@ -821,14 +821,10 @@ class Receiver
|
|||
*
|
||||
* @return array with tags in a simplified format
|
||||
*/
|
||||
private static function processTags($tags)
|
||||
private static function processTags(array $tags)
|
||||
{
|
||||
$taglist = [];
|
||||
|
||||
if (empty($tags)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
foreach ($tags as $tag) {
|
||||
if (empty($tag)) {
|
||||
continue;
|
||||
|
@ -854,17 +850,13 @@ class Receiver
|
|||
/**
|
||||
* Convert emojis from JSON-LD format into a simplified format
|
||||
*
|
||||
* @param $emojis
|
||||
* @param array $emojis
|
||||
* @return array with emojis in a simplified format
|
||||
*/
|
||||
private static function processEmojis($emojis)
|
||||
private static function processEmojis(array $emojis)
|
||||
{
|
||||
$emojilist = [];
|
||||
|
||||
if (empty($emojis)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
foreach ($emojis as $emoji) {
|
||||
if (empty($emoji) || (JsonLD::fetchElement($emoji, '@type') != 'toot:Emoji') || empty($emoji['as:icon'])) {
|
||||
continue;
|
||||
|
@ -876,6 +868,7 @@ class Receiver
|
|||
|
||||
$emojilist[] = $element;
|
||||
}
|
||||
|
||||
return $emojilist;
|
||||
}
|
||||
|
||||
|
@ -886,24 +879,62 @@ class Receiver
|
|||
*
|
||||
* @return array with attachmants in a simplified format
|
||||
*/
|
||||
private static function processAttachments($attachments)
|
||||
private static function processAttachments(array $attachments)
|
||||
{
|
||||
$attachlist = [];
|
||||
|
||||
if (empty($attachments)) {
|
||||
return [];
|
||||
}
|
||||
// Removes empty values
|
||||
$attachments = array_filter($attachments);
|
||||
|
||||
foreach ($attachments as $attachment) {
|
||||
if (empty($attachment)) {
|
||||
continue;
|
||||
}
|
||||
switch (JsonLD::fetchElement($attachment, '@type')) {
|
||||
case 'as:Page':
|
||||
$pageUrl = null;
|
||||
$pageImage = null;
|
||||
|
||||
$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')];
|
||||
$urls = JsonLD::fetchElementArray($attachment, 'as:url');
|
||||
foreach ($urls as $url) {
|
||||
// Single scalar URL case
|
||||
if (is_string($url)) {
|
||||
$pageUrl = $url;
|
||||
continue;
|
||||
}
|
||||
|
||||
$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'),
|
||||
'name' => JsonLD::fetchElement($attachment, 'as:name', '@value'),
|
||||
'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;
|
||||
}
|
||||
|
||||
|
@ -1086,9 +1117,9 @@ class Receiver
|
|||
$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_data, 'longitude', '@value');
|
||||
$object_data['attachments'] = self::processAttachments(JsonLD::fetchElementArray($object, 'as:attachment'));
|
||||
$object_data['tags'] = self::processTags(JsonLD::fetchElementArray($object, 'as:tag'));
|
||||
$object_data['emojis'] = self::processEmojis(JsonLD::fetchElementArray($object, 'as:tag', 'toot:Emoji'));
|
||||
$object_data['attachments'] = self::processAttachments(JsonLD::fetchElementArray($object, 'as:attachment') ?? []);
|
||||
$object_data['tags'] = self::processTags(JsonLD::fetchElementArray($object, 'as:tag') ?? []);
|
||||
$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_data, 'generator', '@value');
|
||||
$object_data['alternate-url'] = JsonLD::fetchElement($object, 'as:url', '@id');
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue