From d15a837188b3992431f6955be7a3583e70e02073 Mon Sep 17 00:00:00 2001 From: xiaomlove Date: Sat, 26 Mar 2022 16:09:39 +0800 Subject: [PATCH] finish elastic --- app/Console/Commands/Test.php | 11 +-- app/Models/Bookmark.php | 10 +++ app/Repositories/SearchRepository.php | 103 +++++++++++++++++++------- public/bookmark.php | 13 ++-- public/delete.php | 6 +- public/fastdelete.php | 5 ++ public/takeedit.php | 5 +- public/takeupload.php | 3 +- 8 files changed, 118 insertions(+), 38 deletions(-) diff --git a/app/Console/Commands/Test.php b/app/Console/Commands/Test.php index a13cdf7f..785fc8dd 100644 --- a/app/Console/Commands/Test.php +++ b/app/Console/Commands/Test.php @@ -71,9 +71,9 @@ class Test extends Command public function handle() { $searchRep = new SearchRepository(); -// $r = $searchRep->deleteIndex(); -// $r = $searchRep->createIndex(); -// $r = $searchRep->import(); + $r = $searchRep->deleteIndex(); + $r = $searchRep->createIndex(); + $r = $searchRep->import(); $arr = [ 'cat' => 'category', @@ -112,9 +112,10 @@ class Test extends Command // $r = $searchRep->updateTorrent(1); // $r = $searchRep->updateUser(1); - $r = $searchRep->addTorrent(1); +// $r = $searchRep->addTorrent(1); +// $r = $searchRep->deleteBookmark(1); +// $r = $searchRep->addBookmark(1); -// TorrentUpdated::dispatch(1); dd($r); } diff --git a/app/Models/Bookmark.php b/app/Models/Bookmark.php index a1a90adc..5cb085bd 100644 --- a/app/Models/Bookmark.php +++ b/app/Models/Bookmark.php @@ -8,4 +8,14 @@ class Bookmark extends NexusModel protected $table = 'bookmarks'; protected $fillable = ['userid', 'torrentid']; + + public function torrent() + { + return $this->belongsTo(Torrent::class, 'torrentid'); + } + + public function user() + { + return $this->belongsTo(Torrent::class, 'userid'); + } } diff --git a/app/Repositories/SearchRepository.php b/app/Repositories/SearchRepository.php index d59300b0..c3c8c37a 100644 --- a/app/Repositories/SearchRepository.php +++ b/app/Repositories/SearchRepository.php @@ -15,6 +15,8 @@ class SearchRepository extends BaseRepository { private Client $es; + private bool $enabled = false; + const INDEX_NAME = 'nexus_torrents'; const DOC_TYPE_TORRENT = 'torrent'; @@ -109,7 +111,13 @@ class SearchRepository extends BaseRepository public function __construct() { - $this->es = $this->getEs(); + $elasticsearchEnabled = nexus_env('ELASTICSEARCH_ENABLED'); + if ($elasticsearchEnabled) { + $this->enabled = true; + $this->es = $this->getEs(); + } else { + $this->enabled = false; + } } private function getEs(): Client @@ -179,6 +187,9 @@ class SearchRepository extends BaseRepository public function import($torrentId = null) { + if (!$this->enabled) { + return true; + } $page = 1; $size = 1000; $fields = $this->getTorrentBaseFields(); @@ -642,7 +653,7 @@ class SearchRepository extends BaseRepository 'total' => 0, 'data' => [], ]; - if (isset($response['errors']) && $response['errors']) { + if ($this->isEsResponseError($response)) { do_log("error response: " . nexus_json_encode($response), 'error'); return $result; } @@ -681,7 +692,10 @@ class SearchRepository extends BaseRepository public function updateTorrent(int $id): bool { - $log = "update torrent: $id"; + if (!$this->enabled) { + return true; + } + $log = "[UPDATE_TORRENT]: $id"; $baseFields = $this->getTorrentBaseFields(); $torrent = Torrent::query()->findOrFail($id, array_merge(['id'], $baseFields)); $data = $this->buildTorrentBody($torrent); @@ -689,17 +703,20 @@ class SearchRepository extends BaseRepository $params['body']['doc'] = $data['body']; $result = $this->es->update($params); if ($this->isEsResponseError($result)) { - do_log("$log, update torrent fail: " . nexus_json_encode($result), 'error'); + do_log("$log, fail: " . nexus_json_encode($result), 'error'); return false; } - do_log("$log, update torrent success: " . nexus_json_encode($result)); + do_log("$log, success: " . nexus_json_encode($result)); return $this->syncTorrentTags($torrent); } public function addTorrent(int $id): bool { - $log = "add torrent: $id"; + if (!$this->enabled) { + return true; + } + $log = "[ADD_TORRENT]: $id"; $baseFields = $this->getTorrentBaseFields(); $torrent = Torrent::query()->findOrFail($id, array_merge(['id'], $baseFields)); $data = $this->buildTorrentBody($torrent, true); @@ -708,16 +725,39 @@ class SearchRepository extends BaseRepository $params['body'][] = $data['body']; $result = $this->es->bulk($params); if ($this->isEsResponseError($result)) { - do_log("$log, add torrent fail: " . nexus_json_encode($result), 'error'); + do_log("$log, fail: " . nexus_json_encode($result), 'error'); return false; } - do_log("$log, add torrent success: " . nexus_json_encode($result)); + do_log("$log, success: " . nexus_json_encode($result)); return $this->syncTorrentTags($torrent); } - public function syncTorrentTags($torrent): bool + public function deleteTorrent(int $id): bool { + if (!$this->enabled) { + return true; + } + $log = "[DELETE_TORRENT]: $id"; + $params = [ + 'index' => self::INDEX_NAME, + 'id' => $this->getTorrentId($id), + ]; + $result = $this->es->delete($params); + if ($this->isEsResponseError($result)) { + do_log("$log, fail: " . nexus_json_encode($result), 'error'); + return false; + } + do_log("$log, success: " . nexus_json_encode($result)); + + return $this->syncTorrentTags($id, true); + } + + public function syncTorrentTags($torrent, $onlyDelete = false): bool + { + if (!$this->enabled) { + return true; + } if (!$torrent instanceof Torrent) { $torrent = Torrent::query()->findOrFail((int)$torrent, ['id']); } @@ -742,6 +782,11 @@ class SearchRepository extends BaseRepository return false; } do_log("$log, delete torrent tag success: " . nexus_json_encode($result)); + if ($onlyDelete) { + do_log("$log, only delete, return true"); + return true; + } + //then insert new $bulk = ['body' => []]; foreach ($torrent->torrent_tags as $torrentTag) { @@ -764,57 +809,65 @@ class SearchRepository extends BaseRepository public function updateUser($user): bool { + if (!$this->enabled) { + return true; + } if (!$user instanceof User) { $user = User::query()->findOrFail((int)$user, ['id', 'username']); } - $log = "update user: " . $user->id; + $log = "[UPDATE_USER]: " . $user->id; $data = $this->buildUserBody($user); $params = $data['index']; $params['body']['doc'] = $data['body']; $result = $this->es->update($params); if ($this->isEsResponseError($result)) { - do_log("$log, update user fail: " . nexus_json_encode($result), 'error'); + do_log("$log, fail: " . nexus_json_encode($result), 'error'); return false; } - do_log("$log, update user success: " . nexus_json_encode($result)); + do_log("$log, success: " . nexus_json_encode($result)); return true; } public function addBookmark($bookmark): bool { - if (!$bookmark instanceof Bookmark) { - $bookmark = Bookmark::query()->with('torrent')->findOrFail((int)$bookmark); + if (!$this->enabled) { + return true; } - $log = "add bookmark: " . $bookmark->toJson(); + if (!$bookmark instanceof Bookmark) { + $bookmark = Bookmark::query()->with([ + 'torrent' => function ($query) {$query->select(['id', 'owner']);} + ])->findOrFail((int)$bookmark); + } + $log = "[ADD_BOOKMARK]: " . $bookmark->toJson(); $bulk = ['body' => []]; - $body = $this->buildBookmarkBody($bookmark->torrent, $bookmark); + $body = $this->buildBookmarkBody($bookmark->torrent, $bookmark, true); $bulk['body'][] = ['index' => $body['index']]; $bulk['body'][] = $body['body']; $result = $this->es->bulk($bulk); if ($this->isEsResponseError($result)) { - do_log("$log, add bookmark fail: " . nexus_json_encode($result), 'error'); + do_log("$log, fail: " . nexus_json_encode($result), 'error'); return false; } - do_log("$log, add bookmark success: " . nexus_json_encode($result)); + do_log("$log, success: " . nexus_json_encode($result)); return true; } - public function deleteBookmark($bookmark): bool + public function deleteBookmark(int $id): bool { - if (!$bookmark instanceof Bookmark) { - $bookmark = Bookmark::query()->with('torrent')->findOrFail((int)$bookmark); + if (!$this->enabled) { + return true; } - $log = "add bookmark: " . $bookmark->toJson(); + $log = "[DELETE_BOOKMARK]: $id"; $params = [ 'index' => self::INDEX_NAME, - 'id' => $this->getBookmarkId($bookmark->id), + 'id' => $this->getBookmarkId($id), ]; $result = $this->es->delete($params); if ($this->isEsResponseError($result)) { - do_log("$log, delete bookmark fail: " . nexus_json_encode($result), 'error'); + do_log("$log, fail: " . nexus_json_encode($result), 'error'); return false; } - do_log("$log, delete bookmark success: " . nexus_json_encode($result)); + do_log("$log, success: " . nexus_json_encode($result)); return true; } diff --git a/public/bookmark.php b/public/bookmark.php index 0d24233f..20c0ec54 100644 --- a/public/bookmark.php +++ b/public/bookmark.php @@ -3,24 +3,27 @@ require "../include/bittorrent.php"; dbconn(); //Send some headers to keep the user's browser from caching the response. -header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" ); -header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" ); -header("Cache-Control: no-cache, must-revalidate" ); +header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" ); +header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" ); +header("Cache-Control: no-cache, must-revalidate" ); header("Pragma: no-cache" ); header("Content-Type: text/xml; charset=utf-8"); $torrentid = intval($_GET['torrentid'] ?? 0); if(isset($CURUSER)) { + $searchRep = new \App\Repositories\SearchRepository(); $res_bookmark = sql_query("SELECT * FROM bookmarks WHERE torrentid=" . sqlesc($torrentid) . " AND userid=" . sqlesc($CURUSER['id'])); if (mysql_num_rows($res_bookmark) == 1){ + $bookmarkResult = mysql_fetch_assoc($res_bookmark); + $searchRep->deleteBookmark($bookmarkResult['id']); sql_query("DELETE FROM bookmarks WHERE torrentid=" . sqlesc($torrentid) . " AND userid=" . sqlesc($CURUSER['id'])) or sqlerr(__FILE__,__LINE__); $Cache->delete_value('user_'.$CURUSER['id'].'_bookmark_array'); echo "deleted"; - } - else{ + } else { sql_query("INSERT INTO bookmarks (torrentid, userid) VALUES (" . sqlesc($torrentid) . "," . sqlesc($CURUSER['id']) . ")") or sqlerr(__FILE__,__LINE__); $Cache->delete_value('user_'.$CURUSER['id'].'_bookmark_array'); + $searchRep->addBookmark(mysql_insert_id()); echo "added"; } } diff --git a/public/delete.php b/public/delete.php index 0dfb619a..f53113b4 100644 --- a/public/delete.php +++ b/public/delete.php @@ -53,7 +53,11 @@ else bark($lang_delete['std_enter_reason']); $reasonstr = trim($reason[3]); } - +$searchRep = new \App\Repositories\SearchRepository(); +$deleteEsResult = $searchRep->deleteTorrent($id); +if ($deleteEsResult === false) { + bark('Delete es fail.'); +} deletetorrent($id); if ($row['anonymous'] == 'yes' && $CURUSER["id"] == $row["owner"]) { diff --git a/public/fastdelete.php b/public/fastdelete.php index 1f551eb7..cf08a0ab 100644 --- a/public/fastdelete.php +++ b/public/fastdelete.php @@ -32,6 +32,11 @@ if (!$sure) stderr($lang_fastdelete['std_delete_torrent'], $lang_fastdelete['std_delete_torrent_note']."".$lang_fastdelete['std_here_if_sure'],false); } +$searchRep = new \App\Repositories\SearchRepository(); +$deleteEsResult = $searchRep->deleteTorrent($id); +if ($deleteEsResult === false) { + bark('Delete es fail.'); +} deletetorrent($id); KPS("-",$uploadtorrent_bonus,$row["owner"]); if ($row['anonymous'] == 'yes' && $CURUSER["id"] == $row["owner"]) { diff --git a/public/takeedit.php b/public/takeedit.php index 50d3312f..1c4a9fbc 100644 --- a/public/takeedit.php +++ b/public/takeedit.php @@ -243,7 +243,10 @@ else { write_log("Torrent $id ($name) was edited by {$CURUSER['username']}, Mod Edit" . $pick_info . $place_info); } -\App\Events\TorrentUpdated::dispatch($id); + +$searchRep = new \App\Repositories\SearchRepository(); +$searchRep->updateTorrent($id); + $returl = "details.php?id=$id&edited=1"; if (isset($_POST["returnto"])) $returl = $_POST["returnto"]; diff --git a/public/takeupload.php b/public/takeupload.php index acbd7a10..4118ed72 100644 --- a/public/takeupload.php +++ b/public/takeupload.php @@ -372,7 +372,8 @@ KPS("+",$uploadtorrent_bonus,$CURUSER["id"]); write_log("Torrent $id ($torrent) was uploaded by $anon"); -\App\Events\TorrentUpdated::dispatch($id); +$searchRep = new \App\Repositories\SearchRepository(); +$searchRep->addTorrent($id); //===notify people who voted on offer thanks CoLdFuSiOn :) if ($is_offer)