Prefer longest AI rate limit blocker

This commit is contained in:
2026-04-07 16:35:58 +02:00
parent 1a66f27507
commit 52a21ca089

View File

@@ -1197,66 +1197,83 @@ function buildAIAutoCommentRateLimitStatus(profileNumber, settings = getAIAutoCo
let blockedReason = null; let blockedReason = null;
let blockedUntil = null; let blockedUntil = null;
if (!settings.enabled) { if (settings.enabled) {
blocked = false; const blockingCandidates = [];
} else if (profileState && profileState.cooldown_until) { const addBlockingCandidate = (reason, untilIso) => {
const cooldownUntil = new Date(profileState.cooldown_until); if (!untilIso) {
if (!Number.isNaN(cooldownUntil.getTime()) && cooldownUntil.getTime() > now.getTime()) { return;
blocked = true;
blockedReason = 'cooldown';
blockedUntil = cooldownUntil.toISOString();
} }
const untilDate = new Date(untilIso);
if (Number.isNaN(untilDate.getTime()) || untilDate.getTime() <= now.getTime()) {
return;
}
blockingCandidates.push({
reason,
until: untilDate.toISOString(),
untilMs: untilDate.getTime()
});
};
if (profileState && profileState.cooldown_until) {
addBlockingCandidate('cooldown', profileState.cooldown_until);
} }
if (!blocked && activeHours.configured && !activeHours.active) { if (activeHours.configured && !activeHours.active) {
blocked = true; addBlockingCandidate('active_hours', activeHours.nextAllowedAt);
blockedReason = 'active_hours';
blockedUntil = activeHours.nextAllowedAt;
} }
if (!blocked && settings.min_delay_seconds > 0 && lastEvent && lastEvent.created_at) { if (settings.min_delay_seconds > 0 && lastEvent && lastEvent.created_at) {
const lastEventDate = new Date(lastEvent.created_at); const lastEventDate = new Date(lastEvent.created_at);
if (!Number.isNaN(lastEventDate.getTime())) { if (!Number.isNaN(lastEventDate.getTime())) {
const nextAllowedAt = new Date(lastEventDate.getTime() + (settings.min_delay_seconds * 1000)); addBlockingCandidate(
if (nextAllowedAt.getTime() > now.getTime()) { 'min_delay',
blocked = true; new Date(lastEventDate.getTime() + (settings.min_delay_seconds * 1000)).toISOString()
blockedReason = 'min_delay'; );
blockedUntil = nextAllowedAt.toISOString();
}
} }
} }
if (!blocked && settings.requests_per_minute > 0 && usedMinute >= settings.requests_per_minute) { if (settings.requests_per_minute > 0 && usedMinute >= settings.requests_per_minute) {
const oldest = getAIAutoCommentOldestEventSince(normalizedProfileNumber, minuteWindowStart); const oldest = getAIAutoCommentOldestEventSince(normalizedProfileNumber, minuteWindowStart);
if (oldest && oldest.created_at) { if (oldest && oldest.created_at) {
blocked = true; addBlockingCandidate(
blockedReason = 'per_minute'; 'per_minute',
blockedUntil = new Date(new Date(oldest.created_at).getTime() + (60 * 1000)).toISOString(); new Date(new Date(oldest.created_at).getTime() + (60 * 1000)).toISOString()
);
} }
} }
if (!blocked && settings.burst_limit > 0 && usedBurst >= settings.burst_limit) { if (settings.burst_limit > 0 && usedBurst >= settings.burst_limit) {
const oldest = getAIAutoCommentOldestEventSince(normalizedProfileNumber, burstWindowStart); const oldest = getAIAutoCommentOldestEventSince(normalizedProfileNumber, burstWindowStart);
if (oldest && oldest.created_at) { if (oldest && oldest.created_at) {
blocked = true; addBlockingCandidate(
blockedReason = 'burst'; 'burst',
blockedUntil = new Date(new Date(oldest.created_at).getTime() + (settings.burst_window_minutes * 60 * 1000)).toISOString(); new Date(new Date(oldest.created_at).getTime() + (settings.burst_window_minutes * 60 * 1000)).toISOString()
);
} }
} }
if (!blocked && settings.requests_per_hour > 0 && usedHour >= settings.requests_per_hour) { if (settings.requests_per_hour > 0 && usedHour >= settings.requests_per_hour) {
const oldest = getAIAutoCommentOldestEventSince(normalizedProfileNumber, hourWindowStart); const oldest = getAIAutoCommentOldestEventSince(normalizedProfileNumber, hourWindowStart);
if (oldest && oldest.created_at) { if (oldest && oldest.created_at) {
blocked = true; addBlockingCandidate(
blockedReason = 'per_hour'; 'per_hour',
blockedUntil = new Date(new Date(oldest.created_at).getTime() + (60 * 60 * 1000)).toISOString(); new Date(new Date(oldest.created_at).getTime() + (60 * 60 * 1000)).toISOString()
);
} }
} }
if (!blocked && settings.requests_per_day > 0 && usedDay >= settings.requests_per_day) { if (settings.requests_per_day > 0 && usedDay >= settings.requests_per_day) {
addBlockingCandidate('per_day', getNextLocalMidnightIso(now));
}
if (blockingCandidates.length > 0) {
const primaryBlocker = blockingCandidates.reduce((selected, candidate) => (
!selected || candidate.untilMs > selected.untilMs ? candidate : selected
), null);
blocked = true; blocked = true;
blockedReason = 'per_day'; blockedReason = primaryBlocker.reason;
blockedUntil = getNextLocalMidnightIso(now); blockedUntil = primaryBlocker.until;
}
} }
return { return {