finish elastic

This commit is contained in:
xiaomlove
2022-03-26 16:09:39 +08:00
parent 0bc1e2f9d3
commit d15a837188
8 changed files with 118 additions and 38 deletions
+6 -5
View File
@@ -71,9 +71,9 @@ class Test extends Command
public function handle() public function handle()
{ {
$searchRep = new SearchRepository(); $searchRep = new SearchRepository();
// $r = $searchRep->deleteIndex(); $r = $searchRep->deleteIndex();
// $r = $searchRep->createIndex(); $r = $searchRep->createIndex();
// $r = $searchRep->import(); $r = $searchRep->import();
$arr = [ $arr = [
'cat' => 'category', 'cat' => 'category',
@@ -112,9 +112,10 @@ class Test extends Command
// $r = $searchRep->updateTorrent(1); // $r = $searchRep->updateTorrent(1);
// $r = $searchRep->updateUser(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); dd($r);
} }
+10
View File
@@ -8,4 +8,14 @@ class Bookmark extends NexusModel
protected $table = 'bookmarks'; protected $table = 'bookmarks';
protected $fillable = ['userid', 'torrentid']; protected $fillable = ['userid', 'torrentid'];
public function torrent()
{
return $this->belongsTo(Torrent::class, 'torrentid');
}
public function user()
{
return $this->belongsTo(Torrent::class, 'userid');
}
} }
+78 -25
View File
@@ -15,6 +15,8 @@ class SearchRepository extends BaseRepository
{ {
private Client $es; private Client $es;
private bool $enabled = false;
const INDEX_NAME = 'nexus_torrents'; const INDEX_NAME = 'nexus_torrents';
const DOC_TYPE_TORRENT = 'torrent'; const DOC_TYPE_TORRENT = 'torrent';
@@ -109,7 +111,13 @@ class SearchRepository extends BaseRepository
public function __construct() 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 private function getEs(): Client
@@ -179,6 +187,9 @@ class SearchRepository extends BaseRepository
public function import($torrentId = null) public function import($torrentId = null)
{ {
if (!$this->enabled) {
return true;
}
$page = 1; $page = 1;
$size = 1000; $size = 1000;
$fields = $this->getTorrentBaseFields(); $fields = $this->getTorrentBaseFields();
@@ -642,7 +653,7 @@ class SearchRepository extends BaseRepository
'total' => 0, 'total' => 0,
'data' => [], 'data' => [],
]; ];
if (isset($response['errors']) && $response['errors']) { if ($this->isEsResponseError($response)) {
do_log("error response: " . nexus_json_encode($response), 'error'); do_log("error response: " . nexus_json_encode($response), 'error');
return $result; return $result;
} }
@@ -681,7 +692,10 @@ class SearchRepository extends BaseRepository
public function updateTorrent(int $id): bool public function updateTorrent(int $id): bool
{ {
$log = "update torrent: $id"; if (!$this->enabled) {
return true;
}
$log = "[UPDATE_TORRENT]: $id";
$baseFields = $this->getTorrentBaseFields(); $baseFields = $this->getTorrentBaseFields();
$torrent = Torrent::query()->findOrFail($id, array_merge(['id'], $baseFields)); $torrent = Torrent::query()->findOrFail($id, array_merge(['id'], $baseFields));
$data = $this->buildTorrentBody($torrent); $data = $this->buildTorrentBody($torrent);
@@ -689,17 +703,20 @@ class SearchRepository extends BaseRepository
$params['body']['doc'] = $data['body']; $params['body']['doc'] = $data['body'];
$result = $this->es->update($params); $result = $this->es->update($params);
if ($this->isEsResponseError($result)) { 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; return false;
} }
do_log("$log, update torrent success: " . nexus_json_encode($result)); do_log("$log, success: " . nexus_json_encode($result));
return $this->syncTorrentTags($torrent); return $this->syncTorrentTags($torrent);
} }
public function addTorrent(int $id): bool public function addTorrent(int $id): bool
{ {
$log = "add torrent: $id"; if (!$this->enabled) {
return true;
}
$log = "[ADD_TORRENT]: $id";
$baseFields = $this->getTorrentBaseFields(); $baseFields = $this->getTorrentBaseFields();
$torrent = Torrent::query()->findOrFail($id, array_merge(['id'], $baseFields)); $torrent = Torrent::query()->findOrFail($id, array_merge(['id'], $baseFields));
$data = $this->buildTorrentBody($torrent, true); $data = $this->buildTorrentBody($torrent, true);
@@ -708,16 +725,39 @@ class SearchRepository extends BaseRepository
$params['body'][] = $data['body']; $params['body'][] = $data['body'];
$result = $this->es->bulk($params); $result = $this->es->bulk($params);
if ($this->isEsResponseError($result)) { 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; return false;
} }
do_log("$log, add torrent success: " . nexus_json_encode($result)); do_log("$log, success: " . nexus_json_encode($result));
return $this->syncTorrentTags($torrent); 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) { if (!$torrent instanceof Torrent) {
$torrent = Torrent::query()->findOrFail((int)$torrent, ['id']); $torrent = Torrent::query()->findOrFail((int)$torrent, ['id']);
} }
@@ -742,6 +782,11 @@ class SearchRepository extends BaseRepository
return false; return false;
} }
do_log("$log, delete torrent tag success: " . nexus_json_encode($result)); 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 //then insert new
$bulk = ['body' => []]; $bulk = ['body' => []];
foreach ($torrent->torrent_tags as $torrentTag) { foreach ($torrent->torrent_tags as $torrentTag) {
@@ -764,57 +809,65 @@ class SearchRepository extends BaseRepository
public function updateUser($user): bool public function updateUser($user): bool
{ {
if (!$this->enabled) {
return true;
}
if (!$user instanceof User) { if (!$user instanceof User) {
$user = User::query()->findOrFail((int)$user, ['id', 'username']); $user = User::query()->findOrFail((int)$user, ['id', 'username']);
} }
$log = "update user: " . $user->id; $log = "[UPDATE_USER]: " . $user->id;
$data = $this->buildUserBody($user); $data = $this->buildUserBody($user);
$params = $data['index']; $params = $data['index'];
$params['body']['doc'] = $data['body']; $params['body']['doc'] = $data['body'];
$result = $this->es->update($params); $result = $this->es->update($params);
if ($this->isEsResponseError($result)) { 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; return false;
} }
do_log("$log, update user success: " . nexus_json_encode($result)); do_log("$log, success: " . nexus_json_encode($result));
return true; return true;
} }
public function addBookmark($bookmark): bool public function addBookmark($bookmark): bool
{ {
if (!$bookmark instanceof Bookmark) { if (!$this->enabled) {
$bookmark = Bookmark::query()->with('torrent')->findOrFail((int)$bookmark); 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' => []]; $bulk = ['body' => []];
$body = $this->buildBookmarkBody($bookmark->torrent, $bookmark); $body = $this->buildBookmarkBody($bookmark->torrent, $bookmark, true);
$bulk['body'][] = ['index' => $body['index']]; $bulk['body'][] = ['index' => $body['index']];
$bulk['body'][] = $body['body']; $bulk['body'][] = $body['body'];
$result = $this->es->bulk($bulk); $result = $this->es->bulk($bulk);
if ($this->isEsResponseError($result)) { 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; return false;
} }
do_log("$log, add bookmark success: " . nexus_json_encode($result)); do_log("$log, success: " . nexus_json_encode($result));
return true; return true;
} }
public function deleteBookmark($bookmark): bool public function deleteBookmark(int $id): bool
{ {
if (!$bookmark instanceof Bookmark) { if (!$this->enabled) {
$bookmark = Bookmark::query()->with('torrent')->findOrFail((int)$bookmark); return true;
} }
$log = "add bookmark: " . $bookmark->toJson(); $log = "[DELETE_BOOKMARK]: $id";
$params = [ $params = [
'index' => self::INDEX_NAME, 'index' => self::INDEX_NAME,
'id' => $this->getBookmarkId($bookmark->id), 'id' => $this->getBookmarkId($id),
]; ];
$result = $this->es->delete($params); $result = $this->es->delete($params);
if ($this->isEsResponseError($result)) { 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; return false;
} }
do_log("$log, delete bookmark success: " . nexus_json_encode($result)); do_log("$log, success: " . nexus_json_encode($result));
return true; return true;
} }
+5 -2
View File
@@ -12,15 +12,18 @@ header("Content-Type: text/xml; charset=utf-8");
$torrentid = intval($_GET['torrentid'] ?? 0); $torrentid = intval($_GET['torrentid'] ?? 0);
if(isset($CURUSER)) if(isset($CURUSER))
{ {
$searchRep = new \App\Repositories\SearchRepository();
$res_bookmark = sql_query("SELECT * FROM bookmarks WHERE torrentid=" . sqlesc($torrentid) . " AND userid=" . sqlesc($CURUSER['id'])); $res_bookmark = sql_query("SELECT * FROM bookmarks WHERE torrentid=" . sqlesc($torrentid) . " AND userid=" . sqlesc($CURUSER['id']));
if (mysql_num_rows($res_bookmark) == 1){ 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__); 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'); $Cache->delete_value('user_'.$CURUSER['id'].'_bookmark_array');
echo "deleted"; echo "deleted";
} } else {
else{
sql_query("INSERT INTO bookmarks (torrentid, userid) VALUES (" . sqlesc($torrentid) . "," . sqlesc($CURUSER['id']) . ")") or sqlerr(__FILE__,__LINE__); 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'); $Cache->delete_value('user_'.$CURUSER['id'].'_bookmark_array');
$searchRep->addBookmark(mysql_insert_id());
echo "added"; echo "added";
} }
} }
+5 -1
View File
@@ -53,7 +53,11 @@ else
bark($lang_delete['std_enter_reason']); bark($lang_delete['std_enter_reason']);
$reasonstr = trim($reason[3]); $reasonstr = trim($reason[3]);
} }
$searchRep = new \App\Repositories\SearchRepository();
$deleteEsResult = $searchRep->deleteTorrent($id);
if ($deleteEsResult === false) {
bark('Delete es fail.');
}
deletetorrent($id); deletetorrent($id);
if ($row['anonymous'] == 'yes' && $CURUSER["id"] == $row["owner"]) { if ($row['anonymous'] == 'yes' && $CURUSER["id"] == $row["owner"]) {
+5
View File
@@ -32,6 +32,11 @@ if (!$sure)
stderr($lang_fastdelete['std_delete_torrent'], $lang_fastdelete['std_delete_torrent_note']."<a class=altlink href=fastdelete.php?id=$id&sure=1>".$lang_fastdelete['std_here_if_sure'],false); stderr($lang_fastdelete['std_delete_torrent'], $lang_fastdelete['std_delete_torrent_note']."<a class=altlink href=fastdelete.php?id=$id&sure=1>".$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); deletetorrent($id);
KPS("-",$uploadtorrent_bonus,$row["owner"]); KPS("-",$uploadtorrent_bonus,$row["owner"]);
if ($row['anonymous'] == 'yes' && $CURUSER["id"] == $row["owner"]) { if ($row['anonymous'] == 'yes' && $CURUSER["id"] == $row["owner"]) {
+4 -1
View File
@@ -243,7 +243,10 @@ else
{ {
write_log("Torrent $id ($name) was edited by {$CURUSER['username']}, Mod Edit" . $pick_info . $place_info); 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"; $returl = "details.php?id=$id&edited=1";
if (isset($_POST["returnto"])) if (isset($_POST["returnto"]))
$returl = $_POST["returnto"]; $returl = $_POST["returnto"];
+2 -1
View File
@@ -372,7 +372,8 @@ KPS("+",$uploadtorrent_bonus,$CURUSER["id"]);
write_log("Torrent $id ($torrent) was uploaded by $anon"); 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 :) //===notify people who voted on offer thanks CoLdFuSiOn :)
if ($is_offer) if ($is_offer)