Skip to content

Commit

Permalink
feat: search in meilisearch (step.2)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rhilip committed Mar 12, 2023
1 parent eb7d0cf commit 0219030
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
7 changes: 4 additions & 3 deletions announce.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@
if (isset($self) && $event == "stopped") {
\NexusPHP\Components\Database::query("DELETE FROM peers WHERE $selfwhere") or err("D Err");
if (\NexusPHP\Components\Database::affected_rows()) {
$updateset[] = ($self["seeder"] == "yes" ? "seeders = seeders - 1" : "leechers = leechers - 1");
$Cache->hIncrBy('torrent_peer_count_content', $torrentid . ':' . ($seeder == 'yes' ? 'seeders' : 'leechers'), -1);
\NexusPHP\Components\Database::query("UPDATE snatched SET uploaded = uploaded + $trueupthis, downloaded = downloaded + $truedownthis, to_go = $left, $announcetime, last_action = ".$dt." WHERE torrentid = $torrentid AND userid = $userid") or err("SL Err 1");
}
} elseif (isset($self)) {
Expand All @@ -356,7 +356,8 @@

if (\NexusPHP\Components\Database::affected_rows()) {
if ($seeder <> $self["seeder"]) {
$updateset[] = ($seeder == "yes" ? "seeders = seeders + 1, leechers = leechers - 1" : "seeders = seeders - 1, leechers = leechers + 1");
$Cache->hIncrBy('torrent_peer_count_content', $torrentid . ':seeders', $seeder == "yes" ? 1 : -1);
$Cache->hIncrBy('torrent_peer_count_content', $torrentid . ':leechers', $seeder == "yes" ? -1 : 1);
}
\NexusPHP\Components\Database::query("UPDATE snatched SET uploaded = uploaded + $trueupthis, downloaded = downloaded + $truedownthis, to_go = $left, $announcetime, last_action = ".$dt." $finished_snatched WHERE torrentid = $torrentid AND userid = $userid") or err("SL Err 2");
}
Expand All @@ -371,7 +372,7 @@
\NexusPHP\Components\Database::query("INSERT INTO peers (torrent, userid, peer_id, ip, port, connectable, uploaded, downloaded, to_go, started, last_action, seeder, agent, downloadoffset, uploadoffset, passkey) VALUES ($torrentid, $userid, ".\NexusPHP\Components\Database::escape($peer_id).", ".\NexusPHP\Components\Database::escape($ip).", $port, '$connectable', $uploaded, $downloaded, $left, $dt, $dt, '$seeder', ".\NexusPHP\Components\Database::escape($agent).", $downloaded, $uploaded, ".\NexusPHP\Components\Database::escape($passkey).")") or err("PL Err 2");

if (\NexusPHP\Components\Database::affected_rows()) {
$updateset[] = ($seeder == "yes" ? "seeders = seeders + 1" : "leechers = leechers + 1");
$Cache->hIncrBy('torrent_peer_count_content', $torrentid . ':' . ($seeder == 'yes' ? 'seeders' : 'leechers'), 1);

$check = @mysqli_fetch_row(@\NexusPHP\Components\Database::query("SELECT COUNT(*) FROM snatched WHERE torrentid = $torrentid AND userid = $userid"));
if (!$check['0']) {
Expand Down
10 changes: 9 additions & 1 deletion include/cleanup.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,8 @@ function torrent_promotion_expire($days, $type = 2, $targettype = 1)
}

//4.update count of seeders, leechers, comments for torrents
$torrents = array();
$torrents = [];
$peer_count_content = [];
$res = \NexusPHP\Components\Database::query("SELECT torrent, seeder, COUNT(*) AS c FROM peers GROUP BY torrent, seeder") or sqlerr(__FILE__, __LINE__);
while ($row = mysqli_fetch_assoc($res)) {
if ($row["seeder"] == "yes") {
Expand All @@ -219,8 +220,15 @@ function torrent_promotion_expire($days, $type = 2, $targettype = 1)
$key = "leechers";
}
$torrents[$row["torrent"]][$key] = $row["c"];
$peer_count_content[$row['torrent'] . ':' . $key] = (int)$row['c'];
}

// 更新peer_count缓存
$Cache->multi()
->del('torrent_peer_count_content')
->hMSet('torrent_peer_count_content', $peer_count_content)
->exec();

$res = \NexusPHP\Components\Database::query("SELECT torrent, COUNT(*) AS c FROM comments GROUP BY torrent") or sqlerr(__FILE__, __LINE__);
while ($row = mysqli_fetch_assoc($res)) {
$torrents[$row["torrent"]]["comments"] = $row["c"];
Expand Down
31 changes: 29 additions & 2 deletions torrents.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
}

$addparams = []; // 用于生成 分页所需要的 params
$wherea = []; // 用于生成SQL语句

$searchstr = trim($_GET['search'] ?? ''); // 搜索字符串
$options = [
Expand Down Expand Up @@ -274,8 +273,36 @@ function filter_int_input(string $field, int $default = 0, array $allowed_values

$torrents_index = \NexusPHP\Components\Meili::getMeiliSearch()->index('torrents');
$search = $torrents_index->search($searchstr, $options);
$res = $search->getHits();
$count = $search->getHitsCount();
$hits = $search->getHits();

// 对Meilisearch返回的搜索结果做一些处理
function get_peer_status ($tids) {
global $Cache;
$peer_status_map = [];
foreach ($tids as $tid) {
$peer_status_map[] = $tid . ':seeders';
$peer_status_map[] = $tid . ':leechers';
}

return $Cache->hMGet('torrent_peer_count_content', $peer_status_map);
}

$peer_status = get_peer_status(array_column($hits, 'id'));
$can_viewanonymous = get_user_class() >= $viewanonymous_class;

$res = array_map(function ($t) use ($peer_status, $can_viewanonymous) {
foreach (['seeders', 'leechers'] as $key) { // 合并peers
if (isset($t[$key])) {
$t[$key] = $peer_status[$t['id'] . ':' . $key] ?? 0;
}
}

if (!$can_viewanonymous && $t['anonymous']) {
$t['owner'] = -1;
}
return $t;
}, $hits);

list($pagertop, $pagerbottom, $limit) = pager($limit, $search->getEstimatedTotalHits(), "?" . implode("&", $addparams));

Expand Down

0 comments on commit 0219030

Please sign in to comment.