Move to ServiceAPI

This commit is contained in:
lalka2016 2023-06-15 18:05:23 +03:00
parent 73986b2992
commit 98403a8692
5 changed files with 97 additions and 97 deletions

76
ServiceAPI/Search.php Normal file
View file

@ -0,0 +1,76 @@
<?php declare(strict_types=1);
namespace openvk\ServiceAPI;
use openvk\Web\Models\Entities\{User, Club};
use openvk\Web\Models\Repositories\{Users, Clubs, Videos};
use Chandler\Database\DatabaseConnection;
class Search implements Handler
{
protected $user;
private $users;
private $clubs;
private $videos;
function __construct(?User $user)
{
$this->user = $user;
$this->users = new Users;
$this->clubs = new Clubs;
$this->videos = new Videos;
}
function fastSearch(string $query, string $type = "users", callable $resolve, callable $reject)
{
if($query == "" || strlen($query) < 3)
$reject(12, "No input or input < 3");
$repo;
$sort;
switch($type) {
default:
case "users":
$repo = (new Users);
$sort = "rating DESC";
break;
case "groups":
$repo = (new Clubs);
$sort = "id ASC";
break;
case "videos":
$repo = (new Videos);
$sort = "created ASC";
break;
}
$res = $repo->find($query, ["doNotSearchMe" => $this->user->getId()], $sort);
$results = array_slice(iterator_to_array($res), 0, 5);
$count = sizeof($results);
$arr = [
"count" => $count,
"items" => []
];
if(sizeof($results) < 1) {
$reject(2, "No results");
}
foreach($results as $res) {
$arr["items"][] = [
"id" => $res->getId(),
"name" => $type == "users" ? $res->getCanonicalName() : $res->getName(),
"avatar" => $type != "videos" ? $res->getAvatarUrl() : $res->getThumbnailURL(),
"url" => $type != "videos" ? $res->getUrl() : "/video".$res->getPrettyId(),
"description" => ovk_proc_strtr($res->getDescription() ?? "...", 40)
];
}
$resolve($arr);
}
}

View file

@ -100,56 +100,4 @@ final class SearchPresenter extends OpenVKPresenter
$this->template->type = $type; $this->template->type = $type;
$this->template->page = $page; $this->template->page = $page;
} }
function renderFastSearch()
{
$this->assertUserLoggedIn();
$this->willExecuteWriteAction();
if($_SERVER["REQUEST_METHOD"] === "POST") {
$query = $this->queryParam("query") ?? "";
if($query == "" || strlen($query) < 3)
$this->returnJson([
"error" => "type something longer"
]);
$type = $this->queryParam("type") ?? "users";
$isUsers = $type == "users";
$repo = $isUsers ? (new Users) : (new Clubs);
$sort = $isUsers ? "rating DESC" : "id ASC";
$res = $repo->find($query, ["doNotSearchMe" => $this->user->id], $sort);
$results = array_slice(iterator_to_array($res), 0, 5);
$count = sizeof($results);
$arr = [
"count" => $count,
"items" => []
];
if(sizeof($results) < 1) {
$this->returnJson(["err" => "No results"]);
}
foreach($results as $res) {
$arr["items"][] = [
"id" => $res->getId(),
"name" => $isUsers ? $res->getCanonicalName() : $res->getName(),
"avatar" => $res->getAvatarUrl(),
"url" => $res->getUrl(),
"description" => ovk_proc_strtr($res->getDescription() ?? "...", 40)
];
}
$this->returnJson($arr);
} else {
$this->returnJson(["err" => "or"]);
}
}
} }

View file

@ -251,8 +251,6 @@ routes:
handler: "Search->index" handler: "Search->index"
- url: "/search/content" - url: "/search/content"
handler: "ContentSearch->index" handler: "ContentSearch->index"
- url: "/fastSearch"
handler: "Search->fastSearch"
- url: "/notes{num}" - url: "/notes{num}"
handler: "Notes->list" handler: "Notes->list"
- url: "/note{num}_{num}" - url: "/note{num}_{num}"

View file

@ -2497,7 +2497,6 @@ a.poll-retract-vote {
padding-bottom: 6px; padding-bottom: 6px;
border: 1px solid #C0CAD5; border: 1px solid #C0CAD5;
border-top: 0px; border-top: 0px;
font-size: 15px;
width: fit-content; width: fit-content;
z-index: 666666; z-index: 666666;
margin-top: -1px; margin-top: -1px;

View file

@ -1,6 +1,4 @@
 
const { json } = require("stream/consumers");
function expand_wall_textarea(id) { function expand_wall_textarea(id) {
var el = document.getElementById('post-buttons'+id); var el = document.getElementById('post-buttons'+id);
var wi = document.getElementById('wall-post-input'+id); var wi = document.getElementById('wall-post-input'+id);
@ -650,55 +648,36 @@ async function checkSearchTips()
{ {
let query = searchInput.value; let query = searchInput.value;
await new Promise(r => setTimeout(r, 500)); await new Promise(r => setTimeout(r, 1000));
let type = typer.value; let type = typer.value;
let smt = type == "users" || type == "groups"; let smt = type == "users" || type == "groups" || type == "videos";
if(query.length > 3 && query == searchInput.value && smt) { if(query.length > 3 && query == searchInput.value && smt) {
srcht.removeAttribute("hidden") srcht.removeAttribute("hidden")
let etype = type == "groups" ? "clubs" : "users" let etype = type
let xhr = new XMLHttpRequest() try {
xhr.open("POST", "/fastSearch?query="+query+"&type="+etype) let results = await API.Search.fastSearch(escapeHtml(query), etype)
xhr.onloadstart = () => { srchrr.innerHTML = ""
srchrr.innerHTML = `<img id="loader" src="/assets/packages/static/openvk/img/loading_mini.gif">`
}
xhr.onloadend = async () => { for(const el of results["items"]) {
let results; srchrr.insertAdjacentHTML("beforeend", `
<tr class="restip" onmousedown="if (event.which === 2) { window.open('${el.url}', '_blank'); } else {location.href='${el.url}'}">
try { <td>
results = JSON.parse(xhr.responseText) <img src="${el.avatar}" width="30">
} catch { </td>
srchrr.innerHTML = "Rate limits" <td valign="top">
} <p class="nameq" style="margin-top: -2px;">${el.name}</p>
<p class="desq">${el.description}</p>
if(results["items"] != null) { </td>
srchrr.innerHTML = "" </tr>
for(const el of results["items"]) {
srchrr.insertAdjacentHTML("beforeend", `
<tr class="restip" onmousedown="if (event.which === 2) { window.open('${el.url}', '_blank'); } else {location.href='${el.url}'}">
<td>
<img src="${el.avatar}" width="30">
</td>
<td valign="top">
<p class="nameq" style="margin-top: -2px;">${el.name}</p>
<p class="desq">${el.description}</p>
</td>
</tr>
`) `)
}
} else {
srchrr.innerHTML = tr("no_results")
} }
} catch(rejection) {
srchrr.innerHTML = tr("no_results")
} }
xhr.send()
} else {
//srcht.setAttribute("hidden", "hidden")
} }
} }