Browse Source

add push events support

main
ghost 1 year ago
parent
commit
64693f7774
  1. 14
      example/environment/env.example.php
  2. 33
      src/library/database.php
  3. 60
      src/public/action.php
  4. 27
      src/public/download.php
  5. 21
      src/public/edit.php

14
example/environment/env.example.php

@ -130,16 +130,20 @@ define('API_VERSION', 1); @@ -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

33
src/library/database.php

@ -1444,6 +1444,17 @@ class Database { @@ -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 { @@ -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 { @@ -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++;

60
src/public/action.php

@ -244,6 +244,30 @@ switch (isset($_GET['target']) ? urldecode($_GET['target']) : false) @@ -244,6 +244,30 @@ switch (isset($_GET['target']) ? urldecode($_GET['target']) : false)
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,19 +352,41 @@ switch (isset($_GET['target']) ? urldecode($_GET['target']) : false) @@ -328,19 +352,41 @@ 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 = [];
}
$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;

27
src/public/download.php

@ -62,8 +62,31 @@ else if (is_null($user->public)) @@ -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)

21
src/public/edit.php

@ -188,6 +188,27 @@ else { @@ -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))
{

Loading…
Cancel
Save