From bade0a2345e977854f906529960c55a35a3197da Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 11 May 2021 19:15:05 +0000 Subject: [PATCH] Improved cards handling, simplified Bearer handling --- src/DI.php | 8 +++ src/Factory/Api/Mastodon/Card.php | 78 +++++++++++++++++++++++++++++ src/Factory/Api/Mastodon/Status.php | 9 ++-- src/Model/Item.php | 2 +- src/Module/BaseApi.php | 36 ++++++------- src/Object/Api/Mastodon/Card.php | 14 +++++- src/Object/Api/Mastodon/Status.php | 2 +- 7 files changed, 120 insertions(+), 29 deletions(-) create mode 100644 src/Factory/Api/Mastodon/Card.php diff --git a/src/DI.php b/src/DI.php index 37091a5ab8..9e77943bda 100644 --- a/src/DI.php +++ b/src/DI.php @@ -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 index 0000000000..cc160b1022 --- /dev/null +++ b/src/Factory/Api/Mastodon/Card.php @@ -0,0 +1,78 @@ +. + * + */ + +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); + } +} diff --git a/src/Factory/Api/Mastodon/Status.php b/src/Factory/Api/Mastodon/Status.php index 310fa26bbe..c26b9aa725 100644 --- a/src/Factory/Api/Mastodon/Status.php +++ b/src/Factory/Api/Mastodon/Status.php @@ -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)) { diff --git a/src/Model/Item.php b/src/Model/Item.php index ab99e8c1a1..e0e4561ae5 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -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'])) { diff --git a/src/Module/BaseApi.php b/src/Module/BaseApi.php index f798320262..bd42e373dc 100644 --- a/src/Module/BaseApi.php +++ b/src/Module/BaseApi.php @@ -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)) { diff --git a/src/Object/Api/Mastodon/Card.php b/src/Object/Api/Mastodon/Card.php index 6abf58fdc8..b18ba28e44 100644 --- a/src/Object/Api/Mastodon/Card.php +++ b/src/Object/Api/Mastodon/Card.php @@ -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'] ?? ''; } /** diff --git a/src/Object/Api/Mastodon/Status.php b/src/Object/Api/Mastodon/Status.php index 26c0705bd1..227395d171 100644 --- a/src/Object/Api/Mastodon/Status.php +++ b/src/Object/Api/Mastodon/Status.php @@ -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; } -- 2.39.5