2021-05-07 23:28:48 +02:00
< ? php
/**
2023-01-01 15:36:24 +01:00
* @ copyright Copyright ( C ) 2010 - 2023 , 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 ;
2023-01-26 00:03:51 +01:00
use Friendica\Core\Logger ;
2021-05-07 23:28:48 +02:00
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 ;
2023-02-21 21:37:32 +01:00
use Friendica\Object\Api\Mastodon\TimelineOrderByTypes ;
2021-05-07 23:28:48 +02:00
/**
* @ 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 ([
2023-02-22 17:02:52 +01:00
'max_id' => '0' , // Return results older than id
2021-05-19 08:55:08 +02:00
'since_id' => 0 , // Return results newer than id
2023-02-22 17:02:52 +01:00
'min_id' => '0' , // Return results immediately newer than id
2021-05-19 08:55:08 +02:00
'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
2023-02-21 21:37:32 +01:00
'friendica_order' => TimelineOrderByTypes :: ID ,
2021-11-28 00:30:41 +01:00
], $request );
2021-05-07 23:28:48 +02:00
2023-02-22 17:27:54 +01:00
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
2023-02-21 21:37:32 +01:00
$condition = $this -> addPagingConditions ( $request , $condition );
2023-02-22 17:27:54 +01:00
$params = $this -> buildOrderAndLimitParams ( $request );
2023-02-21 21:37:32 +01: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-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 );
2023-01-25 07:26:17 +01:00
$display_quotes = self :: appSupportsQuotes ();
2021-05-07 23:28:48 +02:00
$statuses = [];
while ( $item = Post :: fetch ( $items )) {
2023-01-26 00:03:51 +01:00
try {
2023-02-21 21:37:32 +01:00
$status = DI :: mstdnStatus () -> createFromUriId ( $item [ 'uri-id' ], $uid , $display_quotes );
switch ( $request [ 'friendica_order' ]) {
2023-02-22 17:02:52 +01:00
case TimelineOrderByTypes :: CREATED :
2023-02-21 21:37:32 +01:00
self :: setBoundaries ( $status -> createdAtTimestamp ());
break ;
case TimelineOrderByTypes :: ID :
default :
self :: setBoundaries ( $item [ 'uri-id' ]);
}
$statuses [] = $status ;
2023-01-26 00:03:51 +01:00
} catch ( \Throwable $th ) {
Logger :: info ( 'Post not fetchable' , [ 'uri-id' => $item [ 'uri-id' ], 'uid' => $uid , 'error' => $th ]);
}
2021-05-07 23:28:48 +02:00
}
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
}
2023-02-21 21:37:32 +01:00
self :: setLinkHeader ( $request [ 'friendica_order' ] != TimelineOrderByTypes :: ID );
2021-05-07 23:28:48 +02:00
System :: jsonExit ( $statuses );
}
}