]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/OStatus.php
Merge pull request #13161 from annando/bluesky-activities
[friendica.git] / src / Protocol / OStatus.php
index 6531d75af1d8c765af170f3e02d5f682c8564317..ff8fc3cbea9af9cd5294583e8eba98a42a0c39be 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2022, the Friendica project
+ * @copyright Copyright (C) 2010-2023, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -24,6 +24,7 @@ namespace Friendica\Protocol;
 use DOMDocument;
 use DOMElement;
 use DOMXPath;
+use Friendica\App;
 use Friendica\Content\Text\BBCode;
 use Friendica\Content\Text\HTML;
 use Friendica\Core\Cache\Enum\Duration;
@@ -102,9 +103,9 @@ class OStatus
 */
                if ($aliaslink != '') {
                        $contact = DBA::selectFirst('contact', [], [
-                               "`uid` = ? AND `alias` = ? AND `network` != ? AND `rel` IN (?, ?)",
+                               "`uid` = ? AND `alias` = ? AND `rel` IN (?, ?)",
                                $importer['uid'],
-                               $aliaslink, Protocol::STATUSNET,
+                               $aliaslink,
                                Contact::SHARING, Contact::FRIEND,
                        ]);
                }
@@ -115,11 +116,10 @@ class OStatus
                        }
 
                        $contact = DBA::selectFirst('contact', [], [
-                               "`uid` = ? AND `nurl` IN (?, ?) AND `network` != ? AND `rel` IN (?, ?)",
+                               "`uid` = ? AND `nurl` IN (?, ?) AND `rel` IN (?, ?)",
                                $importer['uid'],
                                Strings::normaliseLink($author['author-link']),
                                Strings::normaliseLink($aliaslink),
-                               Protocol::STATUSNET,
                                Contact::SHARING,
                                Contact::FRIEND,
                        ]);
@@ -127,10 +127,9 @@ class OStatus
 
                if (!DBA::isResult($contact) && ($addr != '')) {
                        $contact = DBA::selectFirst('contact', [], [
-                               "`uid` = ? AND `addr` = ? AND `network` != ? AND `rel` IN (?, ?)",
+                               "`uid` = ? AND `addr` = ? AND `rel` IN (?, ?)",
                                $importer['uid'],
                                $addr,
-                               Protocol::STATUSNET,
                                Contact::SHARING,
                                Contact::FRIEND,
                        ]);
@@ -391,7 +390,7 @@ class OStatus
                $header['network'] = Protocol::OSTATUS;
                $header['wall'] = 0;
                $header['origin'] = 0;
-               $header['gravity'] = GRAVITY_COMMENT;
+               $header['gravity'] = Item::GRAVITY_COMMENT;
 
                if (!is_object($doc->firstChild) || empty($doc->firstChild->tagName)) {
                        return false;
@@ -497,9 +496,9 @@ class OStatus
                                $orig_uri = $xpath->query('activity:object/atom:id', $entry)->item(0)->nodeValue;
                                Logger::notice('Favorite', ['uri' => $orig_uri, 'item' => $item]);
 
-                               $item['verb'] = Activity::LIKE;
+                               $item['body'] = $item['verb'] = Activity::LIKE;
                                $item['thr-parent'] = $orig_uri;
-                               $item['gravity'] = GRAVITY_ACTIVITY;
+                               $item['gravity'] = Item::GRAVITY_ACTIVITY;
                                $item['object-type'] = Activity\ObjectType::NOTE;
                        }
 
@@ -714,7 +713,7 @@ class OStatus
                        }
                } else {
                        $item['thr-parent'] = $item['uri'];
-                       $item['gravity'] = GRAVITY_PARENT;
+                       $item['gravity'] = Item::GRAVITY_PARENT;
                }
 
                self::$itemlist[] = $item;
@@ -938,7 +937,7 @@ class OStatus
                                                break;
 
                                        default:
-                                               Logger::warning('Unsupported rel=' . $attribute['rel'] . ', href=' . $attribute['href'] . ', object-type=' . $item['object-type']);
+                                               Logger::notice('Unsupported rel=' . $attribute['rel'] . ', href=' . $attribute['href'] . ', object-type=' . $item['object-type']);
                                }
                        }
                }
@@ -977,44 +976,6 @@ class OStatus
                }
        }
 
