Merge pull request #9128 from annando/api-status

API: New entity "status"
This commit is contained in:
Hypolite Petovan 2020-09-03 15:30:26 -04:00 committed by GitHub
commit 35f68f4bd5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 297 additions and 7 deletions

View file

@ -279,6 +279,14 @@ abstract class DI
return self::$dice->create(Factory\Api\Mastodon\Relationship::class);
}
/**
* @return Factory\Api\Mastodon\Status
*/
public static function mstdnStatus()
{
return self::$dice->create(Factory\Api\Mastodon\Status::class);
}
/**
* @return Factory\Api\Twitter\User
*/

View file

@ -0,0 +1,64 @@
<?php
/**
* @copyright Copyright (C) 2020, Friendica
*
* @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\App\BaseURL;
use Friendica\BaseFactory;
use Friendica\DI;
use Friendica\Model\Item;
use Friendica\Network\HTTPException;
use Friendica\Repository\ProfileField;
use Psr\Log\LoggerInterface;
class Status extends BaseFactory
{
/** @var BaseURL */
protected $baseUrl;
/** @var ProfileField */
protected $profileField;
/** @var Field */
protected $mstdnField;
public function __construct(LoggerInterface $logger, BaseURL $baseURL, ProfileField $profileField, Field $mstdnField)
{
parent::__construct($logger);
$this->baseUrl = $baseURL;
$this->profileField = $profileField;
$this->mstdnField = $mstdnField;
}
/**
* @param int $uriId Uri-ID of the item
* @param int $uid Item user
* @return \Friendica\Object\Api\Mastodon\Status
* @throws HTTPException\InternalServerErrorException
* @throws \ImagickException
*/
public function createFromUriId(int $uriId, $uid = 0)
{
$item = Item::selectFirst([], ['uri-id' => $uriId, 'uid' => $uid]);
$account = DI::mstdnAccount()->createFromContactId($item['author-id']);
return new \Friendica\Object\Api\Mastodon\Status($item, $account);
}
}

View file

@ -685,7 +685,7 @@ class Item
'writable', 'self', 'id' => 'cid', 'alias', 'uid' => 'contact-uid',
'photo', 'name-date', 'uri-date', 'avatar-date', 'thumb', 'dfrn-id'];
$fields['parent-item'] = ['guid' => 'parent-guid', 'network' => 'parent-network'];
$fields['parent-item'] = ['guid' => 'parent-guid', 'network' => 'parent-network', 'author-id' => 'parent-author-id'];
$fields['parent-item-author'] = ['url' => 'parent-author-link', 'name' => 'parent-author-name',
'network' => 'parent-author-network'];

View file

