2020-09-03 20:57:18 +02:00
< ? php
/**
2023-01-01 15:36:24 +01:00
* @ copyright Copyright ( C ) 2010 - 2023 , the Friendica project
2020-09-03 20:57:18 +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 ;
2021-11-08 08:23:18 +01:00
use Friendica\Core\Logger ;
2020-09-05 07:38:12 +02:00
use Friendica\Core\Protocol ;
2020-09-03 20:57:18 +02:00
use Friendica\Core\System ;
use Friendica\Database\DBA ;
use Friendica\DI ;
use Friendica\Model\Item ;
2021-01-16 23:37:27 +01:00
use Friendica\Model\Post ;
2020-09-03 20:57:18 +02:00
use Friendica\Module\BaseApi ;
use Friendica\Network\HTTPException ;
/**
* @ see https :// docs . joinmastodon . org / methods / timelines /
*/
class PublicTimeline extends BaseApi
{
/**
* @ throws HTTPException\InternalServerErrorException
*/
2021-11-20 15:38:03 +01:00
protected function rawContent ( array $request = [])
2020-09-03 20:57:18 +02:00
{
2021-05-23 10:56:24 +02:00
$uid = self :: getCurrentUserID ();
2021-11-28 13:22:27 +01:00
$request = $this -> getRequest ([
2021-05-19 08:55:08 +02:00
'local' => false , // Show only local statuses? Defaults to false.
'remote' => false , // Show only remote statuses? Defaults to false.
'only_media' => false , // Show only statuses with media attached? Defaults to false.
'max_id' => 0 , // Return results older than this id
'since_id' => 0 , // Return results newer than this id
'min_id' => 0 , // Return results immediately newer than this id
'limit' => 20 , // Maximum number of results to return. Defaults to 20.
'with_muted' => false , // Pleroma extension: return activities by muted (not by blocked!) users.
'exclude_replies' => false , // Don't show comments
2021-11-28 00:30:41 +01:00
], $request );
2020-09-03 20:57:18 +02:00
2021-05-18 21:26:46 +02:00
$params = [ 'order' => [ 'uri-id' => true ], 'limit' => $request [ 'limit' ]];
2020-09-05 07:38:12 +02:00
2022-09-12 23:12:11 +02:00
$condition = [ 'gravity' => [ Item :: GRAVITY_PARENT , Item :: GRAVITY_COMMENT ], 'private' => Item :: PUBLIC ,
2022-11-08 22:48:42 +01:00
'network' => Protocol :: FEDERATED , 'author-blocked' => false , 'author-hidden' => false ];
2020-09-06 09:12:04 +02:00
2021-05-18 21:26:46 +02:00
if ( $request [ 'local' ]) {
2021-02-22 20:47:08 +01:00
$condition = DBA :: mergeConditions ( $condition , [ " `uri-id` IN (SELECT `uri-id` FROM `post-user` WHERE `origin`) " ]);
2020-09-03 20:57:18 +02:00
}
2021-05-18 21:26:46 +02:00
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-view`.`uri-id`) " ]);
2020-09-05 07:38:12 +02:00
}
2021-05-18 21:26:46 +02:00
if ( $request [ 'only_media' ]) {
2021-05-08 03:52:23 +02:00
$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 ]);
}
2021-05-18 21:26:46 +02:00
if ( ! empty ( $request [ 'max_id' ])) {
$condition = DBA :: mergeConditions ( $condition , [ " `uri-id` < ? " , $request [ 'max_id' ]]);
2020-09-03 20:57:18 +02:00
}
2021-05-18 21:26:46 +02:00
if ( ! empty ( $request [ 'since_id' ])) {
$condition = DBA :: mergeConditions ( $condition , [ " `uri-id` > ? " , $request [ 'since_id' ]]);
2020-09-03 20:57:18 +02:00
}
2021-05-18 21:26:46 +02:00
if ( ! empty ( $request [ 'min_id' ])) {
$condition = DBA :: mergeConditions ( $condition , [ " `uri-id` > ? " , $request [ 'min_id' ]]);
2020-09-05 07:38:12 +02:00
$params [ 'order' ] = [ 'uri-id' ];
}
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-06-16 12:16:01 +02:00
$items = Post :: selectPostsForUser ( $uid , [ 'uri-id' ], $condition , $params );
2020-09-03 20:57:18 +02:00
2023-01-25 07:26:17 +01:00
$display_quotes = self :: appSupportsQuotes ();
2020-09-03 20:57:18 +02:00
$statuses = [];
2021-01-17 09:15:18 +01:00
while ( $item = Post :: fetch ( $items )) {
2021-06-16 17:02:33 +02:00
self :: setBoundaries ( $item [ 'uri-id' ]);
2021-11-08 08:23:18 +01:00
try {
2023-01-25 21:14:33 +01:00
$statuses [] = DI :: mstdnStatus () -> createFromUriId ( $item [ 'uri-id' ], $uid , $display_quotes );
2021-11-08 08:23:18 +01:00
} catch ( \Throwable $th ) {
Logger :: info ( 'Post not fetchable' , [ 'uri-id' => $item [ 'uri-id' ], 'uid' => $uid , 'error' => $th ]);
}
2020-09-03 20:57:18 +02:00
}
2020-10-31 23:32:26 +01:00
DBA :: close ( $items );
2020-09-03 20:57:18 +02:00
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 );
2020-09-05 07:38:12 +02:00
}
2021-06-16 17:02:33 +02:00
self :: setLinkHeader ();
2020-09-03 20:57:18 +02:00
System :: jsonExit ( $statuses );
}
}