add push events support

This commit is contained in:
ghost 2023-09-15 01:43:20 +03:00
parent 7c054557f3
commit 64693f7774
5 changed files with 151 additions and 24 deletions

View File

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

View File

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

View File

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

View File

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

View File

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