Skip to content

Commit 31090e0

Browse files
authored
Merge pull request #17 from WizardLoop/dev
3.0.4 -Peer filtering is now handled outside of broadcast execution
2 parents c165cba + 0a30395 commit 31090e0

3 files changed

Lines changed: 70 additions & 75 deletions

File tree

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,12 @@ Added functionality to send initial status messages when gathering peers and sta
148148
### Fixed:
149149

150150
* Fix getDataDir() to handle uninitialized $dataDir
151+
152+
---
153+
154+
## [3.0.4] - 2026-01-13
155+
156+
### Added & Fixed:
157+
158+
* Extracted peer filtering from broadcast execution
159+
* Reduced unnecessary processing during broadcasts

README.md

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,18 @@ use BroadcastTool\BroadcastManager;
102102

103103
$manager = new BroadcastManager($api);
104104

105-
$manager->broadcastWithProgress(
106-
allUsers: $users,
107-
messages: $messages,
108-
chatId: $adminChatId,
109-
filterType: 'users',
110-
pin: true,
111-
concurrency: 25
112-
);
105+
$manager->broadcastWithProgress($users, $messages, $adminChatId, true, 20);
106+
```
107+
108+
---
109+
110+
## Filer Peers
111+
112+
```php
113+
$filterSub = $manager->filterPeers($users, 'users');
114+
$targets = $filterSub['targets']; # array
115+
$failed = $filterSub['failed']; # int
116+
$total = $filterSub['total']; # int
113117
```
114118

115119
---
@@ -142,23 +146,15 @@ _default is: __DIR__ . '/../data'_
142146
## 🧹 Delete Last Broadcast
143147

144148
```php
145-
$manager->deleteLastBroadcastForAll(
146-
allUsers: $users,
147-
chatId: $adminChatId,
148-
concurrency: 20
149-
);
149+
$manager->deleteLastBroadcastForAll($users, $adminChatId, 20);
150150
```
151151

152152
---
153153

154154
## ♻️ Delete All Broadcast
155155

156156
```php
157-
$manager->deleteAllBroadcastsForAll(
158-
allUsers: $users,
159-
chatId: $adminChatId,
160-
concurrency: 20
161-
);
157+
$manager->deleteAllBroadcastsForAll($users, $adminChatId, 20);
162158
```
163159

164160
---

src/BroadcastManager.php

Lines changed: 47 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ public function broadcastWithProgress(
5454
array $allUsers,
5555
array $messages,
5656
$chatId,
57-
string $filterType = 'users',
5857
bool $pin = false,
5958
int $concurrency = 20
6059
): array {
@@ -68,36 +67,12 @@ public function broadcastWithProgress(
6867
]);
6968
$statusId = $api->extractMessageId($status);
7069

71-
/* ===== FILTER TARGETS ===== */
72-
$allowedTypes = [
73-
'all' => ['user','bot','chat','supergroup','channel'],
74-
'users' => ['user','bot'],
75-
'groups' => ['chat','supergroup'],
76-
'channels' => ['channel'],
77-
];
78-
79-
$targets = [];
80-
$failedCount = 0;
81-
82-
foreach ($allUsers as $peer) {
83-
try {
84-
$info = $api->getInfo($peer);
85-
$type = $info['type'] ?? 'user';
86-
if (in_array($type, $allowedTypes[$filterType] ?? ['user','bot'], true)) {
87-
$targets[] = (string)$peer;
88-
}
89-
} catch (\Throwable) {
90-
if ($filterType === 'all') $targets[] = (string)$peer;
91-
else $failedCount++;
92-
}
93-
}
94-
95-
$total = count($targets);
70+
$total = count($allUsers);
9671

9772
/* ===== STATE ===== */
9873
$state = [
9974
'sent' => 0,
100-
'failed' => $failedCount,
75+
'failed' => 0,
10176
'queue' => new \SplQueue(),
10277
'inFlight' => [],
10378
'lastMessageIds' => [],
@@ -110,7 +85,7 @@ public function broadcastWithProgress(
11085
/* ===== SET CURRENT BROADCAST STATE FOR PAUSE/CANCEL ===== */
11186
$this->currentBroadcastState = &$state;
11287

113-
foreach ($targets as $peer) {
88+
foreach ($allUsers as $peer) {
11489
$state['queue']->enqueue([
11590
'peer' => $peer,
11691
'attempts' => 0,
@@ -711,7 +686,6 @@ public function deleteAllBroadcastsForAll(
711686
public function unpinAllMessagesForAll(
712687
array $allUsers,
713688
$chatId,
714-
string $filterType = 'users',
715689
int $concurrency = 20
716690
): array {
717691
$api = $this->api;
@@ -724,37 +698,12 @@ public function unpinAllMessagesForAll(
724698
]);
725699
$statusId = $api->extractMessageId($status);
726700

727-
/* ===== FILTER TARGETS ===== */
728-
$allowedTypesByFilter = [
729-
'all' => ['user','bot','chat','supergroup','channel'],
730-
'users' => ['user','bot'],
731-
'groups' => ['chat','supergroup'],
732-
'channels' => ['channel'],
733-
];
734-
735-
$targets = [];
736-
$failedCount = 0;
737-
738-
foreach ($allUsers as $peer) {
739-
try {
740-
$info = $api->getInfo($peer);
741-
$type = $info['type'] ?? 'user';
742-
743-
if (in_array($type, $allowedTypesByFilter[$filterType] ?? ['user','bot'], true)) {
744-
$targets[] = (string)$peer;
745-
}
746-
} catch (\Throwable) {
747-
if ($filterType === 'all') $targets[] = (string)$peer;
748-
else $failedCount++;
749-
}
750-
}
751-
752-
$total = count($targets);
701+
$total = count($allUsers);
753702

754703
/* ===== STATE ===== */
755704
$state = [
756705
'unpin' => 0,
757-
'failed' => $failedCount,
706+
'failed' => 0,
758707
'flood' => 0,
759708
'queue' => new \SplQueue(),
760709
'inFlight' => [],
@@ -763,7 +712,7 @@ public function unpinAllMessagesForAll(
763712
'startedAt' => microtime(true),
764713
];
765714

766-
foreach ($targets as $peer) {
715+
foreach ($allUsers as $peer) {
767716
$state['queue']->enqueue([
768717
'peer' => $peer,
769718
'attempts' => 0,
@@ -953,6 +902,7 @@ public function resume(): void {
953902
public function cancel(): void {
954903
if ($this->currentBroadcastState) {
955904
$this->currentBroadcastState['cancel'] = true;
905+
$this->currentBroadcastState['inFlight'] = [];
956906
}
957907
}
958908

@@ -1034,4 +984,44 @@ public function lastBroadcastData(): string|false {
1034984
return \Amp\File\read($path);
1035985
}
1036986

987+
/**
988+
* Filter peers
989+
*/
990+
public function filterPeers(
991+
array $allUsers,
992+
string $filterType = 'users'
993+
): array {
994+
995+
$api = $this->api;
996+
997+
$allowedTypes = [
998+
'all' => ['user','chat','supergroup','channel'],
999+
'users' => ['user'],
1000+
'groups' => ['chat','supergroup'],
1001+
'channels' => ['channel'],
1002+
];
1003+
1004+
$targets = [];
1005+
$failedCount = 0;
1006+
1007+
foreach ($allUsers as $peer) {
1008+
try {
1009+
$info = $api->getInfo($peer);
1010+
$type = $info['type'] ?? 'user';
1011+
if (in_array($type, $allowedTypes[$filterType] ?? ['user'], true)) {
1012+
$targets[] = (string)$peer;
1013+
}
1014+
} catch (\Throwable) {
1015+
if ($filterType === 'all') $targets[] = (string)$peer;
1016+
else $failedCount++;
1017+
}
1018+
}
1019+
1020+
return [
1021+
'targets' => $targets,
1022+
'failed' => $failedCount,
1023+
'total' => count($targets)
1024+
];
1025+
1026+
}
10371027
}

0 commit comments

Comments
 (0)