-
-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve announce for paid torrents and H&R
- Loading branch information
xiaomlove
committed
Oct 30, 2024
1 parent
8dbf798
commit 653a92a
Showing
1 changed file
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
namespace App\Utils; | ||
|
||
use Nexus\Database\NexusDB; | ||
|
||
final class MsgAlert { | ||
|
||
private static ?self $instance = null; | ||
|
||
private static array $alerts = []; | ||
|
||
private string $redisKeyPrefix = "nexus_alerts"; | ||
|
||
private function __construct() | ||
{ | ||
$redis = NexusDB::redis(); | ||
$result = $redis->lRange($this->getListKey(), 0, 10); | ||
if (!empty($result)) { | ||
$nowTimestamp = time(); | ||
$valid = []; | ||
foreach ($result as $item) { | ||
$arr = json_decode($item, true); | ||
if (is_array($arr) && $arr['deadline'] > $nowTimestamp) { | ||
$valid[$arr['name']] = $arr; | ||
} else { | ||
$redis->lRem($this->getListKey(), $item, 0); | ||
} | ||
} | ||
self::$alerts = $valid; | ||
} | ||
} | ||
|
||
private function __clone() | ||
{ | ||
|
||
} | ||
|
||
public static function getInstance(): MsgAlert | ||
{ | ||
if (isset(self::$instance)) { | ||
return self::$instance; | ||
} | ||
return self::$instance = new self; | ||
} | ||
|
||
|
||
|
||
public function add(string $name, int $deadline, string $text, string $url = "", string $color = "red"): void | ||
{ | ||
if (!isset(self::$alerts[$name])) { | ||
$params = compact('name', 'deadline', 'text', 'url', 'color'); | ||
self::$alerts[$name] = $params; | ||
NexusDB::redis()->rPush($this->getListKey(), json_encode($params)); | ||
} | ||
} | ||
|
||
private function getListKey(): string | ||
{ | ||
return sprintf("%s:%s", $this->redisKeyPrefix, get_user_id()); | ||
} | ||
|
||
|
||
public static function render(): void | ||
{ | ||
$nowTimestamp = time(); | ||
foreach (self::$alerts as $item) { | ||
if ($item['deadline'] > $nowTimestamp) { | ||
msgalert($item['url'] ?: '', $item['text'], $item['color'] ?: 'red'); | ||
} | ||
} | ||
} | ||
|
||
public function remove($name): void | ||
{ | ||
foreach (self::$alerts as $item) { | ||
if ($item['name'] = $name) { | ||
unset(self::$alerts[$name]); | ||
NexusDB::redis()->lRem($this->getListKey(), json_encode($item)); | ||
} | ||
} | ||
} | ||
|
||
|
||
|
||
} |