]> git.mxchange.org Git - friendica.git/commitdiff
The "[attach]" field is replaced by the "post-media" table
authorMichael <heluecht@pirati.ca>
Fri, 6 Nov 2020 04:14:29 +0000 (04:14 +0000)
committerMichael <heluecht@pirati.ca>
Fri, 6 Nov 2020 04:14:29 +0000 (04:14 +0000)
src/Model/Item.php
src/Model/Post/Media.php
src/Module/Diaspora/Fetch.php
src/Protocol/ActivityPub/Transmitter.php
src/Protocol/DFRN.php
src/Protocol/Diaspora.php
src/Protocol/OStatus.php

index 088f783d6ac61404bb2ffb6574fbc87163170f1a..25af1ce5429d4648fb7ce8c7c55c286264a316d7 100644 (file)
@@ -3651,12 +3651,10 @@ class Item
 
                $as = '';
                $vhead = false;
-               $matches = [];
-               preg_match_all('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\"(?: title=\"(.*?)\")?|', $item['attach'], $matches, PREG_SET_ORDER);
-               foreach ($matches as $mtch) {
-                       $mime = $mtch[3];
+               foreach (Post\Media::getByURIId($item['uri-id'], [Post\Media::DOCUMENT, Post\Media::TORRENT, Post\Media::UNKNOWN]) as $attachment) {
+                       $mime = $attachment['mimetype'];
 
-                       $the_url = Contact::magicLinkById($item['author-id'], $mtch[1]);
+                       $the_url = Contact::magicLinkById($item['author-id'], $attachment['url']);
 
                        if (strpos($mime, 'video') !== false) {
                                if (!$vhead) {
@@ -3683,8 +3681,8 @@ class Item
                                $filesubtype = 'unkn';
                        }
 
-                       $title = Strings::escapeHtml(trim(($mtch[4] ?? '') ?: $mtch[1]));
-                       $title .= ' ' . $mtch[2] . ' ' . DI::l10n()->t('bytes');
+                       $title = Strings::escapeHtml(trim(($attachment['description'] ?? '') ?: $attachment['url']));
+                       $title .= ' ' . ($attachment['size'] ?? 0) . ' ' . DI::l10n()->t('bytes');
 
                        $icon = '<div class="attachtype icon s22 type-' . $filetype . ' subtype-' . $filesubtype . '"></div>';
                        $as .= '<a href="' . strip_tags($the_url) . '" title="' . $title . '" class="attachlink" target="_blank" rel="noopener noreferrer" >' . $icon . '</a>';
index 57668fa99e37413eb0b5200027e04619a24d31c5..1a9d4a83ea47c54876521bf61a95209490d74ed9 100644 (file)
@@ -263,11 +263,18 @@ class Media
         * Retrieves the media attachments associated with the provided item ID.
         *
         * @param int $uri_id
+        * @param array $types
         * @return array
         * @throws \Exception
         */
-       public static function getByURIId(int $uri_id)
+       public static function getByURIId(int $uri_id, array $types = [])
        {
-               return DBA::selectToArray('post-media', [], ['uri-id' => $uri_id]);
+               $condition = ['uri-id' => $uri_id];
+
+               if (!empty($types)) {
+                       $condition = DBA::mergeConditions($condition, ['type' => $types]);
+               }
+
+               return DBA::selectToArray('post-media', [], $condition);
        }
 }
index c94badf7e38cbbfca70b8857810b227a6012b1d8..028d7e452e3f010c407fcbec4efc9b9dc8404d12 100644 (file)
@@ -52,7 +52,7 @@ class Fetch extends BaseModule
                // Fetch the item
                $fields = [
                        'uid', 'title', 'body', 'guid', 'contact-id', 'private', 'created', 'received', 'app', 'location', 'coord', 'network',
-                       'event-id', 'resource-id', 'author-link', 'author-avatar', 'author-name', 'plink', 'owner-link', 'attach'
+                       'event-id', 'resource-id', 'author-link', 'author-avatar', 'author-name', 'plink', 'owner-link', 'attach', 'uri-id'
                ];
                $condition = ['wall' => true, 'private' => [Item::PUBLIC, Item::UNLISTED], 'guid' => $guid, 'network' => [Protocol::DFRN, Protocol::DIASPORA]];
                $item = Item::selectFirst($fields, $condition);
index 0044b5240b194049e0bdd2420486d90dd74cb8b3..bb11019b5beff48ad15dcb41d92df84f501962bf 100644 (file)
@@ -37,13 +37,13 @@ use Friendica\Model\Item;
 use Friendica\Model\ItemURI;
 use Friendica\Model\Profile;
 use Friendica\Model\Photo;
+use Friendica\Model\Post;
 use Friendica\Model\Tag;
 use Friendica\Model\User;
 use Friendica\Protocol\Activity;
 use Friendica\Protocol\ActivityPub;
 use Friendica\Util\DateTimeFormat;
 use Friendica\Util\HTTPSignature;
-use Friendica\Util\Images;
 use Friendica\Util\JsonLD;
 use Friendica\Util\LDSignature;
 use Friendica\Util\Map;
@@ -1220,57 +1220,22 @@ class Transmitter
                        $attachments[] = $attachment;
                }
                */
-               $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;
-                               }
-                       }
+               foreach (Post\Media::getByURIId($item['uri-id'], [Post\Media::DOCUMENT, Post\Media::TORRENT, Post\Media::UNKNOWN]) as $attachment) {
+                       $attachments[] = ['type' => 'Document',
+                               'mediaType' => $attachment['mimetype'],
+                               'url' => $attachment['url'],
+                               'name' => $attachment['description']];
                }
 
                if ($type != 'Note') {
                        return $attachments;
                }
 
