diff --git a/src/DI.php b/src/DI.php index 37091a5ab..9e77943bd 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 000000000..cc160b102 --- /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 310fa26bb..c26b9aa72 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 ab99e8c1a..e0e4561ae 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 f79832026..bd42e373d 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 6abf58fdc..b18ba28e4 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 26c0705bd..227395d17 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; }