Skip to content

Commit c165cba

Browse files
authored
Merge pull request #16 from WizardLoop/dev
3.0.3
2 parents 89768c3 + 2e86c9f commit c165cba

3 files changed

Lines changed: 23 additions & 197 deletions

File tree

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,11 @@ Added functionality to send initial status messages when gathering peers and sta
140140
### Added:
141141

142142
* setDataDir & getDataDir
143+
144+
---
145+
146+
## [3.0.3] - 2026-01-11
147+
148+
### Fixed:
149+
150+
* Fix getDataDir() to handle uninitialized $dataDir

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ $manager->cancel();
123123
```
124124

125125
Check state:
126-
127126
```php
128127
if ($manager->isPaused()) echo "Paused";
129128
if ($manager->isCancelled()) echo "Cancelled";
@@ -133,10 +132,10 @@ print_r($manager->progress());
133132
```
134133

135134
Set data dir:
136-
137135
```php
138136
BroadcastManager::setDataDir(__DIR__ . '/data');
139137
```
138+
_default is: __DIR__ . '/../data'_
140139

141140
---
142141

src/BroadcastManager.php

Lines changed: 14 additions & 195 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class BroadcastManager
2323
private API $api;
2424
private ?array $currentBroadcastState = null;
2525
private array $albumTimers = [];
26-
private static string $dataDir;
26+
private static string $dataDir = '';
2727

2828
public function __construct(API $api)
2929
{
@@ -512,195 +512,6 @@ public function deleteLastBroadcastForAll(
512512
/**
513513
* Delete all broadcasts message for all users
514514
*/
515-
/*
516-
public function deleteAllBroadcastsForAll(
517-
array $allUsers,
518-
$chatId,
519-
int $concurrency = 20
520-
): array {
521-
$api = $this->api;
522-
$total = count($allUsers);
523-
524-
$state = [
525-
'deleted' => 0,
526-
'failed' => 0,
527-
'flood' => 0,
528-
'queue' => new \SplQueue(),
529-
'inFlight' => [],
530-
'done' => false,
531-
'cancel' => false,
532-
'startedAt' => microtime(true),
533-
];
534-
535-
foreach ($allUsers as $peer) {
536-
$state['queue']->enqueue([
537-
'peer' => (string)$peer,
538-
'attempts' => 0,
539-
'startedAt' => null,
540-
'availableAt' => 0
541-
]);
542-
}
543-
544-
$status = $api->messages->sendMessage([
545-
'peer' => $chatId,
546-
'message' => "⌛ Deleting all broadcasts...",
547-
'parse_mode' => 'HTML'
548-
]);
549-
$statusId = $api->extractMessageId($status);
550-
551-
\Amp\async(function () use (&$state) {
552-
while (!$state['done']) {
553-
foreach ($state['inFlight'] as $peer => $job) {
554-
if ($job['startedAt'] && microtime(true) - $job['startedAt'] > 60) {
555-
unset($state['inFlight'][$peer]);
556-
$job['attempts']++;
557-
$job['startedAt'] = null;
558-
559-
if ($job['attempts'] >= 3) {
560-
$state['failed']++;
561-
} else {
562-
$state['queue']->enqueue($job);
563-
}
564-
}
565-
}
566-
\danog\MadelineProto\Tools::sleep(5);
567-
}
568-
});
569-
570-
for ($i = 0; $i < $concurrency; $i++) {
571-
\Amp\async(function () use ($api, &$state, $chatId, $statusId, $total) {
572-
while (!$state['cancel']) {
573-
if ($state['queue']->isEmpty()) {
574-
$api->sleep(1);
575-
continue;
576-
}
577-
578-
$job = $state['queue']->dequeue();
579-
580-
if ($job['availableAt'] > microtime(true)) {
581-
$state['queue']->enqueue($job);
582-
$api->sleep(0.5);
583-
continue;
584-
}
585-
586-
$peer = $job['peer'];
587-
$job['startedAt'] = microtime(true);
588-
$state['inFlight'][$peer] = $job;
589-
590-
try {
591-
$file = __DIR__."/data/$peer/messages.txt";
592-
if (!file_exists($file)) {
593-
$state['failed']++;
594-
unset($state['inFlight'][$peer]);
595-
continue;
596-
}
597-
598-
$msgIds = array_filter(
599-
array_map('intval', explode("\n", trim(\Amp\File\read($file))))
600-
);
601-
602-
foreach ($msgIds as $mid) {
603-
if ($mid <= 0) continue;
604-
605-
try {
606-
$api->messages->deleteMessages([
607-
'peer' => $peer,
608-
'id' => [$mid],
609-
'revoke' => true
610-
]);
611-
612-
// ===== עדכון פרוגרס מיד =====
613-
$processed = $state['deleted'] + $state['failed'];
614-
$pending = max(0, $total - $processed);
615-
$elapsed = microtime(true) - $state['startedAt'];
616-
$tps = $elapsed > 0 ? round($state['deleted'] / $elapsed, 2) : 0;
617-
618-
$text =
619-
"<b>📊 Deleting All Broadcasts</b>\n\n".
620-
"<code>".$this->progressBar($processed, max(1,$total))."</code>\n\n".
621-
"✅ Deleted: {$state['deleted']} / $total\n".
622-
"❌ Failed: {$state['failed']}\n".
623-
"⏳ Pending: $pending\n".
624-
"⚡ TPS: {$tps} msg/s";
625-
626-
try {
627-
$api->messages->editMessage([
628-
'peer' => $chatId,
629-
'id' => $statusId,
630-
'message' => $text,
631-
'parse_mode' => 'HTML'
632-
]);
633-
} catch (\Throwable) {}
634-
635-
} catch (\danog\MadelineProto\RPCErrorException $e) {
636-
$msg = $e->getMessage();
637-
638-
if (preg_match('/FLOOD_WAIT_(\d+)/', $msg, $m)) {
639-
$state['flood']++;
640-
unset($state['inFlight'][$peer]);
641-
642-
$job['attempts']++;
643-
$job['availableAt'] = microtime(true) + (int)$m[1];
644-
$job['startedAt'] = null;
645-
646-
if ($job['attempts'] >= 3) $state['failed']++;
647-
else $state['queue']->enqueue($job);
648-
continue 2;
649-
}
650-
651-
if (str_contains($msg, 'USER_IS_BLOCKED') || str_contains($msg, 'PEER_ID_INVALID')) {
652-
continue;
653-
}
654-
655-
throw $e;
656-
}
657-
}
658-
$state['deleted']++;
659-
@unlink($file);
660-
unset($state['inFlight'][$peer]);
661-
662-
} catch (\Throwable) {
663-
unset($state['inFlight'][$peer]);
664-
if (++$job['attempts'] >= 3) $state['failed']++;
665-
else { $job['startedAt'] = null; $state['queue']->enqueue($job); }
666-
}
667-
668-
$api->sleep(0.25);
669-
}
670-
});
671-
}
672-
673-
while (!$state['cancel']) {
674-
if ($state['queue']->isEmpty() && empty($state['inFlight'])) break;
675-
$api->sleep(1);
676-
}
677-
678-
$state['done'] = true;
679-
680-
$processed = $state['deleted'] + $state['failed'];
681-
$elapsed = microtime(true) - $state['startedAt'];
682-
$tps = $elapsed > 0 ? round($state['deleted'] / $elapsed, 2) : 0;
683-
684-
$finalText =
685-
"<b>📊 Deleting All Broadcasts</b>\n\n".
686-
"<code>".$this->progressBar($processed, max(1,$total))."</code>\n\n".
687-
"✅ Deleted: {$state['deleted']} / $total\n".
688-
"❌ Failed: {$state['failed']}\n".
689-
($state['cancel'] ? "🛑 <b>Cancelled</b>" : "✅ <b>Finished</b>");
690-
691-
try {
692-
$api->messages->editMessage([
693-
'peer' => $chatId,
694-
'id' => $statusId,
695-
'message' => $finalText,
696-
'parse_mode' => 'HTML'
697-
]);
698-
} catch (\Throwable) {}
699-
700-
return $state;
701-
}
702-
*/
703-
704515
public function deleteAllBroadcastsForAll(
705516
array $allUsers,
706517
$chatId,
@@ -1208,11 +1019,19 @@ public function progress(): ?array {
12081019
* Last broadcast data
12091020
*/
12101021
public function lastBroadcastData(): string|false {
1211-
if (file_exists(self::getDataDir() ."/LastBrodDATA.txt")) {
1212-
return \Amp\File\read(self::getDataDir() ."/LastBrodDATA.txt");
1213-
}else{
1214-
return false;
1215-
}
1022+
$dir = self::getDataDir();
1023+
1024+
if (!is_dir($dir)) {
1025+
mkdir($dir, 0777, true);
1026+
}
1027+
1028+
$path = $dir . "/LastBrodDATA.txt";
1029+
1030+
if (!file_exists($path)) {
1031+
return false;
1032+
}
1033+
1034+
return \Amp\File\read($path);
12161035
}
12171036

12181037
}

0 commit comments

Comments
 (0)