-               // Simplify image codes
-               $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $item['body']);
-
-               // Grab all pictures without alternative descriptions and create attachments out of them
-               if (preg_match_all("/\[img\]([^\[\]]*)\[\/img\]/Usi", $body, $pictures)) {
-                       foreach ($pictures[1] as $picture) {
-                               $imgdata = Images::getInfoFromURLCached($picture);
-                               if ($imgdata) {
-                                       $attachments[] = ['type' => 'Document',
-                                               'mediaType' => $imgdata['mime'],
-                                               'url' => $picture,
-                                               'name' => null];
-                               }
-                       }
-               }
-
-               // Grab all pictures with alternative description and create attachments out of them
-               if (preg_match_all("/\[img=([^\[\]]*)\]([^\[\]]*)\[\/img\]/Usi", $body, $pictures, PREG_SET_ORDER)) {
-                       foreach ($pictures as $picture) {
-                               $imgdata = Images::getInfoFromURLCached($picture[1]);
-                               if ($imgdata) {
-                                       $attachments[] = ['type' => 'Document',
-                                               'mediaType' => $imgdata['mime'],
-                                               'url' => $picture[1],
-                                               'name' => $picture[2]];
-                               }
-                       }
+               foreach (Post\Media::getByURIId($item['uri-id'], [Post\Media::AUDIO, Post\Media::IMAGE, Post\Media::VIDEO]) as $attachment) {
+                       $attachments[] = ['type' => 'Document',
+                               'mediaType' => $attachment['mimetype'],
+                               'url' => $attachment['url'],
+                               'name' => $attachment['description']];
                }
 
                return $attachments;
@@ -1454,16 +1419,16 @@ class Transmitter
 
                $body = $item['body'];
 
-               if (empty($item['uid']) || !Feature::isEnabled($item['uid'], 'explicit_mentions')) {
-                       $body = self::prependMentions($body, $item['uri-id']);
-               }
-
                if ($type == 'Note') {
-                       $body = self::removePictures($body);
+                       $body = $item['raw-body'] ?? self::removePictures($body);
                } elseif (($type == 'Article') && empty($data['summary'])) {
                        $data['summary'] = BBCode::toPlaintext(Plaintext::shorten(self::removePictures($body), 1000));
                }
 
+               if (empty($item['uid']) || !Feature::isEnabled($item['uid'], 'explicit_mentions')) {
+                       $body = self::prependMentions($body, $item['uri-id']);
+               }
+
                if ($type == 'Event') {
                        $data = array_merge($data, self::createEvent($item));
                } else {
index 2f5cd2effd1bb3ac7ab73d7f0745158370accd81..629962b97bd35f7ebc8c47633d5838dcc42e1ff6 100644 (file)
@@ -866,27 +866,19 @@ class DFRN
         */
        private static function getAttachment($doc, $root, $item)
        {
-               $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 = ["rel" => "enclosure",
-                                                       "href" => $matches[1],
-                                                       "type" => $matches[3]];
-
-                                       if (intval($matches[2])) {
-                                               $attributes["length"] = intval($matches[2]);
-                                       }
-
-                                       if (trim($matches[4]) != "") {
-                                               $attributes["title"] = trim($matches[4]);
-                                       }
+               foreach (Post\Media::getByURIId($item['uri-id'], [Post\Media::DOCUMENT, Post\Media::TORRENT, Post\Media::UNKNOWN]) as $attachment) {
+                       $attributes = ['rel' => 'enclosure',
+                               'href' => $attachment['url'],
+                               'type' => $attachment['mimetype']];
 
-                                       XML::addElement($doc, $root, "link", "", $attributes);
-                               }
+                       if (!empty($attachment['size'])) {
+                               $attributes['length'] = intval($attachment['size']);
+                       }
+                       if (!empty($attachment['description'])) {
+                               $attributes['title'] = $attachment['description'];
                        }
+
+                       XML::addElement($doc, $root, 'link', '', $attributes);
                }
        }
 
index c9a02a4fce28ec903b96f178ac513ba15d29d590..1077238f711c6961e9a12b37e58d5df8e9d307e6 100644 (file)
@@ -3578,13 +3578,11 @@ class Diaspora
                                $body = "### ".html_entity_decode($title)."\n\n".$body;
                        }
 
