Merge pull request #14668 from annando/mime

Use the fetched mimetype if present / improved detection of quoted posts
This commit is contained in:
Hypolite Petovan 2025-01-05 10:34:24 -05:00 committed by GitHub
commit 7a7a74d93e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 267 additions and 214 deletions

View file

@ -90,6 +90,7 @@ class Media
} }
$media['url'] = Network::sanitizeUrl($media['url']); $media['url'] = Network::sanitizeUrl($media['url']);
$media = self::unsetEmptyFields($media); $media = self::unsetEmptyFields($media);
$media = DI::dbaDefinition()->truncateFieldsForTable('post-media', $media); $media = DI::dbaDefinition()->truncateFieldsForTable('post-media', $media);
@ -157,8 +158,11 @@ class Media
public static function getAttachElement(string $href, int $length, string $type, string $title = ''): string public static function getAttachElement(string $href, int $length, string $type, string $title = ''): string
{ {
$media = self::fetchAdditionalData([ $media = self::fetchAdditionalData([
'type' => self::DOCUMENT, 'url' => $href, 'type' => self::DOCUMENT,
'size' => $length, 'mimetype' => $type, 'description' => $title 'url' => $href,
'size' => $length,
'mimetype' => $type,
'description' => $title
]); ]);
return '[attach]href="' . $media['url'] . '" length="' . $media['size'] . return '[attach]href="' . $media['url'] . '" length="' . $media['size'] .
@ -184,7 +188,7 @@ class Media
} }
// Fetch the mimetype or size if missing. // Fetch the mimetype or size if missing.
if (Network::isValidHttpUrl($media['url']) && empty($media['mimetype']) && !in_array($media['type'], [self::IMAGE, self::HLS])) { if (Network::isValidHttpUrl($media['url']) && (empty($media['mimetype']) || $media['type'] == self::HTML) && !in_array($media['type'], [self::IMAGE, self::HLS])) {
$timeout = DI::config()->get('system', 'xrd_timeout'); $timeout = DI::config()->get('system', 'xrd_timeout');
try { try {
$curlResult = DI::httpClient()->head($media['url'], [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::AS_DEFAULT, HttpClientOptions::TIMEOUT => $timeout, HttpClientOptions::REQUEST => HttpClientRequest::CONTENTTYPE]); $curlResult = DI::httpClient()->head($media['url'], [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::AS_DEFAULT, HttpClientOptions::TIMEOUT => $timeout, HttpClientOptions::REQUEST => HttpClientRequest::CONTENTTYPE]);
@ -194,8 +198,8 @@ class Media
$curlResult = DI::httpClient()->get($media['url'], HttpClientAccept::AS_DEFAULT, [HttpClientOptions::TIMEOUT => $timeout]); $curlResult = DI::httpClient()->get($media['url'], HttpClientAccept::AS_DEFAULT, [HttpClientOptions::TIMEOUT => $timeout]);
} }
if ($curlResult->isSuccess()) { if ($curlResult->isSuccess()) {
if (empty($media['mimetype'])) { if (!empty($curlResult->getContentType())) {
$media['mimetype'] = $curlResult->getContentType() ?? ''; $media['mimetype'] = $curlResult->getContentType();
} }
if (empty($media['size'])) { if (empty($media['size'])) {
$media['size'] = (int)($curlResult->getHeader('Content-Length')[0] ?? strlen($curlResult->getBodyString() ?? '')); $media['size'] = (int)($curlResult->getHeader('Content-Length')[0] ?? strlen($curlResult->getBodyString() ?? ''));
@ -218,9 +222,9 @@ class Media
$imagedata = Images::getInfoFromURLCached($media['url'], empty($media['description'])); $imagedata = Images::getInfoFromURLCached($media['url'], empty($media['description']));
if ($imagedata) { if ($imagedata) {
$media['mimetype'] = $imagedata['mime']; $media['mimetype'] = $imagedata['mime'];
$media['size'] = $imagedata['size']; $media['size'] = $imagedata['size'];
$media['width'] = $imagedata[0]; $media['width'] = $imagedata[0];
$media['height'] = $imagedata[1]; $media['height'] = $imagedata[1];
$media['blurhash'] = $imagedata['blurhash'] ?? null; $media['blurhash'] = $imagedata['blurhash'] ?? null;
if (!empty($imagedata['description']) && empty($media['description'])) { if (!empty($imagedata['description']) && empty($media['description'])) {
$media['description'] = $imagedata['description']; $media['description'] = $imagedata['description'];
@ -276,13 +280,13 @@ class Media
// When the original picture is potentially animated but the preview isn't, we override the preview // When the original picture is potentially animated but the preview isn't, we override the preview
if (in_array($media['mimetype'] ?? '', ['image/gif', 'image/png']) && !in_array($imagedata['mime'], ['image/gif', 'image/png'])) { if (in_array($media['mimetype'] ?? '', ['image/gif', 'image/png']) && !in_array($imagedata['mime'], ['image/gif', 'image/png'])) {
$media['preview'] = $media['url']; $media['preview'] = $media['url'];
$media['preview-width'] = $media['width']; $media['preview-width'] = $media['width'];
$media['preview-height'] = $media['height']; $media['preview-height'] = $media['height'];
return $media; return $media;
} }
$media['preview-width'] = $imagedata[0]; $media['preview-width'] = $imagedata[0];
$media['preview-height'] = $imagedata[1]; $media['preview-height'] = $imagedata[1];
} }
@ -336,21 +340,21 @@ class Media
$gserver = DBA::selectFirst('gserver', ['url', 'site_name'], ['id' => $contact['gsid']]); $gserver = DBA::selectFirst('gserver', ['url', 'site_name'], ['id' => $contact['gsid']]);
} }
$media['type'] = self::ACTIVITY; $media['type'] = self::ACTIVITY;
$media['media-uri-id'] = $item['uri-id']; $media['media-uri-id'] = $item['uri-id'];
$media['height'] = null; $media['height'] = null;
$media['width'] = null; $media['width'] = null;
$media['preview'] = null; $media['preview'] = null;
$media['preview-height'] = null; $media['preview-height'] = null;
$media['preview-width'] = null; $media['preview-width'] = null;
$media['blurhash'] = null; $media['blurhash'] = null;
$media['description'] = $item['body']; $media['description'] = $item['body'];
$media['name'] = $item['title']; $media['name'] = $item['title'];
$media['author-url'] = $item['author-link']; $media['author-url'] = $item['author-link'];
$media['author-name'] = $item['author-name']; $media['author-name'] = $item['author-name'];
$media['author-image'] = $contact['avatar'] ?? $item['author-avatar']; $media['author-image'] = $contact['avatar'] ?? $item['author-avatar'];
$media['publisher-url'] = $gserver['url'] ?? null; $media['publisher-url'] = $gserver['url'] ?? null;
$media['publisher-name'] = $gserver['site_name'] ?? null; $media['publisher-name'] = $gserver['site_name'] ?? null;
$media['publisher-image'] = null; $media['publisher-image'] = null;
Logger::debug('Activity detected', ['uri-id' => $media['uri-id'], 'url' => $media['url'], 'plink' => $item['plink'], 'uri' => $item['uri']]); Logger::debug('Activity detected', ['uri-id' => $media['uri-id'], 'url' => $media['url'], 'plink' => $item['plink'], 'uri' => $item['uri']]);
@ -378,21 +382,21 @@ class Media
$gserver = DBA::selectFirst('gserver', ['url', 'site_name'], ['id' => $contact['gsid']]); $gserver = DBA::selectFirst('gserver', ['url', 'site_name'], ['id' => $contact['gsid']]);
} }
$media['type'] = self::ACCOUNT; $media['type'] = self::ACCOUNT;
$media['media-uri-id'] = $contact['uri-id']; $media['media-uri-id'] = $contact['uri-id'];
$media['height'] = null; $media['height'] = null;
$media['width'] = null; $media['width'] = null;
$media['preview'] = null; $media['preview'] = null;
$media['preview-height'] = null; $media['preview-height'] = null;
$media['preview-width'] = null; $media['preview-width'] = null;
$media['blurhash'] = null; $media['blurhash'] = null;
$media['description'] = $contact['about']; $media['description'] = $contact['about'];
$media['name'] = $contact['name']; $media['name'] = $contact['name'];
$media['author-url'] = $contact['url']; $media['author-url'] = $contact['url'];
$media['author-name'] = $contact['name']; $media['author-name'] = $contact['name'];
$media['author-image'] = $contact['avatar']; $media['author-image'] = $contact['avatar'];
$media['publisher-url'] = $gserver['url'] ?? null; $media['publisher-url'] = $gserver['url'] ?? null;
$media['publisher-name'] = $gserver['site_name'] ?? null; $media['publisher-name'] = $gserver['site_name'] ?? null;
$media['publisher-image'] = null; $media['publisher-image'] = null;
Logger::debug('Account detected', ['uri-id' => $media['uri-id'], 'url' => $media['url'], 'uri' => $contact['url']]); Logger::debug('Account detected', ['uri-id' => $media['uri-id'], 'url' => $media['url'], 'uri' => $contact['url']]);
@ -414,22 +418,22 @@ class Media
Logger::debug('Detected site data is empty, use suggested media data instead', ['uri-id' => $media['uri-id'], 'url' => $media['url'], 'type' => $data['type']]); Logger::debug('Detected site data is empty, use suggested media data instead', ['uri-id' => $media['uri-id'], 'url' => $media['url'], 'type' => $data['type']]);
} }
} else { } else {
$media['preview'] = $data['images'][0]['src'] ?? null; $media['preview'] = $data['images'][0]['src'] ?? null;
$media['preview-height'] = $data['images'][0]['height'] ?? null; $media['preview-height'] = $data['images'][0]['height'] ?? null;
$media['preview-width'] = $data['images'][0]['width'] ?? null; $media['preview-width'] = $data['images'][0]['width'] ?? null;
$media['blurhash'] = $data['images'][0]['blurhash'] ?? null; $media['blurhash'] = $data['images'][0]['blurhash'] ?? null;
$media['description'] = $data['text'] ?? null; $media['description'] = $data['text'] ?? null;
$media['name'] = $data['title'] ?? null; $media['name'] = $data['title'] ?? null;
} }
$media['type'] = self::HTML; $media['type'] = self::HTML;
$media['size'] = $data['size'] ?? null; $media['size'] = $data['size'] ?? null;
$media['author-url'] = $data['author_url'] ?? null; $media['author-url'] = $data['author_url'] ?? null;
$media['author-name'] = $data['author_name'] ?? null; $media['author-name'] = $data['author_name'] ?? null;
$media['author-image'] = $data['author_img'] ?? null; $media['author-image'] = $data['author_img'] ?? null;
$media['publisher-url'] = $data['publisher_url'] ?? null; $media['publisher-url'] = $data['publisher_url'] ?? null;
$media['publisher-name'] = $data['publisher_name'] ?? null; $media['publisher-name'] = $data['publisher_name'] ?? null;
$media['publisher-image'] = $data['publisher_img'] ?? null; $media['publisher-image'] = $data['publisher_img'] ?? null;
return $media; return $media;
} }
@ -458,9 +462,9 @@ class Media
$photo = Photo::selectFirst(['type', 'datasize', 'width', 'height', 'blurhash'], ['resource-id' => $matches[1], 'scale' => $matches[2]]); $photo = Photo::selectFirst(['type', 'datasize', 'width', 'height', 'blurhash'], ['resource-id' => $matches[1], 'scale' => $matches[2]]);
if (!empty($photo)) { if (!empty($photo)) {
$media['mimetype'] = $photo['type']; $media['mimetype'] = $photo['type'];
$media['size'] = $photo['datasize']; $media['size'] = $photo['datasize'];
$media['width'] = $photo['width']; $media['width'] = $photo['width'];
$media['height'] = $photo['height']; $media['height'] = $photo['height'];
$media['blurhash'] = $photo['blurhash']; $media['blurhash'] = $photo['blurhash'];
} }
@ -469,7 +473,7 @@ class Media
} }
$photo = Photo::selectFirst(['width', 'height'], ['resource-id' => $matches[1], 'scale' => $matches[2]]); $photo = Photo::selectFirst(['width', 'height'], ['resource-id' => $matches[1], 'scale' => $matches[2]]);
if (!empty($photo)) { if (!empty($photo)) {
$media['preview-width'] = $photo['width']; $media['preview-width'] = $photo['width'];
$media['preview-height'] = $photo['height']; $media['preview-height'] = $photo['height'];
} }
@ -502,7 +506,7 @@ class Media
} }
$filetype = strtolower($type[0]); $filetype = strtolower($type[0]);
$subtype = strtolower($type[1]); $subtype = strtolower($type[1]);
if ($filetype == 'image') { if ($filetype == 'image') {
$type = self::IMAGE; $type = self::IMAGE;
@ -609,23 +613,35 @@ class Media
if (preg_match_all("#\[url=([^\]]+?)\]\s*\[img=([^\[\]]*)\]([^\[\]]*)\[\/img\]\s*\[/url\]$endmatchpattern#ism", $body, $pictures, PREG_SET_ORDER)) { if (preg_match_all("#\[url=([^\]]+?)\]\s*\[img=([^\[\]]*)\]([^\[\]]*)\[\/img\]\s*\[/url\]$endmatchpattern#ism", $body, $pictures, PREG_SET_ORDER)) {
foreach ($pictures as $picture) { foreach ($pictures as $picture) {
if (self::isLinkToImagePage($picture[1], $picture[2])) { if (self::isLinkToImagePage($picture[1], $picture[2])) {
$body = str_replace($picture[0], '', $body); $body = str_replace($picture[0], '', $body);
$image = str_replace(['-1.', '-2.'], '-0.', $picture[2]); $image = str_replace(['-1.', '-2.'], '-0.', $picture[2]);
$attachments[$image] = [ $attachments[$image] = [
'uri-id' => $uriid, 'type' => self::IMAGE, 'url' => $image, 'uri-id' => $uriid,
'preview' => $picture[2], 'description' => $picture[3] 'type' => self::IMAGE,
'url' => $image,
'preview' => $picture[2],
'description' => $picture[3]
]; ];
} elseif (self::isLinkToPhoto($picture[1], $picture[2])) { } elseif (self::isLinkToPhoto($picture[1], $picture[2])) {
$body = str_replace($picture[0], '', $body); $body = str_replace($picture[0], '', $body);
$attachments[$picture[1]] = [ $attachments[$picture[1]] = [
'uri-id' => $uriid, 'type' => self::IMAGE, 'url' => $picture[1], 'uri-id' => $uriid,
'preview' => $picture[2], 'description' => $picture[3] 'type' => self::IMAGE,
'url' => $picture[1],
'preview' => $picture[2],
'description' => $picture[3]
]; ];
} elseif ($removepicturelinks) { } elseif ($removepicturelinks) {
$body = str_replace($picture[0], '', $body); $body = str_replace($picture[0], '', $body);
$attachments[$picture[1]] = [ $attachments[$picture[1]] = [
'uri-id' => $uriid, 'type' => self::UNKNOWN, 'url' => $picture[1], 'uri-id' => $uriid,
'preview' => $picture[2], 'description' => $picture[3] 'type' => self::UNKNOWN,
'url' => $picture[1],
'preview' => $picture[2],
'description' => $picture[3]
]; ];
} }
} }
@ -634,6 +650,7 @@ class Media
if (preg_match_all("/\[img=([^\[\]]*)\]([^\[\]]*)\[\/img\]$endmatchpattern/Usi", $body, $pictures, PREG_SET_ORDER)) { if (preg_match_all("/\[img=([^\[\]]*)\]([^\[\]]*)\[\/img\]$endmatchpattern/Usi", $body, $pictures, PREG_SET_ORDER)) {
foreach ($pictures as $picture) { foreach ($pictures as $picture) {
$body = str_replace($picture[0], '', $body); $body = str_replace($picture[0], '', $body);
$attachments[$picture[1]] = ['uri-id' => $uriid, 'type' => self::IMAGE, 'url' => $picture[1], 'description' => $picture[2]]; $attachments[$picture[1]] = ['uri-id' => $uriid, 'type' => self::IMAGE, 'url' => $picture[1], 'description' => $picture[2]];
} }
} }
@ -641,23 +658,35 @@ class Media
if (preg_match_all("#\[url=([^\]]+?)\]\s*\[img\]([^\[]+?)\[/img\]\s*\[/url\]$endmatchpattern#ism", $body, $pictures, PREG_SET_ORDER)) { if (preg_match_all("#\[url=([^\]]+?)\]\s*\[img\]([^\[]+?)\[/img\]\s*\[/url\]$endmatchpattern#ism", $body, $pictures, PREG_SET_ORDER)) {
foreach ($pictures as $picture) { foreach ($pictures as $picture) {
if (self::isLinkToImagePage($picture[1], $picture[2])) { if (self::isLinkToImagePage($picture[1], $picture[2])) {
$body = str_replace($picture[0], '', $body); $body = str_replace($picture[0], '', $body);
$image = str_replace(['-1.', '-2.'], '-0.', $picture[2]); $image = str_replace(['-1.', '-2.'], '-0.', $picture[2]);
$attachments[$image] = [ $attachments[$image] = [
'uri-id' => $uriid, 'type' => self::IMAGE, 'url' => $image, 'uri-id' => $uriid,
'preview' => $picture[2], 'description' => null 'type' => self::IMAGE,
'url' => $image,
'preview' => $picture[2],
'description' => null
]; ];
} elseif (self::isLinkToPhoto($picture[1], $picture[2])) { } elseif (self::isLinkToPhoto($picture[1], $picture[2])) {
$body = str_replace($picture[0], '', $body); $body = str_replace($picture[0], '', $body);
$attachments[$picture[1]] = [ $attachments[$picture[1]] = [
'uri-id' => $uriid, 'type' => self::IMAGE, 'url' => $picture[1], 'uri-id' => $uriid,
'preview' => $picture[2], 'description' => null 'type' => self::IMAGE,
'url' => $picture[1],
'preview' => $picture[2],
'description' => null
]; ];
} elseif ($removepicturelinks) { } elseif ($removepicturelinks) {
$body = str_replace($picture[0], '', $body); $body = str_replace($picture[0], '', $body);
$attachments[$picture[1]] = [ $attachments[$picture[1]] = [
'uri-id' => $uriid, 'type' => self::UNKNOWN, 'url' => $picture[1], 'uri-id' => $uriid,
'preview' => $picture[2], 'description' => null 'type' => self::UNKNOWN,
'url' => $picture[1],
'preview' => $picture[2],
'description' => null
]; ];
} }
} }
@ -666,6 +695,7 @@ class Media
if (preg_match_all("/\[img\]([^\[\]]*)\[\/img\]$endmatchpattern/ism", $body, $pictures, PREG_SET_ORDER)) { if (preg_match_all("/\[img\]([^\[\]]*)\[\/img\]$endmatchpattern/ism", $body, $pictures, PREG_SET_ORDER)) {
foreach ($pictures as $picture) { foreach ($pictures as $picture) {
$body = str_replace($picture[0], '', $body); $body = str_replace($picture[0], '', $body);
$attachments[$picture[1]] = ['uri-id' => $uriid, 'type' => self::IMAGE, 'url' => $picture[1]]; $attachments[$picture[1]] = ['uri-id' => $uriid, 'type' => self::IMAGE, 'url' => $picture[1]];
} }
} }
@ -673,6 +703,7 @@ class Media
if (preg_match_all("/\[audio\]([^\[\]]*)\[\/audio\]$endmatchpattern/ism", $body, $audios, PREG_SET_ORDER)) { if (preg_match_all("/\[audio\]([^\[\]]*)\[\/audio\]$endmatchpattern/ism", $body, $audios, PREG_SET_ORDER)) {
foreach ($audios as $audio) { foreach ($audios as $audio) {
$body = str_replace($audio[0], '', $body); $body = str_replace($audio[0], '', $body);
$attachments[$audio[1]] = ['uri-id' => $uriid, 'type' => self::AUDIO, 'url' => $audio[1]]; $attachments[$audio[1]] = ['uri-id' => $uriid, 'type' => self::AUDIO, 'url' => $audio[1]];
} }
} }
@ -680,6 +711,7 @@ class Media
if (preg_match_all("/\[video\]([^\[\]]*)\[\/video\]$endmatchpattern/ism", $body, $videos, PREG_SET_ORDER)) { if (preg_match_all("/\[video\]([^\[\]]*)\[\/video\]$endmatchpattern/ism", $body, $videos, PREG_SET_ORDER)) {
foreach ($videos as $video) { foreach ($videos as $video) {
$body = str_replace($video[0], '', $body); $body = str_replace($video[0], '', $body);
$attachments[$video[1]] = ['uri-id' => $uriid, 'type' => self::VIDEO, 'url' => $video[1]]; $attachments[$video[1]] = ['uri-id' => $uriid, 'type' => self::VIDEO, 'url' => $video[1]];
} }
} }
@ -710,7 +742,7 @@ class Media
{ {
do { do {
$prebody = $body; $prebody = $body;
$body = self::insertFromBody(0, $body, true); $body = self::insertFromBody(0, $body, true);
} while ($prebody != $body); } while ($prebody != $body);
return $body; return $body;
} }
@ -725,7 +757,7 @@ class Media
{ {
do { do {
$prebody = $body; $prebody = $body;
$body = self::insertFromBody(0, $body, false, true); $body = self::insertFromBody(0, $body, false, true);
} while ($prebody != $body); } while ($prebody != $body);
return $body; return $body;
} }
@ -808,15 +840,15 @@ class Media
Logger::info('Adding attachment data', ['data' => $data]); Logger::info('Adding attachment data', ['data' => $data]);
$attachment = [ $attachment = [
'uri-id' => $uriid, 'uri-id' => $uriid,
'type' => self::HTML, 'type' => self::HTML,
'url' => $data['url'], 'url' => $data['url'],
'preview' => $data['preview'] ?? null, 'preview' => $data['preview'] ?? null,
'description' => $data['description'] ?? null, 'description' => $data['description'] ?? null,
'name' => $data['title'] ?? null, 'name' => $data['title'] ?? null,
'author-url' => $data['author_url'] ?? null, 'author-url' => $data['author_url'] ?? null,
'author-name' => $data['author_name'] ?? null, 'author-name' => $data['author_name'] ?? null,
'publisher-url' => $data['provider_url'] ?? null, 'publisher-url' => $data['provider_url'] ?? null,
'publisher-name' => $data['provider_name'] ?? null, 'publisher-name' => $data['provider_name'] ?? null,
]; ];
if (!empty($data['image'])) { if (!empty($data['image'])) {
@ -839,11 +871,11 @@ class Media
} }
foreach ($matches as $attachment) { foreach ($matches as $attachment) {
$media['type'] = self::DOCUMENT; $media['type'] = self::DOCUMENT;
$media['uri-id'] = $uriid; $media['uri-id'] = $uriid;
$media['url'] = $attachment[1]; $media['url'] = $attachment[1];
$media['size'] = $attachment[2]; $media['size'] = $attachment[2];
$media['mimetype'] = $attachment[3]; $media['mimetype'] = $attachment[3];
$media['description'] = $attachment[4] ?? ''; $media['description'] = $attachment[4] ?? '';
self::insert($media); self::insert($media);
@ -1036,20 +1068,20 @@ class Media
} }
$data = [ $data = [
'type' => 'link', 'type' => 'link',
'url' => $links[0]['url'], 'url' => $links[0]['url'],
'title' => $links[0]['name'], 'title' => $links[0]['name'],
'text' => $links[0]['description'], 'text' => $links[0]['description'],
'publisher_name' => $links[0]['publisher-name'], 'publisher_name' => $links[0]['publisher-name'],
'publisher_url' => $links[0]['publisher-url'], 'publisher_url' => $links[0]['publisher-url'],
'publisher_img' => $links[0]['publisher-image'], 'publisher_img' => $links[0]['publisher-image'],
'author_name' => $links[0]['author-name'], 'author_name' => $links[0]['author-name'],
'author_url' => $links[0]['author-url'], 'author_url' => $links[0]['author-url'],
'author_img' => $links[0]['author-image'], 'author_img' => $links[0]['author-image'],
'images' => [[ 'images' => [[
'src' => $links[0]['preview'], 'src' => $links[0]['preview'],
'height' => $links[0]['preview-height'], 'height' => $links[0]['preview-height'],
'width' => $links[0]['preview-width'], 'width' => $links[0]['preview-width'],
]] ]]
]; ];
$body .= "\n" . PageInfo::getFooterFromData($data); $body .= "\n" . PageInfo::getFooterFromData($data);

View file

@ -47,18 +47,19 @@ use Friendica\Util\Strings;
class Receiver class Receiver
{ {
const PUBLIC_COLLECTION = 'as:Public'; const PUBLIC_COLLECTION = 'as:Public';
const ACCOUNT_TYPES = ['as:Person', 'as:Organization', 'as:Service', 'as:Group', 'as:Application'];
const CONTENT_TYPES = ['as:Note', 'as:Article', 'as:Video', 'as:Image', 'as:Event', 'as:Audio', 'as:Page', 'as:Question']; const ACCOUNT_TYPES = ['as:Person', 'as:Organization', 'as:Service', 'as:Group', 'as:Application'];
const CONTENT_TYPES = ['as:Note', 'as:Article', 'as:Video', 'as:Image', 'as:Event', 'as:Audio', 'as:Page', 'as:Question'];
const ACTIVITY_TYPES = ['as:Like', 'as:Dislike', 'as:Accept', 'as:Reject', 'as:TentativeAccept', 'as:View', 'as:Read', 'litepub:EmojiReact']; const ACTIVITY_TYPES = ['as:Like', 'as:Dislike', 'as:Accept', 'as:Reject', 'as:TentativeAccept', 'as:View', 'as:Read', 'litepub:EmojiReact'];
const TARGET_UNKNOWN = 0; const TARGET_UNKNOWN = 0;
const TARGET_TO = 1; const TARGET_TO = 1;
const TARGET_CC = 2; const TARGET_CC = 2;
const TARGET_BTO = 3; const TARGET_BTO = 3;
const TARGET_BCC = 4; const TARGET_BCC = 4;
const TARGET_FOLLOWER = 5; const TARGET_FOLLOWER = 5;
const TARGET_ANSWER = 6; const TARGET_ANSWER = 6;
const TARGET_GLOBAL = 7; const TARGET_GLOBAL = 7;
const TARGET_AUDIENCE = 8; const TARGET_AUDIENCE = 8;
const COMPLETION_NONE = 0; const COMPLETION_NONE = 0;
@ -280,7 +281,7 @@ class Receiver
$data = Processor::fetchCachedActivity($object_id, $uid); $data = Processor::fetchCachedActivity($object_id, $uid);
if (!empty($data)) { if (!empty($data)) {
$object = JsonLD::compact($data); $object = JsonLD::compact($data);
$type = JsonLD::fetchElement($object, '@type'); $type = JsonLD::fetchElement($object, '@type');
if (!empty($type)) { if (!empty($type)) {
return $type; return $type;
} }
@ -335,7 +336,7 @@ class Receiver
if (($fetched_id == $id) && !empty($fetched_type) && ($fetched_type == $type)) { if (($fetched_id == $id) && !empty($fetched_type) && ($fetched_type == $type)) {
Logger::info('Activity had been fetched successfully', ['id' => $id]); Logger::info('Activity had been fetched successfully', ['id' => $id]);
$trust_source = true; $trust_source = true;
$activity = $object; $activity = $object;
} elseif (($fetched_id == $object_id) && !empty($fetched_type) && ($fetched_type == $object_type)) { } elseif (($fetched_id == $object_id) && !empty($fetched_type) && ($fetched_type == $object_type)) {
Logger::info('Fetched data is the object instead of the activity', ['id' => $id]); Logger::info('Fetched data is the object instead of the activity', ['id' => $id]);
$trust_source = true; $trust_source = true;
@ -359,9 +360,9 @@ class Receiver
// Fetch all receivers from to, cc, bto and bcc // Fetch all receivers from to, cc, bto and bcc
$receiverdata = self::getReceivers($activity, $original_actor ?: $actor, [], false, $push || $fetched); $receiverdata = self::getReceivers($activity, $original_actor ?: $actor, [], false, $push || $fetched);
$receivers = $reception_types = []; $receivers = $reception_types = [];
foreach ($receiverdata as $key => $data) { foreach ($receiverdata as $key => $data) {
$receivers[$key] = $data['uid']; $receivers[$key] = $data['uid'];
$reception_types[$data['uid']] = $data['type'] ?? self::TARGET_UNKNOWN; $reception_types[$data['uid']] = $data['type'] ?? self::TARGET_UNKNOWN;
} }
@ -370,9 +371,10 @@ class Receiver
// When it is a delivery to a personal inbox we add that user to the receivers // When it is a delivery to a personal inbox we add that user to the receivers
if (!empty($uid)) { if (!empty($uid)) {
$additional = [$uid => $uid]; $additional = [$uid => $uid];
$receivers = array_replace($receivers, $additional); $receivers = array_replace($receivers, $additional);
if (empty($activity['thread-completion']) && (empty($reception_types[$uid]) || in_array($reception_types[$uid], [self::TARGET_UNKNOWN, self::TARGET_FOLLOWER, self::TARGET_ANSWER, self::TARGET_GLOBAL]))) { if (empty($activity['thread-completion']) && (empty($reception_types[$uid]) || in_array($reception_types[$uid], [self::TARGET_UNKNOWN, self::TARGET_FOLLOWER, self::TARGET_ANSWER, self::TARGET_GLOBAL]))) {
$reception_types[$uid] = self::TARGET_BCC; $reception_types[$uid] = self::TARGET_BCC;
$owner = User::getOwnerDataById($uid); $owner = User::getOwnerDataById($uid);
if (!empty($owner['url'])) { if (!empty($owner['url'])) {
$urls['as:bcc'][] = $owner['url']; $urls['as:bcc'][] = $owner['url'];
@ -400,19 +402,21 @@ class Receiver
// Any activities on account types must not be altered // Any activities on account types must not be altered
if (in_array($type, ['as:Flag'])) { if (in_array($type, ['as:Flag'])) {
$object_data = []; $object_data = [];
$object_data['id'] = JsonLD::fetchElement($activity, '@id');
$object_data['object_id'] = JsonLD::fetchElement($activity, 'as:object', '@id'); $object_data['id'] = JsonLD::fetchElement($activity, '@id');
$object_data['object_id'] = JsonLD::fetchElement($activity, 'as:object', '@id');
$object_data['object_ids'] = JsonLD::fetchElementArray($activity, 'as:object', '@id'); $object_data['object_ids'] = JsonLD::fetchElementArray($activity, 'as:object', '@id');
$object_data['content'] = JsonLD::fetchElement($activity, 'as:content', '@type'); $object_data['content'] = JsonLD::fetchElement($activity, 'as:content', '@type');
} elseif (in_array($object_type, self::ACCOUNT_TYPES)) { } elseif (in_array($object_type, self::ACCOUNT_TYPES)) {
$object_data = []; $object_data = [];
$object_data['id'] = JsonLD::fetchElement($activity, '@id');
$object_data['object_id'] = JsonLD::fetchElement($activity, 'as:object', '@id'); $object_data['id'] = JsonLD::fetchElement($activity, '@id');
$object_data['object_actor'] = JsonLD::fetchElement($activity['as:object'], 'as:actor', '@id'); $object_data['object_id'] = JsonLD::fetchElement($activity, 'as:object', '@id');
$object_data['object_actor'] = JsonLD::fetchElement($activity['as:object'], 'as:actor', '@id');
$object_data['object_object'] = JsonLD::fetchElement($activity['as:object'], 'as:object'); $object_data['object_object'] = JsonLD::fetchElement($activity['as:object'], 'as:object');
$object_data['object_type'] = JsonLD::fetchElement($activity['as:object'], '@type'); $object_data['object_type'] = JsonLD::fetchElement($activity['as:object'], '@type');
if (!$trust_source && ($type == 'as:Delete')) { if (!$trust_source && ($type == 'as:Delete')) {
$apcontact = APContact::getByURL($object_data['object_id'], true); $apcontact = APContact::getByURL($object_data['object_id'], true);
$trust_source = empty($apcontact) || ($apcontact['type'] == 'Tombstone') || $apcontact['suspended']; $trust_source = empty($apcontact) || ($apcontact['type'] == 'Tombstone') || $apcontact['suspended'];
} }
} elseif (in_array($type, ['as:Create', 'as:Update', 'as:Invite']) || strpos($type, '#emojiReaction')) { } elseif (in_array($type, ['as:Create', 'as:Update', 'as:Invite']) || strpos($type, '#emojiReaction')) {
@ -436,24 +440,27 @@ class Receiver
// Create a mostly empty array out of the activity data (instead of the object). // Create a mostly empty array out of the activity data (instead of the object).
// This way we later don't have to check for the existence of each individual array element. // This way we later don't have to check for the existence of each individual array element.
$object_data = self::processObject($activity, $original_actor); $object_data = self::processObject($activity, $original_actor);
$object_data['name'] = $type;
$object_data['author'] = JsonLD::fetchElement($activity, 'as:actor', '@id'); $object_data['name'] = $type;
$object_data['object_id'] = $object_id; $object_data['author'] = JsonLD::fetchElement($activity, 'as:actor', '@id');
$object_data['object_id'] = $object_id;
$object_data['object_type'] = ''; // Since we don't fetch the object, we don't know the type $object_data['object_type'] = ''; // Since we don't fetch the object, we don't know the type
} elseif (in_array($type, ['as:Add', 'as:Remove', 'as:Move'])) { } elseif (in_array($type, ['as:Add', 'as:Remove', 'as:Move'])) {
$object_data = []; $object_data = [];
$object_data['id'] = JsonLD::fetchElement($activity, '@id');
$object_data['target_id'] = JsonLD::fetchElement($activity, 'as:target', '@id'); $object_data['id'] = JsonLD::fetchElement($activity, '@id');
$object_data['object_id'] = JsonLD::fetchElement($activity, 'as:object', '@id'); $object_data['target_id'] = JsonLD::fetchElement($activity, 'as:target', '@id');
$object_data['object_type'] = JsonLD::fetchElement($activity['as:object'], '@type'); $object_data['object_id'] = JsonLD::fetchElement($activity, 'as:object', '@id');
$object_data['object_type'] = JsonLD::fetchElement($activity['as:object'], '@type');
$object_data['object_content'] = JsonLD::fetchElement($activity['as:object'], 'as:content', '@type'); $object_data['object_content'] = JsonLD::fetchElement($activity['as:object'], 'as:content', '@type');
} else { } else {
$object_data = []; $object_data = [];
$object_data['id'] = JsonLD::fetchElement($activity, '@id');
$object_data['object_id'] = JsonLD::fetchElement($activity, 'as:object', '@id'); $object_data['id'] = JsonLD::fetchElement($activity, '@id');
$object_data['object_actor'] = JsonLD::fetchElement($activity['as:object'], 'as:actor', '@id'); $object_data['object_id'] = JsonLD::fetchElement($activity, 'as:object', '@id');
$object_data['object_actor'] = JsonLD::fetchElement($activity['as:object'], 'as:actor', '@id');
$object_data['object_object'] = JsonLD::fetchElement($activity['as:object'], 'as:object'); $object_data['object_object'] = JsonLD::fetchElement($activity['as:object'], 'as:object');
$object_data['object_type'] = JsonLD::fetchElement($activity['as:object'], '@type'); $object_data['object_type'] = JsonLD::fetchElement($activity['as:object'], '@type');
// An Undo is done on the object of an object, so we need that type as well // An Undo is done on the object of an object, so we need that type as well
if (($type == 'as:Undo') && !empty($object_data['object_object'])) { if (($type == 'as:Undo') && !empty($object_data['object_object'])) {
@ -482,13 +489,13 @@ class Receiver
} }
} }
$object_data['type'] = $type; $object_data['type'] = $type;
$object_data['actor'] = $actor; $object_data['actor'] = $actor;
$object_data['item_receiver'] = $receivers; $object_data['item_receiver'] = $receivers;
$object_data['receiver'] = array_replace($object_data['receiver'] ?? [], $receivers); $object_data['receiver'] = array_replace($object_data['receiver'] ?? [], $receivers);
$object_data['reception_type'] = array_replace($object_data['reception_type'] ?? [], $reception_types); $object_data['reception_type'] = array_replace($object_data['reception_type'] ?? [], $reception_types);
$account = Contact::selectFirstAccount(['platform'], ['nurl' => Strings::normaliseLink($actor)]); $account = Contact::selectFirstAccount(['platform'], ['nurl' => Strings::normaliseLink($actor)]);
$platform = $account['platform'] ?? ''; $platform = $account['platform'] ?? '';
Logger::info('Processing', ['type' => $object_data['type'], 'object_type' => $object_data['object_type'], 'id' => $object_data['id'], 'actor' => $actor, 'platform' => $platform]); Logger::info('Processing', ['type' => $object_data['type'], 'object_type' => $object_data['object_type'], 'id' => $object_data['id'], 'actor' => $actor, 'platform' => $platform]);
@ -555,6 +562,7 @@ class Receiver
$user = User::getById(array_key_first($receivers), ['language']); $user = User::getById(array_key_first($receivers), ['language']);
$l10n = DI::l10n()->withLang($user['language']); $l10n = DI::l10n()->withLang($user['language']);
$object_data['name'] = $l10n->t('Chat'); $object_data['name'] = $l10n->t('Chat');
$mail = DBA::selectFirst('mail', ['uri'], ['uid' => array_key_first($receivers), 'title' => $object_data['name']], ['order' => ['id' => true]]); $mail = DBA::selectFirst('mail', ['uri'], ['uid' => array_key_first($receivers), 'title' => $object_data['name']], ['order' => ['id' => true]]);
@ -633,7 +641,7 @@ class Receiver
if (!empty($published) && $object_id !== null && in_array($type, ['as:Create', 'as:Update']) && in_array($object_type, self::CONTENT_TYPES) if (!empty($published) && $object_id !== null && in_array($type, ['as:Create', 'as:Update']) && in_array($object_type, self::CONTENT_TYPES)
&& ($push || ($completion != self::COMPLETION_MANUAL)) && DI::contentItem()->isTooOld($published) && !Post::exists(['uri' => $object_id])) { && ($push || ($completion != self::COMPLETION_MANUAL)) && DI::contentItem()->isTooOld($published) && !Post::exists(['uri' => $object_id])) {
Logger::debug('Activity is too old. It will not be processed', ['push' => $push, 'completion' => $completion, 'type' => $type, 'object-type' => $object_type, 'published' => $published, 'id' => $id, 'object-id' => $object_id]); Logger::debug('Activity is too old. It will not be processed', ['push' => $push, 'completion' => $completion, 'type' => $type, 'object-type' => $object_type, 'published' => $published, 'id' => $id, 'object-id' => $object_id]);
return true; return true;
} }
} else { } else {
@ -662,8 +670,8 @@ class Receiver
} elseif (in_array($object_type, array_merge(self::ACTIVITY_TYPES, ['as:Delete', 'as:Undo', 'as:Update']))) { } elseif (in_array($object_type, array_merge(self::ACTIVITY_TYPES, ['as:Delete', 'as:Undo', 'as:Update']))) {
Logger::debug('Change announced activity to activity', ['type' => $object_type]); Logger::debug('Change announced activity to activity', ['type' => $object_type]);
$original_actor = $actor; $original_actor = $actor;
$type = $object_type; $type = $object_type;
$activity = $activity['as:object']; $activity = $activity['as:object'];
} else { } else {
Logger::info('Unhandled announced activity', ['type' => $object_type, 'object_type' => $object_object_type]); Logger::info('Unhandled announced activity', ['type' => $object_type, 'object_type' => $object_object_type]);
} }
@ -699,7 +707,7 @@ class Receiver
} }
if ($type == 'as:Announce') { if ($type == 'as:Announce') {
$object_data['object_activity'] = $activity; $object_data['object_activity'] = $activity;
} }
if (($type == 'as:Create') && $trust_source && !in_array($completion, [self::COMPLETION_MANUAL, self::COMPLETION_ANNOUNCE])) { if (($type == 'as:Create') && $trust_source && !in_array($completion, [self::COMPLETION_MANUAL, self::COMPLETION_ANNOUNCE])) {
@ -717,7 +725,7 @@ class Receiver
self::addArrivedId($object_data['object_id']); self::addArrivedId($object_data['object_id']);
} }
$object_data['children'] = $activity['children'] ?? []; $object_data['children'] = $activity['children'] ?? [];
$object_data['callstack'] = $activity['callstack'] ?? []; $object_data['callstack'] = $activity['callstack'] ?? [];
$decouple = DI::config()->get('system', 'decoupled_receiver') && !in_array($completion, [self::COMPLETION_MANUAL, self::COMPLETION_ANNOUNCE]) && empty($object_data['directmessage']); $decouple = DI::config()->get('system', 'decoupled_receiver') && !in_array($completion, [self::COMPLETION_MANUAL, self::COMPLETION_ANNOUNCE]) && empty($object_data['directmessage']);
@ -727,7 +735,7 @@ class Receiver
} }
if (!$trust_source) { if (!$trust_source) {
Logger::info('Activity trust could not be achieved.', ['id' => $object_data['object_id'], 'type' => $type, 'signer' => $signer, 'actor' => $actor, 'attributedTo' => $attributed_to]); Logger::info('Activity trust could not be achieved.', ['id' => $object_data['object_id'], 'type' => $type, 'signer' => $signer, 'actor' => $actor, 'attributedTo' => $attributed_to]);
return true; return true;
} }
@ -1090,7 +1098,7 @@ class Receiver
*/ */
private static function getBestUserForActivity(array $activity, string $actor): int private static function getBestUserForActivity(array $activity, string $actor): int
{ {
$uid = 0; $uid = 0;
$actor = $actor ?: JsonLD::fetchElement($activity, 'as:actor', '@id') ?? ''; $actor = $actor ?: JsonLD::fetchElement($activity, 'as:actor', '@id') ?? '';
$receivers = self::getReceivers($activity, $actor, [], false, false); $receivers = self::getReceivers($activity, $actor, [], false, false);
@ -1177,12 +1185,12 @@ class Receiver
if (!empty($actor)) { if (!empty($actor)) {
$profile = APContact::getByURL($actor); $profile = APContact::getByURL($actor);
$followers = $profile['followers'] ?? ''; $followers = $profile['followers'] ?? '';
$isGroup = ($profile['type'] ?? '') == 'Group'; $isGroup = ($profile['type'] ?? '') == 'Group';
Logger::info('Got actor and followers', ['actor' => $actor, 'followers' => $followers]); Logger::info('Got actor and followers', ['actor' => $actor, 'followers' => $followers]);
} else { } else {
Logger::info('Empty actor', ['activity' => $activity]); Logger::info('Empty actor', ['activity' => $activity]);
$followers = ''; $followers = '';
$isGroup = false; $isGroup = false;
} }
$parent_followers = ''; $parent_followers = '';
@ -1228,7 +1236,7 @@ class Receiver
// Fetching all directly addressed receivers // Fetching all directly addressed receivers
$condition = ['self' => true, 'nurl' => Strings::normaliseLink($receiver)]; $condition = ['self' => true, 'nurl' => Strings::normaliseLink($receiver)];
$contact = DBA::selectFirst('contact', ['uid', 'contact-type'], $condition); $contact = DBA::selectFirst('contact', ['uid', 'contact-type'], $condition);
if (!DBA::isResult($contact)) { if (!DBA::isResult($contact)) {
continue; continue;
} }
@ -1236,9 +1244,11 @@ class Receiver
// Check if the potential receiver is following the actor // Check if the potential receiver is following the actor
// Exception: The receiver is targetted via "to" or this is a comment // Exception: The receiver is targetted via "to" or this is a comment
if ((($element != 'as:to') && empty($replyto)) || ($contact['contact-type'] == Contact::TYPE_COMMUNITY)) { if ((($element != 'as:to') && empty($replyto)) || ($contact['contact-type'] == Contact::TYPE_COMMUNITY)) {
$networks = Protocol::FEDERATED; $networks = Protocol::FEDERATED;
$condition = ['nurl' => Strings::normaliseLink($actor), 'rel' => [Contact::SHARING, Contact::FRIEND], $condition = [
'network' => $networks, 'archive' => false, 'pending' => false, 'uid' => $contact['uid']]; 'nurl' => Strings::normaliseLink($actor), 'rel' => [Contact::SHARING, Contact::FRIEND],
'network' => $networks, 'archive' => false, 'pending' => false, 'uid' => $contact['uid']
];
// Group posts are only accepted from group contacts // Group posts are only accepted from group contacts
if ($contact['contact-type'] == Contact::TYPE_COMMUNITY) { if ($contact['contact-type'] == Contact::TYPE_COMMUNITY) {
@ -1277,6 +1287,7 @@ class Receiver
if (empty($receivers) && !empty($parent['parent-author-link'])) { if (empty($receivers) && !empty($parent['parent-author-link'])) {
$uid = User::getIdForURL($parent['parent-author-link']); $uid = User::getIdForURL($parent['parent-author-link']);
$receivers[$uid] = ['uid' => $uid, 'type' => self::TARGET_BTO]; $receivers[$uid] = ['uid' => $uid, 'type' => self::TARGET_BTO];
} }
@ -1319,11 +1330,13 @@ class Receiver
*/ */
private static function getReceiverForActor(array $tags, array $receivers, int $target_type, array $profile): array private static function getReceiverForActor(array $tags, array $receivers, int $target_type, array $profile): array
{ {
$basecondition = ['rel' => [Contact::SHARING, Contact::FRIEND, Contact::FOLLOWER], $basecondition = [
'network' => Protocol::FEDERATED, 'archive' => false, 'pending' => false]; 'rel' => [Contact::SHARING, Contact::FRIEND, Contact::FOLLOWER],
'network' => Protocol::FEDERATED, 'archive' => false, 'pending' => false
];
$condition = DBA::mergeConditions($basecondition, ["`uri-id` = ? AND `uid` != ?", $profile['uri-id'], 0]); $condition = DBA::mergeConditions($basecondition, ["`uri-id` = ? AND `uid` != ?", $profile['uri-id'], 0]);
$contacts = DBA::select('contact', ['uid', 'rel'], $condition); $contacts = DBA::select('contact', ['uid', 'rel'], $condition);
while ($contact = DBA::fetch($contacts)) { while ($contact = DBA::fetch($contacts)) {
if (empty($receivers[$contact['uid']]) && self::isValidReceiverForActor($contact, $tags)) { if (empty($receivers[$contact['uid']]) && self::isValidReceiverForActor($contact, $tags)) {
$receivers[$contact['uid']] = ['uid' => $contact['uid'], 'type' => $target_type]; $receivers[$contact['uid']] = ['uid' => $contact['uid'], 'type' => $target_type];
@ -1434,7 +1447,7 @@ class Receiver
return false; return false;
} }
Logger::info('Using already stored item for url ' . $object_id); Logger::info('Using already stored item for url ' . $object_id);
$data = ActivityPub\Transmitter::createNote($item); $data = ActivityPub\Transmitter::createNote($item);
$object = JsonLD::compact($data); $object = JsonLD::compact($data);
} }
@ -1511,9 +1524,9 @@ class Receiver
} }
$element = [ $element = [
'type' => str_replace('as:', '', JsonLD::fetchElement($tag, '@type') ?? ''), 'type' => str_replace('as:', '', JsonLD::fetchElement($tag, '@type') ?? ''),
'href' => JsonLD::fetchElement($tag, 'as:href', '@id'), 'href' => JsonLD::fetchElement($tag, 'as:href', '@id'),
'name' => JsonLD::fetchElement($tag, 'as:name', '@value'), 'name' => JsonLD::fetchElement($tag, 'as:name', '@value'),
'mediaType' => JsonLD::fetchElement($tag, 'as:mediaType', '@value') 'mediaType' => JsonLD::fetchElement($tag, 'as:mediaType', '@value')
]; ];
@ -1545,10 +1558,9 @@ class Receiver
continue; continue;
} }
$url = JsonLD::fetchElement($emoji['as:icon'], 'as:url', '@id');
$element = [ $element = [
'name' => JsonLD::fetchElement($emoji, 'as:name', '@value'), 'name' => JsonLD::fetchElement($emoji, 'as:name', '@value'),
'href' => $url 'href' => JsonLD::fetchElement($emoji['as:icon'], 'as:url', '@id')
]; ];
$emojilist[] = $element; $emojilist[] = $element;
@ -1574,7 +1586,7 @@ class Receiver
foreach ($attachments as $attachment) { foreach ($attachments as $attachment) {
switch (JsonLD::fetchElement($attachment, '@type')) { switch (JsonLD::fetchElement($attachment, '@type')) {
case 'as:Page': case 'as:Page':
$pageUrl = null; $pageUrl = null;
$pageImage = null; $pageImage = null;
$urls = JsonLD::fetchElementArray($attachment, 'as:url'); $urls = JsonLD::fetchElementArray($attachment, 'as:url');
@ -1585,7 +1597,7 @@ class Receiver
continue; continue;
} }
$href = JsonLD::fetchElement($url, 'as:href', '@id'); $href = JsonLD::fetchElement($url, 'as:href', '@id');
$mediaType = JsonLD::fetchElement($url, 'as:mediaType', '@value'); $mediaType = JsonLD::fetchElement($url, 'as:mediaType', '@value');
if (Strings::startsWith($mediaType, 'image')) { if (Strings::startsWith($mediaType, 'image')) {
$pageImage = $href; $pageImage = $href;
@ -1603,12 +1615,12 @@ class Receiver
]; ];
break; break;
case 'as:Image': case 'as:Image':
$mediaType = JsonLD::fetchElement($attachment, 'as:mediaType', '@value'); $mediaType = JsonLD::fetchElement($attachment, 'as:mediaType', '@value');
$imageFullUrl = JsonLD::fetchElement($attachment, 'as:url', '@id'); $imageFullUrl = JsonLD::fetchElement($attachment, 'as:url', '@id');
$imagePreviewUrl = null; $imagePreviewUrl = null;
// Multiple URLs? // Multiple URLs?
if (!$imageFullUrl && ($urls = JsonLD::fetchElementArray($attachment, 'as:url'))) { if (!$imageFullUrl && ($urls = JsonLD::fetchElementArray($attachment, 'as:url'))) {
$imageVariants = []; $imageVariants = [];
$previewVariants = []; $previewVariants = [];
foreach ($urls as $url) { foreach ($urls as $url) {
// Scalar URL, no discrimination possible // Scalar URL, no discrimination possible
@ -1651,22 +1663,22 @@ class Receiver
} }
$attachlist[] = [ $attachlist[] = [
'type' => str_replace('as:', '', JsonLD::fetchElement($attachment, '@type')), 'type' => str_replace('as:', '', JsonLD::fetchElement($attachment, '@type')),
'mediaType' => $mediaType, 'mediaType' => $mediaType,
'name' => JsonLD::fetchElement($attachment, 'as:name', '@value'), 'name' => JsonLD::fetchElement($attachment, 'as:name', '@value'),
'url' => $imageFullUrl, 'url' => $imageFullUrl,
'image' => $imagePreviewUrl !== $imageFullUrl ? $imagePreviewUrl : null, 'image' => $imagePreviewUrl !== $imageFullUrl ? $imagePreviewUrl : null,
]; ];
break; break;
default: default:
$attachlist[] = [ $attachlist[] = [
'type' => str_replace('as:', '', JsonLD::fetchElement($attachment, '@type')), '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') ?? JsonLD::fetchElement($attachment, 'as:href', '@id'), 'url' => JsonLD::fetchElement($attachment, 'as:url', '@id') ?? JsonLD::fetchElement($attachment, 'as:href', '@id'),
'height' => JsonLD::fetchElement($attachment, 'as:height', '@value'), 'height' => JsonLD::fetchElement($attachment, 'as:height', '@value'),
'width' => JsonLD::fetchElement($attachment, 'as:width', '@value'), 'width' => JsonLD::fetchElement($attachment, 'as:width', '@value'),
'image' => JsonLD::fetchElement($attachment, 'as:image', '@id') 'image' => JsonLD::fetchElement($attachment, 'as:image', '@id')
]; ];
} }
} }
@ -1687,10 +1699,10 @@ class Receiver
if (!empty($object['as:oneOf'])) { if (!empty($object['as:oneOf'])) {
$question['multiple'] = false; $question['multiple'] = false;
$options = JsonLD::fetchElementArray($object, 'as:oneOf') ?? []; $options = JsonLD::fetchElementArray($object, 'as:oneOf') ?? [];
} elseif (!empty($object['as:anyOf'])) { } elseif (!empty($object['as:anyOf'])) {
$question['multiple'] = true; $question['multiple'] = true;
$options = JsonLD::fetchElementArray($object, 'as:anyOf') ?? []; $options = JsonLD::fetchElementArray($object, 'as:anyOf') ?? [];
} else { } else {
return []; return [];
} }
@ -1847,6 +1859,7 @@ class Receiver
} }
$size = (int)JsonLD::fetchElement($url, 'pt:size', '@value'); $size = (int)JsonLD::fetchElement($url, 'pt:size', '@value');
$attachments[] = ['type' => $filetype, 'mediaType' => $mediatype, 'url' => $href, 'height' => $height, 'size' => $size, 'name' => '']; $attachments[] = ['type' => $filetype, 'mediaType' => $mediatype, 'url' => $href, 'height' => $height, 'size' => $size, 'name' => ''];
} elseif (in_array($mediatype, ['application/x-bittorrent', 'application/x-bittorrent;x-scheme-handler/magnet'])) { } elseif (in_array($mediatype, ['application/x-bittorrent', 'application/x-bittorrent;x-scheme-handler/magnet'])) {
$height = (int)JsonLD::fetchElement($url, 'as:height', '@value'); $height = (int)JsonLD::fetchElement($url, 'as:height', '@value');
@ -1909,7 +1922,8 @@ class Receiver
return $object_data; return $object_data;
} }
private static function getCapabilities($object) { private static function getCapabilities($object)
{
$capabilities = []; $capabilities = [];
foreach (['pixelfed:canAnnounce', 'pixelfed:canLike', 'pixelfed:canReply'] as $element) { foreach (['pixelfed:canAnnounce', 'pixelfed:canLike', 'pixelfed:canReply'] as $element) {
$capabilities_list = JsonLD::fetchElementArray($object['pixelfed:capabilities'], $element, '@id'); $capabilities_list = JsonLD::fetchElementArray($object['pixelfed:capabilities'], $element, '@id');
@ -1931,8 +1945,9 @@ class Receiver
public static function getObjectDataFromActivity(array $object): array public static function getObjectDataFromActivity(array $object): array
{ {
$object_data = []; $object_data = [];
$object_data['object_type'] = JsonLD::fetchElement($object, '@type'); $object_data['object_type'] = JsonLD::fetchElement($object, '@type');
$object_data['id'] = JsonLD::fetchElement($object, '@id'); $object_data['id'] = JsonLD::fetchElement($object, '@id');
$object_data['reply-to-id'] = JsonLD::fetchElement($object, 'as:inReplyTo', '@id'); $object_data['reply-to-id'] = JsonLD::fetchElement($object, 'as:inReplyTo', '@id');
// An empty "id" field is translated to "./" by the compactor, so we have to check for this content // An empty "id" field is translated to "./" by the compactor, so we have to check for this content
@ -1956,7 +1971,7 @@ class Receiver
} }
$object_data['published'] = JsonLD::fetchElement($object, 'as:published', '@value'); $object_data['published'] = JsonLD::fetchElement($object, 'as:published', '@value');
$object_data['updated'] = JsonLD::fetchElement($object, 'as:updated', '@value'); $object_data['updated'] = JsonLD::fetchElement($object, 'as:updated', '@value');
if (empty($object_data['updated'])) { if (empty($object_data['updated'])) {
$object_data['updated'] = $object_data['published']; $object_data['updated'] = $object_data['published'];
@ -1980,37 +1995,37 @@ class Receiver
$location = BBCode::toPlaintext($location); $location = BBCode::toPlaintext($location);
} }
$object_data['sc:identifier'] = JsonLD::fetchElement($object, 'sc:identifier', '@value'); $object_data['sc:identifier'] = JsonLD::fetchElement($object, 'sc:identifier', '@value');
$object_data['diaspora:guid'] = JsonLD::fetchElement($object, 'diaspora:guid', '@value'); $object_data['diaspora:guid'] = JsonLD::fetchElement($object, 'diaspora:guid', '@value');
$object_data['diaspora:comment'] = JsonLD::fetchElement($object, 'diaspora:comment', '@value'); $object_data['diaspora:comment'] = JsonLD::fetchElement($object, 'diaspora:comment', '@value');
$object_data['diaspora:like'] = JsonLD::fetchElement($object, 'diaspora:like', '@value'); $object_data['diaspora:like'] = JsonLD::fetchElement($object, 'diaspora:like', '@value');
$object_data['actor'] = $object_data['author'] = $actor; $object_data['actor'] = $object_data['author'] = $actor;
$element = JsonLD::fetchElement($object, 'as:context', '@id'); $element = JsonLD::fetchElement($object, 'as:context', '@id');
$object_data['context'] = $element != './' ? $element : null; $object_data['context'] = $element != './' ? $element : null;
$element = JsonLD::fetchElement($object, 'ostatus:conversation', '@id'); $element = JsonLD::fetchElement($object, 'ostatus:conversation', '@id');
$object_data['conversation'] = $element != './' ? $element : null; $object_data['conversation'] = $element != './' ? $element : null;
$object_data['sensitive'] = JsonLD::fetchElement($object, 'as:sensitive'); $object_data['sensitive'] = JsonLD::fetchElement($object, 'as:sensitive');
$object_data['name'] = JsonLD::fetchElement($object, 'as:name', '@value'); $object_data['name'] = JsonLD::fetchElement($object, 'as:name', '@value');
$object_data['summary'] = JsonLD::fetchElement($object, 'as:summary', '@value'); $object_data['summary'] = JsonLD::fetchElement($object, 'as:summary', '@value');
$object_data['content'] = JsonLD::fetchElement($object, 'as:content', '@value'); $object_data['content'] = JsonLD::fetchElement($object, 'as:content', '@value');
$object_data['mediatype'] = JsonLD::fetchElement($object, 'as:mediaType', '@value'); $object_data['mediatype'] = JsonLD::fetchElement($object, 'as:mediaType', '@value');
$object_data = self::getSource($object, $object_data); $object_data = self::getSource($object, $object_data);
$object_data['start-time'] = JsonLD::fetchElement($object, 'as:startTime', '@value'); $object_data['start-time'] = JsonLD::fetchElement($object, 'as:startTime', '@value');
$object_data['end-time'] = JsonLD::fetchElement($object, 'as:endTime', '@value'); $object_data['end-time'] = JsonLD::fetchElement($object, 'as:endTime', '@value');
$object_data['location'] = $location; $object_data['location'] = $location;
$object_data['latitude'] = JsonLD::fetchElement($object, 'as:location', 'as:latitude', '@type', 'as:Place'); $object_data['latitude'] = JsonLD::fetchElement($object, 'as:location', 'as:latitude', '@type', 'as:Place');
$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', null, '@type', 'toot:Emoji') ?? []); $object_data['emojis'] = self::processEmojis(JsonLD::fetchElementArray($object, 'as:tag', null, '@type', 'toot:Emoji') ?? []);
$object_data['languages'] = self::processLanguages(JsonLD::fetchElementArray($object, 'sc:inLanguage') ?? []); $object_data['languages'] = self::processLanguages(JsonLD::fetchElementArray($object, 'sc:inLanguage') ?? []);
$object_data['transmitted-languages'] = Processor::getPostLanguages($object); $object_data['transmitted-languages'] = Processor::getPostLanguages($object);
$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');
$object_data['replies'] = JsonLD::fetchElement($object, 'as:replies', '@id'); $object_data['replies'] = JsonLD::fetchElement($object, 'as:replies', '@id');
// Special treatment for Hubzilla links // Special treatment for Hubzilla links
if (is_array($object_data['alternate-url'])) { if (is_array($object_data['alternate-url'])) {
@ -2027,7 +2042,7 @@ class Receiver
if (in_array($object_data['object_type'], ['as:Audio', 'as:Video'])) { if (in_array($object_data['object_type'], ['as:Audio', 'as:Video'])) {
$object_data['alternate-url'] = self::extractAlternateUrl($object['as:url'] ?? []) ?: $object_data['alternate-url']; $object_data['alternate-url'] = self::extractAlternateUrl($object['as:url'] ?? []) ?: $object_data['alternate-url'];
$object_data['attachments'] = array_merge($object_data['attachments'], self::processAttachmentUrls($object['as:url'] ?? [])); $object_data['attachments'] = array_merge($object_data['attachments'], self::processAttachmentUrls($object['as:url'] ?? []));
} }
$object_data['can-comment'] = JsonLD::fetchElement($object, 'pt:commentsEnabled', '@value'); $object_data['can-comment'] = JsonLD::fetchElement($object, 'pt:commentsEnabled', '@value');
@ -2037,9 +2052,15 @@ class Receiver
// Support for quoted posts (Pleroma, Fedibird and Misskey) // Support for quoted posts (Pleroma, Fedibird and Misskey)
$object_data['quote-url'] = JsonLD::fetchElement($object, 'as:quoteUrl', '@id'); $object_data['quote-url'] = JsonLD::fetchElement($object, 'as:quoteUrl', '@id');
if (empty($object_data['quote-url'])) {
$object_data['quote-url'] = JsonLD::fetchElement($object, 'as:quoteUrl', '@value');
}
if (empty($object_data['quote-url'])) { if (empty($object_data['quote-url'])) {
$object_data['quote-url'] = JsonLD::fetchElement($object, 'fedibird:quoteUri', '@id'); $object_data['quote-url'] = JsonLD::fetchElement($object, 'fedibird:quoteUri', '@id');
} }
if (empty($object_data['quote-url'])) {
$object_data['quote-url'] = JsonLD::fetchElement($object, 'fedibird:quoteUri', '@value');
}
if (empty($object_data['quote-url'])) { if (empty($object_data['quote-url'])) {
$object_data['quote-url'] = JsonLD::fetchElement($object, 'misskey:_misskey_quote', '@id'); $object_data['quote-url'] = JsonLD::fetchElement($object, 'misskey:_misskey_quote', '@id');
} }