-       /**
-        * Cleans the body of a post if it contains picture links
-        *
-        * @param string $body The body
-        * @param integer $uriId
-        * @return string The cleaned body
-        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
-        */
-       public static function formatPicturePost(string $body, int $uriid): string
-       {
-               $siteinfo = BBCode::getAttachedData($body);
-
-               if (($siteinfo['type'] == 'photo') && (!empty($siteinfo['preview']) || !empty($siteinfo['image']))) {
-                       if (isset($siteinfo['preview'])) {
-                               $preview = $siteinfo['preview'];
-                       } else {
-                               $preview = $siteinfo['image'];
-                       }
-
-                       // Is it a remote picture? Then make a smaller preview here
-                       $preview = Post\Link::getByLink($uriid, $preview, Proxy::SIZE_SMALL);
-
-                       // Is it a local picture? Then make it smaller here
-                       $preview = str_replace(['-0.jpg', '-0.png'], ['-2.jpg', '-2.png'], $preview);
-                       $preview = str_replace(['-1.jpg', '-1.png'], ['-2.jpg', '-2.png'], $preview);
-
-                       if (isset($siteinfo['url'])) {
-                               $url = $siteinfo['url'];
-                       } else {
-                               $url = $siteinfo['image'];
-                       }
-
-                       $body = trim($siteinfo['text']) . ' [url]' . $url . "[/url]\n[img]" . $preview . '[/img]';
-               }
-
-               return $body;
-       }
-
        /**
         * Adds the header elements to the XML document
         *
@@ -1061,9 +1022,9 @@ class OStatus
 
                $attributes = [
                        'uri' => 'https://friendi.ca',
-                       'version' => FRIENDICA_VERSION . '-' . DB_UPDATE_VERSION,
+                       'version' => App::VERSION . '-' . DB_UPDATE_VERSION,
                ];
-               XML::addElement($doc, $root, 'generator', FRIENDICA_PLATFORM, $attributes);
+               XML::addElement($doc, $root, 'generator', App::PLATFORM, $attributes);
                XML::addElement($doc, $root, 'id', DI::baseUrl() . '/profile/' . $owner['nick']);
                XML::addElement($doc, $root, 'title', $title);
                XML::addElement($doc, $root, 'subtitle', sprintf("Updates from %s on %s", $owner['name'], DI::config()->get('config', 'sitename')));
@@ -1141,55 +1102,7 @@ class OStatus
         */
        public static function getAttachment(DOMDocument $doc, DOMElement $root, array $item)
        {
-               $siteinfo = BBCode::getAttachedData($item['body']);
-
-               switch ($siteinfo['type']) {
-                       case 'photo':
-                               if (!empty($siteinfo['image'])) {
-                                       $imgdata = Images::getInfoFromURLCached($siteinfo['image']);
-                                       if ($imgdata) {
-                                               $attributes = [
-                                                       'rel' => 'enclosure',
-                                                       'href' => $siteinfo['image'],
-                                                       'type' => $imgdata['mime'],
-                                                       'length' => intval($imgdata['size']),
-                                               ];
-                                               XML::addElement($doc, $root, 'link', '', $attributes);
-                                       }
-                               }
-                               break;
-
-                       case 'video':
-                               $attributes = [
-                                       'rel' => 'enclosure',
-                                       'href' => $siteinfo['url'],
-                                       'type' => 'text/html; charset=UTF-8',
-                                       'length' => '0',
-                                       'title' => ($siteinfo['title'] ?? '') ?: $siteinfo['url'],
-                               ];
-                               XML::addElement($doc, $root, 'link', '', $attributes);
-                               break;
-
-                       default:
-                               Logger::warning('Unsupported type', ['type' => $siteinfo['type'], 'url' => $siteinfo['url'] ?? '']);
-                               break;
-               }
-
-               if (!DI::config()->get('system', 'ostatus_not_attach_preview') && ($siteinfo['type'] != 'photo') && isset($siteinfo['image'])) {
-                       $imgdata = Images::getInfoFromURLCached($siteinfo['image']);
-                       if ($imgdata) {
-                               $attributes = [
-                                       'rel' => 'enclosure',
-                                       'href' => $siteinfo['image'],
-                                       'type' => $imgdata['mime'],
-                                       'length' => intval($imgdata['size']),
-                               ];
-
-                               XML::addElement($doc, $root, 'link', '', $attributes);
-                       }
-               }
-
-               foreach (Post\Media::getByURIId($item['uri-id'], [Post\Media::DOCUMENT, Post\Media::TORRENT, Post\Media::UNKNOWN]) as $attachment) {
+               foreach (Post\Media::getByURIId($item['uri-id'], [Post\Media::AUDIO, Post\Media::IMAGE, Post\Media::VIDEO, Post\Media::DOCUMENT, Post\Media::TORRENT]) as $attachment) {
                        $attributes = ['rel' => 'enclosure',
                                'href' => $attachment['url'],
                                'type' => $attachment['mimetype']];
@@ -1363,7 +1276,7 @@ class OStatus
         */
        private static function likeEntry(DOMDocument $doc, array $item, array $owner, bool $toplevel): DOMElement
        {
-               if (($item['gravity'] != GRAVITY_PARENT) && (Strings::normaliseLink($item['author-link']) != Strings::normaliseLink($owner['url']))) {
+               if (($item['gravity'] != Item::GRAVITY_PARENT) && (Strings::normaliseLink($item['author-link']) != Strings::normaliseLink($owner['url']))) {
                        Logger::info('OStatus entry is from author ' . $owner['url'] . ' - not from ' . $item['author-link'] . '. Quitting.');
                }
 
@@ -1482,8 +1395,8 @@ class OStatus
                }
 
                $item['uri'] = $item['parent-uri'] = $item['thr-parent']
-                               = 'tag:' . DI::baseUrl()->getHostname().
-                               ','.date('Y-m-d').':'.$action.':'.$owner['uid'].
+                               = 'tag:' . DI::baseUrl()->getHost() .
+                                 ','.date('Y-m-d').':'.$action.':'.$owner['uid'].
                                ':person:'.$connect_id.':'.$item['created'];
 
                $item['body'] = sprintf($message, $owner['nick'], $contact['nick']);
@@ -1513,7 +1426,7 @@ class OStatus
         */
        private static function noteEntry(DOMDocument $doc, array $item, array $owner, bool $toplevel): DOMElement
        {
-               if (($item['gravity'] != GRAVITY_PARENT) && (Strings::normaliseLink($item['author-link']) != Strings::normaliseLink($owner['url']))) {
+               if (($item['gravity'] != Item::GRAVITY_PARENT) && (Strings::normaliseLink($item['author-link']) != Strings::normaliseLink($owner['url']))) {
                        Logger::info('OStatus entry is from author ' . $owner['url'] . ' - not from ' . $item['author-link'] . '. Quitting.');
                }
 
@@ -1556,7 +1469,7 @@ class OStatus
 
                        if ($owner['contact-type'] == Contact::TYPE_COMMUNITY) {
                                $contact = Contact::getByURL($item['author-link']) ?: $owner;
-                               $contact['nickname'] = $contact['nickname'] ?? $contact['nick']; 
+                               $contact['nickname'] = $contact['nickname'] ?? $contact['nick'];
                                $author = self::addAuthor($doc, $contact, false);
                                $entry->appendChild($author);
                        }
@@ -1601,8 +1514,8 @@ class OStatus
                XML::addElement($doc, $entry, 'id', $item['uri']);
                XML::addElement($doc, $entry, 'title', html_entity_decode($title, ENT_QUOTES, 'UTF-8'));
 
-               $body = Post\Media::addAttachmentsToBody($item['uri-id'], $item['body']);
-               $body = self::formatPicturePost($body, $item['uri-id']);
+               $body = Post\Media::addAttachmentsToBody($item['uri-id'], DI::contentItem()->addSharedPost($item));
+               $body = Post\Media::addHTMLLinkToBody($item['uri-id'], $body);
 
                if (!empty($item['title'])) {
                        $body = '[b]' . $item['title'] . "[/b]\n\n" . $body;
@@ -1643,7 +1556,7 @@ class OStatus
        {
                $mentioned = [];
 
-               if ($item['gravity'] != GRAVITY_PARENT) {
+               if ($item['gravity'] != Item::GRAVITY_PARENT) {
                        $parent = Post::selectFirst(['guid', 'author-link', 'owner-link'], ['id' => $item['parent']]);
 
                        $thrparent = Post::selectFirst(['guid', 'author-link', 'owner-link', 'plink'], ['uid' => $owner['uid'], 'uri' => $item['thr-parent']]);
@@ -1796,7 +1709,7 @@ class OStatus
 
                $previous_created = $last_update;
 
-               // Don't cache when the last item was posted less then 15 minutes ago (Cache duration)
+               // Don't cache when the last item was posted less than 15 minutes ago (Cache duration)
                if ((time() - strtotime($owner['last-item'])) < 15*60) {
                        $result = DI::cache()->get($cachekey);
                        if (!$nocache && !is_null($result)) {