-                       if ($item["attach"]) {
-                               $cnt = preg_match_all('/href=\"(.*?)\"(.*?)title=\"(.*?)\"/ism', $item["attach"], $matches, PREG_SET_ORDER);
-                               if ($cnt) {
-                                       $body .= "\n".DI::l10n()->t("Attachments:")."\n";
-                                       foreach ($matches as $mtch) {
-                                               $body .= "[".$mtch[3]."](".$mtch[1].")\n";
-                                       }
+                       $attachments = Post\Media::getByURIId($item['uri-id'], [Post\Media::DOCUMENT, Post\Media::TORRENT, Post\Media::UNKNOWN]);
+                       if (!empty($attachments)) {
+                               $body .= "\n".DI::l10n()->t("Attachments:")."\n";
+                               foreach ($attachments as $attachment) {
+                                       $body .= "[" . $attachment['description'] . "](" . $attachment['url'] . ")\n";
                                }
                        }
 
index 5c157c98052fc3fc9f176c689866e7fccacf5204..e27e34b6b44c40e3ed136223d12266ea55268baf 100644 (file)
@@ -1392,25 +1392,19 @@ class OStatus
                        }
                }
 
-               $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 = ["rel" => "enclosure",
-                                                       "href" => $matches[1],
-                                                       "type" => $matches[3]];
-
-                                       if (intval($matches[2])) {
-                                               $attributes["length"] = intval($matches[2]);
-                                       }
-                                       if (trim($matches[4]) != "") {
-                                               $attributes["title"] = trim($matches[4]);
-                                       }
-                                       XML::addElement($doc, $root, "link", "", $attributes);
-                               }
+               foreach (Post\Media::getByURIId($item['uri-id'], [Post\Media::DOCUMENT, Post\Media::TORRENT, Post\Media::UNKNOWN]) as $attachment) {
+                       $attributes = ['rel' => 'enclosure',
+                               'href' => $attachment['url'],
+                               'type' => $attachment['mimetype']];
+
+                       if (!empty($attachment['size'])) {
+                               $attributes['length'] = intval($attachment['size']);
+                       }
+                       if (!empty($attachment['description'])) {
+                               $attributes['title'] = $attachment['description'];
                        }
+
+                       XML::addElement($doc, $root, 'link', '', $attributes);
                }
        }