Merge pull request #7941 from MrPetovan/task/7889-api-post-contact-search

[API] Provide post/contact search
This commit is contained in:
Michael Vogel 2019-12-13 21:13:19 +01:00 committed by GitHub
commit d3a4558e80
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1416,32 +1416,37 @@ function api_users_search($type)
$userlist = []; $userlist = [];
if (!empty($_GET['q'])) { if (!empty($_GET['q'])) {
$r = q("SELECT id FROM `contact` WHERE `uid` = 0 AND `name` = '%s'", DBA::escape($_GET["q"])); $contacts = Contact::selectToArray(
['id'],
[
'`uid` = 0 AND (`name` = ? OR `nick` = ? OR `url` = ? OR `addr` = ?)',
$_GET['q'],
$_GET['q'],
$_GET['q'],
$_GET['q'],
]
);
if (!DBA::isResult($r)) { if (DBA::isResult($contacts)) {
$r = q("SELECT `id` FROM `contact` WHERE `uid` = 0 AND `nick` = '%s'", DBA::escape($_GET["q"]));
}
if (DBA::isResult($r)) {
$k = 0; $k = 0;
foreach ($r as $user) { foreach ($contacts as $contact) {
$user_info = api_get_user($a, $user["id"]); $user_info = api_get_user($a, $contact['id']);
if ($type == "xml") { if ($type == 'xml') {
$userlist[$k++.":user"] = $user_info; $userlist[$k++ . ':user'] = $user_info;
} else { } else {
$userlist[] = $user_info; $userlist[] = $user_info;
} }
} }
$userlist = ["users" => $userlist]; $userlist = ['users' => $userlist];
} else { } else {
throw new BadRequestException("User ".$_GET["q"]." not found."); throw new NotFoundException('User ' . $_GET['q'] . ' not found.');
} }
} else { } else {
throw new BadRequestException("No user specified."); throw new BadRequestException('No search term specified.');
} }
return api_format_data("users", $type, $userlist); return api_format_data('users', $type, $userlist);
} }
/// @TODO move to top of file or somewhere better /// @TODO move to top of file or somewhere better
@ -1502,7 +1507,9 @@ 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) { throw new ForbiddenException(); } if (api_user() === false || $user_info === false) {
throw new ForbiddenException();
}
if (empty($_REQUEST['q'])) { if (empty($_REQUEST['q'])) {
throw new BadRequestException('q parameter is required.'); throw new BadRequestException('q parameter is required.');
@ -1566,7 +1573,21 @@ function api_search($type)
} }
} }
$statuses = Item::selectForUser(api_user(), [], $condition, $params); $statuses = [];
if (parse_url($searchTerm, PHP_URL_SCHEME) != '') {
$id = Item::fetchByLink($searchTerm, api_user());
if (!$id) {
// Public post
$id = Item::fetchByLink($searchTerm);
}
if (!empty($id)) {
$statuses = Item::select([], ['id' => $id]);
}
}
$statuses = $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);