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

View File

@@ -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);
}

View File

@@ -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');
}
}

View File

@@ -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;
}

View File

@@ -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";
}
}

View File

@@ -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"]) {

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);
}
$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"]) {

View File

@@ -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"];

View File

@@ -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)