From a9a1ac808b475b4ccd27ab7e2aa833cf098f461a Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 3 Oct 2018 17:36:55 +0000 Subject: [PATCH] Attachments do work now --- src/Protocol/ActivityPub.php | 1 + src/Protocol/ActivityPub/Transmitter.php | 73 +++++++++++++++++++++++- 2 files changed, 72 insertions(+), 2 deletions(-) diff --git a/src/Protocol/ActivityPub.php b/src/Protocol/ActivityPub.php index 9ccd90eed6..7c0a2b706c 100644 --- a/src/Protocol/ActivityPub.php +++ b/src/Protocol/ActivityPub.php @@ -43,6 +43,7 @@ use Friendica\Core\Config; * * To-do: * - Polling the outboxes for missing content? + * - "about" needs HTML2BBCode parser */ class ActivityPub { diff --git a/src/Protocol/ActivityPub/Transmitter.php b/src/Protocol/ActivityPub/Transmitter.php index c4a90996d8..8463a91e16 100644 --- a/src/Protocol/ActivityPub/Transmitter.php +++ b/src/Protocol/ActivityPub/Transmitter.php @@ -21,6 +21,8 @@ use Friendica\Util\JsonLD; use Friendica\Util\LDSignature; use Friendica\Protocol\ActivityPub; use Friendica\Model\Profile; +use Friendica\Core\Config; +use Friendica\Object\Image; /** * @brief ActivityPub Transmitter Protocol class @@ -33,7 +35,6 @@ use Friendica\Model\Profile; * - Undo Announce * * General: - * - Attachments * - nsfw (sensitive) * - Queueing unsucessful deliveries */ @@ -595,6 +596,74 @@ class Transmitter return $tags; } + /** + * @brief Adds attachment data to the JSON document + * + * @param array $item Data of the item that is to be posted + * @return attachment array + */ + + private static function createAttachmentList($item) + { + $attachments = []; + + $siteinfo = BBCode::getAttachedData($item['body']); + + switch ($siteinfo['type']) { + case 'photo': + if (!empty($siteinfo['image'])) { + $imgdata = Image::getInfoFromURL($siteinfo['image']); + if ($imgdata) { + $attachments[] = ['type' => 'Document', + 'mediaType' => $imgdata['mime'], + 'url' => $siteinfo['image'], + 'name' => null]; + } + } + break; + case 'video': + $attachments[] = ['type' => 'Document', + 'mediaType' => 'text/html; charset=UTF-8', + 'url' => $siteinfo['url'], + 'name' => defaults($siteinfo, 'title', $siteinfo['url'])]; + break; + default: + break; + } + + if (!Config::get('system', 'ostatus_not_attach_preview') && ($siteinfo['type'] != 'photo') && isset($siteinfo['image'])) { + $imgdata = Image::getInfoFromURL($siteinfo['image']); + if ($imgdata) { + $attachments[] = ['type' => 'Document', + 'mediaType' => $imgdata['mime'], + 'url' => $siteinfo['image'], + 'name' => null]; + } + } + + $arr = explode('[/attach],', $item['attach']); + if (count($arr)) { + foreach ($arr as $r) { + $matches = false; + $cnt = preg_match('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\" title=\"(.*?)\"|', $r, $matches); + if ($cnt) { + $attributes = ['type' => 'Document', + 'mediaType' => $matches[3], + 'url' => $matches[1], + 'name' => null]; + + if (trim($matches[4]) != '') { + $attributes['name'] = trim($matches[4]); + } + + $attachments[] = $attributes; + } + } + } + + return $attachments; + } + /** * @brief Fetches the "context" value for a givem item array from the "conversation" table * @@ -674,7 +743,7 @@ class Transmitter $data['diaspora:comment'] = $item['signed_text']; } - $data['attachment'] = []; // @ToDo + $data['attachment'] = self::createAttachmentList($item); $data['tag'] = self::createTagList($item); $data = array_merge($data, self::createPermissionBlockForItem($item));