[ActivityPub] Attachment fetch should happen on StoreRemoteMedia

This commit is contained in:
Diogo Peralta Cordeiro 2020-09-21 21:54:14 +01:00
parent e51520bd63
commit f9290705f8

View File

@ -85,7 +85,7 @@ class Activitypub_notice
'id' => self::getUri($notice), 'id' => self::getUri($notice),
'type' => 'Delete', 'type' => 'Delete',
// XXX: A bit of ugly code here // XXX: A bit of ugly code here
'object' => array_merge(Activitypub_tombstone::tombstone_to_array((int)substr(explode(':', $notice->getUri())[2],9)), ['deleted' => str_replace(' ', 'T', $notice->getCreated()) . 'Z']), 'object' => array_merge(Activitypub_tombstone::tombstone_to_array((int)substr(explode(':', $notice->getUri())[2], 9)), ['deleted' => str_replace(' ', 'T', $notice->getCreated()) . 'Z']),
'url' => $notice->getUrl(), 'url' => $notice->getUrl(),
'actor' => $profile->getUri(), 'actor' => $profile->getUri(),
'to' => $to, 'to' => $to,
@ -236,14 +236,40 @@ class Activitypub_notice
$attachments = []; $attachments = [];
if (isset($object['attachment']) && is_array($object['attachment'])) { if (isset($object['attachment']) && is_array($object['attachment'])) {
foreach ($object['attachment'] as $attachment) { foreach ($object['attachment'] as $attachment) {
if (array_key_exists('type', $attachment) && $attachment['type'] == 'Document') { if (array_key_exists('type', $attachment)
&& $attachment['type'] === 'Document'
&& array_key_exists('url', $attachment)) {
try { try {
// throws exception on failure $file = new File();
$attachment = MediaFile::fromUrl($attachment['url'], $actor_profile, $attachment['name']); $file->url = $attachment['url'];
$act->enclosures[] = $attachment->getEnclosure(); $file->title = array_key_exists('type', $attachment) ? $attachment['name'] : null;
$attachments[] = $attachment; if (array_key_exists('type', $attachment)) {
$file->mimetype = $attachment['mediaType'];
} else {
$http = new HTTPClient();
common_debug(
'Performing HEAD request for incoming activity '
. 'to avoid unnecessarily downloading too '
. 'large files. URL: ' . $file->url
);
$head = $http->head($file->url);
$headers = $head->getHeader();
$headers = array_change_key_case($headers, CASE_LOWER);
if (array_key_exists('content-type', $headers)) {
$file->mimetype = $headers['content-type'];
} else {
continue;
}
if (array_key_exists('content-length', $headers)) {
$file->size = $headers['content-length'];
}
}
$file->saveFile();
$act->enclosures[] = $file->getEnclosure();
$attachments[] = $file;
} catch (Exception $e) { } catch (Exception $e) {
// Whatever. // Whatever.
continue;
} }
} }
} }
@ -259,8 +285,8 @@ class Activitypub_notice
$note = Notice::saveActivity($act, $actor_profile, $options); $note = Notice::saveActivity($act, $actor_profile, $options);
// Attachments (last part) // Attachments (last part)
foreach($attachments as $attachment) { foreach ($attachments as $file) {
$attachment->attachToNotice($note); File_to_post::processNew($file, $note);
} }
return $note; return $note;