Skip to content

Commit d9b2960

Browse files
committed
Add hashtag search support.
And check for empty queries so we don't waste time with them.
1 parent 1653069 commit d9b2960

File tree

1 file changed

+40
-3
lines changed

1 file changed

+40
-3
lines changed

includes/class-mastodon-api.php

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1526,7 +1526,9 @@ public function api_tag_timelines( $request ) {
15261526
$args['tag'] = $request->get_param( 'hashtag' );
15271527

15281528
$ppp_param = $request->get_param( 'limit' );
1529-
if( $ppp_param != null ) { $args['posts_per_page'] = $ppp_param; }
1529+
if ( null !== $ppp_param ) {
1530+
$args['posts_per_page'] = $ppp_param;
1531+
}
15301532

15311533
$args = apply_filters( 'mastodon_api_timelines_args', $args, $request );
15321534

@@ -1547,7 +1549,12 @@ public function api_search( $request ) {
15471549
'hashtags' => array(),
15481550
);
15491551

1550-
$q = $request->get_param( 'q' );
1552+
$q = trim( $request->get_param( 'q' ) );
1553+
// Don't allow empty search queries.
1554+
if ( '' === $q ) {
1555+
return $ret;
1556+
}
1557+
15511558
$query_is_url = parse_url( $q );
15521559
if ( $query_is_url ) {
15531560
if ( 'true' !== $request->get_param( 'resolve' ) || ! is_user_logged_in() ) {
@@ -1596,7 +1603,6 @@ public function api_search( $request ) {
15961603
array(
15971604
'id' => $json['id'] . '#create-activity',
15981605
'object' => $json,
1599-
16001606
),
16011607
$user_id
16021608
);
@@ -1609,6 +1615,37 @@ public function api_search( $request ) {
16091615
$ret['statuses'] = array_merge( $ret['statuses'], $this->get_posts( $args ) );
16101616
}
16111617
}
1618+
if ( ! $type || 'hashtags' === $type ) {
1619+
$q_param = $request->get_param( 'q' );
1620+
$categories = get_categories(
1621+
array(
1622+
'orderby' => 'name',
1623+
'hide_empty' => false,
1624+
'search' => $q_param,
1625+
)
1626+
);
1627+
foreach ( $categories as $category ) {
1628+
$ret['hashtags'][] = array(
1629+
'name' => $category->name,
1630+
'url' => get_category_link( $category ),
1631+
'history' => array(),
1632+
);
1633+
}
1634+
$tags = get_tags(
1635+
array(
1636+
'orderby' => 'name',
1637+
'hide_empty' => false,
1638+
'search' => $q_param,
1639+
)
1640+
);
1641+
foreach ( $tags as $tag ) {
1642+
$ret['hashtags'][] = array(
1643+
'name' => $tag->name,
1644+
'url' => get_tag_link( $tag ),
1645+
'history' => array(),
1646+
);
1647+
}
1648+
}
16121649
}
16131650
return $ret;
16141651
}

0 commit comments

Comments
 (0)