From 39e25a7e10fd4d7abcf7234ca9286d9781b1e822 Mon Sep 17 00:00:00 2001 From: Quinten Stokkink Date: Fri, 13 Sep 2024 14:59:41 +0200 Subject: [PATCH] Query peers for popular torrents --- .../database/restapi/database_endpoint.py | 2 ++ src/tribler/ui/src/pages/Popular/index.tsx | 22 ++++++++++++++++++- src/tribler/ui/src/pages/Search/index.tsx | 4 ++-- .../ui/src/services/tribler.service.ts | 8 +++---- 4 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/tribler/core/database/restapi/database_endpoint.py b/src/tribler/core/database/restapi/database_endpoint.py index bf48558373..eae6c908e0 100644 --- a/src/tribler/core/database/restapi/database_endpoint.py +++ b/src/tribler/core/database/restapi/database_endpoint.py @@ -122,6 +122,8 @@ def sanitize_parameters(cls: type[Self], sanitized["channel_pk"] = unhexlify(parameters["channel_pk"]) if "origin_id" in parameters: sanitized["origin_id"] = int(parameters["origin_id"]) + if "popular" in parameters: + sanitized["popular"] = parse_bool(parameters.get("popular", "false")) return sanitized @db_session diff --git a/src/tribler/ui/src/pages/Popular/index.tsx b/src/tribler/ui/src/pages/Popular/index.tsx index 4f3e907d5d..350286eccb 100644 --- a/src/tribler/ui/src/pages/Popular/index.tsx +++ b/src/tribler/ui/src/pages/Popular/index.tsx @@ -1,6 +1,6 @@ import SimpleTable from "@/components/ui/simple-table"; import SaveAs from "@/dialogs/SaveAs"; -import { useCallback, useMemo, useState } from "react"; +import { useCallback, useEffect, useMemo, useState } from "react"; import { triblerService } from "@/services/tribler.service"; import { Torrent } from "@/models/torrent.model"; import { ColumnDef } from "@tanstack/react-table"; @@ -57,12 +57,32 @@ export default function Popular() { const [open, setOpen] = useState(false) const [torrents, setTorrents] = useState([]) const [torrentDoubleClicked, setTorrentDoubleClicked] = useState(); + const [request, setRequest] = useState(""); useInterval(async () => { const popular = await triblerService.getPopularTorrents(true); setTorrents(filterDuplicates(popular, 'infohash')); + const remoteQuery = await triblerService.searchTorrentsRemote('', true); + setRequest(remoteQuery.request_uuid); }, 5000, true); + useEffect(() => { + (async () => { triblerService.addEventListener("remote_query_results", OnSearchEvent) })(); + return () => { + (async () => { triblerService.removeEventListener("remote_query_results", OnSearchEvent) })(); + } + }, [request]); + + const OnSearchEvent = (event: MessageEvent) => { + const data = JSON.parse(event.data); + if (data.uuid !== request) + return; + + for (const result of data.results) { + setTorrents((prevTorrents) => [...prevTorrents, result]); + } + } + const handleDownload = useCallback((torrent: Torrent) => { setTorrentDoubleClicked(torrent); setOpen(true); diff --git a/src/tribler/ui/src/pages/Search/index.tsx b/src/tribler/ui/src/pages/Search/index.tsx index ac2658c58f..7e70c19f78 100644 --- a/src/tribler/ui/src/pages/Search/index.tsx +++ b/src/tribler/ui/src/pages/Search/index.tsx @@ -73,9 +73,9 @@ export default function Search() { useEffect(() => { const searchTorrents = async () => { if (!query) return; - const localResults = await triblerService.searchTorrentsLocal(query, true); + const localResults = await triblerService.searchTorrentsLocal(query); setTorrents(filterDuplicates(localResults, 'infohash')); - const remoteQuery = await triblerService.searchTorrentsRemote(query, true); + const remoteQuery = await triblerService.searchTorrentsRemote(query, false); setRequest(remoteQuery.request_uuid); } searchTorrents(); diff --git a/src/tribler/ui/src/services/tribler.service.ts b/src/tribler/ui/src/services/tribler.service.ts index 2d414b7678..de593d8316 100644 --- a/src/tribler/ui/src/services/tribler.service.ts +++ b/src/tribler/ui/src/services/tribler.service.ts @@ -147,12 +147,12 @@ export class TriblerService { return (await this.http.get(`/metadata/search/completions?q=${txt_filter}`)).data.completions; } - async searchTorrentsLocal(txt_filter: string, hide_xxx: boolean): Promise { - return (await this.http.get(`/metadata/search/local?first=1&last=200&metadata_type=300&exclude_deleted=1&fts_text=${txt_filter}&hide_xxx=${+hide_xxx}`)).data.results; + async searchTorrentsLocal(txt_filter: string): Promise { + return (await this.http.get(`/metadata/search/local?first=1&last=200&metadata_type=300&exclude_deleted=1&fts_text=${txt_filter}`)).data.results; } - async searchTorrentsRemote(txt_filter: string, hide_xxx: boolean): Promise<{ request_uuid: string, peers: string[] }> { - return (await this.http.put(`/search/remote?fts_text=${txt_filter}&hide_xxx=${+hide_xxx}&metadata_type=300&exclude_deleted=1`)).data; + async searchTorrentsRemote(txt_filter: string, popular: boolean): Promise<{ request_uuid: string, peers: string[] }> { + return (await this.http.put(`/search/remote?fts_text=${txt_filter}&popular=${+popular}&metadata_type=300&exclude_deleted=1`)).data; } // Settings