]> git.mxchange.org Git - friendica.git/commitdiff
Improved cards handling, simplified Bearer handling
authorMichael <heluecht@pirati.ca>
Tue, 11 May 2021 19:15:05 +0000 (19:15 +0000)
committerMichael <heluecht@pirati.ca>
Tue, 11 May 2021 19:15:05 +0000 (19:15 +0000)
src/DI.php
src/Factory/Api/Mastodon/Card.php [new file with mode: 0644]
src/Factory/Api/Mastodon/Status.php
src/Model/Item.php
src/Module/BaseApi.php
src/Object/Api/Mastodon/Card.php
src/Object/Api/Mastodon/Status.php

index 37091a5ab82d5d439f525279975ef054fa508860..9e77943bda8403f707f4bb2216a321a78053af3b 100644 (file)
@@ -255,6 +255,14 @@ abstract class DI
                return self::$dice->create(Factory\Api\Mastodon\Attachment::class);
        }
 
+       /**
+        * @return Factory\Api\Mastodon\Card
+        */
+       public static function mstdnCard()
+       {
+               return self::$dice->create(Factory\Api\Mastodon\Card::class);
+       }
+
        /**
         * @return Factory\Api\Mastodon\Emoji
         */
diff --git a/src/Factory/Api/Mastodon/Card.php b/src/Factory/Api/Mastodon/Card.php
new file mode 100644 (file)
index 0000000..cc160b1
--- /dev/null
@@ -0,0 +1,78 @@
+<?php
+/**
+ * @copyright Copyright (C) 2010-2021, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace Friendica\Factory\Api\Mastodon;
+
+use Friendica\BaseFactory;
+use Friendica\Content\Text\BBCode;
+use Friendica\Model\Post;
+use Friendica\Network\HTTPException;
+use Friendica\Util\Strings;
+
+class Card extends BaseFactory
+{
+       /**
+        * @param int $uriId Uri-ID of the item
+        * @return \Friendica\Object\Api\Mastodon\Card
+        * @throws HTTPException\InternalServerErrorException
+        * @throws \ImagickException
+        */
+       public function createFromUriId(int $uriId)
+       {
+               $item = Post::selectFirst(['nody'], ['uri-id' => $uriId]);
+               if (!empty($item['body'])) {
+                       $data = BBCode::getAttachmentData($item['body']);
+               }
+
+               foreach (Post\Media::getByURIId($uriId, [Post\Media::HTML]) as $attached) {
+                       if ((empty($data['url']) || Strings::compareLink($data['url'], $attached['url'])) &&
+                               (!empty($attached['description']) || !empty($attached['image']) || !empty($attached['preview']))) {
+                               $parts = parse_url($attached['url']);
+                               if (!empty($parts['scheme']) && !empty($parts['host'])) {
+                                       if (empty($attached['publisher-name'])) {
+                                               $attached['publisher-name'] = $parts['host'];
+                                       }
+                                       if (empty($attached['publisher-url']) || empty(parse_url($attached['publisher-url'], PHP_URL_SCHEME))) {
+                                               $attached['publisher-url'] = $parts['scheme'] . '://' . $parts['host'];
+
+                                               if (!empty($parts['port'])) {
+                                                       $attached['publisher-url'] .= ':' . $parts['port'];
+                                               }
+                                       }
+                               }
+
+                               $data['url']           = $attached['url'];
+                               $data['title']         = $attached['name'];
+                               $data['description']   = $attached['description'];
+                               $data['type']          = 'link';
+                               $data['author_name']   = $attached['author-name'];
+                               $data['author_url']    = $attached['author-url'];
+                               $data['provider_name'] = $attached['publisher-name'];
+                               $data['provider_url']  = $attached['publisher-url'];
+                               $data['image']         = $attached['preview'];
+                               $data['width']         = $attached['preview-width'];
+                               $data['height']        = $attached['preview-height'];
+                       }
+               }
+
+               return new \Friendica\Object\Api\Mastodon\Card($data);
+       }
+}
index 310fa26bbea354f0c900bc892885aa7a030e6a7a..c26b9aa72524f006677fe581b89f195ab1235aa5 100644 (file)
@@ -24,7 +24,6 @@ namespace Friendica\Factory\Api\Mastodon;
 use Friendica\App\BaseURL;
 use Friendica\BaseFactory;
 use Friendica\Content\ContactSelector;
-use Friendica\Content\Text\BBCode;
 use Friendica\Database\DBA;
 use Friendica\DI;
 use Friendica\Model\Post;
@@ -86,12 +85,10 @@ class Status extends BaseFactory
 
                $sensitive = DBA::exists('tag-view', ['uri-id' => $uriId, 'name' => 'nsfw']);
                $application = new \Friendica\Object\Api\Mastodon\Application($item['app'] ?: ContactSelector::networkToName($item['network'], $item['author-link']));
-               $mentions = DI::mstdnMention()->createFromUriId($uriId);
-               $tags = DI::mstdnTag()->createFromUriId($uriId);
-
-               $data = BBCode::getAttachmentData($item['body']);
-               $card = new \Friendica\Object\Api\Mastodon\Card($data);
 
