diff --git a/ServiceAPI/Search.php b/ServiceAPI/Search.php new file mode 100644 index 00000000..46def4f4 --- /dev/null +++ b/ServiceAPI/Search.php @@ -0,0 +1,76 @@ +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); + } +} diff --git a/Web/Presenters/SearchPresenter.php b/Web/Presenters/SearchPresenter.php index 48d7b8c8..fadf9954 100644 --- a/Web/Presenters/SearchPresenter.php +++ b/Web/Presenters/SearchPresenter.php @@ -100,56 +100,4 @@ final class SearchPresenter extends OpenVKPresenter $this->template->type = $type; $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"]); - } - } } diff --git a/Web/routes.yml b/Web/routes.yml index 9c214a50..d1a0e7ae 100644 --- a/Web/routes.yml +++ b/Web/routes.yml @@ -251,8 +251,6 @@ routes: handler: "Search->index" - url: "/search/content" handler: "ContentSearch->index" - - url: "/fastSearch" - handler: "Search->fastSearch" - url: "/notes{num}" handler: "Notes->list" - url: "/note{num}_{num}" diff --git a/Web/static/css/main.css b/Web/static/css/main.css index d7a7b3d5..b8c435c3 100644 --- a/Web/static/css/main.css +++ b/Web/static/css/main.css @@ -2497,7 +2497,6 @@ a.poll-retract-vote { padding-bottom: 6px; border: 1px solid #C0CAD5; border-top: 0px; - font-size: 15px; width: fit-content; z-index: 666666; margin-top: -1px; diff --git a/Web/static/js/openvk.cls.js b/Web/static/js/openvk.cls.js index 11ddb1a4..80f16218 100644 --- a/Web/static/js/openvk.cls.js +++ b/Web/static/js/openvk.cls.js @@ -1,6 +1,4 @@  -const { json } = require("stream/consumers"); - function expand_wall_textarea(id) { var el = document.getElementById('post-buttons'+id); var wi = document.getElementById('wall-post-input'+id); @@ -650,55 +648,36 @@ async function checkSearchTips() { let query = searchInput.value; - await new Promise(r => setTimeout(r, 500)); + await new Promise(r => setTimeout(r, 1000)); 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) { srcht.removeAttribute("hidden") - let etype = type == "groups" ? "clubs" : "users" + let etype = type - let xhr = new XMLHttpRequest() - xhr.open("POST", "/fastSearch?query="+query+"&type="+etype) + try { + let results = await API.Search.fastSearch(escapeHtml(query), etype) + + srchrr.innerHTML = "" - xhr.onloadstart = () => { - srchrr.innerHTML = `` - } - - xhr.onloadend = async () => { - let results; - - try { - results = JSON.parse(xhr.responseText) - } catch { - srchrr.innerHTML = "Rate limits" - } - - if(results["items"] != null) { - srchrr.innerHTML = "" - - for(const el of results["items"]) { - srchrr.insertAdjacentHTML("beforeend", ` - - - - - -

${el.name}

-

${el.description}

- - + for(const el of results["items"]) { + srchrr.insertAdjacentHTML("beforeend", ` + + + + + +

${el.name}

+

${el.description}

+ + `) - - } - } else { - srchrr.innerHTML = tr("no_results") } + } catch(rejection) { + srchrr.innerHTML = tr("no_results") } - - xhr.send() - } else { - //srcht.setAttribute("hidden", "hidden") } }