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 ;
use Friendica\Core\Protocol ;
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 Tag 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-14 23:19:25 +01:00
if ( empty ( $this -> parameters [ 'hashtag' ])) {
2021-05-12 16:00:15 +02:00
DI :: mstdnError () -> UnprocessableEntity ();
2021-05-07 23:28:48 +02:00
}
2021-06-07 09:45:20 +02:00
/**
* @ todo Respect missing parameters
* @ see https :// github . com / tootsuite / mastodon / blob / main / app / controllers / api / v1 / timelines / tag_controller . rb
2023-01-01 15:36:24 +01:00
*
2021-06-07 09:45:20 +02:00
* There seem to be the parameters " any " , " all " , and " none " .
*/
2021-11-28 13:22:27 +01:00
$request = $this -> getRequest ([
2021-05-19 08:55:08 +02:00
'local' => false , // If true, return only local statuses. Defaults to false.
'remote' => false , // Show only remote statuses? Defaults to false.
'only_media' => false , // If true, return only statuses with media attachments. 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 );
2021-05-19 08:18:42 +02:00
$params = [ 'order' => [ 'uri-id' => true ], 'limit' => $request [ 'limit' ]];
2021-05-07 23:28:48 +02:00
$condition = [ " `name` = ? AND (`uid` = ? OR (`uid` = ? AND NOT `global`))
AND ( `network` IN ( ? , ? , ? , ? ) OR ( `uid` = ? AND `uid` != ? )) " ,
2021-11-14 23:19:25 +01:00
$this -> parameters [ 'hashtag' ], 0 , $uid , Protocol :: ACTIVITYPUB , Protocol :: DFRN , Protocol :: DIASPORA , Protocol :: OSTATUS , $uid , 0 ];
2021-05-07 23:28:48 +02:00
2021-05-19 08:18:42 +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 [ '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` = `tag-search-view`.`uri-id`) " ]);
2021-05-19 08:18:42 +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-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-19 08:18:42 +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-19 08:18:42 +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-19 08:18:42 +02:00
if ( ! empty ( $request [ 'min_id' ])) {
$condition = DBA :: mergeConditions ( $condition , [ " `uri-id` > ? " , $request [ 'min_id' ]]);
2021-05-07 23:33:42 +02:00
2021-05-07 23:28:48 +02:00
$params [ 'order' ] = [ 'uri-id' ];
}
$items = DBA :: select ( 'tag-search-view' , [ '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-19 08:18:42 +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 );
}
}