From 64693f77746605df52636e6d0f062af9c19dd663 Mon Sep 17 00:00:00 2001 From: ghost Date: Fri, 15 Sep 2023 01:43:20 +0300 Subject: [PATCH] add push events support --- example/environment/env.example.php | 14 +++-- src/library/database.php | 33 ++++++++++++ src/public/action.php | 80 +++++++++++++++++++++++------ src/public/download.php | 27 +++++++++- src/public/edit.php | 21 ++++++++ 5 files changed, 151 insertions(+), 24 deletions(-) diff --git a/example/environment/env.example.php b/example/environment/env.example.php index 602010a..ee64fbe 100644 --- a/example/environment/env.example.php +++ b/example/environment/env.example.php @@ -130,16 +130,20 @@ define('API_VERSION', 1); /// Export define('API_EXPORT_ENABLED', true); +define('API_EXPORT_PUSH_ENABLED', true); // depends of API_EXPORT_ENABLED + define('API_EXPORT_USERS_ENABLED', true); // depends of API_EXPORT_ENABLED -define('API_EXPORT_MAGNETS_ENABLED', true); // depends of API_EXPORT_ENABLED -define('API_EXPORT_MAGNET_DOWNLOADS_ENABLED', true); // depends of API_EXPORT_ENABLED -define('API_EXPORT_MAGNET_COMMENTS_ENABLED', true); // depends of API_EXPORT_ENABLED -define('API_EXPORT_MAGNET_STARS_ENABLED', true); // depends of API_EXPORT_ENABLED -define('API_EXPORT_MAGNET_VIEWS_ENABLED', true); // depends of API_EXPORT_ENABLED +define('API_EXPORT_MAGNETS_ENABLED', true); // depends of API_EXPORT_ENABLED, API_EXPORT_USERS_ENABLED +define('API_EXPORT_MAGNET_DOWNLOADS_ENABLED', true); // depends of API_EXPORT_ENABLED, API_EXPORT_USERS_ENABLED, API_EXPORT_MAGNETS_ENABLED +define('API_EXPORT_MAGNET_COMMENTS_ENABLED', true); // depends of API_EXPORT_ENABLED, API_EXPORT_USERS_ENABLED, API_EXPORT_MAGNETS_ENABLED +define('API_EXPORT_MAGNET_STARS_ENABLED', true); // depends of API_EXPORT_ENABLED, API_EXPORT_USERS_ENABLED, API_EXPORT_MAGNETS_ENABLED +define('API_EXPORT_MAGNET_VIEWS_ENABLED', true); // depends of API_EXPORT_ENABLED, API_EXPORT_USERS_ENABLED, API_EXPORT_MAGNETS_ENABLED /// Import define('API_IMPORT_ENABLED', true); +define('API_IMPORT_PUSH_ENABLED', true); // depends of API_IMPORT_ENABLED + define('API_IMPORT_USERS_ENABLED', true); // depends of API_IMPORT_ENABLED define('API_IMPORT_USERS_APPROVED_ONLY', false); // depends of API_IMPORT_ENABLED, API_IMPORT_USERS_ENABLED define('API_IMPORT_MAGNETS_ENABLED', true); // depends of API_IMPORT_ENABLED, API_IMPORT_USERS_ENABLED diff --git a/src/library/database.php b/src/library/database.php index 4b1addc..ccb6c0e 100644 --- a/src/library/database.php +++ b/src/library/database.php @@ -1444,6 +1444,17 @@ class Database { return $this->_db->lastInsertId(); } + public function getMagnetStar(int $magnetStarId) { + + $this->_debug->query->select->total++; + + $query = $this->_db->prepare('SELECT * FROM `magnetStar` WHERE `magnetStarId` = ?'); + + $query->execute([$magnetStarId]); + + return $query->fetch(); + } + public function getMagnetStars() { $this->_debug->query->select->total++; @@ -1533,6 +1544,17 @@ class Database { return $this->_db->lastInsertId(); } + public function getMagnetDownload(int $magnetDownloadId) { + + $this->_debug->query->select->total++; + + $query = $this->_db->prepare('SELECT * FROM `magnetDownload` WHERE `magnetDownloadId` = ?'); + + $query->execute([$magnetDownloadId]); + + return $query->fetch(); + } + public function getMagnetDownloads() { $this->_debug->query->select->total++; @@ -1629,6 +1651,17 @@ class Database { return $query->fetchAll(); } + public function getMagnetView(int $magnetViewId) { + + $this->_debug->query->select->total++; + + $query = $this->_db->prepare('SELECT * FROM `magnetView` WHERE `magnetViewId` = ?'); + + $query->execute([$magnetViewId]); + + return $query->fetch(); + } + public function getMagnetViewsTotal() : int { $this->_debug->query->select->total++; diff --git a/src/public/action.php b/src/public/action.php index 51b77dc..8e0f829 100644 --- a/src/public/action.php +++ b/src/public/action.php @@ -237,13 +237,37 @@ switch (isset($_GET['target']) ? urldecode($_GET['target']) : false) else { if ($magnetCommentId = $db->addMagnetComment($magnet->magnetId, - $user->userId, - null, // @TODO implement threads - trim($_POST['comment']), - $user->approved || in_array($user->address, MODERATOR_IP_LIST) ? true : MAGNET_COMMENT_DEFAULT_APPROVED, - MAGNET_COMMENT_DEFAULT_PUBLIC, - time())) + $user->userId, + null, // @TODO implement threads + trim($_POST['comment']), + $user->approved || in_array($user->address, MODERATOR_IP_LIST) ? true : MAGNET_COMMENT_DEFAULT_APPROVED, + MAGNET_COMMENT_DEFAULT_PUBLIC, + time())) { + + // Push event to other nodes + if (API_EXPORT_ENABLED && + API_EXPORT_PUSH_ENABLED && + API_EXPORT_USERS_ENABLED && + API_EXPORT_MAGNETS_ENABLED && + API_EXPORT_MAGNET_COMMENTS_ENABLED) + { + if (!$memoryApiExportPush = $memory->get('api.export.push')) + { + $memoryApiExportPush = []; + } + + $memoryApiExportPush[] = (object) + [ + 'time' => time(), + 'userId' => $user->userId, + 'magnetId' => $magnet->magnetId, + 'magnetCommentId' => $magnetCommentId + ]; + + $memory->set('api.export.push', $memoryApiExportPush, 3600); + } + // Redirect to referrer page header( sprintf('Location: %s#comment-%s', $callback, $magnetCommentId) @@ -328,18 +352,40 @@ switch (isset($_GET['target']) ? urldecode($_GET['target']) : false) // Request valid else { - // Save value - $db->addMagnetStar( - $magnet->magnetId, - $userId, - !$db->findLastMagnetStarValue($magnet->magnetId, $userId), - time() - ); + // Save star + if ($magnetStarId = $db->addMagnetStar( $magnet->magnetId, + $user->userId, + !$db->findLastMagnetStarValue($magnet->magnetId, $user->userId), + time())) + { + // Push event to other nodes + if (API_EXPORT_ENABLED && + API_EXPORT_PUSH_ENABLED && + API_EXPORT_USERS_ENABLED && + API_EXPORT_MAGNETS_ENABLED && + API_EXPORT_MAGNET_STARS_ENABLED) + { + if (!$memoryApiExportPush = $memory->get('api.export.push')) + { + $memoryApiExportPush = []; + } - // Redirect to edit page - header( - sprintf('Location: %s', $callback) - ); + $memoryApiExportPush[] = (object) + [ + 'time' => time(), + 'userId' => $user->userId, + 'magnetId' => $magnet->magnetId, + 'magnetStarId' => $magnetStarId + ]; + + $memory->set('api.export.push', $memoryApiExportPush, 3600); + } + + // Redirect to edit page + header( + sprintf('Location: %s', $callback) + ); + } } break; diff --git a/src/public/download.php b/src/public/download.php index 7eda422..af46593 100644 --- a/src/public/download.php +++ b/src/public/download.php @@ -62,8 +62,31 @@ else if (is_null($user->public)) // Request valid else { - // Update download stats - $db->addMagnetDownload($magnet->magnetId, $userId, time()); + // Register magnet download + $magnetDownloadId = $db->addMagnetDownload($magnet->magnetId, $user->userId, time()); + + // Push event to other nodes + if (API_EXPORT_ENABLED && + API_EXPORT_PUSH_ENABLED && + API_EXPORT_USERS_ENABLED && + API_EXPORT_MAGNETS_ENABLED && + API_EXPORT_MAGNET_DOWNLOADS_ENABLED) + { + if (!$memoryApiExportPush = $memory->get('api.export.push')) + { + $memoryApiExportPush = []; + } + + $memoryApiExportPush[] = (object) + [ + 'time' => time(), + 'userId' => $user->userId, + 'magnetId' => $magnet->magnetId, + 'magnetDownloadId' => $magnetDownloadId + ]; + + $memory->set('api.export.push', $memoryApiExportPush, 3600); + } // Build magnet link $link = (object) diff --git a/src/public/edit.php b/src/public/edit.php index aac7208..7a09f3c 100644 --- a/src/public/edit.php +++ b/src/public/edit.php @@ -188,6 +188,27 @@ else { // Update form if (!empty($_POST)) { + // Push event to other nodes + if (API_EXPORT_ENABLED && + API_EXPORT_PUSH_ENABLED && + API_EXPORT_USERS_ENABLED && + API_EXPORT_MAGNETS_ENABLED) + { + if (!$memoryApiExportPush = $memory->get('api.export.push')) + { + $memoryApiExportPush = []; + } + + $memoryApiExportPush[] = (object) + [ + 'time' => time(), + 'userId' => $user->userId, + 'magnetId' => $magnet->magnetId, + ]; + + $memory->set('api.export.push', $memoryApiExportPush, 3600); + } + // Approve by moderation request if (in_array($user->address, MODERATOR_IP_LIST)) {