API - /search #6274

- fixed: search result always return empty
- add hashtag search: friendica_tag params
- ApiTest & api.md updated
This commit is contained in:
root 2018-12-16 16:11:55 +00:00 committed by Hypolite Petovan
parent b43dd28b7a
commit 939364f9a0
3 changed files with 49 additions and 22 deletions

View File

@ -751,6 +751,7 @@ Friendica doesn't allow showing followers of other users.
#### Parameters #### Parameters
* q: search query * q: search query
* friendica_tag: search hashtag (optional; query condition only accept one of the two [ q | friendica_tag ] )
* page: the page number (starting at 1) to return * page: the page number (starting at 1) to return
* rpp: the number of statuses to return per page * rpp: the number of statuses to return per page
* count: alias for the rpp parameter * count: alias for the rpp parameter

View File

@ -1519,41 +1519,53 @@ function api_search($type)
$a = \get_app(); $a = \get_app();
$user_info = api_get_user($a); $user_info = api_get_user($a);
if (api_user() === false || $user_info === false) { if (api_user() === false || $user_info === false) { throw new ForbiddenException(); }
throw new ForbiddenException();
} if (empty($_REQUEST['q']) && empty($_REQUEST['friendica_tag'])) { throw new BadRequestException("q or friendica_tag parameter is required."); }
$data = []; $data = [];
$count = 15;
if (empty($_REQUEST['q'])) {
throw new BadRequestException("q parameter is required.");
}
if (!empty($_REQUEST['rpp'])) { if (!empty($_REQUEST['rpp'])) {
$count = $_REQUEST['rpp']; $count = $_REQUEST['rpp'];
} elseif (!empty($_REQUEST['count'])) { } elseif (!empty($_REQUEST['count'])) {
$count = $_REQUEST['count']; $count = $_REQUEST['count'];
} else {
$count = 15;
} }
$since_id = defaults($_REQUEST, 'since_id', 0); $since_id = defaults($_REQUEST, 'since_id', 0);
$max_id = defaults($_REQUEST, 'max_id', 0); $max_id = defaults($_REQUEST, 'max_id', 0);
$page = (!empty($_REQUEST['page']) ? $_REQUEST['page'] - 1 : 0); $page = (!empty($_REQUEST['page']) ? $_REQUEST['page'] - 1 : 0);
$start = $page * $count; $start = $page * $count;
$params = ['order' => ['id' => true], 'limit' => [$start, $count]];
$condition = ["`gravity` IN (?, ?) AND `item`.`id` > ? if (!empty($_REQUEST['q'])) {
AND (`item`.`uid` = 0 OR (`item`.`uid` = ? AND NOT `item`.`global`)) $condition = [
AND `item`.`body` LIKE CONCAT('%',?,'%')", "`id` > ?
GRAVITY_PARENT, GRAVITY_COMMENT, $since_id, api_user(), $_REQUEST['q']]; AND (`uid` = 0 OR (`uid` = ? AND NOT `global`))
AND `body` LIKE CONCAT('%',?,'%')",
if ($max_id > 0) { $since_id, api_user(), $_REQUEST['q']];
$condition[0] .= " AND `item`.`id` <= ?"; if ($max_id > 0) {
$condition[] = $max_id; $condition[0] .= " AND `id` <= ?";
$condition[] = $max_id;
}
} elseif (!empty($_REQUEST['friendica_tag'])) {
$condition = [
"`oid` > ?
AND (`uid` = 0 OR (`uid` = ? AND NOT `global`))
AND `otype` = ? AND `type` = ? AND `term` = ?",
$since_id, local_user(), TERM_OBJ_POST, TERM_HASHTAG,
Strings::escapeTags(trim(rawurldecode($_REQUEST['friendica_tag'])))
];
if ($max_id > 0) {
$condition[0] .= " AND `oid` <= ?";
$condition[] = $max_id;
}
$terms = DBA::select('term', ['oid'], $condition, []);
$itemIds = [];
while($term = DBA::fetch($terms)){ $itemIds[] = $term['oid']; }
DBA::close($terms);
$condition = ['id' => empty($itemIds) ? [0] : $itemIds ];
} }
$params = ['order' => ['id' => true], 'limit' => [$start, $count]];
$statuses = Item::selectForUser(api_user(), [], $condition, $params); $statuses = Item::selectForUser(api_user(), [], $condition, $params);
$data['status'] = api_format_items(Item::inArray($statuses), $user_info); $data['status'] = api_format_items(Item::inArray($statuses), $user_info);
@ -2863,7 +2875,7 @@ function api_format_items($r, $user_info, $filter_user = false, $type = "json")
$ret = []; $ret = [];
foreach ($r as $item) { foreach ((array)$r as $item) {
localize_item($item); localize_item($item);
list($status_user, $owner_user) = api_item_get_user($a, $item); list($status_user, $owner_user) = api_item_get_user($a, $item);

View File

@ -1118,7 +1118,7 @@ class ApiTest extends DatabaseTest
*/ */
public function testApiStatusesUpdate() public function testApiStatusesUpdate()
{ {
$_GET['status'] = 'Status content'; $_GET['status'] = 'Status content #friendica';
$_GET['in_reply_to_status_id'] = -1; $_GET['in_reply_to_status_id'] = -1;
$_GET['lat'] = 48; $_GET['lat'] = 48;
$_GET['long'] = 7; $_GET['long'] = 7;
@ -1390,6 +1390,20 @@ class ApiTest extends DatabaseTest
} }
} }
/**
* Test the api_search() function with friendica_tag
* @return void
*/
public function testApiSearchWithFriendicaTag()
{
$_REQUEST['friendica_tag'] = 'friendica';
$result = api_search('json');
foreach ($result['status'] as $status) {
$this->assertStatus($status);
$this->assertContains('#friendica', $status['text'], null, true);
}
}
/** /**
* Test the api_search() function with an rpp parameter. * Test the api_search() function with an rpp parameter.
* @return void * @return void