Skip to content

Commit

Permalink
Query peers for popular torrents
Browse files Browse the repository at this point in the history
  • Loading branch information
qstokkink committed Sep 13, 2024
1 parent 79a1509 commit 39e25a7
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/tribler/core/database/restapi/database_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 21 additions & 1 deletion src/tribler/ui/src/pages/Popular/index.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -57,12 +57,32 @@ export default function Popular() {
const [open, setOpen] = useState<boolean>(false)
const [torrents, setTorrents] = useState<Torrent[]>([])
const [torrentDoubleClicked, setTorrentDoubleClicked] = useState<Torrent | undefined>();
const [request, setRequest] = useState<string>("");

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);
Expand Down
4 changes: 2 additions & 2 deletions src/tribler/ui/src/pages/Search/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions src/tribler/ui/src/services/tribler.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Torrent[]> {
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<Torrent[]> {
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
Expand Down

0 comments on commit 39e25a7

Please sign in to comment.