2021-05-07 23:28:48 +02:00
< ? php
/**
2022-01-02 08:27:47 +01:00
* @ copyright Copyright ( C ) 2010 - 2022 , the Friendica project
2021-05-07 23:28:48 +02:00
*
* @ 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 ;
2022-09-12 23:12:11 +02:00
use Friendica\Model\Item ;
2021-05-07 23:28:48 +02:00
use Friendica\Model\Post ;
use Friendica\Module\BaseApi ;
use Friendica\Network\HTTPException ;
/**
* @ see https :// docs . joinmastodon . org / methods / timelines /
*/
class Home extends BaseApi
{
/**
* @ throws HTTPException\InternalServerErrorException
*/
2021-11-20 15:38:03 +01:00
protected function rawContent ( array $request = [])
2021-05-07 23:28:48 +02:00
{
2021-06-08 14:00:22 +02:00
self :: checkAllowedScope ( self :: SCOPE_READ );
2021-05-07 23:28:48 +02:00
$uid = self :: getCurrentUserID ();
2021-11-28 13:22:27 +01:00
$request = $this -> getRequest ([
2021-05-19 08:55:08 +02:00
'max_id' => 0 , // Return results older than id
'since_id' => 0 , // Return results newer than id
'min_id' => 0 , // Return results immediately newer than id
'limit' => 20 , // Maximum number of results to return. Defaults to 20.
'local' => false , // Return only local statuses?
'with_muted' => false , // Pleroma extension: return activities by muted (not by blocked!) users.
'only_media' => false , // Show only statuses with media attached? Defaults to false.
'remote' => false , // Show only remote statuses? Defaults to false.
'exclude_replies' => false , // Don't show comments
2021-11-28 00:30:41 +01:00
], $request );
2021-05-07 23:28:48 +02:00
2021-05-18 21:26:46 +02:00
$params = [ 'order' => [ 'uri-id' => true ], 'limit' => $request [ 'limit' ]];
2021-05-07 23:28:48 +02:00
2022-09-12 23:12:11 +02:00
$condition = [ 'gravity' => [ Item :: GRAVITY_PARENT , Item :: GRAVITY_COMMENT ], 'uid' => $uid ];
2021-05-07 23:28:48 +02:00
2021-05-18 21:26:46 +02:00
if ( $request [ 'local' ]) {
2021-05-07 23:28:48 +02:00
$condition = DBA :: mergeConditions ( $condition , [ " `uri-id` IN (SELECT `uri-id` FROM `post-user` WHERE `origin`) " ]);
}
2021-05-18 21:26:46 +02:00
if ( ! empty ( $request [ 'max_id' ])) {
$condition = DBA :: mergeConditions ( $condition , [ " `uri-id` < ? " , $request [ 'max_id' ]]);
2021-05-07 23:28:48 +02:00
}
2021-05-18 21:26:46 +02:00
if ( ! empty ( $request [ 'since_id' ])) {
$condition = DBA :: mergeConditions ( $condition , [ " `uri-id` > ? " , $request [ 'since_id' ]]);
2021-05-07 23:28:48 +02:00
}
2021-05-18 21:26:46 +02:00
if ( ! empty ( $request [ 'min_id' ])) {
$condition = DBA :: mergeConditions ( $condition , [ " `uri-id` > ? " , $request [ 'min_id' ]]);
2021-05-07 23:35:07 +02:00
2021-05-07 23:28:48 +02:00
$params [ 'order' ] = [ 'uri-id' ];
}
2021-05-19 08:18:42 +02:00
if ( $request [ 'only_media' ]) {
$condition = DBA :: mergeConditions ( $condition , [ " `uri-id` IN (SELECT `uri-id` FROM `post-media` WHERE `type` IN (?, ?, ?)) " ,
Post\Media :: AUDIO , Post\Media :: IMAGE , Post\Media :: VIDEO ]);
}
if ( $request [ 'remote' ]) {
2022-11-01 07:02:44 +01:00
$condition = DBA :: mergeConditions ( $condition , [ " NOT `uri-id` IN (SELECT `uri-id` FROM `post-user` WHERE `origin` AND `post-user`.`uri-id` = `post-user-view`.`uri-id`) " ]);
2021-05-19 08:18:42 +02:00
}
2021-05-19 08:55:08 +02:00
if ( $request [ 'exclude_replies' ]) {
2022-09-12 23:12:11 +02:00
$condition = DBA :: mergeConditions ( $condition , [ 'gravity' => Item :: GRAVITY_PARENT ]);
2021-05-19 08:55:08 +02:00
}
2021-05-07 23:28:48 +02:00
$items = Post :: selectForUser ( $uid , [ 'uri-id' ], $condition , $params );
$statuses = [];
while ( $item = Post :: fetch ( $items )) {
2021-06-16 17:02:33 +02:00
self :: setBoundaries ( $item [ 'uri-id' ]);
2021-05-07 23:28:48 +02:00
$statuses [] = DI :: mstdnStatus () -> createFromUriId ( $item [ 'uri-id' ], $uid );
}
DBA :: close ( $items );
2021-05-18 21:26:46 +02:00
if ( ! empty ( $request [ 'min_id' ])) {
2021-12-05 07:22:04 +01:00
$statuses = array_reverse ( $statuses );
2021-05-07 23:28:48 +02:00
}
2021-06-16 17:02:33 +02:00
self :: setLinkHeader ();
2021-05-07 23:28:48 +02:00
System :: jsonExit ( $statuses );
}
}