@ -0,0 +1,81 @@
<?php
/**
* @copyright Copyright (C) 2020, Friendica
*
* @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\Module\Api\Mastodon\Timelines;
use Friendica\Core\System;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Model\Item;
use Friendica\Module\BaseApi;
use Friendica\Network\HTTPException;
/**
* @see https://docs.joinmastodon.org/methods/timelines/
*/
class PublicTimeline extends BaseApi
{
/**
* @param array $parameters
* @throws HTTPException\InternalServerErrorException
*/
public static function rawContent(array $parameters = [])
{
// Show only local statuses? Defaults to false.
$local = (bool)!isset($_REQUEST['local']) ? false : ($_REQUEST['local'] == 'true');
// Show only remote statuses? Defaults to false.
$remote = (bool)!isset($_REQUEST['remote']) ? false : ($_REQUEST['remote'] == 'true'); // Currently not supported
// Show only statuses with media attached? Defaults to false.
$only_media = (bool)!isset($_REQUEST['only_media']) ? false : ($_REQUEST['only_media'] == 'true'); // Currently not supported
// Return results older than this id
$max_id = (int)!isset($_REQUEST['max_id']) ? 0 : $_REQUEST['max_id'];
// Return results newer than this id
$since_id = (int)!isset($_REQUEST['since_id']) ? 0 : $_REQUEST['since_id'];
// Return results immediately newer than this id
$min_id = (int)!isset($_REQUEST['min_id']) ? 0 : $_REQUEST['min_id']; // Currently not supported
// Maximum number of results to return. Defaults to 20.
$limit = (int)!isset($_REQUEST['limit']) ? 20 : $_REQUEST['limit'];
$condition = ['gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT], 'private' => Item::PUBLIC];
if ($local) {
$condition['origin'] = true;
} else {
$condition['uid'] = 0;
}
if (!empty($max_id)) {
$condition = DBA::mergeConditions($condition, ["`id` <= ?", $max_id]);
}
if (!empty($since_id)) {
$condition = DBA::mergeConditions($condition, ["`id` >= ?", $since_id]);
}
$items = Item::selectForUser(0, ['uri-id', 'uid'], $condition, ['order' => ['id' => true], 'limit' => $limit]);
$statuses = [];
foreach ($items as $item) {
$statuses[] = DI::mstdnStatus()->createFromUriId($item['uri-id'], $item['uid']);
}
System::jsonExit($statuses);
}
}

View file

@ -0,0 +1,136 @@
<?php
/**
* @copyright Copyright (C) 2020, Friendica
*
* @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\Object\Api\Mastodon;
use Friendica\BaseEntity;
use Friendica\Content\Text\BBCode;
use Friendica\Util\DateTimeFormat;
/**
* Class Status
*
* @see https://docs.joinmastodon.org/entities/status
*/
class Status extends BaseEntity
{
/** @var string */
protected $id;
/** @var string (Datetime) */
protected $created_at;
/** @var string|null */
protected $in_reply_to_id = null;
/** @var string|null */
protected $in_reply_to_account_id = null;
/** @var bool */
protected $sensitive = false;
/** @var string */
protected $spoiler_text = "";
/** @var string (Enum of public, unlisted, private, direct)*/
protected $visibility;
/** @var string|null */
protected $language = null;
/** @var string */
protected $uri;
/** @var string|null (URL)*/
protected $url = null;
/** @var int */
protected $replies_count = 0;
/** @var int */
protected $reblogs_count = 0;
/** @var int */
protected $favourites_count = 0;
/** @var bool */
protected $favourited = false;
/** @var bool */
protected $reblogged = false;
/** @var bool */
protected $muted = false;
/** @var bool */
protected $bookmarked = false;
/** @var bool */
protected $pinned = false;
/** @var string */
protected $content;
/** @var Status|null */
protected $reblog = null;
/** @var Application */
protected $application = null;
/** @var Account */
protected $account;
/** @var Attachment */
protected $media_attachments = [];
/** @var Mention */
protected $mentions = [];
/** @var Tag */
protected $tags = [];
/** @var Emoji[] */
protected $emojis = [];
/** @var Card|null */
protected $card = null;
/** @var Poll|null */
protected $poll = null;
/**
* Creates a status record from an item record.
*
* @param array $item
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public function __construct(array $item, Account $account)
{
$this->id = (string)$item['uri-id'];
$this->created_at = DateTimeFormat::utc($item['created'], DateTimeFormat::ATOM);
if ($item['gravity'] == GRAVITY_COMMENT) {
$this->in_reply_to_id = (string)$item['thr-parent-id'];
$this->in_reply_to_account_id = (string)$item['parent-author-id'];
}
$this->sensitive = false;
$this->spoiler_text = $item['title'];
$visibility = ['public', 'private', 'unlisted'];
$this->visibility = $visibility[$item['private']];
$this->language = null;
$this->uri = $item['uri'];
$this->url = $item['plink'] ?? null;
$this->replies_count = 0;
$this->reblogs_count = 0;
$this->favourites_count = 0;
$this->favourited = false;
$this->reblogged = false;
$this->muted = false;
$this->bookmarked = false;
$this->pinned = false;
$this->content = BBCode::convert($item['body'], false);
$this->reblog = null;
$this->application = null;
$this->account = $account->toArray();
$this->media_attachments = [];
$this->mentions = [];
$this->tags = [];
$this->emojis = [];
$this->card = null;
$this->poll = null;
}
}

View file

@ -48,12 +48,13 @@ return [
'/api' => [
'/v1' => [
'/custom_emojis' => [Module\Api\Mastodon\CustomEmojis::class, [R::GET ]],
'/directory' => [Module\Api\Mastodon\Directory::class, [R::GET ]],
'/follow_requests' => [Module\Api\Mastodon\FollowRequests::class, [R::GET ]],
'/follow_requests/{id:\d+}/{action}' => [Module\Api\Mastodon\FollowRequests::class, [ R::POST]],
'/instance' => [Module\Api\Mastodon\Instance::class, [R::GET ]],
'/instance/peers' => [Module\Api\Mastodon\Instance\Peers::class, [R::GET ]],
'/custom_emojis' => [Module\Api\Mastodon\CustomEmojis::class, [R::GET ]],
'/directory' => [Module\Api\Mastodon\Directory::class, [R::GET ]],
'/follow_requests' => [Module\Api\Mastodon\FollowRequests::class, [R::GET ]],
'/follow_requests/{id:\d+}/{action}' => [Module\Api\Mastodon\FollowRequests::class, [ R::POST]],
'/instance' => [Module\Api\Mastodon\Instance::class, [R::GET ]],
'/instance/peers' => [Module\Api\Mastodon\Instance\Peers::class, [R::GET ]],
'/timelines/public' => [Module\Api\Mastodon\Timelines\PublicTimeline::class, [R::GET ]],
],
'/friendica' => [
'/profile/show' => [Module\Api\Friendica\Profile\Show::class , [R::GET ]],