Skip to content
24 changes: 14 additions & 10 deletions src/modules/moderation/ban-all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export class BanAllQueue extends Module<ModuleShared> {

public async initiateBanAll(banAll: BanAll, messageId: number) {
const allGroups = await api.tg.groups.getAll.query()
const chats = allGroups.map((g) => g.telegramId)
const chats = allGroups.filter((g) => !g.hide).map((g) => g.telegramId)
const banType = banAll.type === "BAN" ? "ban" : "unban"

await api.tg.auditLog.create
Expand All @@ -195,6 +195,7 @@ export class BanAllQueue extends Module<ModuleShared> {
children: chats.map((chat) => ({
name: banType,
queueName: CONFIG.EXECUTOR_QUEUE,
opts: { continueParentOnFailure: true },
data: {
chatId: chat,
targetId: banAll.target.id,
Expand All @@ -208,8 +209,7 @@ export class BanAllQueue extends Module<ModuleShared> {
* Register event listeners when the module is loaded
*/
override async start() {
// set the listener to update the parent job progress
this.executor.on("completed", async (job) => {
const reportProgress = async (job: BanJob) => {
// this listener recomputes the progress for the parent job every time a child job is completed
const parentID = job.parent?.id
if (!parentID) return
Expand All @@ -221,6 +221,7 @@ export class BanAllQueue extends Module<ModuleShared> {
ignored: true,
unprocessed: true,
})

// get child counts
const { failed, ignored, processed, unprocessed } = {
failed: 0,
Expand All @@ -230,13 +231,16 @@ export class BanAllQueue extends Module<ModuleShared> {
...rawNumbers,
}

const successCount = processed - (failed + ignored)
const total = processed + unprocessed
await parent.updateProgress({
jobCount: total,
successCount,
failedCount: failed,
jobCount: processed + unprocessed + ignored + failed,
successCount: processed,
failedCount: failed + ignored,
} satisfies BanAllState)
}

this.executor.on("completed", (job) => reportProgress(job))
this.executor.on("failed", (job) => {
if (job) void reportProgress(job)
})
Comment thread
toto04 marked this conversation as resolved.

// throttled call to update the message, to avoid spamming Telegram API
Expand All @@ -245,8 +249,8 @@ export class BanAllQueue extends Module<ModuleShared> {
void modules
.get("tgLogger")
.banAllProgress(banAll, messageId)
.catch(() => {
logger.warn("[BanAllQueue] Failed to update ban all progress message")
.catch((error) => {
logger.warn({ error }, "[BanAllQueue] Failed to update ban all progress message")
})
}, CONFIG.UPDATE_MESSAGE_THROTTLE_MS)

Expand Down
Loading