+               $mentions    = DI::mstdnMention()->createFromUriId($uriId);
+               $tags        = DI::mstdnTag()->createFromUriId($uriId);
+               $card        = DI::mstdnCard()->createFromUriId($uriId);
                $attachments = DI::mstdnAttachment()->createFromUriId($uriId);
 
                if ($item['vid'] == Verb::getID(Activity::ANNOUNCE)) {
index ab99e8c1a1c804994cb4e41ec7e043af00b6eff3..e0e4561ae50f0e0f7b53e2de2c124a929a5a382c 100644 (file)
@@ -2927,7 +2927,7 @@ class Item
                DI::profiler()->saveTimestamp($stamp1, 'rendering');
 
                if (isset($data['url']) && !in_array($data['url'], $ignore_links)) {
-                       if (!empty($data['description']) || !empty($data['image'] || !empty($data['preview']))) {
+                       if (!empty($data['description']) || !empty($data['image']) || !empty($data['preview'])) {
                                $parts = parse_url($data['url']);
                                if (!empty($parts['scheme']) && !empty($parts['host'])) {
                                        if (empty($data['provider_name'])) {
index f7983202625256a856ce10966ab14328af286348..bd42e373dce200dd8d48ee028f665b26c2dc0a0f 100644 (file)
@@ -138,15 +138,13 @@ class BaseApi extends BaseModule
         */
        protected static function login()
        {
-               $authorization = $_SERVER['HTTP_AUTHORIZATION'] ?? '';
-               $authorization = $_SERVER['AUTHORIZATION'] ?? $authorization;
-
-               if (self::checkBearer($authorization)) {
-                       self::$current_user_id = self::getUserByBearer($authorization);
-                       return (bool)self::$current_user_id;
+               if (empty(self::$current_user_id)) {
+                       self::$current_user_id = self::getUserByBearer();
                }
 
-               api_login(DI::app());
+               if (empty(self::$current_user_id)) {
+                       api_login(DI::app());
+               }
 
                self::$current_user_id = api_user();
 
@@ -160,15 +158,11 @@ class BaseApi extends BaseModule
         */
        protected static function getCurrentUserID()
        {
-               $authorization = $_SERVER['HTTP_AUTHORIZATION'] ?? '';
-               $authorization = $_SERVER['AUTHORIZATION'] ?? $authorization;
-
-               if (self::checkBearer($authorization)) {
-                       self::$current_user_id = self::getUserByBearer($authorization);
-                       return (int)self::$current_user_id;
+               if (empty(self::$current_user_id)) {
+                       self::$current_user_id = self::getUserByBearer();
                }
 
-               if (is_null(self::$current_user_id)) {
+               if (empty(self::$current_user_id)) {
                        api_login(DI::app(), false);
 
                        self::$current_user_id = api_user();
@@ -177,14 +171,16 @@ class BaseApi extends BaseModule
                return (int)self::$current_user_id;
        }
 
-       private static function checkBearer(string $authorization)
+       private static function getUserByBearer()
        {
-               return (substr($authorization, 0, 7) == 'Bearer ');
-       }
+               $authorization = $_SERVER['HTTP_AUTHORIZATION'] ?? '';
+               $authorization = $_SERVER['AUTHORIZATION'] ?? $authorization;
 
-       private static function getUserByBearer(string $authorization)
-       {
-               $bearer = trim(substr($authorization, 6));
+               if (substr($authorization, 0, 7) != 'Bearer ') {
+                       return 0;
+               }
+
+               $bearer = trim(substr($authorization, 7));
                $condition = ['access_token' => $bearer];
                $token = DBA::selectFirst('application-token', ['uid'], $condition);
                if (!DBA::isResult($token)) {
index 6abf58fdc817b83422c75aab03936c567703e3d3..b18ba28e44cc0b83c2a10bc9beedc340406467b9 100644 (file)
@@ -39,9 +39,17 @@ class Card extends BaseDataTransferObject
        /** @var string */
        protected $type;
        /** @var string */
+       protected $author_name;
+       /** @var string */
+       protected $author_url;
+       /** @var string */
        protected $provider_name;
        /** @var string */
        protected $provider_url;
+       /** @var int */
+       protected $width;
+       /** @var int */
+       protected $height;
        /** @var string */
        protected $image;
 
@@ -57,9 +65,13 @@ class Card extends BaseDataTransferObject
                $this->title         = $attachment['title'] ?? '';
                $this->description   = $attachment['description'] ?? '';
                $this->type          = $attachment['type'] ?? '';
-               $this->image         = $attachment['image'] ?? '';
+               $this->author_name   = $attachment['author_name'] ?? '';
+               $this->author_url    = $attachment['author_url'] ?? '';
                $this->provider_name = $attachment['provider_name'] ?? '';
                $this->provider_url  = $attachment['provider_url'] ?? '';
+               $this->width         = $attachment['width'] ?? 0;
+               $this->height        = $attachment['height'] ?? 0;
+               $this->image         = $attachment['image'] ?? '';
        }
 
        /**
index 26c0705bd16a6570ea407e208e8a94eb19a378a8..227395d171bc8a6d29e8f1f36274a4f5745dea1c 100644 (file)
@@ -134,7 +134,7 @@ class Status extends BaseDataTransferObject
                $this->mentions = $mentions;
                $this->tags = $tags;
                $this->emojis = [];
-               //$this->card = $card;
+               $this->card = $card->toArray() ?: null;
                $this->poll = null;
        }