Improved cards handling, simplified Bearer handling
This commit is contained in:
parent
7d8c152aaf
commit
bade0a2345
|
@ -255,6 +255,14 @@ abstract class DI
|
||||||
return self::$dice->create(Factory\Api\Mastodon\Attachment::class);
|
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
|
* @return Factory\Api\Mastodon\Emoji
|
||||||
*/
|
*/
|
||||||
|
|
78
src/Factory/Api/Mastodon/Card.php
Normal file
78
src/Factory/Api/Mastodon/Card.php
Normal file
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -24,7 +24,6 @@ namespace Friendica\Factory\Api\Mastodon;
|
||||||
use Friendica\App\BaseURL;
|
use Friendica\App\BaseURL;
|
||||||
use Friendica\BaseFactory;
|
use Friendica\BaseFactory;
|
||||||
use Friendica\Content\ContactSelector;
|
use Friendica\Content\ContactSelector;
|
||||||
use Friendica\Content\Text\BBCode;
|
|
||||||
use Friendica\Database\DBA;
|
use Friendica\Database\DBA;
|
||||||
use Friendica\DI;
|
use Friendica\DI;
|
||||||
use Friendica\Model\Post;
|
use Friendica\Model\Post;
|
||||||
|
@ -86,12 +85,10 @@ class Status extends BaseFactory
|
||||||
|
|
||||||
$sensitive = DBA::exists('tag-view', ['uri-id' => $uriId, 'name' => 'nsfw']);
|
$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']));
|
$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);
|
$attachments = DI::mstdnAttachment()->createFromUriId($uriId);
|
||||||
|
|
||||||
if ($item['vid'] == Verb::getID(Activity::ANNOUNCE)) {
|
if ($item['vid'] == Verb::getID(Activity::ANNOUNCE)) {
|
||||||
|
|
|
@ -2927,7 +2927,7 @@ class Item
|
||||||
DI::profiler()->saveTimestamp($stamp1, 'rendering');
|
DI::profiler()->saveTimestamp($stamp1, 'rendering');
|
||||||
|
|
||||||
if (isset($data['url']) && !in_array($data['url'], $ignore_links)) {
|
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']);
|
$parts = parse_url($data['url']);
|
||||||
if (!empty($parts['scheme']) && !empty($parts['host'])) {
|
if (!empty($parts['scheme']) && !empty($parts['host'])) {
|
||||||
if (empty($data['provider_name'])) {
|
if (empty($data['provider_name'])) {
|
||||||
|
|
|
@ -138,15 +138,13 @@ class BaseApi extends BaseModule
|
||||||
*/
|
*/
|
||||||
protected static function login()
|
protected static function login()
|
||||||
{
|
{
|
||||||
$authorization = $_SERVER['HTTP_AUTHORIZATION'] ?? '';
|
if (empty(self::$current_user_id)) {
|
||||||
$authorization = $_SERVER['AUTHORIZATION'] ?? $authorization;
|
self::$current_user_id = self::getUserByBearer();
|
||||||
|
|
||||||
if (self::checkBearer($authorization)) {
|
|
||||||
self::$current_user_id = self::getUserByBearer($authorization);
|
|
||||||
return (bool)self::$current_user_id;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
api_login(DI::app());
|
if (empty(self::$current_user_id)) {
|
||||||
|
api_login(DI::app());
|
||||||
|
}
|
||||||
|
|
||||||
self::$current_user_id = api_user();
|
self::$current_user_id = api_user();
|
||||||
|
|
||||||
|
@ -160,15 +158,11 @@ class BaseApi extends BaseModule
|
||||||
*/
|
*/
|
||||||
protected static function getCurrentUserID()
|
protected static function getCurrentUserID()
|
||||||
{
|
{
|
||||||
$authorization = $_SERVER['HTTP_AUTHORIZATION'] ?? '';
|
if (empty(self::$current_user_id)) {
|
||||||
$authorization = $_SERVER['AUTHORIZATION'] ?? $authorization;
|
self::$current_user_id = self::getUserByBearer();
|
||||||
|
|
||||||
if (self::checkBearer($authorization)) {
|
|
||||||
self::$current_user_id = self::getUserByBearer($authorization);
|
|
||||||
return (int)self::$current_user_id;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_null(self::$current_user_id)) {
|
if (empty(self::$current_user_id)) {
|
||||||
api_login(DI::app(), false);
|
api_login(DI::app(), false);
|
||||||
|
|
||||||
self::$current_user_id = api_user();
|
self::$current_user_id = api_user();
|
||||||
|
@ -177,14 +171,16 @@ class BaseApi extends BaseModule
|
||||||
return (int)self::$current_user_id;
|
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)
|
if (substr($authorization, 0, 7) != 'Bearer ') {
|
||||||
{
|
return 0;
|
||||||
$bearer = trim(substr($authorization, 6));
|
}
|
||||||
|
|
||||||
|
$bearer = trim(substr($authorization, 7));
|
||||||
$condition = ['access_token' => $bearer];
|
$condition = ['access_token' => $bearer];
|
||||||
$token = DBA::selectFirst('application-token', ['uid'], $condition);
|
$token = DBA::selectFirst('application-token', ['uid'], $condition);
|
||||||
if (!DBA::isResult($token)) {
|
if (!DBA::isResult($token)) {
|
||||||
|
|
|
@ -39,9 +39,17 @@ class Card extends BaseDataTransferObject
|
||||||
/** @var string */
|
/** @var string */
|
||||||
protected $type;
|
protected $type;
|
||||||
/** @var string */
|
/** @var string */
|
||||||
|
protected $author_name;
|
||||||
|
/** @var string */
|
||||||
|
protected $author_url;
|
||||||
|
/** @var string */
|
||||||
protected $provider_name;
|
protected $provider_name;
|
||||||
/** @var string */
|
/** @var string */
|
||||||
protected $provider_url;
|
protected $provider_url;
|
||||||
|
/** @var int */
|
||||||
|
protected $width;
|
||||||
|
/** @var int */
|
||||||
|
protected $height;
|
||||||
/** @var string */
|
/** @var string */
|
||||||
protected $image;
|
protected $image;
|
||||||
|
|
||||||
|
@ -57,9 +65,13 @@ class Card extends BaseDataTransferObject
|
||||||
$this->title = $attachment['title'] ?? '';
|
$this->title = $attachment['title'] ?? '';
|
||||||
$this->description = $attachment['description'] ?? '';
|
$this->description = $attachment['description'] ?? '';
|
||||||
$this->type = $attachment['type'] ?? '';
|
$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_name = $attachment['provider_name'] ?? '';
|
||||||
$this->provider_url = $attachment['provider_url'] ?? '';
|
$this->provider_url = $attachment['provider_url'] ?? '';
|
||||||
|
$this->width = $attachment['width'] ?? 0;
|
||||||
|
$this->height = $attachment['height'] ?? 0;
|
||||||
|
$this->image = $attachment['image'] ?? '';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -134,7 +134,7 @@ class Status extends BaseDataTransferObject
|
||||||
$this->mentions = $mentions;
|
$this->mentions = $mentions;
|
||||||
$this->tags = $tags;
|
$this->tags = $tags;
|
||||||
$this->emojis = [];
|
$this->emojis = [];
|
||||||
//$this->card = $card;
|
$this->card = $card->toArray() ?: null;
|
||||||
$this->poll = null;
|
$this->poll = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue