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
*/
--- /dev/null
+<?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);
+ }
+}
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;
$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)) {
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'])) {
*/
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();
*/
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();
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)) {
/** @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;
$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'] ?? '';
}
/**
$this->mentions = $mentions;
$this->tags = $tags;
$this->emojis = [];
- //$this->card = $card;
+ $this->card = $card->toArray() ?: null;
$this->poll = null;
}