]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/Feed.php
Merge pull request #11079 from annando/attachments
[friendica.git] / src / Protocol / Feed.php
index d793ac817f28526cd8cdc055c7f118882dfc5b12..8cb08f65df3788a93cf2d528d3837ff360c4dbed 100644 (file)
@@ -26,7 +26,7 @@ use DOMXPath;
 use Friendica\Content\PageInfo;
 use Friendica\Content\Text\BBCode;
 use Friendica\Content\Text\HTML;
-use Friendica\Core\Cache\Duration;
+use Friendica\Core\Cache\Enum\Duration;
 use Friendica\Core\Logger;
 use Friendica\Core\Protocol;
 use Friendica\Database\DBA;
@@ -69,6 +69,8 @@ class Feed
                        Logger::info("Import Atom/RSS feed '" . $contact["name"] . "' (Contact " . $contact["id"] . ") for user " . $importer["uid"]);
                }
 
+               $xml = trim($xml);
+
                if (empty($xml)) {
                        Logger::info('XML is empty.');
                        return [];
@@ -83,7 +85,7 @@ class Feed
                }
 
                $doc = new DOMDocument();
-               @$doc->loadXML(trim($xml));
+               @$doc->loadXML($xml);
                $xpath = new DOMXPath($doc);
                $xpath->registerNamespace('atom', ActivityNamespace::ATOM1);
                $xpath->registerNamespace('dc', "http://purl.org/dc/elements/1.1/");
@@ -399,7 +401,16 @@ class Feed
                                }
 
                                if (!empty($href)) {
-                                       $attachments[] = ['type' => Post\Media::DOCUMENT, 'url' => $href, 'mimetype' => $type, 'size' => $length];
+                                       $attachment = ['type' => Post\Media::UNKNOWN, 'url' => $href, 'mimetype' => $type, 'size' => $length];
+
+                                       $attachment = Post\Media::fetchAdditionalData($attachment);
+
+                                       // By now we separate the visible media types (audio, video, image) from the rest
+                                       // In the future we should try to avoid the DOCUMENT type and only use the real one - but not in the RC phase.
+                                       if (!in_array($attachment['type'], [Post\Media::AUDIO, Post\Media::IMAGE, Post\Media::VIDEO])) {
+                                               $attachment['type'] = Post\Media::DOCUMENT;
+                                       }
+                                       $attachments[] = $attachment;
                                }
                        }
 
@@ -523,6 +534,29 @@ class Feed
                                        $taglist = $contact["fetch_further_information"] == 2 ? PageInfo::getTagsFromUrl($item["plink"], $preview, $contact["ffi_keyword_denylist"] ?? '') : [];
                                        $item["object-type"] = Activity\ObjectType::BOOKMARK;
                                        $attachments = [];
+
+                                       foreach (['audio', 'video'] as $elementname) {
+                                               if (!empty($data[$elementname])) {
+                                                       foreach ($data[$elementname] as $element) {
+                                                               if (!empty($element['src'])) {
+                                                                       $src = $element['src'];
+                                                               } elseif (!empty($element['content'])) {
+                                                                       $src = $element['content'];
+                                                               } else {
+                                                                       continue;
+                                                               }
+
+                                                               $attachments[] = [
+                                                                       'type'        => ($elementname == 'audio') ? Post\Media::AUDIO : Post\Media::VIDEO,
+                                                                       'url'         => $src,
+                                                                       'preview'     => $element['image']       ?? null,
+                                                                       'mimetype'    => $element['contenttype'] ?? null,
+                                                                       'name'        => $element['name']        ?? null,
+                                                                       'description' => $element['description'] ?? null,
+                                                               ];
+                                                       }
+                                               }
+                                       }
                                }
                        } else {
                                if (!empty($summary)) {
@@ -539,7 +573,7 @@ class Feed
                                }
 
                                // Add the link to the original feed entry if not present in feed
-                               if (($item['plink'] != '') && !strstr($item["body"], $item['plink'])) {
+                               if (($item['plink'] != '') && !strstr($item["body"], $item['plink']) && !in_array($item['plink'], array_column($attachments, 'url'))) {
                                        $item["body"] .= "[hr][url]" . $item['plink'] . "[/url]";
                                }
                        }
@@ -911,7 +945,7 @@ class Feed
                $root = self::addHeader($doc, $owner, $filter);
 
                foreach ($items as $item) {
-                       $entry = self::entry($doc, $item, $owner);
+                       $entry = self::noteEntry($doc, $item, $owner);
                        $root->appendChild($entry);
 
                        if ($last_update < $item['created']) {
@@ -1001,69 +1035,6 @@ class Feed
                return $author;
        }
 
-       /**
-        * Adds an entry element to the XML document
-        *
-        * @param DOMDocument $doc       XML document
-        * @param array       $item      Data of the item that is to be posted
-        * @param array       $owner     Contact data of the poster
-        * @param bool        $toplevel  optional default false
-        *
-        * @return \DOMElement Entry element
-        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
-        * @throws \ImagickException
-        */
-       private static function entry(DOMDocument $doc, array $item, array $owner)
-       {
-               $xml = null;
-
-               $repeated_guid = OStatus::getResharedGuid($item);
-               if ($repeated_guid != "") {
-                       $xml = self::reshareEntry($doc, $item, $owner, $repeated_guid);
-               }
-
-               if ($xml) {
-                       return $xml;
-               }
-
-               return self::noteEntry($doc, $item, $owner);
-       }
-
-               /**
-        * Adds an entry element with reshared content
-        *
-        * @param DOMDocument $doc           XML document
-        * @param array       $item          Data of the item that is to be posted
-        * @param array       $owner         Contact data of the poster
-        * @param string      $repeated_guid guid
-        * @param bool        $toplevel      Is it for en entry element (false) or a feed entry (true)?
-        *
-        * @return bool Entry element
-        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
-        * @throws \ImagickException
-        */
-       private static function reshareEntry(DOMDocument $doc, array $item, array $owner, $repeated_guid)
-       {
-               if (($item['gravity'] != GRAVITY_PARENT) && (Strings::normaliseLink($item["author-link"]) != Strings::normaliseLink($owner["url"]))) {
-                       Logger::info('Feed entry author does not match feed owner', ['owner' => $owner["url"], 'author' => $item["author-link"]]);
-               }
-
-               $entry = OStatus::entryHeader($doc, $owner, $item, false);
-
-               $condition = ['uid' => $owner["uid"], 'guid' => $repeated_guid, 'private' => [Item::PUBLIC, Item::UNLISTED],
-                       'network' => Protocol::FEDERATED];
-               $repeated_item = Post::selectFirst(Item::DELIVER_FIELDLIST, $condition);
-               if (!DBA::isResult($repeated_item)) {
-                       return false;
-               }
-
-               self::entryContent($doc, $entry, $item, self::getTitle($repeated_item), Activity::SHARE, false);
-
-               self::entryFooter($doc, $entry, $item, $owner);
-
-               return $entry;
-       }
-
        /**
         * Adds a regular entry element
         *